-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useList): port useList from react-use (#810)
* feat(useList): Port useList from react-use Port the useList hook from react-use, retaining the same API. * docs(useList): Write documentation for the useList hook
- Loading branch information
Showing
8 changed files
with
657 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react'; | ||
import { useList } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [ | ||
list, | ||
{ | ||
set, | ||
push, | ||
updateAt, | ||
insertAt, | ||
update, | ||
updateFirst, | ||
upsert, | ||
sort, | ||
filter, | ||
removeAt, | ||
clear, | ||
reset, | ||
}, | ||
] = useList([1, 2, 3, 4, 5]); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => set([1, 2, 3])}>Set to [1, 2, 3]</button> | ||
<button onClick={() => push(0)}>Push 0 to the list</button> | ||
<button onClick={() => updateAt(1, 0)}>Update value at index 1 to 0</button> | ||
<br /> | ||
<br /> | ||
<button onClick={() => insertAt(2, 0)}>Insert value 0 to index 2</button> | ||
<button onClick={() => update((iteratedItem) => iteratedItem > 4, 0)}> | ||
Update all values that are greater than 4 to 0 | ||
</button> | ||
<button onClick={() => updateFirst((iteratedItem) => iteratedItem > 3, 0)}> | ||
Update the first value that is greater than 3 to 0 | ||
</button> | ||
<br /> | ||
<br /> | ||
<button onClick={() => upsert((iteratedItem) => iteratedItem > 5, 0)}> | ||
Upsert the first value that is greater than 5 to 0 | ||
</button> | ||
<button onClick={() => filter((item: number) => item % 2 === 0)}> | ||
Filter out odd values | ||
</button> | ||
<button onClick={() => removeAt(2)}>Remove the element at index 2</button> | ||
<br /> | ||
<br /> | ||
<button onClick={() => sort((a: number, b: number) => a - b)}>Sort ascending</button> | ||
<button onClick={() => sort((a: number, b: number) => b - a)}>Sort descending</button> | ||
<br /> | ||
<br /> | ||
<button onClick={clear}>Clear</button> | ||
<button onClick={reset}>Reset</button> | ||
<pre>{JSON.stringify(list, null, 2)}</pre> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="State/useList" component={Example} /> | ||
|
||
# useList | ||
|
||
Tracks a list and offers functions for manipulating it. | ||
|
||
Manipulating the list directly will not cause a rerender. Instead, use the offered functions. | ||
|
||
> **_This hook provides a stable API, meaning the returned functions do not change between renders_** | ||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
function useList<T>(initialList: IInitialState<T[]>): [T[], ListActions<T>]; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> | ||
|
||
#### Arguments | ||
|
||
- _**initialList**_ _`IInitialState<T[]>`_ - Initial list or function returning a list | ||
|
||
#### Return | ||
|
||
1. **list** - The current list. | ||
|
||
2. **actions** | ||
|
||
- **set** - Replaces the current list. | ||
- **push** - Adds an item or items to the end of the list. | ||
- **updateAt** - Replaces the item at the given index of the list. If the given index is out of | ||
bounds, empty elements are appended to the list until the given item can be set to the given index. | ||
- **insertAt** - Inserts an item at the given index of the list. All items following the given | ||
index are shifted one position. If the given index is out of bounds, empty elements are appended | ||
to the list until the given item can be set to the given index. | ||
- **update** - Replaces all items of the list that match the given predicate with the given item. | ||
- **updateFirst** - Replaces the first item of the list that matches the given predicate with the | ||
given item. | ||
- **upsert** - Replaces the first item of the list that matches the given predicate with the | ||
given item. If none of the items match the predicate, the given item is pushed to the list. | ||
- **sort** - Sorts the list with the given sorting function. If no sorting function is given, | ||
the default Array.prototype.sort() sorting is used. | ||
- **filter** - Filters the list with the given filter function. | ||
- **removeAt** - Removes the item at the given index of the list. All items following the given | ||
index will be shifted. If the given index is out of the bounds of the list, the list will not be | ||
modified, but a rerender will occur. | ||
- **clear** - Deletes all items of the list. | ||
- **reset** - Replaces the current list with the initial list given to this hook. |
Oops, something went wrong.