SelectButton is used to choose single or multiple items from a list using buttons.
import { SelectButton } from 'primereact/selectbutton';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/selectbutton/selectbutton.min.js"></script>
Dropdown is used as a controlled component with value and onChange properties along with the options collection. There are two alternatives of how to define the options property; One way is providing a collection of SelectItem instances having label-value pairs whereas other way is providing an array of arbitrary objects along with the optionLabel and optionValue properties to specify the label/value field pair. In addition, options can be simple primitive values such as a string array, in this case no optionLabel or optionValue is necessary.
Options as SelectItems
const citySelectItems = [
{label: 'New York', value: 'NY'},
{label: 'Rome', value: 'RM'},
{label: 'London', value: 'LDN'},
{label: 'Istanbul', value: 'IST'},
{label: 'Paris', value: 'PRS'}
];
<SelectButton value={city} options={citySelectItems} onChange={(e) => setValue(e.value)}></SelectButton>
Options as any type
const cities = [
{name: 'New York', code: 'NY'},
{name: 'Rome', code: 'RM'},
{name: 'London', code: 'LDN'},
{name: 'Istanbul', code: 'IST'},
{name: 'Paris', code: 'PRS'}
];
<SelectButton optionLabel="name" value={city} options={cities} onChange={(e) => setValue(e.value)}></SelectButton>
<SelectButton optionLabel="name" optionValue="code" value={city} options={cities} onChange={(e) => setValue(e.value)}></SelectButton>
When optionValue is not defined, value of an option refers to the option object itself.
SelectButton allows selecting only one item by default and setting multiple option enables choosing more than one item. In multiple case, model property should be an array.
Options support templating using the itemTemplate property that references a function to render the content. Notice the usage of optionLabel, although it is not rendered visually, it is still required to be used as the list key.
<SelectButton optionLabel="name" optionValue="code" value={city} options={cities} onChange={(e) => setValue(e.value)} itemTemplate={itemTemplate} />
const itemTemplate = (option) => {
// custom item content
}
Name | Type | Default | Description |
---|---|---|---|
label | string | null | Label of the option. |
value | string | null | Value of the option. |
className | string | null | ClassName of the option. |
title | string | null | Tooltip text of the option. (Not supported) |
disabled | boolean | false | Whether the option is disabled or not. |
Any valid attribute is passed to the root element implicitly, extended properties are as follows;
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
value | any | null | Value of the component. |
options | array | null | An array of objects to display as the available options. |
optionLabel | string | null | Name of the label field of an option when an arbitrary objects instead of SelectItems are used as options. |
optionValue | string | null | Name of the value field of an option when arbitrary objects are used as options instead of SelectItems. |
optionDisabled | function | string | null | Property name or getter function to use as the disabled flag of an option, defaults to false when not defined. |
tabIndex | number | null | Index of the element in tabbing order. |
multiple | boolean | false | When specified, allows selecting multiple values. |
unselectable | boolean | true | Whether selection can be cleared. |
disabled | boolean | false | When present, it specifies that the element should be disabled. |
style | string | null | Inline style of the element. |
className | string | null | Style class of the element. |
dataKey | string | null | A property to uniquely match the value in options for better performance. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
itemTemplate | function | null | Function that gets the option and returns the content. |
Name | Parameters | Description |
---|---|---|
onChange | event.originalEvent: browser event event.value: Single value or an array of values that are selected. | Callback to invoke on value change. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.