1.0.0 • Published 11 months ago

@element-public/react-select v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

Select

Description

Select provides a single-option select menu.

See live demos on storybook

Storybook Select Demos

Install bundle from npm-e

npm i @element-public/react-components @element-public/themes

Optional: install the component individually

npm i @element-public/react-select @element-public/themes

Open ~/.npmrc in an editor and add the following line to enable the @element-public scope:

@element-public:registry=https://npm.platforms.engineering

Troubleshooting

See below if you have never installed a package from Bayer's npm-enterprise or run into the following error:

npm ERR! code E401
npm ERR! Unable to authenticate, your authentication token seems to be invalid.
npm ERR! To correct this please trying logging in again with:
npm ERR!     npm login

Setup an access token

See the devtools npm-e guide to learn how to create an access token if this is the first time you are using a npm-e package at Bayer or you do not have a line that starts with the following in your ~/.npmrc file:

//npm.platforms.engineering/:_authToken=

Notes

There are two categories of the Select component, Select and Multi-Select. Select allows a user to choose one option from a list and Multi-Select allows a user to choose one or more options from a list.

Notable props for the Select component include clearable, which adds an Icon Button that allows users to clear their choice(s) in Select, and showSelectAll, which shows an option for the user to select all options from a list. Another important prop is requireConfirmation which is used with Multi-Select, requires a confirmation before the onChange will be called, and is required for the cancelLabel and confirmLabel props to be used.

Additionally, several combinations of styles and features exist for Select.

The Filled style of Select shows a textbox with a light gray background color behind the text, whereas the Outlined style of Select shows a textbox with no added background color but a solid line around it.

The Autocomplete feature fills in an option from the list based on characters input by the user and can be used with either style of Select.

Usage

The Select component can be used within many other components, like Menu, Card, and Modal, and is most commonly used in forms. Select can also include smaller components like Icons, as shown in the 'Options with Leading Icon' story.

Option Schema Examples

export const simpleArray = ['Cat', 'Dog', 'Ferret'];

export const options = [
    { displayName: 'Grains', value: 'grains' },
    { displayName: 'Vegetables', value: 'vegetables' },
    { displayName: 'Fruit', value: 'fruit' }
];

export const foodSchema = [
    {
        groupName: 'Carbs',
        id: 'carbs',
        options: [
            { text: 'Fries', value: 'fries' },
            { text: 'Bread', value: 'bread' }
        ]
    },
    {
        groupName: 'Dairy',
        id: 'dairy',
        options: [
            { text: 'Milk', value: 'milk' },
            { text: 'Cheese', value: 'cheese' }
        ]
    }
];

export const groupSchema = [
    {
        groupName: 'Chevy',
        id: 'chevy',
        options: [
            { text: 'Malibu', value: 'malibu' },
            { text: 'Suburban', value: 'suburban' }
        ]
    },
    {
        groupName: 'Ford',
        id: 'ford',
        options: [
            { text: 'Focus', value: 'focus' },
            {
                text: 'F-150',
                value: 'f150'
            }
        ]
    }
];

    const nestedOptions = [
        {
            id: 'cars',
            options: [
                {
                    groupName: 'Chevy',
                    id: 'chevy',
                    options: [
                        {
                            text: 'Malibu',
                            value: 'malibu'
                        },
                        {
                            text: 'Suburban',
                            value: 'suburban'
                        }
                    ]
                },
                {
                    groupName: 'Ford',
                    id: 'ford',
                    options: [
                        {
                            text: 'Focus',
                            value: 'focus'
                        },
                        {
                            text: 'F-150',
                            value: 'f150'
                        }
                    ]
                }
            ]
        }
        {
            id: 'food',
            options: [
                {
                    groupName: 'Carbs',
                    id: 'carbs',
                    options: [
                        {
                            text: 'Fries',
                            value: 'fries'
                        },
                        {
                            text: 'Bread',
                            value: 'bread'
                        }
                    ]
                },
                {
                    groupName: 'Dairy',
                    id: 'dairy',
                    options: [
                        {
                            text: 'Milk',
                            value: 'milk'
                        },
                        {
                            text: 'Cheese',
                            value: 'cheese'
                        }
                    ]
                }
            ]
        }
    ];

Testing

When unit testing the Select - unlike previous versions the menu is not rendered until visible. If you are testing for specific menu options, use defaultOpen so that the menu will be rendered and testable.

Select Props

NameTypeDefaultRequiredDescription
ariaLabelstringnullfalseAccessibility label for assistive technologies.
ariaLabelledBystringnullfalseId of the component being labeled for assistive technologies.
cancelLabelstring'Cancel'falseLabel for the Cancel button on the multi-select label.
categoryNameKeystring'categoryName'falseName of the key for category name in options.
categoryValueKeystring'categoryValue'falseName of the key for category value in options.
classNamestringundefinedfalseThe css class name to be passed through to the component markup.
clearablebooleanfalsefalseIf true select is clearable.
confirmLabelstring'Confirm'falseLabel for the Confirm button on the multi-select label.
containerPropsobjectnullfalseCustom properties to be passed to the react-select instance.
defaultOpenbooleanfalsefalseThe menu will be visible by default.
densebooleanfalsefalseStyles the select in a more compact format. When used with filled an external label must be used. Dense is not recommended when using as a multi select and multiDisplayType is set to pills.
disabledbooleanfalsefalsePrevents the user from interacting with the component.
filterConfigobject{ 'ignoreCase': true, 'ignoreAccents': true, 'matchFrom': 'any', 'stringify': option => ${option.label} ${option.value}, 'trim': true }falseCustom configuration for the select filter.
filterFunctionfunctionundefinedfalseCustom filter function.
fixedHeightInputbooleanfalsefalseIf true the select input will remain the same height even if rendered selections exceed the max height.
fixedHeightInputbooleanfalsefalseIf true the select input will remain the same height even if rendered selections exceed the max height.
groupNameKeystring'groupName'falseName of the key for group name in options.
groupValueKeystring'groupValue'falseName of the key for group value in options.
helperTextstringnullfalseHelper text to be displayed below Select, by default only visible on focus.
helperTextIdstringundefinedfalseRandomly generated id for the helper text container element. May be overwritten.
helperTextPersistentbooleanfalsefalseSet the helper text to be permanently visible.
helperTextValidationbooleanfalsefalseSet the helper text to use validation styles. Helper text changes with valid (e.g., helper text will appear the current theme's danger color when valid is set to 'false').
hideSelectedOptionsbooleanfalsefalseRemove the selection option(s) from the list.
hoistedbooleanfalsefalseWhether or not the menu is hoisted to render outside of the parent element. Useful for when the select needs to render in a parent with overflow: hidden. Note, hoisted will disable menuPosition.
idstringnullfalseOverwrite the default, randomly generated id. Each must be unique.
labelstringnullfalseThe placeholder/floating display label.
leadingIconstring|React.ReactNodenullfalseAccepts the name of any valid material icon (see Icon component documentation for a list of valid icons).
menuMaxHeightstringundefinedfalseMax height the menu may take up. If using groups, we recommend adding a menu max height.
menuPositionstring'auto'falseThe direction the menu will open. Auto will attempt to open the menu upwards when appropriate, but this can be explicitly set as well. Note, hoisted will disable menuPosition.Accepted Values: auto, bottom, top
multiDisplayTypestring'pills'falseHow value is displayed if select is a multiselect.Accepted Values: pills, text
multiSelectbooleanfalsefalseEnable multi-selection mode. Note, row highlighting is disabled when using multi-select.
namestringnullfalseName to be added to hidden html input.
noOptionsMessagestring|React.ReactNode'No options'falseMessage to show if no options are available.
openbooleanundefinedfalseThe menu will always be visible.
optionFilterLabelstring'Show only selected options'false(??) When using multi-select mode the menu items will show a switch to show all options or only selected.
optionsobject | string | numbernullfalseOptions for select. See README for schema architecture. Options should not be used with children. If both are sent children will be ignored.
portalContainerstring|React.ReactNodeundefinedfalseThe DOM element the menu should render in if hoisted is set to true. If a string is sent it should be a valid DOM query selector. If one is not sent, Portal will default to document.body.
requireConfirmationbooleanfalsefalseWhen in multi-select mode a confirmation will be required before the onChange will be called.
requiredbooleanfalsefalseWhether or not the input is required. Can be used for validation purposes.
scrollablebooleanfalsefalseIf true, wrapping will be disabled and a scrollable area will be created.
searchablebooleanfalsefalseEnable autocomplete behavior.
selectAllLabelstring'Select All Options'falseLabel for the Select All checkbox.
selectOnTabbooleanfalsefalseThe currently highlighted item will be select when tabbing to the next field.
showCheckboxOptionsbooleantruefalseWhen using a multi-select list the items will have checkboxes to indicate selection. If false, options will not have a checkbox, and selected options will be removed from the list.
showOptionFilterbooleantruefalseWhen using a multi-select mode the menu will include a switch to show all options or only selected.
showSelectAllbooleantruefalseShows a checkbox to select all options.
styleobjectnullfalsePassthrough style object.
suppressInvalidOptionWarningbooleanfalsefalsePrevent console warning when value is not found in the options list.
textKeystring'text'falseName of the key for the text in options.
themeColorstring'primary'falseColor of the select based on the theme.Accepted Values: primary, secondary
tooltipobjectnullfalseIf defined, a Tooltip will appear on the Select input. Useful to show the full value, if selected value is too long for the Select width. The object should be the needed Tooltip properties. Tooltip textprop will be set to the current selected value of the Select. See Tooltip docs for available props.
validbooleannullfalseWhether or not the selected value is valid. Can be used for validation purposes.
valueobject|object|string|numberundefinedfalseThe value of the select.
valueKeystring'value'falseName of the key for the value in options.
variantstring'filled'falseApplies the selected style to the select input.Accepted Values: filled, outlined

Select Deprecated Props

NameTypeDefaultRequiredDeprecatedDescription
isClearablebooleanundefinedfalseRenamed to clearable for consistency.If true select is clearable.
isMultibooleanundefinedfalseRenamed to multiSelect for consistency.If true select is a multi-select.
valueDisplaystringundefinedfalseRenamed to multiDisplayType for clarity.How value is displayed if select is a multiselect.

Select Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects one or more SelectOption components. Options should not be used with children. If both are sent children will be ignored.

Select Events

NameDefaultRequiredParamsDescription
onBlurnullfalseFired when the Select looses focus.
onChangenullfalse1. Name: value, Type: string, Description: Value that is selected.,2. Name: event, Type: object, Description: The javascript eventFired when value of the selected option is changed.
onFocusnullfalseFired when the Select gains focus.
onInputChangenullfalse1. Name: value, Type: string, Description: Value that is entered into the input.,2. Name: action, Type: object, Description: The action that fired the event.Fired when the select is searchable and a user changes the search term.

Select Breaking Changes

Description
box (removed): Use variant='filled' instead.
enhanced (removed): Enhanced is no longer available due to Material Web Components removing native select support.
filled (removed): Use variant='filled' instead.
float (removed): This prop is no longer needed.
fullWidth (removed): Selects are full width by default. Use custom css or a container div to set the width.
includeEmptyOption (removed): This feature is no longer supported, however you may use clearable to allow select to be easily cleared.
inputProps (removed): No longer available.
leadingContent (removed): This feature has been temporarily removed.
nestedList (removed): No longer needed.
outlined (removed): Use variant='outlined' instead.
trailingContent (removed): This feature has been temporarily removed.
width (removed): Selects are full width by default. Use custom css or a container div to set the width.

Select Clear Indicator Props

NameTypeDefaultRequiredDescription
innerPropsobjectundefinedfalseInner props from the react select.

Select Confirmation Props

NameTypeDefaultRequiredDescription
cancelLabelstring'Cancel'falseLabel for the Cancel button on the multi select label.
confirmLabelstring'Confirm'falseLabel for the Confirm button on the multi select label.
themeColorstring'primary'falseSets the theme color for the option filter switch, confirm/cancel buttons and checkboxesAccepted Values: primary, secondary

Select Confirmation Events

NameDefaultRequiredParamsDescription
onConfirmnullfalseFired when confirmation is clicked.

Select Content Props

NameTypeDefaultRequiredDescription
classNamestringundefinedfalseThe css class name to be passed through to the component markup.
leadingbooleanundefinedfalseIndicates the content will appear before the list, otherwise content will assume it is trailing.

Select Content Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenulltrueContent to be rendered inside the component. Accepts any valid markup.

Select Control Component Props

NameTypeDefaultRequiredDescription
hasValuebooleannullfalseWhether or not the select has a value.
innerPropsobjectnullfalseInner props from the react select.
isFocusedbooleannullfalseWhether or not the select is focused.
selectPropsobjectnullfalseSelect props sent to react select.

Select Control Component Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Indicators Container Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the indicators.

Select Label Props

NameTypeDefaultRequiredDescription
extendedHeightbooleannullfalseWhether or not the select control has expanded in height.
fixedHeightInputbooleannullfalseWhether or not the select control has a fixed height.
floatbooleannullfalseWhether or not the label should float.
isOutlinedbooleannullfalseWhether or not the select is the outlined variant.
labelstringempty stringfalseLabel for the select.
selectIdstringempty stringfalseId for the select.
selectPropsobjectnullfalseSelect props sent to react select.

Select Label Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Menu Props

NameTypeDefaultRequiredDescription
getValuefunctionnullfalseReturns the value of the select.
selectPropsobjectnullfalseSelect props sent to react select.
setValuefunctionnullfalseSet the value of the select.

Select Menu Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Menu List Props

NameTypeDefaultRequiredDescription
selectPropsobjectnullfalseSelect props sent to react select.

Select Menu List Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Multi Value Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Multi Value Label Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Option Props

NameTypeDefaultRequiredDescription
dataobjectundefinedfalseData for the select option.
disabledbooleanundefinedfalsePrevent the user from interacting with the component.
getValuefunctionundefinedfalseReturns the value of the select.
innerPropsobjectundefinedfalseInner props from the react select.
isChildOptionbooleanfalsefalseThe option is nested.
isFocusedbooleanundefinedfalseWhether or not the option is focused.
isSelectedbooleanundefinedfalseWhether or not the select is selected.
labelstring|number|React.ReactNodeundefinedfalseThe display value of the SelectOption.
selectPropsobjectundefinedfalseSelect props sent to react select.
setValuefunctionundefinedfalseSets the value of the select.
valueanyundefinedfalseThe value of the SelectOption.

Select Option Deprecated Props

NameTypeDefaultRequiredDeprecatedDescription
selectedbooleanundefinedfalseThe parent Select component will manage the selected state of child SelectOptions. If this is set, it will only change the appearance of the option to show as highlighted.Whether or not the select is selected.

Select Option Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodeundefinedfalseThe display value of the SelectOption. If using more than a simple string or number, custom css may be necessary.

Select Option Filter Props

NameTypeDefaultRequiredDescription
optionFilterLabelstringempty stringfalseLabel for the option filter switch.
optionFilterThemeColorstringempty stringfalseTheme color for the switch.Accepted Values: primary, secondary
setShowOnlySelectedItemsfunctionnullfalseSets the value of the showOnlySelectedItems prop.
showOnlySelectedItemsbooleannullfalseIf true only selected items are shown in the menu.

Select Portal Props

NameTypeDefaultRequiredDescription
getStylesfunctionnullfalseReturns the styles of the component.
menuPositionstringempty stringfalsePosition of the select menu.

Select Portal Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.

Select Value Container Props

NameTypeDefaultRequiredDescription
selectPropsobjectnullfalseSelect props sent to react select.

Select Value Container Render Props

NameTypeDefaultRequiredDescription
childrenReact.ReactNodenullfalseExpects the value containers and indicators.