Tabler React 2 Docs
Dropdown Input
Dropdown Input
Dropdown inputs are used to select an option from a list of options.
Signature
import { DropdownInput } from "tabler-react-2";
<DropdownInput {...props}>{children}</DropdownInput>;
Props
Prop | Required | Type | Default | Description |
---|---|---|---|---|
prompt | Yes | String | The text to display in the dropdown. | |
values | Yes | ItemList | A list of items to display in the dropdown. | |
items | Yes | ItemList | A list of items to display in the dropdown. Alias of values . | |
value | No | String | The selected value's id. | |
onChange | No | Function | The function to call when the value is changed. | |
loading | No | Boolean | Whether to show a loading indicator. | |
disabled | No | Boolean | Whether the dropdown is disabled. | |
disabledText | No | String | The text to display when the dropdown is disabled. | |
label | No | String | The text to display in the label. |
Basic Usage
The DropdownInput
component is used to select an option from a list of options.
<DropdownInput prompt="Select a value" items={[ { id: 1, label: "Sam" }, { id: 2, label: "Bob" }, { id: 3, label: "John" }, ]}/>
State Machine
The DropdownInput
component manages its own state. To fully implement a stateful DropdownInput
, you need to provide the value
, onChange
and items
props. The items
prop will be an ItemList type, the value
prop should be the id of the value you wish to be selected.
If I am selecting from a list of states, and my value
should ne the 2-letter state abbreviation, but to offer the user the full state name, you would set up your items
prop as follows:
items={[ { id: "AL", label: "Alabama", isGoodState: false }, { id: "AK", label: "Alaska", isGoodState: false }, { id: "AZ", label: "Arizona", isGoodState: false }, { id: "AR", label: "Arkansas", isGoodState: false }, { id: "OH", label: "Ohio", isGoodState: true }, // long live the best state! ...]}
If you wanted to default to Ohio, you would pass in the ID of the state as the value:
value = "OH";
Then, when you handle the change event, you would get the full object back:
onChange={(value) => console.log(value)}
// Output:// >> { id: "OH", label: "Ohio", isGoodState: true }
You can also pass in other keys to the items
object. They will not be used by the dropdown, but they will be returned in the change event (example the isGoodState
key above).
Hiding the search input
By default, the dropdown will show a search input. You can hide it by passing in showSearch={false}
.
<DropdownInput prompt="Select a value" items={[ { id: 1, label: "Sam" }, { id: 2, label: "Bob" }, { id: 3, label: "John" }, ]} showSearch={false}/>
Disabled
You can disable the dropdown by passing in disabled={true}
.
<DropdownInput prompt="Select a value" items={[...]} disabled={true}/><DropdownInput prompt="Select a value" items={[...]} disabled={true} disabledText="Custom disabled text"/>
label
You can pass in a label
prop to show a label for the dropdown.
<DropdownInput prompt="Select a value" items={[ { id: 1, label: "Sam" }, { id: 2, label: "Bob" }, { id: 3, label: "John" }, ]} label="Select a value"/>
Loading
You can show a loading indicator by passing in loading={true}
.
<DropdownInput prompt="Select a vadlue" items={[ { id: 1, label: "Sam" }, { id: 2, label: "Bob" }, { id: 3, label: "John" }, ]} loading={true}/>
ItemList
The items
prop of the DropdownInput
component accepts an array of objects. It accepts 2 types of entries: an item
and a divider
.
Item
An item
object has the following properties:
Prop | Required | Type | Default | Description |
---|---|---|---|---|
id | Yes | Any | The id of the item. Must be unique. | |
label | Yes | String | The text to display in the item. | |
dropdownText | No | String | The text to display in the dropdown. If not provided, the label will be used. |
type ItemList = Array<{ id: string | number; label: string; dropdownText?: string;}