1.3.0 • Published 3 years ago

selectable-options v1.3.0

Weekly downloads
27
License
MIT
Repository
github
Last release
3 years ago

selectable-options NPM version Test coverage bundle size

React component for selecting options.

This is a fork of the npm package, rc-select, because I'm still waiting for them to review my pull request that refactored a sizable chunk of their codebase while adding new features, and it is uncertain if my changes will ever get merged in.

This may turn into an entirely separate endeavor, but I'm thankful to Ant Design for the initial code to create this component.

Currently the primary difference between this npm package and rc-select is the addition of the props: optionSelectableLabelProp, optionSelectedLabelProp, and persistSelectedLabelsOnly. These props are described below.

Features

  • Customizable labels for when an option is selected vs when they are selectable
  • Use keys Up, Down, Enter to navigate selectable options
  • Supports browsers: Chrome, Firefox, Safari, Edge, IE11+

Install

selectable-options

Usage

Simple

import SelectableOptions from 'selectable-options';

export default () => (
  <SelectableOptions
    options={[
      { value: 'jack' },
      { value: 'lucy' },
      { value: 'yiminghe' },
    ]}
  />
);

API

SelectableOptions props

namedescriptiontypedefault
idhtml id to set on the component wrapperstring''
classNameadditional css class of root dom nodestring''
data-*html data attributes to set on the component wrapperstring''
prefixClsprefix classstring''
animationdropdown animation name. only support slide-up nowstring''
transitionNamedropdown css animation namestring''
choiceTransitionNamecss animation name for selected items at multiple modestring''
dropdownMatchSelectWidthwhether dropdown's with is same with selectbooleantrue
dropdownClassNameadditional className applied to dropdownstring-
dropdownStyleadditional style applied to dropdownobject{}
dropdownAlignadditional align applied to dropdownobject{}
dropdownMenuStyleadditional style applied to dropdown menuobject{}
notFoundContentspecify content to show when no result matches.string'Not Found'
tokenSeparatorsseparator used to tokenize on tag/multiple modestring[]
openControl whether the selectable options dropdown is visibleboolean
defaultOpenControl whether the selectable options dropdown is visible by defaultboolean
placeholderThe placeholder to be shown when no option has been selected yet and nothing has been typed into the search boxReact Node
showSearchWhether the search box for search for selectable options within its dropdown is enabled (only toggles when in a mode that only allows for one option to be selected at a time)booleantrue
showArrowWhether the arrow is shown that toggles the selectable options dropdown visibilitybooleantrue (single mode), false (multiple mode)
allowClearWhether the icon is shown that allows the user to clear the currently selected option as well as anything that is currently typed into the search boxbooleanfalse
tagRenderCustomize how tags are rendered(props: CustomTagProps) => ReactNode-
maxTagTextLengthThe maximum number of characters to show for a tag's labelnumber-
maxTagCountThe maximum number of tags to show (the remaining are truncated)number-
maxTagPlaceholderPlaceholder shown for truncated tagsReactNode | (truncatedTagValues) => ReactNode-
modeRecognized modes: 'combobox', 'multiple', 'tags'stringundefined
disabledWhether the component is disabledbooleanfalse
filterOptionwhether filter options by input value. default filter by option's optionFilterProp prop's valuebooleantrue | function(inputValue: string, option: OptionData)
optionFilterPropwhich prop value of option will be used for filter if filterOption is truestring'value'
filterSortSort function for search options sorting, see Array.sort's compareFunction.function(optionA:Option, optionB: Option)-
optionSelectedLabelPropwhich property of option data is used as the selected option's labelstring'value' | 'children' | 'label'
optionSelectableLabelPropwhich property of option data is used as an option's label to appear in the option dropdownstring'children' | 'label'
persistSelectedLabelsOnlyensures that the search value only matches a selected option's selected label when the search field no longer has focus (only works in combobox mode)booleanfalse
optionLabelProprender option value or option children as content of selectstring'value' | 'children'
defaultValueDefault selected option(s)string | number | OptionData | Array<string | number | OptionData>-
valueControlled currently selected option(s)string | number | OptionData | Array<string | number | OptionData>-
labelInValuewhether to embed label in value, see above value typebooleanfalse
backfillwhether backfill select option to search input (only works in single and combobox mode)booleanfalse
onChangecalled when select an option or input value change(combobox)(value, option: OptionData | OptionData[]) => void-
onSearchcalled when input changed() => void-
onBlurcalled when blur() => void-
onFocuscalled when focus() => void-
onPopupScrollcalled when menu is scrolled() => void-
onSelectcalled when a option is selected. param is option's value and option instance(value, option: Option) => void-
onDeselectcalled when a option is deselected. param is option's value. only called for multiple or tagsfunction(value, option:Option)-
onInputKeyDowncalled when key down on inputfunction(event)-
defaultActiveFirstOptionwhether active first option by defaultbooleantrue
getPopupContainercontainer which popup select menu rendered into(trigger: Node) => Nodefunction() { return document.body;}
getInputElementcustomize input element() => ReactNode-
showActionactions trigger the dropdown to showArray<'focus' | 'click'>[]
autoFocusFocus the component immediately after mountingboolean-
autoClearSearchValueauto clear search input value when multiple select is selected/deselectedbooleantrue
inputIconspecify the select arrow iconReactNode-
clearIconspecify the clear iconReactNode-
removeIconspecify the remove iconReactNode-
menuItemSelectedIconspecify the remove iconReactNode | (props: MenuItemProps) => ReactNode-
appendSelectedTagOptionsToDropdownWhether the selected options (when in "tags" mode) are appended to the end of the dropdown if they are not currently deselectable within the dropdown. (Deselecting options is also possible by clicking the close button for each selected option)booleantrue
dropdownRenderCustomize how the selectable options dropdown is rendered(menu: ReactNode, props: MenuProps) => ReactNode-
loadingWhether the loading icon should be shown (if visible, it appears where the arrow icon would be)booleanfalse
virtualEnable virtual scrollbooleantrue
directiondirection of dropdown'ltr' | 'rtl''ltr'

