@idui/react-tree-select

React TreeSelect Component

Usage no npm install needed!

<script type="module">
  import iduiReactTreeSelect from 'https://cdn.skypack.dev/@idui/react-tree-select';
</script>

README

TreeSelect React Component

NPM Size JavaScript Style Guide Coverage Status LICENSE

Install

npm install --save @idui/react-tree-select
yarn add @idui/react-tree-select

See props in Docs

Basic Example

import React from 'react'
import TreeSelect from '@idui/react-tree-select'

const nodes = [
    { label: 'Cake', value: 'cake' },
    { label: 'Coffee', value: 'coffee', children: [
        { label: 'Cappuccino', value: 'cappuccino' },
        { label: 'Latte', value: 'latte' },
        { label: 'Americano', value: 'americano' },
    ]},
]

function Example() {
  return <TreeSelect 
      nodes={nodes}
      valueKey="value"
      labelKey="label"
      canSelectParentNode={false}
      closeOnSelect={true}
      maxTreeHeight="300px"
      hideScrollbar={false}
      searchPlaceholder="Search"
      // searchTreeBottomAddon={<Pagination ... />}
      // searchTreeScrollBottomAddon={<Waypoint ... />}
  />
}

Custom renderers

import React from 'react'
import styled from 'styled-components';
import TreeSelect, { SearchTreeLeaf } from '@idui/react-tree-select'
import { TextInput, SearchInput } from '@idui/react-inputs';

function Example() {
  return <TreeSelect 
            nodes={nodes}
            renderInput={({ value, onClear, placeholder, open, close, toggle, isOpen }) => <Input 
                {...props} 
                readOnly 
                placeholder="Select value" 
                rightAddon={<ArrowDown color={isOpen ? "lightskyblue" : "gray"} />}
                colors={customColors}
            />}
            renderSearchInput={(props) => <StyledSearchTreeInput 
                {...props} 
                autoFocus 
                placeholder="Search"
            />}
            renderLeaf={(props) => <SearchTreeLeaf {...props} />}
        />
}

const Input = styled(TextInput)`
  width: 100%;
`;

const SearchTreeInput = styled(SearchInput)`
  width: 100%;
`;

const StyledSearchTreeInput = styled(SearchTreeLeaf)`
  .highlight {
    background-color: #ffa7a7;
  }
  padding: 5px 10px;
  &:hover {
    background-color: #fafafa;
  }
`

See more details in storybook

License

MIT © kaprisa57@gmail.com