2.0.8 • Published 5 months ago

@zafle/select_menu v2.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Coverage Status

Select_Menu React component

A very easy to use and customisable React select dropdown.

Live Select_Menu example usage

To try and design your own Select_Menu component and read more doc, visit Select_Menu page example

Features

V2 new releases

  • Unique ID for each Select_Menu component

  • Enable reset programmatically

    • New selectedOption prop

    • In V2, selectedOption allows to reset the SelectMenu component by clearing selection value programmatically by changing its value to null | ''.

    • To reset on default value (which can be set with defaultSelectedOption), set resetToDefault prop to true.

    NOTE : These functionnalities only work with controlled forms. Make sure to provide as welle onChangeValue prop.

  • ClassNames for all essentials tags

    • In V2, some classNames are added for a better CSS control.

All versions features

  • Different types of options data :

    • array of options with or without values
    • array of optgroup with labels
  • Controlled form and uncontrolled form

    • handles a custom onChange function to get the selected option's value
    • handles the select native HTML behaviour
  • Label and SelectMenu component link

    • link your select label with the SelectMenu component
  • Accessibility

    • aria attributes
    • 100% control with keyboard
  • Default selected option

    • a default selected option can be set
  • Clear selection

    • displays a button to clear the selected option
  • Dropdown position

    • dropdown can display eather on bottom or top of the select input
  • Stylable component

    • via props
    • via external CSS

Installation

npm i @zafle/select_menu

Basic usage

Import SelectMenu component

import { SelectMenu } from "@zafle/select_menu"

Use

The only required prop is options for a basic usage.

const options = ["Blue", "Yellow", "Green", "Red", "Purple"]

<SelectMenu options={options} />

Advanced usage

Different types of options datas

  • Options with values

To use an array of options with values, use textField and valueField props.

const options = [{option: "Blue", code: 01}, {option: "Red", code: 02}]

<SelectMenu options={options} textField="option" valueField="code" />
  • Options with optGroups

To use an array of options with optGroups, use optGroupLabelField and optGroupOptionsField props.

const options = [
{
  label: "Fruits",
  options: ["Apple","Banana","Orange"]
},
{
  label: "Vegetables",
  options: ["Bean","Pea","Leek"]
}
]

<SelectMenu 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.

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"},
  ]
}
]

<SelectMenu
  options={options}
  optGroupLabelField="label"
  optGroupOptionsField="options"
  textField="option"
  valueField="id"
/>

Controlled and uncontrolled Forms

  • Controlled Form

To use the SelectMenu component in a controlled form, use onChangeValue prop.

const [selectedValue, setSelectedValue] = useState('')

const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']

const handleChange = (option) => {
  setSelectedValue(option)
}

return (
  <>
    <p>The selected option is {selectedValue} </p>
    <SelectMenu options={options} onChangeValue={handleChange} />
  </>
)
  • Uncontrolled Form

To use SelectMenu in an uncontrolled form, use name prop.

const [formData, setFormData] = useState('')

const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']

const getFormData = (e) => {
  e.preventDefault()
  setFormData(e.target.color.value)
}

return (
  <>
    <p>The selected option is {formData} </p>
    <form
      onSubmit={(e) => {
        getFormData(e)
      }}
    >
      <SelectMenu options={options} name="color" />
      <button type="submit">Validate</button>
    </form>
  </>
)

Enable reset programmatically

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 | ''.

  • To reset on default value (which can be set with defaultSelectedOption), set resetToDefault prop to true.

const [selectedValue, setSelectedValue] = useState('')

const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']

const handleChange = (option) => {
  setSelectedValue(option)
}

const handleOnClickReset = () => {
  setSelectedValue('')
}

return (
  <>
    <p>The selected option is {selectedValue} </p>
    <SelectMenu
      options={options}
      onChangeValue={handleChange}
      selectedOption={selectedValue}
    />

    <button onClick={handleClickReset}>Reset</button>
  </>
)

Label and SelectMenu component link

To link your <label> tag to the SelectMenu component, use id prop, and set htmlFor attribute in your <label> tag accordingly. This will allow a click event on the label to trigger the opening of the SelectMenu dropdown.

const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']

<label htmlFor="color-select">Choose a color</label>
<SelectMenu options={options} id="color-select" />

Accessibility

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 <label> tag to enable the aria-labelledBy attribute to be efficient.

const options = ['Blue', 'Yellow', 'Green', 'Red', 'Purple']

<label id="color-select-label">Choose a color</label>
<SelectMenu options={options} labelId="color-select-label" />

Component props

