react-ts-controlled-select
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

React controlled Select

This is a React controlled select.

How to install

npm i react-ts-controlled-select

Interfaces

interface IOption {
  label: string
  value: string
}

interface ISelectProps {
  options: IOption[]
  selected: IOption
  setSelected: React.Dispatch<React.SetStateAction<IOption>>
}

Usage

This component require 3 props to work:

interface ISelectProps {
  options: IOption[]
  selected: IOption
  setSelected: React.Dispatch<React.SetStateAction<IOption>>
}
interface IOption {
  label: string
  value: string
}

const option = {
  label: 'Option label',
  value: 'option value'
}

A set of options look like this:

const options = [
  {
    label: 'Option 1',
    value: 'value 1'
  },
  {
    label: 'Option 2',
    value: 'value 2'
  }
]

Example:

import React, { useState } from 'react'
import Select from 'react-controlled-select'

const ParentComponent = () => {

  const currentOptions = [
    {
      label: 'Option 1',
      value: 'one'
    },
    {
      label: 'Option 2',
      value: 'two'
    },
  ]
  const [selectedOption, setSelectedOption] = useState(currentOptions[0])

  return <Select options={currentOptions} selected={selectedOption} setSelected={setSelectedOption} />
}

Readme

Keywords

none

Package Sidebar

Install

npm i react-ts-controlled-select

Weekly Downloads

0

Version

2.1.0

License

none

Unpacked Size

4.21 kB

Total Files

6

Last publish

Collaborators

  • failsystem