|
2 | 2 |
|
3 | 3 | [Example](https://redmadrobot.github.io/use-dropdown)
|
4 | 4 |
|
5 |
| -**useDropdown** is a hook that helps you build a custom |
| 5 | +**useDropdown** is a hook that helps you to build a custom |
6 | 6 | select element. It also takes away the pain
|
7 | 7 | of positioning of a dropdown element and repositioning it
|
8 | 8 | on scroll.
|
9 | 9 |
|
10 | 10 | ## Usage
|
11 | 11 |
|
12 | 12 | ```typescript jsx
|
13 |
| -const { |
14 |
| - isOpen, |
15 |
| - highlightedIndex, |
16 |
| - inputValue, |
17 |
| - getWrapperProps, |
18 |
| - getInputProps, |
19 |
| - getMenuProps, |
20 |
| - getItemProps, |
21 |
| -} = useDropdown<T>({ |
22 |
| - items, |
23 |
| - onSelect, |
24 |
| - reducer, |
25 |
| - autoScroll, |
26 |
| - direction, |
27 |
| -}); |
| 13 | +import React, {useState} from 'react'; |
| 14 | +import {useDropdown} from './useDropdown'; |
| 15 | + |
| 16 | +const Select = ({value, onChange, items}) => { |
| 17 | + const [input, setInput] = useState(''); |
| 18 | + const handleSelect = (value) => { |
| 19 | + onChange(value); |
| 20 | + } |
| 21 | + |
| 22 | + const { |
| 23 | + isOpen, |
| 24 | + setOpen, |
| 25 | + highlightedIndex, |
| 26 | + getWrapperProps, |
| 27 | + getInputProps, |
| 28 | + getMenuProps, |
| 29 | + getItemProps, |
| 30 | + } = useDropdown({ |
| 31 | + items, |
| 32 | + onSelect: handleSelect, |
| 33 | + }); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className='wrapper' {...getWrapperProps()}> |
| 37 | + <input |
| 38 | + {...getInputProps()} |
| 39 | + className='input' |
| 40 | + value={input} |
| 41 | + onChange={(ev) => setInput(ev.target.value)} |
| 42 | + /> |
| 43 | + { |
| 44 | + isOpen && createPortal( |
| 45 | + <ul className='menu' {...getMenuProps()}> |
| 46 | + { |
| 47 | + items.map(item, index) => ( |
| 48 | + <li |
| 49 | + key={item.value} |
| 50 | + className={highlightedIndex === index ? 'item active' : 'item'} |
| 51 | + {...getItemProps(item, index)} |
| 52 | + > |
| 53 | + {item.label} |
| 54 | + </li> |
| 55 | + ) |
| 56 | + } |
| 57 | + </ul>, document.body |
| 58 | + ) |
| 59 | + } |
| 60 | + </div> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | + |
28 | 65 | ```
|
29 | 66 |
|
30 | 67 | ### Arguments
|
31 | 68 |
|
32 | 69 | `useDropdown` accepts following arguments:
|
33 | 70 |
|
34 |
| -- **items** - `Array<T>` |
| 71 | +- **items** - `Array<T>` _required_ |
35 | 72 | Menu elements of your dropdown. It is expected that they will
|
36 | 73 | be the same type you passed to `useDropdown` generic
|
37 | 74 |
|
38 |
| -- **onSelect** - `(item: T) => void` |
| 75 | +- **onSelect** - `(item: T) => void` _required_ |
39 | 76 | Function which is called each time you click on element or
|
40 | 77 | select it with Enter key
|
41 | 78 |
|
42 |
| -- **reducer** |
| 79 | +- **reducer** - `(state: DropdownState, action: ReducerAction) => DropdownState` |
43 | 80 | Using this function you can change how `useDropdown` reacts
|
44 | 81 | to certain events; For example, you can prevent dropdown
|
45 | 82 | from closing after user selects an option
|
46 | 83 |
|
47 |
| -- **autoScroll** - `boolean` |
48 |
| - Use it when dropdown is inside scrolling container. |
49 |
| - |
50 |
| -- **direction** - `Direction` |
51 |
| - Direction in which dropdown should be opened. |
52 |
| - |
53 | 84 | ### State and methods
|
54 | 85 |
|
55 | 86 | `useDropdown` returns it's state and provides methods that
|
56 | 87 | you should use to build your dropdown:
|
57 | 88 |
|
58 |
| -- **isOpen** - `boolean` |
| 89 | +- **isOpen** - `boolean` |
59 | 90 | Current state of dropdown. Use it to decide whether you should
|
60 | 91 | show menu or not
|
61 | 92 |
|
62 |
| -- **highlightedIndex** - `number` |
| 93 | +- **highlightedIndex** - `number` |
63 | 94 | Shows the index of an element that is currently highlighted by
|
64 | 95 | cursor or with arrow keys. Use it to apply styles.
|
65 | 96 |
|
66 |
| -- **inputValue** - `string` |
| 97 | +- **inputValue** - `string` |
67 | 98 | Current value of input.
|
68 | 99 |
|
69 |
| -- **getWrapperProps** - `(): WrapperProps` - _required_ |
| 100 | +- **getWrapperProps** - `(): WrapperProps` - _required_ |
70 | 101 | Apply these props to block that represents your dropdown element.
|
71 | 102 | This block will be used to calculate the width of dropdown along
|
72 | 103 | with it's position on the screen.
|
73 | 104 |
|
74 |
| -- **getInputProps** - `(): InputProps` - _optional_ |
| 105 | +- **getInputProps** - `(): InputProps` - _optional_ |
75 | 106 | You can use it on your input. This method will help `useDropdown`
|
76 | 107 | to track input's value and it also allows menu to be opened each time
|
77 | 108 | input recieves focus.
|
78 | 109 |
|
79 |
| -- **getMenuProps** - `(): MenuProps` - _required_ |
| 110 | +- **getMenuProps** - `(): MenuProps` - _required_ |
80 | 111 | Returns props for block element that wraps your menu items. It is
|
81 | 112 | necessary for correct positioning of you dropdown.
|
82 | 113 |
|
83 |
| -- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_ |
| 114 | +- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_ |
84 | 115 | Method for getting props for every item in your items list. Pass
|
85 | 116 | item and it's index to this method.
|
0 commit comments