Select
The Select component allows users to choose an option from a dropdown list. It provides several features, such as placeholder text, label, disabled state, and help text. The component is highly customizable and can be integrated into various forms and layouts.
Usage
To use the Select component, import it into your React component and render it with the appropriate props. The following props are available:
- isDisabled (boolean): If true, the select will be disabled.
- isHelping (boolean): If true, a help message will be displayed next to the select.
- isLabelable (boolean): If true, the select will have a visible label.
- helpMode (HelpType): The type of help message to display, either "warning" or "error".
- helpText (string): The text to display in the help message.
- placeholder (string): The text to display as a placeholder in the select.
- name (string): The name of the select field.
- className (string): A CSS class to apply to the select.
- children: The options to be rendered within the select.
import Select from '@aura-design/system/select'; <Select placeholder="Select an option"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </Select>
States
The Select component can be in various states depending on user interaction. The following states are available:
- Default: This is the default state of the select.
- Disabled: This state is triggered when the select is disabled.
- Helping: This state is triggered when the select has a help text associated with it and the user has interacted with it.
- Error: This state is triggered when the select has an error associated with it.
- Focused: This state is triggered when the select is focused.
Accessibility
Selects should be designed with accessibility in mind. The following guidelines should be followed:
- Selects should have a descriptive label that explains the information that should be selected.
- Selects should have a discernible focus state and be keyboard navigable.
- The select and its label should have an appropriate contrast ratio between the text and background color.
- If a select has an error associated with it, it should be announced to screen reader users when the select receives focus.
- Similarly, if a select has a help text associated with it, it should be announced to screen reader users when the select receives focus.
Usage of Select Component with useForm Hook
The Select component can be used in a form along with the useForm hook to easily manage form values and validations.
To use the Select component with useForm, first import the useForm and useInputValue hooks from the same file as the Select component.
import { useForm } from '@aura-design/system/form'
Next, create an object with key/value pairs where the keys match the names of the form select and the values are the initial values for those Selectors.
const initialFormValues = { name: '', email: '', password: '', selector: '' }
Then, call the useForm hook and pass the object of initial form values. This hook will return an object where each key is the name of an select and the value is an object containing the current value, any errors, and whether or not the select has been touched.
const formData = useForm(initialFormValues)
Now, you can use the Input component in your form and pass in the appropriate props. To link the Select component to the useForm hook, use the name of the select as the key to access the corresponding value object from the formData object.
<Select placeholder="Select an option" {...formData.selector}> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </Select>
Note that the useForm hook does not handle any form submissions or server-side logic. It is solely responsible for managing form values and validations on the client-side.