Skip to content

Commit 1216ea7

Browse files
committed
Updated readme and storybook stories
1 parent d2b7f72 commit 1216ea7

20 files changed

+402
-394
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ max_line_length = 120
1010
tab_width = 2
1111
trim_trailing_whitespace = true
1212

13+
[*.md]
14+
trim_trailing_whitespace = false
15+
1316
[{*.ats,*.ts}]
1417
indent_size = 2
1518
tab_width = 2

.storybook/main.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ module.exports = {
55
],
66
"addons": [
77
"@storybook/addon-links",
8-
"@storybook/addon-essentials"
8+
"@storybook/addon-essentials",
9+
{
10+
name: "@storybook/addon-docs",
11+
options: { transcludeMarkdown: true }
12+
}
913
]
1014
}

README.md

+63-32
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,115 @@
22

33
[Example](https://redmadrobot.github.io/use-dropdown)
44

5-
**useDropdown** is a hook that helps you build a custom
5+
**useDropdown** is a hook that helps you to build a custom
66
select element. It also takes away the pain
77
of positioning of a dropdown element and repositioning it
88
on scroll.
99

1010
## Usage
1111

1212
```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+
2865
```
2966

3067
### Arguments
3168

3269
`useDropdown` accepts following arguments:
3370

34-
- **items** - `Array<T>`
71+
- **items** - `Array<T>` _required_
3572
Menu elements of your dropdown. It is expected that they will
3673
be the same type you passed to `useDropdown` generic
3774

38-
- **onSelect** - `(item: T) => void`
75+
- **onSelect** - `(item: T) => void` _required_
3976
Function which is called each time you click on element or
4077
select it with Enter key
4178

42-
- **reducer**
79+
- **reducer** - `(state: DropdownState, action: ReducerAction) => DropdownState`
4380
Using this function you can change how `useDropdown` reacts
4481
to certain events; For example, you can prevent dropdown
4582
from closing after user selects an option
4683

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-
5384
### State and methods
5485

5586
`useDropdown` returns it's state and provides methods that
5687
you should use to build your dropdown:
5788

58-
- **isOpen** - `boolean`
89+
- **isOpen** - `boolean`
5990
Current state of dropdown. Use it to decide whether you should
6091
show menu or not
6192

62-
- **highlightedIndex** - `number`
93+
- **highlightedIndex** - `number`
6394
Shows the index of an element that is currently highlighted by
6495
cursor or with arrow keys. Use it to apply styles.
6596

66-
- **inputValue** - `string`
97+
- **inputValue** - `string`
6798
Current value of input.
6899

69-
- **getWrapperProps** - `(): WrapperProps` - _required_
100+
- **getWrapperProps** - `(): WrapperProps` - _required_
70101
Apply these props to block that represents your dropdown element.
71102
This block will be used to calculate the width of dropdown along
72103
with it's position on the screen.
73104

74-
- **getInputProps** - `(): InputProps` - _optional_
105+
- **getInputProps** - `(): InputProps` - _optional_
75106
You can use it on your input. This method will help `useDropdown`
76107
to track input's value and it also allows menu to be opened each time
77108
input recieves focus.
78109

79-
- **getMenuProps** - `(): MenuProps` - _required_
110+
- **getMenuProps** - `(): MenuProps` - _required_
80111
Returns props for block element that wraps your menu items. It is
81112
necessary for correct positioning of you dropdown.
82113

83-
- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_
114+
- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_
84115
Method for getting props for every item in your items list. Pass
85116
item and it's index to this method.

example/Dropdown.css

-32
This file was deleted.

example/MultiSelect/MultiSelect.css

-126
This file was deleted.

0 commit comments

Comments
 (0)