8.0.0 • Published 3 years ago

@exmg/exmg-searchbar v8.0.0

Weekly downloads
81
License
MIT
Repository
github
Last release
3 years ago

exmg-searchbar

A customizable search bar element.

This search bar uses mwc-textfield for the input element. More information about styling and the input element itself can be found on the link below: https://github.com/material-components/material-components-web-components/tree/master/packages/textfield

Usage

Import this element to your project with npm i @exmg/exmg-searchbar.

Then add the snippet below into your code.

<exmg-searchbar></exmg-searchbar>

Default Filtering

ExmgSearchBar has it's own filtering methods in it which you can also override. To achieve default filtering, take the example of html code below:

<exmg-searchbar .data="${dataArray}" .filterKeys="${filterKeys}" suggestion-label-key="url"> </exmg-searchbar>

When you pass the data array into data property along with filterKeys, exmg-search bar automatically filters data and shows suggestions.

filterKeys property has to contain specific key names from the type of each item in data.

For example; if you have [{username:"exmachina",name: "Ex Machina"},{username:"livery",name:"Livery"}] as the data array, you can pass ['username'] to filter out username values of data. Or pass ['username', 'name'] to filter out values of both keys.

To determine which value of items passed into data should be shown as the label of each suggestion, use suggestion-label-key just like filterKeys but the only difference is, it accepts a single key as a simple string, not an array.

Custom Suggestions and Filtering

By extending ExmgSearchBar, you can override any of three functions of your selection. These functions are:

  • filter(data: any[], filterKeys: string[], query: string): ExmgSearchSuggestion[]: TemplateResult
  • renderSuggestions(suggestions: ExmgSearchSuggestion[])
  • renderSuggestionsLoading(): TemplateResult

filter expects array of ExmgSearchSuggestion objects. By implementing your own filtering method, you might not need suggestion-label-key since you can place any string value into text property of ExmgSearchSuggestion.

Please check ExmgSearchBar class in order to see how these three functions are working. After that you can extend ExmgSearchBar class and override those functions to create your fancy filtering methods or suggestions.

Showing Suggestions Manually

If you are handling filtering logic manually on the parent, you can follow this way to show suggestions manually. To show suggestons, pass the suggestion data to suggestions property of exmg-searchbar. It accepts array of ExmgSearchSuggestion type of elements into suggestions.

Follow this document for available properties, events, methods and styling.

Styling

VariableDefaultDescription
--exmg-searchbar-error-color#b00020Error color of search bar
--exmg-searchbar-hint-colorrgba(0, 0, 0, 0.6)Hint text color of search bar
--exmg-searchbar-primary-color#0071dcPrimary theme color of search bar
--exmg-searchbar-text-colorrgba(0, 0, 0, 0.87)Text color of search bar
--exmg-searchbar-suggestions-spinner-color#0071dcSuggestions loading indicator color
--exmg-searchbar-suggestions-spinner-width3pxSuggestions loading indicator spinner width
--exmg-searchbar-suggestions-max-visible-suggestions5Max visible suggestions before showing scrollbar
--exmg-searchbar-suggestions-min-height40pxSuggestion item row height
--exmg-searchbar-suggestions-text-colorrgba(0, 0, 0, 0.6)Suggestion item text color
--exmg-searchbar-suggestions-background-color#ffffffSuggestions list background color
--exmg-searchbar-suggestions-z-index1z-index of suggestions list
--exmg-searchbar-width100%Width of search bar

Properties

NameTypeDefaultDescription
filterKeysstring[]undefinedSet of which keys of data should be checked to filter data by default.
dataany[]undefinedThe data in search bar to filter.
keepFocusbooleanfalseIf true, keeps focus on search bar after query-submit is fired
keepSuggestionsOnSelectbooleanfalseOption to whether keep suggestions visible or not on suggestion selection
submitKeysstring[]['ENTER']Determines which keys should trigger submitting search query
notifyOnQueryChangebooleantrueIf true, dispatches query-change event with the current query on every change of search input.
searchQuerystring''Current query of search bar
suggestionLabelKeystringundefinedDefines which property of each data item should be placed into suggestions' labels.
submitOnKeyPressbooleantrueIf true, dispatches query-submit event with the current query upon receiving key press event with specific keys passed to keys string array
suggestionsExmgSearchSuggestion[][]List of suggestions to be passed into and displayed
suggestionsLoadingbooleanfalseIf true and there are no suggestions passed to element, a loading indicator should be shownbar

Events

@query-change

Fired on query change.

Payload: {value: string}

@query-submit

Fired on query submit.

Payload: {value: string}

@suggestion-select

Fired on suggestion selection.

This event is also fired when there is only one suggestion and user submits query.

Payload: {value: any, index: number}

Methods

clearSuggestions()

Clears suggestions.

hideSuggestionsLoading()

Hides the loading indicator.

search()

Fires query-submit event with passed query into search bar.

setQuery(query: string)

Sets query.

setSuggestions(suggestions: ExmgSearchSuggestion[])

Sets suggestions.

showSuggestionsLoading()

Shows the loading indicator if there are no suggestions available.