Skip to main content

SearchInput

A type of input field that allows searching through content.

Submit feedback
github
import { SearchInput } from '@uhg-abyss/mobile';
() => {
const data = [
{ value: 'react', label: 'React' },
{ value: 'ng', label: 'Angular' },
{ value: 'svelte', label: 'Svelte' },
{ value: 'vue', label: 'Vue' },
{ value: 'alpine', label: 'Alpine' },
{ value: 'ember', label: 'Ember' },
{ value: 'stimulus', label: 'Stimulus' },
{ value: 'preact', label: 'Preact' },
];
const [value, setValue] = React.useState('');
const [results, setResults] = useState([]);
const handleSubmit = ()=>{
console.log(results)
}
return (
<SearchInput
label="Search Sandbox"
value={value}
onChangeText={setValue}
onChange={setResults}
onSubmit={handleSubmit}
options={data}
keys={['label']}
/>
);
};

useState

Using the useState hook sets state for the component.

onChange

The onChange prop handles the action for the search results.

Fuse.js

Search Input filtering uses the Fuse.js library to fuzzy filter results. What is fuzzy searching? Generally speaking, fuzzy searching (more formally known as approximate string matching) is the technique of finding strings that are approximately equal to a given pattern (rather than exactly).

Fuse Configurations

To adjust the fuse configurations, pass the desired options into the fuseConfigs prop. Note that keys is required when using fuse. By default the fuse search configurations are:

{
includeMatches: true,
findAllMatches: true,
threshold: 0,
ignoreLocation: true,
minMatchCharLength: searchText.length,
keys,
}

The default configurations return the search results object below:

{
item: []
}

Fuse Data

The options prop is the information that fuse will filter on and display in the search dropdown. You can search on any value(s) in the object, see Fuse Keys below. Required when using fuse.

Fuse Keys

List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of objects. Required when using fuse. Below the keys set are "label" and "value".

Custom Filtering

Use the customFilter prop to override the fuse.js filtering. In the example function below, the filter is checking the first letter typed in the search bar against the first letter of each item in the list. If the letter is a match the item is included in the filtered list.

SearchInput Props

Prop NameTypeDefaultDescription
customFilterfunction-Custom function used for search
fuseConfigsshape-Configs for Fuse
helpButtonAccessibilityLabelstring-Set the accessibility label for the help button
helpContentstring | ReactNode-When defined, displays a help icon that can be tapped to view the provided content in a modal screen
hintTextstring-Set the text displayed below label
keysarray[string][]Keys used by Fuse
labelstring-Label for input field
onBlurfunction-Callback fired every time the component is unfocused
onChangefunction-Callback fired when search is changed
onChangeTextfunction-Callback that is called when the text input text changes. Changed text is passed as a single string argument to the callback handler
onFocusfunction-Callback fired every time the component is focused
onSubmitfunction-Callback fired when input is submitted
optionsobject[][]Data filtered on by Fuse
returnKeyLabelstringsearchSets the label of the return key. Defaults to "search"
returnKeyTypestringsearchSets the type of return key
valuestring-Value of the text input

SearchInput Classes

Class NameDescription
abyss-search-input-rootSearchInput root element
Table of Contents