|
| 1 | +import React, { ChangeEvent, EventHandler, FunctionComponent, useCallback, useEffect, useState } from 'react'; |
| 2 | +import PageContainer from '../../layouts/pages/pageContainer/PageContainer'; |
| 3 | +import NpmLink from '../../components/NpmLink'; |
| 4 | +import { Button, Col, Input, Space } from 'antd'; |
| 5 | +import RooksHookHeading from './components/RooksHookHeading'; |
| 6 | +import { |
| 7 | + useCounter, |
| 8 | + useDebounce, |
| 9 | + useDidMount, |
| 10 | + useInput, |
| 11 | + useIntervalWhen, |
| 12 | + useLocalstorageState, |
| 13 | + useSessionstorageState, |
| 14 | + useToggle, |
| 15 | + useWillUnmount |
| 16 | +} from 'rooks'; |
| 17 | +import delay from '../../utils/delay'; |
| 18 | +import Flex from '../../components/flex/Flex'; |
| 19 | +import { MinusOutlined, PlusOutlined, ReloadOutlined } from '@ant-design/icons'; |
| 20 | +import Text from 'antd/lib/typography/Text'; |
| 21 | +import getLocalStorageKey from '../../utils/getLocalStorageKey'; |
| 22 | +import TextArea from 'antd/lib/input/TextArea'; |
| 23 | +import searchWords from './data/words.json'; |
| 24 | +import { isEmpty } from 'lodash'; |
| 25 | +import pluralize from 'pluralize'; |
| 26 | + |
| 27 | +interface StorageState { |
| 28 | + text: string; |
| 29 | +} |
| 30 | + |
| 31 | +const initialStorageState: StorageState = { |
| 32 | + text: 'Initial text' |
| 33 | +}; |
| 34 | + |
| 35 | +const titleExtra = ( |
| 36 | + <NpmLink packageName="rooks" plain> |
| 37 | + npm package |
| 38 | + </NpmLink> |
| 39 | +); |
| 40 | + |
| 41 | +const RooksDemoPage: FunctionComponent = () => { |
| 42 | + const [didMountValue, setDidMountValue] = useState<string>('not mounted'); |
| 43 | + |
| 44 | + //! useInput |
| 45 | + const debounceInput = useInput(''); |
| 46 | + const [debounceResults, setDebounceResults] = useState<string[]>(searchWords); |
| 47 | + |
| 48 | + //! useCounter |
| 49 | + const { |
| 50 | + value: timeoutCounter, |
| 51 | + increment: incrementTimeoutCounter, |
| 52 | + decrement: decrementTimeoutCounter, |
| 53 | + reset: resetTimeoutCounter |
| 54 | + } = useCounter(0); |
| 55 | + |
| 56 | + //! useToggle |
| 57 | + const [isIntervalEnabled, toggleIntervalEnabled] = useToggle(true); |
| 58 | + |
| 59 | + //! useLocalstorageState |
| 60 | + const [localStorageState, setLocalStorageState] = useLocalstorageState<StorageState>( |
| 61 | + getLocalStorageKey('rooks-demo', 'localStorageState'), |
| 62 | + initialStorageState |
| 63 | + ); |
| 64 | + //! useSessionstorageState |
| 65 | + const [sessionStorageState, setSessionStorageState] = useSessionstorageState<StorageState>( |
| 66 | + getLocalStorageKey('rooks-demo', 'sessionStorageState'), |
| 67 | + initialStorageState |
| 68 | + ); |
| 69 | + |
| 70 | + //! useDidMount |
| 71 | + useDidMount(async () => { |
| 72 | + await delay(1000); |
| 73 | + setDidMountValue('mounted 1s ago'); |
| 74 | + }); |
| 75 | + |
| 76 | + //! useWillUnmount |
| 77 | + useWillUnmount(async () => { |
| 78 | + await delay(10); |
| 79 | + setDidMountValue('unmounted 0.01s ago'); |
| 80 | + }); |
| 81 | + |
| 82 | + const timeoutCallback = useCallback(() => { |
| 83 | + incrementTimeoutCounter(); |
| 84 | + }, []); |
| 85 | + |
| 86 | + //! useIntervalWhen |
| 87 | + useIntervalWhen(timeoutCallback, 500, isIntervalEnabled); |
| 88 | + |
| 89 | + const handleLocalStorageTextChange = useCallback<EventHandler<ChangeEvent<HTMLTextAreaElement>>>((event) => { |
| 90 | + setLocalStorageState({ |
| 91 | + ...localStorageState, |
| 92 | + text: event.target.value |
| 93 | + }); |
| 94 | + }, []); |
| 95 | + |
| 96 | + const handleSessionStorageTextChange = useCallback<EventHandler<ChangeEvent<HTMLTextAreaElement>>>((event) => { |
| 97 | + setSessionStorageState({ |
| 98 | + ...sessionStorageState, |
| 99 | + text: event.target.value |
| 100 | + }); |
| 101 | + }, []); |
| 102 | + |
| 103 | + const handleEveryDebounceInputChange = useCallback((searchQuery: string) => { |
| 104 | + setDebounceResults( |
| 105 | + searchWords.filter((word) => { |
| 106 | + if (isEmpty(searchQuery)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + return word.trim().toLocaleLowerCase().includes(searchQuery.trim().toLocaleLowerCase()); |
| 111 | + }) |
| 112 | + ); |
| 113 | + }, []); |
| 114 | + |
| 115 | + const handleDebounceInputChange = useDebounce(handleEveryDebounceInputChange, 500); |
| 116 | + |
| 117 | + useEffect(() => handleDebounceInputChange(debounceInput.value), [debounceInput.value]); |
| 118 | + |
| 119 | + return ( |
| 120 | + <PageContainer title="Rooks demo" titleExtra={titleExtra}> |
| 121 | + <Space direction="vertical" size="middle"> |
| 122 | + <Col> |
| 123 | + <RooksHookHeading hooks={['useDidMount', 'useWillUnmount']} /> |
| 124 | + {didMountValue} |
| 125 | + </Col> |
| 126 | + <Col> |
| 127 | + <RooksHookHeading hooks={['useIntervalWhen', 'useCounter', 'useToggle']} /> |
| 128 | + <Flex column gap={6}> |
| 129 | + <Flex row gap={6}> |
| 130 | + <Button onClick={toggleIntervalEnabled}> |
| 131 | + <Text type={isIntervalEnabled ? 'success' : 'danger'}> |
| 132 | + {isIntervalEnabled ? 'Interval enabled' : 'Interval disabled'} |
| 133 | + </Text> |
| 134 | + </Button> |
| 135 | + </Flex> |
| 136 | + <Flex row gap={6} align="center"> |
| 137 | + <Button icon={<PlusOutlined />} size="small" onClick={incrementTimeoutCounter} /> |
| 138 | + <Button icon={<MinusOutlined />} size="small" onClick={decrementTimeoutCounter} /> |
| 139 | + <Button icon={<ReloadOutlined />} size="small" onClick={resetTimeoutCounter} /> |
| 140 | + <h3 className="m-0">{timeoutCounter}</h3> |
| 141 | + </Flex> |
| 142 | + </Flex> |
| 143 | + </Col> |
| 144 | + <Col> |
| 145 | + <RooksHookHeading hooks={['useLocalstorageState']} /> |
| 146 | + <TextArea value={localStorageState.text} onChange={handleLocalStorageTextChange} /> |
| 147 | + </Col> |
| 148 | + <Col> |
| 149 | + <RooksHookHeading hooks={['useSessionstorageState']} /> |
| 150 | + <TextArea value={sessionStorageState.text} onChange={handleSessionStorageTextChange} /> |
| 151 | + </Col> |
| 152 | + <Col> |
| 153 | + <RooksHookHeading hooks={['useDebounce', 'useInput']} /> |
| 154 | + <Flex column gap={6}> |
| 155 | + <Input {...debounceInput} placeholder="Search..." /> |
| 156 | + <TextArea |
| 157 | + readOnly |
| 158 | + placeholder="Results" |
| 159 | + rows={4} |
| 160 | + value={debounceResults.join('\n')} |
| 161 | + showCount={{ formatter: () => pluralize('result', debounceResults.length, true) }} |
| 162 | + /> |
| 163 | + </Flex> |
| 164 | + </Col> |
| 165 | + </Space> |
| 166 | + </PageContainer> |
| 167 | + ); |
| 168 | +}; |
| 169 | + |
| 170 | +export default RooksDemoPage; |
0 commit comments