Dropdown
Reference
- The
Dropdowncomponent is designed to render a dropdown menu with options, allowing users to select one or multiple values. - It applies custom styles and configurations through props.
Props
- The
Dropdowncomponent accepts the following props:
-
defaultValues(optional):- The currently selected value(s) in the dropdown.
- For single-select, this is an object representing the selected option.
- For multi-select, this is an array of selected option objects.
-
picklistOptions(required):- An array of objects representing the options available in the dropdown.
- Each object should have the following structure:
{
value: 'optionValue', // The value of the option
label: 'Option Label' // The label displayed in the dropdown
}
-
isMultiselect(optional, default:false):- A boolean that determines whether the dropdown allows multiple selections.
- If
true, the dropdown supports selecting multiple options.
-
customStyle(optional):- An object containing custom styles to override the default styles.
-
onValueChange(optional):- A callback function triggered when the selected values change.
- It receives the updated values as an argument.
-
onFocusOut(optional):- A callback function triggered when the dropdown loses focus (blur event).
-
isDisabled(optional, default:false):- A boolean that determines whether the dropdown is disabled.
- If
true, the dropdown is read-only and cannot be interacted with.
-
components(optional):- An object that allows customization of the dropdown's internal components (e.g., custom option rendering).
-
menuPlacement(optional, default:'auto'):- A string that determines the placement of the dropdown menu.
- Possible values:
'auto','top','bottom'.
-
placeholder(optional):- A string that specifies the placeholder text displayed when no value is selected.
- Defaults to
'Select an option'.
-
isLoading(optional, default:false):- A boolean that determines whether a loading indicator is displayed in the dropdown.
- Useful for asynchronous data fetching.
-
isClearable(optional):- A boolean that determines whether the selected values can be cleared using a clear button.
Usage Example
Example 1: Basic Single-Select Dropdown
function Example(pageContext) {
const { protrakComponents } = React.useContext(customWidgetContext);
const { Dropdown } = protrakComponents;
const [selectedValue, setSelectedValue] = useState(null);
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'option3', label: 'Option 3' },
];
const handleValueChange = (newValue) => {
setSelectedValue(newValue);
};
return (
<Dropdown
defaultValues={selectedValue}
picklistOptions={options}
onValueChange={handleValueChange}
placeholder="Select an option"
/>
);
};
- This renders a single-select dropdown with three options.
- The selected value is managed in the
selectedValuestate.
Example 2: Multi-Select Dropdown
function Example(pageContext) {
const { protrakComponents } = React.useContext(customWidgetContext);
const { Dropdown } = protrakComponents;
const [selectedValues, setSelectedValues] = React.useState([]);
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'option3', label: 'Option 3' },
];
const handleValueChange = (newValues) => {
setSelectedValues(newValues);
};
return (
<Dropdown
defaultValues={selectedValues}
picklistOptions={options}
isMultiselect={true} // pass true
onValueChange={handleValueChange}
placeholder="Select multiple options"
/>
);
};
- This renders a multi-select dropdown where users can select multiple options.
- The selected value is managed in the
selectedValuestate.