
SelectMenu React component A very easy to use and customisable React select dropdown.
Interactive example page Design your own SelectMenu component here : Interactive SelectMenu example page
Features
$3 - array of options with or without values - array of optgroup with labels
$3 - handles a custom onChange function to get the selected option's value - handles the select native HTML behaviour
$3 - link your select label with the SelectMenu component
$3
$3
$3 - a default selected option can be set
$3 - displays a button to clear the selected option
$3 - dropdown can display eather on bottom or top of the select input
$3 - aria attributes - 100% control with keyboard
$3
$3 - In V2, some classNames are added for a better CSS control.
$3 - via props - via external CSS
Installation > npm i @zafle/select_menu
$3 - NodeJS >= 20.18.0 - npm >= 10.8.1
Basic usage The only required prop is options for a basic usage.
``jsx import { SelectMenu } from '@zafle/select_menu'
export default function App() { const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
return } `
Advanced usage
$3 - #### Options with values
To use an array of options with values, use textField and valueField props.
`jsx import { SelectMenu } from '@zafle/select_menu'
export default function App() { const options = [ { option: 'Blue', code: 01 }, { option: 'Red', code: 02 }, ]
return } `
- #### Options with optGroups
To use an array of options with optGroups, use optGroupLabelField and optGroupOptionsField props.
`jsx import { SelectMenu } from '@zafle/select_menu'
export default function App() { const options = [ { label: 'Fruits', options: ['Apple', 'Banana', 'Orange'], }, { label: 'Vegetables', options: ['Bean', 'Pea', 'Leek'], }, ]
return ( options={options} optGroupLabelField="label" optGroupOptionsField="options" /> ) } `
- #### Options with optGroups and values
To use an array of options with optGroups and values, use optGroupLabelField, optGroupOptionsField, textField and valueField props.
`jsx import { SelectMenu } from '@zafle/select_menu'
export default function App() { const options = [ { label: 'Fruits', options: [ { option: 'Apple', id: 'F1' }, { option: 'Banana', id: 'F2' }, { option: 'Orange', id: 'F3' }, ], }, { label: 'Vegetables', options: [ { option: 'Bean', id: 'V1' }, { option: 'Pea', id: 'V2' }, { option: 'Leek', id: 'V3' }, ], }, ]
return ( options={options} optGroupLabelField="label" optGroupOptionsField="options" textField="option" valueField="id" /> ) } `
$3 - #### Controlled Form
To use the SelectMenu component in a controlled form, use onChangeValue prop.
`jsx import { SelectMenu } from '@zafle/select_menu'
export default function App() { const [selectedValue, setSelectedValue] = useState('')
const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
const handleChange = (option) => { setSelectedValue(option) }
return ( <>
The selected option is {selectedValue}
>
)
}
`
- #### Uncontrolled Form
To use SelectMenu in an uncontrolled form, use
name
prop.
`
jsx import { SelectMenu } from '@zafle/select_menu'export default function App() { const [formData, setFormData] = useState('')
const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
const getFormData = (e) => { e.preventDefault() setFormData(e.target.color.value) }
return ( <>
The selected option is {formData}
onSubmit={(e) => { getFormData(e) }} > Validate > ) }`
$3 In V2 only, to enable reset programmatically, the form must be controlled and the
selectedOption
prop is required.-
selectedOption
allows to reset the SelectMenu component by clearing selection value programmatically by changing its value to null
| ''. -
null
: giving a null value to selectedOption
will reset the selection to the default value even when selected option === '' - ''
: giving an empty string value will not reset the selected option to its default value when selected option === '' (it will reset on empty string)- To reset on default value (which can be set with
defaultSelectedOption
), set resetToDefault
prop to true
.
`
jsx import { SelectMenu } from '@zafle/select_menu'export default function App() { const [selectedValue, setSelectedValue] = useState('')
const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
const handleChange = (option) => { setSelectedValue(option) }
const handleOnClickReset = () => { setSelectedValue(null) }
return ( <>
The selected option is {selectedValue}
options={options} onChangeValue={handleChange} selectedOption={selectedValue} // Allows to reset programmatically defaultSelectedOption="Blue" // Optional - Set default selected option resetToDefault={true} // Optional /> Reset > ) }`
$3 To link your
tag to the SelectMenu component, use id prop, and set htmlFor attribute in your tag accordingly. This will allow a click event on the label to trigger the opening of the SelectMenu dropdown.
`jsx import { SelectMenu } from '@zafle/select_menu'export default function App() { const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
return ( <> Choose a color > ) }
`
$3 SelectMenu is set to display all necessary aria attributes. The whole component and its functionalities can be controlled with keyboard (tab and arrows).
For a full accessible experience set the
labelId prop accordingly to the id attribute of the tag to enable the aria-labelledBy attribute to be efficient.
`jsx import { SelectMenu } from '@zafle/select_menu'export default function App() { const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']
return ( <> Choose a color > ) }
`
Component props | Prop | Description | Value example(s) | | :------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- | | options _type: Array_ _default: none_ | Required Array of options to display in the dropdown menu. | Different types of options data | | onChangeValue _type: Function_ _default: null_ | Callback function triggered when an option is selected. The returned value is the selected option's value. | Controlled Form | | selectedOption _type: string_ _default: undefined_ | Selected option state controlled with
onChangeValue prop.NOTE: for controlled forms only. | Enable reset programmatically | | resetToDefault _type: boolean_ _default: false_ | Indicates if the selected option must reset on default value.NOTE: for controlled forms only. | Enable reset programmatically | | name _type: string_ _default: none_ | Gives a name attribute to the input that will stock the value of the selected option. | Uncontrolled Form | | textField _type: string_ _default: none_ | The property name in the options array for the option's text to display. Use if the value option is different from the text option.NOTE: Must be used with valueField. | Options with values | | valueField _type: string_ _default: none_ | The property name in the options array for the option's value. Use if the value option is different from the text option.NOTE: Must be used with textField. | Options with values | | optGroupLabelField _type: string_ _default: none_ | The property name in the options array for the optGroup label.NOTE: Must be used with optGroupOptionsField. | Options with optGroups | | optGroupOptionsField _type: string_ _default: none_ | The property name in the options array for the optGroup options.NOTE: Must be used with optGroupLabelField. | Options with optGroups | | id _type: string_ _default: nanoid()_ | Gives a custom id attribute to the input that stocks the selected option value. | Label and SelectMenu component link | | labelId _type: string_ _default: none_ | Enables the input's aria-labelledBy attribute that stocks the selected option value to be efficient. | Accessibility | | defaultSelectedOption _type: string_ _default: undefined_ | Sets a default selected option. | "option\*text" or "first" for first option | | maxWidth \_type: string\* _default: "250px"_ | Gives the max-width CSS property for the whole component. | "200px", "50%" | | border _type: string_ _default: "1px solid #2b2b2b"_ | Sets the border CSS property for the whole component. | "unset" for no border, "2px solid blue" | | borderRadius _type: string_ _default: "4px"_ | Sets the border-radius for the component. | "unset" for no border-radius, "10px" (single value required) | | containerMargin _type: string_ _default: "0"_ | Defines the margin around the component. | Any valid CSS margin value ("10px", "1em", "0 auto") | | boxShadow _type: string_ _default: "4px 4px 10px rgba(0, 0, 0, 0.4)"_ | Sets the box-shadow of the component. | "unset" for no shadow, "4px 4px 10px black" | | boxShadowOnOpen _type: boolean_ _default: false_ | Defines if the shadow appears only when the dropdown is open. | true = only when dropdown is open, false` = always visible | | colorOnFocus _type: string_ _default: "default"_ | Defines the focus color behavior. | "none" for no color on focus, "default" for browser default, Custom color | | inputHeight _type: string_ _default: "unset"_ | Sets the height of the select input. | Any valid CSS height value ("40px", "auto", "unset") | | inputBackground _type: string_ _default: "#d5d5d5"_ | Defines the background of the input. | Any valid CSS background value | | inputTextColor _type: string_ _default: "inherit"_ | Sets the text color inside the input. | Any valid CSS color | | inputVerticalPadding _type: string_ _default: "8px"_ | Sets the vertical padding inside the input. | Any valid CSS padding value | | inputHorizontalPadding _type: string_ _default: "10px"_ | Sets the horizontal padding inside the input. | Any valid CSS padding value | | inputFontSize _type: string_ _default: "16px"_ | Defines the font size of the text inside the input. | Any valid CSS font-size value ("14px", "1rem") | | dropdownBackground _type: string_ _default: "white"_ | Sets the background of the dropdown menu. | Any valid CSS background value | | dropdownMaxHeight _type: string_ _default: "unset"_ | Defines the maximum height of the dropdown menu. | Any valid CSS max-height value ("300px", "unset") | | dropdownVerticalPadding _type: string_ _default: "8px"_ | Sets the vertical padding inside the dropdown. | Any valid CSS padding value | | dropdownPosition _type: string_ _default: "bottom"_ | Defines whether the dropdown opens above or below the input. | "bottom" or "top" | | optionTextColor _type: string_ _default: "inherit"_ | Sets the text color of options in the dropdown. | Any valid CSS color | | hoveredOptionBackground _type: string_ _default: "#484848"_ | Defines the background of a hovered option. | Any valid CSS background value | | hoveredOptionTextColor _type: string_ _default: "white"_ | Defines the text color of a hovered option. | Any valid CSS color | | optionVerticalPadding _type: string_ _default: "4px"_ | Sets the vertical padding of options. | Any valid CSS padding value | | optionHorizontalPadding _type: string_ _default: "14px"_ | Sets the horizontal padding of options. | Any valid CSS padding value | | optionFontSize _type: string_ _default: "14px"_ | Defines the font size of options. | Any valid CSS font-size value | | optGroupLabelTextColor _type: string_ _default: "inherit"_ | Sets the text color of optgroup labels. | Any valid CSS color | | optGroupLabelFontSize _type: string_ _default: "16px"_ | Defines the font size of optgroup labels. | Any valid CSS font-size value | | optGroupVerticalPadding _type: string_ _default: "4px"_ | Sets the vertical padding of optgroup labels. | Any valid CSS padding value | | optGroupHorizontalPadding _type: string_ _default: "10px"_ | Sets the horizontal padding of optgroup labels. | Any valid CSS padding value | | optGroupMarginTop _type: string_ _default: "2px"_ | Defines the top margin of optgroup labels. | Any valid CSS margin value |
License MIT
Author Zafle