The <Select>
component is a variant of the <Dropdown>
one, especially targeted to be used in form components. The main differences are:
- It can only work in declarative mode, i.e. it cannot be virtualized by passing a child function.
- Every
<SelectItem>
accepts avalue
prop, that will be matched against thevalue
passed to<Select>
, and the right option will be selected. As opposed to a traditional<SELECT>
DOM element, any reference can be used as a value, both primitives and objects. - Items do not accept an
onClick
. Instead, you will get the value of the selected option by receiving it from anonChange
method.
The component is always controlled, meaning you have to update its value
from an onChange
callback for it to reflect the newly selected property.
Usage
import { Select } from 'nr1'
Examples
Basic
<Select onChange={(evt, value) => alert(value)}> <SelectItem value="a">Value is "a"</SelectItem> <SelectItem value="b">Value is "b"</SelectItem> <SelectItem value="c">Value is "c"</SelectItem></Select>
With label and info
<Select label="Items" info="Info value" onChange={(evt, value) => alert(value)}> <SelectItem value="a">Value is "a"</SelectItem> <SelectItem value="b">Value is "b"</SelectItem> <SelectItem value="c">Value is "c"</SelectItem></Select>
With inline label
<Select label="Items" labelInline onChange={(evt, value) => alert(value)}> <SelectItem value="a">Value is "a"</SelectItem> <SelectItem value="b">Value is "b"</SelectItem> <SelectItem value="c">Value is "c"</SelectItem></Select>
With description
<Select description="Description value" onChange={(evt, value) => alert(value)}> <SelectItem value="a">Value is "a"</SelectItem> <SelectItem value="b">Value is "b"</SelectItem> <SelectItem value="c">Value is "c"</SelectItem></Select>
With invalid message
<Select invalid="Invalid message value" onChange={(evt, value) => alert(value)}> <SelectItem value="a">Value is "a"</SelectItem> <SelectItem value="b">Value is "b"</SelectItem> <SelectItem value="c">Value is "c"</SelectItem></Select>
Controlled component
class ControlledSelect extends React.Component { constructor() { super(...arguments);
this.state = { value: null, };
this._onChange = this._onChange.bind(this); }
_onChange(event, value) { this.setState({ value }); }
render() { return ( <Select onChange={this._onChange} value={this.state.value}> <SelectItem value="1">Item 1</SelectItem> <SelectItem value="2">Item 2</SelectItem> <SelectItem value="3">Item 3</SelectItem> </Select> ); }}
Props
string | Provide a descriptive label for this control, e.g. "Accounts". |
REQUIREDnode | List of options expressed as a set of |
string | Appends class names to the component. |
string | Message with instructions on how to fill the form field. |
boolean | If |
string | Additional information can be displayed in an info tooltip next to the Label. |
boolean|string | When true, sets the field in an invalid state, in order to notify the user attention is needed over this particular field. This property can be a |
string | Text to display as label. |
boolean | Display the label inline the form control.Use only when the component is not inside a |
function | Callback fired any time the value of the select is changed.You can get the value back as the second argument of the function ( |
boolean | If |
enum[] | Spacing property. Spacing is defined as a tuple of zero to four values, which follow the same conventions as CSS properties like <Array of |
object | Inline style for custom styling.Should be used only for positioning and spacing purposes. |
string | Adds a |
any | Value matching the item selected. |