Select
Installation
yarn add @spark-web/select
Allows the user to make a single selection from a list of values — usually in a
form. If only a few options are provided, consider using a RadioButton
instead.
Usage
Field
Each select input must be accompanied by a Field with a label. Effective form labeling helps inform users which selection to make.
Examples
Controlled
A <Select>
can be both controlled and uncontrolled. To control a <Select>
provide a value
, as well as an onChange
function to set the new value when
the select is updated.
Uncontrolled
The <Select>
, by default, is an uncontrolled component, meaning that the form
data is controlled directly by the DOM itself. To access the value, instead of
writing an onChange
handler, you would use a ref
to get form values from the
DOM.
Groups
Related options can be grouped by passing in an array of objects with a label
and option key — where each option is an array of objects with label, value and
(optionally) disabled keys. Internally this uses the
<optgroup>
element.
Props
Prop | Type | Description |
---|---|---|
options | OptionsOrGroups | The values that can be selected by the input. |
data? | DataAttributeMap | Sets data attributes for the element. |
placeholder? | string | Placeholder text for when the input does not have an initial value. |