useMultiSelectField
Reads the value of a multi-select field.
function useMultiSelectField(props: {field: string; defaultValue?: string[]}): string[]
Arguments
| Property | Type | Description |
|---|---|---|
| field | string | The name of the field. |
| defaultValue | string[] | The values returned while the field isn't set. |
Returns
The values of the picked options, in the order users picked them. Never null: while the field isn't set, the hook
returns the defaultValue, or an empty array without a defaultValue.
Only a field that isn't set falls back to the defaultValue. If users removed every option, the hook returns an empty
array, so users can turn off everything even if the field has defaults.
Example
src/widgets/example/Example.tsx
import React from 'react'
import {useMultiSelectField} from '@modbros/dashboard-sdk'
export default function Example() {
const parts = useMultiSelectField({field: 'visible_parts', defaultValue: ['label', 'value']})
return (
<div>
{parts.includes('label') && <span>CPU</span>}
{parts.includes('value') && <span>42</span>}
{parts.includes('unit') && <span>%</span>}
</div>
)
}
Setter
useSetMultiSelectField({field}) returns a setter to change the picked options from within the widget, see
Setter Hooks.