Search
Baseβ
Search on submitβ
The onSubmit property must be used to define a callback that will be called when the user clicks the submit button or presses enter on the keyboard.
Search on changeβ
The onChange property is used to define a callback that will be called when the user types something.
The onClear propertyβ
The onClear property is used to define a callback that will be called when the user clicks the clear button.
Setting up a default valueβ
To render Search with an initial value it is necessary to use the defaultValue property. With it, the Search component will handle state changes by itself.
Controlling the state change'sβ
To control state changes, it is necessary to use the value property together with the callback onChange return.
The loading propertyβ
It is possible to add the loading property to display a Spinner, indicating that some asynchronous search is running.
Async searchβ
React Hook Formβ
Use the Search component together with the React Hook Form. To do this, install this dependency in the project.
yarn add react-hook-form
import React from 'react';
import { useForm } from 'react-hook-form';
import {
Form,
Search,
Button,
Text
} from '@resultadosdigitais/tangram-components';
export default function WithReactHookForm() {
const {
register,
handleSubmit,
formState: { errors }
} = useForm();
const [submitted, onSubmit] = React.useState();
return (
<>
{submitted && (
<Text>
<strong>Submitted:</strong> <code>{JSON.stringify(submitted)}</code>
</Text>
)}
<Form onSubmit={handleSubmit(onSubmit)}>
<Form.Control error={Boolean(errors.search)}>
<Form.Label htmlFor="search-with-rhf">Search</Form.Label>
<Search
id="search-with-rhf"
name="search"
placeholder="Type your search..."
aria-describedby="search-with-rhf"
onSubmit={handleSubmit(onSubmit)}
{...register('search', { required: true })}
/>
{errors.search && (
<Form.Feedback id="search-with-rhf-feedback">
This field is required
</Form.Feedback>
)}
</Form.Control>
<Button type="submit">Submit</Button>
</Form>
</>
);
}
See and test the full example below: