diff --git a/docs/src/pages/Tokens.tsx b/docs/src/pages/Tokens.tsx index 4a954df9..25d7e21f 100644 --- a/docs/src/pages/Tokens.tsx +++ b/docs/src/pages/Tokens.tsx @@ -1,5 +1,5 @@ import {useAdaptivity} from '@vkontakte/vkui'; -import React, {FC, useState} from 'react'; +import React, {FC, useMemo, useState} from 'react'; import { TokensActions, @@ -7,6 +7,7 @@ import { TokensHeader, } from '@/components/pages/Tokens'; import tokensData from '@/public/static/data/tokensData.json'; +import {useDebounce} from '@/shared/hooks/useDebounce'; import {ChipOption, Tokens as TokensType, ValueType} from '@/shared/types'; const themes = Object.keys(tokensData); @@ -29,18 +30,34 @@ function transformTags(tokens: TokensType) { .filter((key, index, arr) => arr.indexOf(key) === index); } +function findThemeTags(themeNames: string[]): string[] { + const tags: Record = {}; + + for (const themeName of themeNames) { + const themeTags = transformTags(tokensData[themeName]); + + for (const themeTag of themeTags) { + tags[themeTag] = true; + } + } + + const tagsList = Object.keys(tags); + tagsList.sort(); + + return tagsList; +} + const Tokens: FC = () => { const {viewWidth} = useAdaptivity(); const isTablet = viewWidth > 3; - const [themeTags] = useState>( - transformTags(tokensData[themes[0]]), - ); + const themeTags = useMemo(() => findThemeTags(themes), [themes]); const [selectedTags, setSelectedTags] = useState>([]); const [selectedTheme, setSelectedTheme] = useState(themes[0]); const [selectedValueType, setSelectedValueType] = useState('regular'); const [searchValue, setSearchValue] = useState(''); + const searchValueDebounced = useDebounce(searchValue, 500); const searchChangeHandler = ( event: React.ChangeEvent, @@ -82,11 +99,15 @@ const Tokens: FC = () => { /> - String(tagOption.value), + selectedTags={React.useMemo( + () => + selectedTags.map((tagOption) => + String(tagOption.value), + ), + [selectedTags.join(' ')], )} selectedValueType={selectedValueType} - searchValue={searchValue} + searchValue={searchValueDebounced} /> ); diff --git a/docs/src/shared/hooks/useDebounce.ts b/docs/src/shared/hooks/useDebounce.ts new file mode 100644 index 00000000..be5c408d --- /dev/null +++ b/docs/src/shared/hooks/useDebounce.ts @@ -0,0 +1,17 @@ +import React from 'react'; + +export function useDebounce(value: T, delay: number): T { + const [debouncedValue, setDebouncedValue] = React.useState(value); + + React.useEffect(() => { + const handler = setTimeout(() => { + setDebouncedValue(value); + }, delay); + + return () => { + clearTimeout(handler); + }; + }, [value]); + + return debouncedValue; +}