|
| 1 | +# React Toolkit |
| 2 | + |
| 3 | +A set of reusables for React mainly used by ourselves. |
| 4 | + |
| 5 | +## Components |
| 6 | + |
| 7 | +### ErrorBoundary |
| 8 | + |
| 9 | +Catches errors and passes it to the provided element. |
| 10 | + |
| 11 | +Usage: |
| 12 | + |
| 13 | +```tsx |
| 14 | +import { ErrorBoundary } from '@maskingtech/react-toolkit'; |
| 15 | + |
| 16 | +function ErrorHandler({ error }: { error: unknown }) |
| 17 | +{ |
| 18 | + return <>Oops...</>; |
| 19 | +} |
| 20 | + |
| 21 | +<ErrorBoundary element={ErrorHandler}> |
| 22 | + {/* Content goes here */} |
| 23 | +</ErrorBoundary>; |
| 24 | +``` |
| 25 | + |
| 26 | +## Hooks |
| 27 | + |
| 28 | +### useDebouncedValue |
| 29 | + |
| 30 | +Delays value updates for a period of time. |
| 31 | + |
| 32 | +Usage: |
| 33 | + |
| 34 | +```tsx |
| 35 | +import { useDebouncedValue } from '@maskingtech/react-toolkit'; |
| 36 | + |
| 37 | +function MyComponent() |
| 38 | +{ |
| 39 | + const initialValue: number = 0; // required |
| 40 | + const onChange = (debouncedValue: number) => { }; // optional |
| 41 | + const delay = 300; // optional, default 500 |
| 42 | + |
| 43 | + const [debouncedValue, setValue] = useDebouncedValue(initialValue, onChange, delay); |
| 44 | + |
| 45 | + return <p> |
| 46 | + {debouncedValue} |
| 47 | + <button onClick={() => setValue(debouncedValue + 1)}>Increase</button> |
| 48 | + </p>; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### useFocusOnMount |
| 53 | + |
| 54 | +Gives a form element focus after mount. |
| 55 | + |
| 56 | +Usage: |
| 57 | + |
| 58 | +```tsx |
| 59 | +import { useFocusOnMount } from '@maskingtech/react-toolkit'; |
| 60 | + |
| 61 | +function MyComponent() |
| 62 | +{ |
| 63 | + const ref = useFocusOnMount(); |
| 64 | + |
| 65 | + return <input type="text" ref={ref} />; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### useForm |
| 70 | + |
| 71 | +Provides access to the data of a form after submitting. |
| 72 | + |
| 73 | +Usage: |
| 74 | + |
| 75 | +```tsx |
| 76 | +import { useForm } from '@maskingtech/react-toolkit'; |
| 77 | + |
| 78 | +function MyComponent() |
| 79 | +{ |
| 80 | + const submitHandler = (data: FormData) { console.log(data.get('name')); }; |
| 81 | + |
| 82 | + const [ref, state, handleSubmit] = useForm(submitHandler); |
| 83 | + |
| 84 | + // states: 'pristine' | 'dirty' | 'submitting' |
| 85 | + |
| 86 | + return <form ref={ref} onSubmit={handleSubmit}> |
| 87 | + <input type="text" name="name" /> |
| 88 | + <input type="submit" value="Submit" disabled={state !== 'dirty'} /> |
| 89 | + </form>; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### useFormData |
| 94 | + |
| 95 | +Provides access to the data of a form without submitting. |
| 96 | + |
| 97 | +Usage: |
| 98 | + |
| 99 | +```tsx |
| 100 | +import { useFormData } from '@maskingtech/react-toolkit'; |
| 101 | + |
| 102 | +function MyComponent() |
| 103 | +{ |
| 104 | + const dataHandler = (data: FormData) { console.log(data.get('name')); }; |
| 105 | + |
| 106 | + const [ref, state, handleData] = useFormData(dataHandler); |
| 107 | + |
| 108 | + // states: 'idle' | 'working' |
| 109 | + |
| 110 | + return <form ref={ref}> |
| 111 | + <input type="text" name="name" /> |
| 112 | + <input type="button" value="Go!" disabled={state !== 'working'} /> |
| 113 | + </form>; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### useLoadData |
| 118 | + |
| 119 | +Provides helpers for loading data. |
| 120 | + |
| 121 | +Usage: |
| 122 | + |
| 123 | +```tsx |
| 124 | +import { useLoadData } from '@maskingtech/react-toolkit'; |
| 125 | + |
| 126 | +async function getData() { /* get data here */ } |
| 127 | + |
| 128 | +function MyComponent() |
| 129 | +{ |
| 130 | + const [data, isLoading, refresh, setData] = useLoadData(getData); |
| 131 | + |
| 132 | + if (isLoading) return <>Loading...</>; |
| 133 | + |
| 134 | + return <p> |
| 135 | + {data} |
| 136 | + <button onClick={() => refresh()}>Refresh</button> |
| 137 | + </p>; |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### usePagination |
| 142 | + |
| 143 | +Provides helpers for loading paginated data. |
| 144 | + |
| 145 | +Usage: |
| 146 | + |
| 147 | +```tsx |
| 148 | +import { useLoadData } from '@maskingtech/react-toolkit'; |
| 149 | + |
| 150 | +async function getPageData(page: number) { /* get data here */ } |
| 151 | + |
| 152 | +function MyComponent() |
| 153 | +{ |
| 154 | + const [data, isLoading, isFinished, next, previous, reset, setData] = useLoadData(useLoadData); |
| 155 | + |
| 156 | + if (isLoading) return <>Loading...</>; |
| 157 | + |
| 158 | + return <p> |
| 159 | + {data} |
| 160 | + <button onClick={() => previous()}>Previous</button> |
| 161 | + <button onClick={() => next()}>Next</button> |
| 162 | + <button onClick={() => reset()}>Reset</button> |
| 163 | + </p>; |
| 164 | +} |
| 165 | +``` |
0 commit comments