Nordnet Design System
Getting StartedPlayground 🎮
Components
Components/Input
SelectGeneral InfoArchitectureProps

Input.Select

Storybook

General Info

There is so much in place when talking about Select component aka Dropdown. So much different use-cases, different visuals, different behaviours.

User stories:

  1. When user clicks (or using the keyboard) on some trigger area, some list of items appears
  2. When user clicks outside, list hides
  3. When user hovers/changing focus on items in list, they appears focused
  4. When user presses up/down, the next/previous list item gets highlighted/focused
  5. When user clicks an item in the list, different things can happen:
    • If the item was selected and this is single select component, do nothing
    • If the item was selected and this is multi select component, unselect the item
    • If the item was not selected before and this is single select component, select the item, close the Dropdown
    • If the item was not selected before and this is multi select component, select the item

Contexts (where the component can live):

  1. Inside a form
  2. Standalone component

One component simply can't deal with all that complexity. Or can it?

Architecture

  1. OnChange and value
  2. Customizable reducer (inner behavior)
  3. Customizable renderers (visuals)

OnChange and value

value of the Input.Select is always an array of objects, which corresponds to the options you provided to the component. The onChange will be called with the new value, again, an array of selected options. This allows to have the same api for multi- and single- selects.

Behavior:

To be able to manage that complexity, the reducer pattern is used in this component. Everything is managed through 1 reducer, which is available through Input.Select.defaults.reducer. Also, default initialState is available through Input.Select.defaults.initialState.

You can easily redefine this reducer, for example to do multiselect:

(Pay attention that in this example SelectedValue renderer is still showing only first selected value, you will need to redefine that)

Renderers:

FormField
SelectedValue
List
ListItem

You can redefine SelectedValue, List and ListItem renderers using

components

prop.

Every renderer has type:

(props, ref) => JSX.Element;

⚠️ You must assign ref to the corresponding node, otherwise you will lose focusing feature

Example:

You can find more examples in storybook

You can also hide FormField with

noFormField

prop, but then you need to redefine SelectedValue renderer

Example:

Props

options
Option[]
required
value
Any
error
Reactnode
extraInfo
Reactnode
success
Boolean | undefined
hideLabel
Boolean | undefined
placeholder
String | undefined
label
Reactnode
required
name
String | undefined
size
"s" | "m" | undefined
disabled
Boolean | undefined
onChange
((newvalue: option[]) => void) | undefined
onFocus
((event: focusevent<element>) => void) | undefined
onBlur
((event: focusevent<element>) => void) | undefined
fullWidth
Boolean | undefined
width
String | undefined
className
String | undefined
components
Partial<record<componenttypes, (props: any, ref: ref<any>) => reactnode>> | undefined
reducer
((state: selectstate, action: action) => selectstate) | undefined
initialState
Any
noFormField
Boolean | undefined
autoFocusFirstOption
Boolean | undefined