import { SearchInput } from "@vitality-ds/components";
SearchInput
under the hood is a TextInput
(Read more about TextInput
here) with preset adornments. Both the startAdorn
and the endAdorn
are handled by the SearchInput
and a loading state is provided. The purpose of this component is to perform asynchronous functions on external components based on the value of the TextInput and the process is communicated to the user through the isLoading
loading state.
<SearchInput value={undefined} onChange={(event) => console.log(event.target.value)} id="search" name="search" />
The key difference between SearchInput and TextInput is the loading state, which can be indicated by simply passing the isLoading boolean to the component.
<SearchInput isLoading value={undefined} onChange={(event) => console.log(event.target.value)} id="search" name="search" />
SearchInput
renders a Search
icon by default, which can be customized using the icon prop to any valid Vitality Icon. To remove the icon, pass null
to the icon
prop.
() => { const requiredProps = { value: undefined, name: "name", id: "id", onChange: () => {}, }; return ( <Stack spacing="md"> <SearchInput {...requiredProps} /> <SearchInput icon={Hospital} {...requiredProps} /> <SearchInput icon={null} {...requiredProps} /> </Stack> ); };
This demo shows the SearchInput
in action:
() => { const [value, setValue] = React.useState(); const [isLoading, setIsLoading] = React.useState(false); function fetchPatients(filter) { const patientNames = [ { name: "John Campbell" }, { name: "Jonathan Cash" }, { name: "John Hawthorne" }, { name: "John Smith" }, { name: "John Wilson" }, { name: "Johnny Wilson" }, ]; if (typeof filter === "string") { return patientNames.filter((patient) => patient.name.toLowerCase().includes(filter.toLowerCase()) ); } return patientNames; } const [list, setList] = React.useState(fetchPatients("")); function changeHandler(event) { setValue(event.target.value); setIsLoading(true); } React.useEffect(() => { const timeoutId = setTimeout(() => { const patients = fetchPatients(value); setList(patients); setIsLoading(false); }, 2000); return () => clearTimeout(timeoutId); }, [value]); return ( <div style={{ width: "180px" }}> <Stack> <SearchInput value={value} isLoading={isLoading} onChange={(event) => changeHandler(event)} id="search" name="search" /> {!isLoading && list.map((item, key) => <p key={key}>{item.name}</p>)} </Stack> </div> ); };
Description
Determines whether the user has the ability to interact with the input.
Type
boolean
Default Value
false
Description
Adds additional styling to indicate failed validation.
Type
boolean
Default Value
false
Description
Replaces the default icon with another.
Type
FC<IconType>
Description
The id of the input. Used to connect label to input.
Type
string
Description
Determines if the Search input is currently loading. This adds a a spinner adornment to the end of the input.
Type
boolean
Description
Mask Props
Type
MaskProps
Description
The name of the text input. Submitted with its owning form as part of a name/value pair.
Type
string
Description
The callback function. Triggered when any character is added or removed from the input.
Type
ChangeEventHandler<HTMLInputElement> & ((e: ChangeEvent<HTMLInputElement>) => void)
Description
Shows an example of what could be inputted. Only visible when no value is present. Commonly used as a "pseudo" helper message.
Type
string
Description
aligns the Text of the input
Type
("left" | "right"
Description
A string specifying the type of input element type to render. If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.
Type
HTMLInputTypeAttribute
Description
The inputted value. Can be used to prepopulate data.
Type
(string | number | readonly string[]) & string