0.0.5 • Published 10 months ago

react-simple-stylable-select v0.0.5

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

React simple styleable and accessible select

This is a simple animatable select implementation which is customisable via several components that can be passed in. It uses the ARIA combobox pattern for accessibility and is modelled after the sonder-ui select (see the linked page for more notes on accessibility).

Demo

Stackblitz demo

Installation and usage

npm install react-simple-stylable-select

Usage:

import { Select } from "react-simple-stylable-select";

const countries = ["France", "South Africa", "Japan", "Antigua and Barbuda", "Argentina", "United Kingdom"];
<Select
  name="select"
  options={countries}
  defaultValue="United Kingdom"
  components={{
    SelectValue: ({ selectedIndex }: { selectedIndex: number }) => (
      <div
        style={{
          minWidth: '200px',
          padding: '0.5rem 2rem 0.5rem 0.5rem',
          border: '2px solid #eee',
          background: '#f9f9f9',
          color: '#111',
        }}
      >
        {countries[selectedIndex]}
      </div>
    ),
    OptionListWrapper: ({ children }: { children: React.ReactNode }) => (
      <div
        style={{
          backgroundColor: '#eee',
          color: '#111',
        }}
      >
        {children}
      </div>
    ),
    OptionValue: ({
      option,
      isSelected,
      isFocused,
    }: {
      option: string;
      isSelected: boolean;
      isFocused: boolean;
    }) => (
      <div
        style={{
          padding: '0.5rem',
          outlineOffset: '-2px',
          outline: isFocused ? '2px solid #ccc' : 'none',
          background: isSelected ? '#ccc' : 'none',
        }}
      >
        {isSelected ? '✓ ' : ''}
        {option}
      </div>
    ),
  }}
/>

Props

PropTypeDescription
namestringThe name of the Select when submitted as part of a form
optionsstring[]List of options
id?stringHTML id attribute, it is passed inside
value?stringControllable value which can be used to set the value of the Select
defaultValue?stringInitial value of the Select
onChange?(value: string) => voidA callback function which is called when the value of the Select changes
components?DisplayComponentsComponents which allow additional html/jsx and styling to be added (see table below)

Anatomy

Select view

Display components

ComponentPropsTypeDescription
SelectValue?selectedIndexnumberThe selected option
OptionListWrapper?childrenReact.ReactNodeWraps all options
OptionValue?optionisSelectedisFocusedstringbooleanbooleanWraps each option
Icon?React.ElementTypeAny html/jsx can be used as an icon
0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago