Skip to content

Commit

Permalink
Updated readme and storybook stories
Browse files Browse the repository at this point in the history
  • Loading branch information
eden-lane committed Nov 26, 2021
1 parent d2b7f72 commit 1216ea7
Show file tree
Hide file tree
Showing 20 changed files with 402 additions and 394 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ max_line_length = 120
tab_width = 2
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[{*.ats,*.ts}]
indent_size = 2
tab_width = 2
6 changes: 5 additions & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module.exports = {
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials"
"@storybook/addon-essentials",
{
name: "@storybook/addon-docs",
options: { transcludeMarkdown: true }
}
]
}
95 changes: 63 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,115 @@

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

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

## Usage

```typescript jsx
const {
isOpen,
highlightedIndex,
inputValue,
getWrapperProps,
getInputProps,
getMenuProps,
getItemProps,
} = useDropdown<T>({
items,
onSelect,
reducer,
autoScroll,
direction,
});
import React, {useState} from 'react';
import {useDropdown} from './useDropdown';

const Select = ({value, onChange, items}) => {
const [input, setInput] = useState('');
const handleSelect = (value) => {
onChange(value);
}

const {
isOpen,
setOpen,
highlightedIndex,
getWrapperProps,
getInputProps,
getMenuProps,
getItemProps,
} = useDropdown({
items,
onSelect: handleSelect,
});

return (
<div className='wrapper' {...getWrapperProps()}>
<input
{...getInputProps()}
className='input'
value={input}
onChange={(ev) => setInput(ev.target.value)}
/>
{
isOpen && createPortal(
<ul className='menu' {...getMenuProps()}>
{
items.map(item, index) => (
<li
key={item.value}
className={highlightedIndex === index ? 'item active' : 'item'}
{...getItemProps(item, index)}
>
{item.label}
</li>
)
}
</ul>, document.body
)
}
</div>
)
}


```

### Arguments

`useDropdown` accepts following arguments:

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

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

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

- **autoScroll** - `boolean`
Use it when dropdown is inside scrolling container.

- **direction** - `Direction`
Direction in which dropdown should be opened.

### State and methods

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

- **isOpen** - `boolean`
- **isOpen** - `boolean`
Current state of dropdown. Use it to decide whether you should
show menu or not

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

- **inputValue** - `string`
- **inputValue** - `string`
Current value of input.

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

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

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

- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_
- **getItemProps** - `(item: T, index: number) => ItemProps` - _required_
Method for getting props for every item in your items list. Pass
item and it's index to this method.
32 changes: 0 additions & 32 deletions example/Dropdown.css

This file was deleted.

126 changes: 0 additions & 126 deletions example/MultiSelect/MultiSelect.css

This file was deleted.

Loading

0 comments on commit 1216ea7

Please sign in to comment.