Value Selection
The Select component can be used to select one or multiple values from a list. The values can be of any type, for example number, string or object as described in the data binding section.
Default value and default selection
The value and the default selection of the Select are two different things:
- Value - the actual value of the component. It is displayed in the input, returned as the selected value and passed to the
onChangeevent. Set an initial value with thevalueprop (controlled, together withonChange) or thedefaultValueprop (uncontrolled). - Default selection - the
defaultSelectionoption. It is only a pre-selection inside the opened picker, shown while the component has no value. It is not set as the value: the input stays empty and nothing is returned until the user confirms the selection, for example with the Set button. Without it, the desktop UI pre-selects nothing, while the touch UI (seetouchUi) pre-selects the first item in the selection highlight line.
// Initial value: shown in the input, returned as the value
<Select data={drinks} defaultValue="margarita" />
// Default selection: only pre-selected in the opened picker, the input stays empty
<Select data={drinks} defaultSelection="margarita" />
Single value selection
By default the Select operates in single value selection mode, but can be used to select multiple values from a list as well.
Selecting a single value
import { useState } from 'react';
import { Select } from '@mobiscroll/react';
function App() {
const [drinks] = useState([
{ text: 'Mohito', value: 'mohito'},
{ text: 'Long Island', value: 'longisland'},
{ text: 'Margarita', value: 'margarita'}
]);
const [selectedValue, setSelectedValue] = useState(null);
const myChange = (ev) => setSelectedValue(ev.value);
return <Select data={drinks} value={selectedValue} onChange={myChange} />
}
Multiple value selection
Multiselection can be enabled using the selectMultiple option.
Selecting multiple items from a list
import { useState } from 'react';
import { Select } from '@mobiscroll/react';
function App() {
const [bonuses] = useState(['Free ticket', 'Free Drink', 'Free Hug']);
const [selectedValues, setSelectedValues] = useState(null);
const myChange = (ev) => setSelectedValues(ev.value);
return <Select selectMultiple={true} data={bonuses} value={selectedValues} onChange={myChange} />
}