PropDescriptionValue example(s)
optionstype: Arraydefault: noneRequiredArray of options to display in the dropdown menu.Different types of options data
onChangeValuetype: Functiondefault: nullCallback function triggered when an option is selected. The returned value is the selected option's value.Controlled Form
selectedOptiontype: stringdefault: undefinedSelected option state controlled with onChangeValue prop.NOTE: for controlled forms only.Enable reset programmatically
resetToDefaulttype: booleandefault: falseIndicates if the selected option must reset on default value.NOTE: for controlled forms only.Enable reset programmatically
nametype: stringdefault: noneGives a name attribute to the input that will stock the value of the selected option.Uncontrolled Form
textFieldtype: stringdefault: noneThe 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
valueFieldtype: stringdefault: noneThe 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
optGroupLabelFieldtype: stringdefault: noneThe property name in the options array for the optGroup label.NOTE: Must be used with optGroupOptionsField.Options with optGroups
optGroupOptionsFieldtype: stringdefault: noneThe property name in the options array for the optGroup options.NOTE: Must be used with optGroupLabelField.Options with optGroups
idtype: stringdefault: nanoid()Gives a custom id attribute to the input that stocks the selected option value.Label and SelectMenu component link
labelIdtype: stringdefault: noneEnables the input's aria-labelledBy attribute that stocks the selected option value to be efficient.Accessibility
defaultSelectedOptiontype: stringdefault: undefinedSets 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%"
bordertype: stringdefault: "1px solid #2b2b2b"Sets the border CSS property for the whole component."unset" for no border, "2px solid blue"
borderRadiustype: stringdefault: "4px"Sets the border-radius for the component."unset" for no border-radius, "10px" (single value required)
containerMargintype: stringdefault: "0"Defines the margin around the component.Any valid CSS margin value ("10px", "1em", "0 auto")
boxShadowtype: stringdefault: "4px 4px 10px rgba(0, 0, 0, 0.4)"Sets the box-shadow of the component."unset" for no shadow, "4px 4px 10px black"
boxShadowOnOpentype: booleandefault: falseDefines if the shadow appears only when the dropdown is open.true = only when dropdown is open, false = always visible
colorOnFocustype: stringdefault: "default"Defines the focus color behavior."none" for no color on focus, "default" for browser default, Custom color
inputHeighttype: stringdefault: "unset"Sets the height of the select input.Any valid CSS height value ("40px", "auto", "unset")
inputBackgroundtype: stringdefault: "#d5d5d5"Defines the background of the input.Any valid CSS background value
inputTextColortype: stringdefault: "inherit"Sets the text color inside the input.Any valid CSS color
inputVerticalPaddingtype: stringdefault: "8px"Sets the vertical padding inside the input.Any valid CSS padding value
inputHorizontalPaddingtype: stringdefault: "10px"Sets the horizontal padding inside the input.Any valid CSS padding value
inputFontSizetype: stringdefault: "16px"Defines the font size of the text inside the input.Any valid CSS font-size value ("14px", "1rem")
dropdownBackgroundtype: stringdefault: "white"Sets the background of the dropdown menu.Any valid CSS background value
dropdownMaxHeighttype: stringdefault: "unset"Defines the maximum height of the dropdown menu.Any valid CSS max-height value ("300px", "unset")
dropdownVerticalPaddingtype: stringdefault: "8px"Sets the vertical padding inside the dropdown.Any valid CSS padding value
dropdownPositiontype: stringdefault: "bottom"Defines whether the dropdown opens above or below the input."bottom" or "top"
optionTextColortype: stringdefault: "inherit"Sets the text color of options in the dropdown.Any valid CSS color
hoveredOptionBackgroundtype: stringdefault: "#484848"Defines the background of a hovered option.Any valid CSS background value
hoveredOptionTextColortype: stringdefault: "white"Defines the text color of a hovered option.Any valid CSS color
optionVerticalPaddingtype: stringdefault: "4px"Sets the vertical padding of options.Any valid CSS padding value
optionHorizontalPaddingtype: stringdefault: "14px"Sets the horizontal padding of options.Any valid CSS padding value
optionFontSizetype: stringdefault: "14px"Defines the font size of options.Any valid CSS font-size value
optGroupLabelTextColortype: stringdefault: "inherit"Sets the text color of optgroup labels.Any valid CSS color
optGroupLabelFontSizetype: stringdefault: "16px"Defines the font size of optgroup labels.Any valid CSS font-size value
optGroupVerticalPaddingtype: stringdefault: "4px"Sets the vertical padding of optgroup labels.Any valid CSS padding value
optGroupHorizontalPaddingtype: stringdefault: "10px"Sets the horizontal padding of optgroup labels.Any valid CSS padding value
optGroupMarginToptype: stringdefault: "2px"Defines the top margin of optgroup labels.Any valid CSS margin value

License

MIT

Author

Zafle

2.0.7

5 months ago

2.0.8

5 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.5

5 months ago

2.0.4

5 months ago

2.0.6

5 months ago

2.0.1

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.0

5 months ago