0.0.1 • Published 9 months ago

@glide-ui/react-listbox v0.0.1

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

@glide-ui/react-listbox

An accessible Listbox component for React applications, offering custom select functionality with full WAI-ARIA compliance. Currently, it supports single-value selection, with multi-select support planned for future releases.

📦 Installation

You can install the library using either npm or yarn:

npm install --save @glide-ui/react-listbox

or

yarn add @glide-ui/react-listbox

Demo

Check out the Demo Usage

Controlled listbox example

import { useState } from 'react';
import Listbox from '@glide-ui/react-listbox';

const Options = [
  { id: 1, label: 'United States', value: 'us' },
  { id: 2, label: 'Canada', value: 'ca' },
  { id: 3, label: 'United Kingdom', value: 'un' },
  { id: 4, label: 'Australia', value: 'au' },
  { id: 5, label: 'India', value: 'in' },
  { id: 6, label: 'Germany', value: 'de' },
  { id: 7, label: 'France', value: 'fr' },
  { id: 8, label: 'Japan', value: 'jp' },
  { id: 9, label: 'China', value: 'cn' },
  { id: 10, label: 'Brazil', value: 'br' },
];

const ListboxBasic = () => {
  const [value, setValue] = useState('');
  return (
    <Listbox value={value} label="Select a country" onChange={setValue}>
      {Options.map(option => (
        <Listbox.Option
          key={option.id}
          value={option.value}
          label={option.label}
        >
          {option.label}
        </Listbox.Option>
      ))}
    </Listbox>
  );
};

export default ListboxBasic;

API Props

Listbox Component – API Props

Prop NameTypeRequiredDescription
valuestringYesThe currently selected value in the listbox.
labelstringYesThe label displayed for the listbox, shown when no value is selected.
optionsOption[]NoAn array of options to display in the listbox. Each option should include id, label, and value.
childrenReact.ReactNodeNoCustom child components to render inside the listbox.
renderOption(option: Option, selected: boolean, index: number) => React.ReactNodeNoA custom renderer function for each option. The function also provides a flag to indicate if the option is selected. See the option interface below.
renderValue(label: string, value: string) => React.ReactNodeNoA custom renderer function for the selected value.
onChange(value: string) => voidYesA callback function triggered when the selected value changes.

Listbox.Option Component - API Props

Prop NameTypeRequiredDescription
labelstringYesThe text displayed for the option.
valuestringYesThe value associated with the option, used for selection and identification.
childrenReact.ReactNodeNoCustom content to render inside the option. Overrides the label if provided.
disabledbooleanNoSpecifies whether the option is disabled. Defaults to false.

Option Interface

Field NameTypeRequiredDescription
idnumberYesA unique identifier for the option.
labelstringYesThe label to display for the option.
valuestringYesThe value associated with the option.