Modes

  • default - Allows the user to only select one option at a time.
  • combobox - Allows the user to only select one option at a time.
  • multiple - Allows the user to select multiple options at a time.
  • tags - Allows the user to select multiple options at a time. In addition to the provided selectable options, the user may also create their own option by selecting whatever they've typed into the search box.

Methods

namedescriptionparametersreturn
focusfocus select programmatically--
blurblur select programmatically--

OptionData (JSON Object)

namedescriptiontypedefault
classNameadditional class to optionstring''
disabledno effect for click or keydown for this itembooleanfalse
valueThe selectable option's unique value (no other options may have the same value)string | number-
labelThe selectable option's label as it appears when it is selectable in the options dropdown as well as how it appears when shown as a selected option (this can be overridden using props optionSelectedLabelProp and optionSelectableLabelProp)string | ReactNode-
selectableLabel*The option's label as it appears when it is selectable in the options dropdown (there is nothing special about the "selectableLabel" property, it only becomes meaningful when the optionSelectableLabelProp is set to "selectableLabel")string | ReactNode-
selectedLabel*The option's label as it appears when shown as a selected option (there is nothing special about the "selectedLabel" property, it only becomes meaningful when the optionSelectedLabelProp is set to "selectedLabel")string | ReactNode-
titleCustom title shown while hovering on selected value (if not provided, this is created automatically using the option's label or value)string-

*The properties "selectableLabel" and "selectedLabel" do not work without using optionSelectableLabelProp and optionSelectedLabelProp to point to these properties respectively.

OptionGroupData (JSON Object)

namedescriptiontypedefault
labelThe option group's label as it appears in the selectable options dropdownstring | ReactNode-
valuedefault filter by this attribute. if react want you to set key, then key is same as value, you can omit valuestring-
optionsThe options belonging to this option groupOptionData[]-

Development

npm install
npm start

Demos

Tests

npm test -- --silent --coverage

Coverage

npm run coverage

Maintainers

License

selectable-options is released under the MIT license.

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago