2.1.0 • Published 2 years ago

react-ts-controlled-select v2.1.0

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

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} />
}
2.1.0

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago

0.1.0

2 years ago