-
Notifications
You must be signed in to change notification settings - Fork 0
hooks / useDebounce #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VaseninaNastya
wants to merge
36
commits into
master
Choose a base branch
from
feature/useDebounce
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
44fe1f8
feat: add useDebounce
687b9c1
feat: add use-debounce-callback
865b3e8
feat: add useDebounse
51cf537
feat: resolve conflicts
256b0de
resolve conflicts
b04673f
fix: fix name
ecdb5be
fix: delete wrong changes
4565f11
fix: fix test
6f85519
fix: fix tests
0fe4443
chore: fix package.json
sadcitizen ef01236
refactor(usedebounce): рефакторинг хуков
458ed3e
Merge branch 'master' into feature/useDebounce
66f4185
feat(useDebounce): добавил tsconfig.build.json
a577238
feat(useDebounce): добавил tsconfig.build.json
610f581
test(hooks/usedebounce): тесты и ридми
c19db26
feat(hooks/usedebounce): рефактор и истории
02e2257
feat(hooks/useDebouncedValue): разобрался со storybook и mdx
4934f14
feat(hooks/useDebouncedCallback): истории
SmorodinVik 26c5f8d
feat(usedebounce): убрал story из документации
SmorodinVik 39661b7
Merge branch 'master' into feature/useDebounce
SmorodinVik e358b55
feat(usedebounce): обновил истории
SmorodinVik 157d3e4
feat(useDebounce): поправил стили
40feece
feat(useDebounce): поправил стили
6fce773
fix(useDebounce): фиксы после ревью
ef4b293
feat(useDebounce): css модули и обновления шаблонов
377bf15
feat(codeOfConduct): обновление имени файла стилей историй
6ddc8c2
Merge branch 'master' into feature/useDebounce
AMZhukov b7a1a1b
feat(useDebounce): add tests for useDebouncedCallback hook
AMZhukov f0665de
feat(useDebounce): refactor useDebouncedCallback to use useTimeout
AMZhukov afc67f2
feat(useDebounce): refactor` index.ts` to export directly
AMZhukov 1554c4f
fix(useDebounce): fix dependencies for `useDebounce`
AMZhukov 22daa71
refactor(useDebounce): fix markdown code `useDebounced`
AMZhukov 328d771
feat(useDebounce): improve type for `useDebouncedValue`
AMZhukov 757042a
fix(useDebounce): fix test for `useDebouncedCallback`
AMZhukov 51f20d4
feat(useArray): добавил css modules в истории
ad7093f
Merge branch 'master' into feature/useDebounce
sadcitizen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| src |
This file contains hidden or 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,9 @@ | ||
| # `@byndyusoft-ui/use-debounce-callback` | ||
| --- | ||
| > A React hook that uses for delaying the execution of functions updates until a specified time period has passed without any further changes | ||
|
|
||
| ### Installation | ||
|
|
||
| ``` | ||
| npm i @byndyusoft-ui/use-debounce-callback | ||
| ``` |
This file contains hidden or 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,33 @@ | ||
| { | ||
| "name": "@byndyusoft-ui/use-debounce-callback", | ||
| "version": "0.0.0", | ||
| "description": "Byndyusoft UI React Hook", | ||
| "keywords": [ | ||
| "byndyusoft", | ||
| "byndyusoft-ui", | ||
| "react", | ||
| "hook", | ||
| "debounce" | ||
| ], | ||
| "author": "Anastasia Vasenina <vasenina@byndyusoft.com>", | ||
| "homepage": "https://github.com/Byndyusoft/ui/tree/master/hooks/use-debounce#readme", | ||
| "license": "Apache-2.0", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Byndyusoft/ui.git" | ||
| }, | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "rimraf dist", | ||
| "lint": "eslint src --config ../../eslint.config.js", | ||
| "test": "jest --config ../../jest.config.js" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/Byndyusoft/ui/issues" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
This file contains hidden or 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,2 @@ | ||
| import useDebounceCallback from './useDebounceCallback'; | ||
| export default useDebounceCallback; |
10 changes: 10 additions & 0 deletions
10
hooks/use-debounce-callback/src/useDebounceCallback.stories.css
This file contains hidden or 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,10 @@ | ||
| .container { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 8px; | ||
| } | ||
|
|
||
| input{ | ||
| max-height: 40px; | ||
| width: 100px; | ||
| } |
7 changes: 7 additions & 0 deletions
7
hooks/use-debounce-callback/src/useDebounceCallback.stories.mdx
This file contains hidden or 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,7 @@ | ||
| import { Meta } from '@storybook/addon-docs'; | ||
| import Readme from '../README.md'; | ||
| import { Markdown } from '@storybook/blocks'; | ||
|
|
||
| <Meta title="hooks/useDebounce" /> | ||
|
|
||
| <Markdown>{Readme}</Markdown> |
35 changes: 35 additions & 0 deletions
35
hooks/use-debounce-callback/src/useDebounceCallback.stories.tsx
This file contains hidden or 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,35 @@ | ||
| import React, { useState } from 'react'; | ||
| import useDebounceCallback from './useDebounceCallback'; | ||
| import './useDebounceCallback.stories.css'; | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
|
|
||
| type Story = StoryObj<typeof Template>; | ||
|
|
||
| const oldValue = 'old value'; | ||
| const newValue = 'new value'; | ||
| const timeout = 2000; | ||
|
|
||
| const Template = () => { | ||
| const [value, setValue] = useState(oldValue); | ||
| const setDebounceValue = useDebounceCallback(setValue, timeout); | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <span>Value: {value}</span> | ||
| <button onClick={() => setDebounceValue(newValue)}>debounced change</button> | ||
| <button onClick={() => setValue(newValue)}>change</button> | ||
| <button onClick={() => setValue(oldValue)}>reset</button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const HookStory: Story = { | ||
| decorators: [() => <Template />] | ||
| }; | ||
|
|
||
| const meta: Meta<typeof Template> = { | ||
| title: 'hooks/useDebounceCallback', | ||
| component: Template | ||
| }; | ||
|
|
||
| export default meta; |
56 changes: 56 additions & 0 deletions
56
hooks/use-debounce-callback/src/useDebounceCallback.tests.tsx
This file contains hidden or 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,56 @@ | ||
| import React, { useState } from 'react'; | ||
| import { render, screen, RenderResult, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import useDebounceCallback from './useDebounceCallback'; | ||
|
|
||
| const oldValue = 'old value'; | ||
| const newValue = 'new value'; | ||
| const timeout = 2000; | ||
| const debouncedChangeLabel = 'debounced change label'; | ||
| const changeLabel = 'change label'; | ||
| const resetLabel = 'reset label'; | ||
|
|
||
| const Setup = (): JSX.Element => { | ||
| const [value, setValue] = useState(oldValue); | ||
| const setDebounceValue = useDebounceCallback(setValue, timeout); | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <span> | ||
| Value: <span>{value}</span> | ||
| </span> | ||
| <button aria-label={debouncedChangeLabel} onClick={() => setDebounceValue(newValue)}> | ||
| debounced change | ||
| </button> | ||
| <button aria-label={changeLabel} onClick={() => setValue(newValue)}> | ||
| change | ||
| </button> | ||
| <button aria-label={resetLabel} onClick={() => setValue(oldValue)}> | ||
| reset | ||
| </button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| describe('hooks/useDebounceCallback', () => { | ||
| test('useDebounceCallback works', async () => { | ||
| render(<Setup />); | ||
|
|
||
| const debouncedChangeButton = await screen.findByLabelText(debouncedChangeLabel); | ||
| const changeButton = await screen.findByLabelText(changeLabel); | ||
| const resetButton = await screen.findByLabelText(resetLabel); | ||
| expect(screen.findByText(oldValue)).toBeInTheDocument(); | ||
| userEvent.click(changeButton); | ||
| expect(screen.findByText(newValue)).toBeInTheDocument(); | ||
| userEvent.click(resetButton); | ||
| expect(screen.findByText(oldValue)).toBeInTheDocument(); | ||
| userEvent.click(debouncedChangeButton); | ||
| expect(screen.findByText(oldValue)).toBeInTheDocument(); | ||
| await waitFor( | ||
| () => { | ||
| expect(screen.findByText(newValue)).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 2500 } | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,30 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| export default function useDebouncedCallback<A extends any[]>( | ||
| callback: (...args: A) => void, | ||
| delay: number | ||
| ): (...args: A) => void { | ||
| const argsRef = useRef<A>(); | ||
| const timeout = useRef<ReturnType<typeof setTimeout>>(); | ||
|
|
||
| function cleanup() { | ||
| if (timeout.current) { | ||
| clearTimeout(timeout.current); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => cleanup, []); | ||
|
|
||
| const setDebouncedValue = (...args: A) => { | ||
| argsRef.current = args; | ||
| cleanup(); | ||
|
|
||
| timeout.current = setTimeout(() => { | ||
| if (argsRef.current) { | ||
| callback(...argsRef.current); | ||
| } | ||
| }, delay); | ||
| }; | ||
|
|
||
| return setDebouncedValue; | ||
| } |
This file contains hidden or 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,12 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "declaration": true, | ||
| "declarationDir": "dist", | ||
| "outDir": "dist", | ||
| "module": "commonjs", | ||
| "target": "es6" | ||
| }, | ||
| "include": ["src"], | ||
| "exclude": ["node_modules", "src/*.tests.ts"] | ||
| } |
This file contains hidden or 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 @@ | ||
| src |
This file contains hidden or 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,9 @@ | ||
| # `@byndyusoft-ui/use-debounce` | ||
| --- | ||
| > A React hook that uses to delay update state updates until a specified time period has passed without any further changes | ||
|
|
||
| ### Installation | ||
|
|
||
| ``` | ||
| npm i @byndyusoft-ui/use-debounce | ||
| ``` |
This file contains hidden or 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,33 @@ | ||
| { | ||
| "name": "@byndyusoft-ui/use-debounce", | ||
| "version": "0.0.0", | ||
| "description": "Byndyusoft UI React Hook", | ||
| "keywords": [ | ||
| "byndyusoft", | ||
| "byndyusoft-ui", | ||
| "react", | ||
| "hook", | ||
| "debounce" | ||
| ], | ||
| "author": "Anastasia Vasenina <vasenina@byndyusoft.com>", | ||
| "homepage": "https://github.com/Byndyusoft/ui/tree/master/hooks/use-debounce#readme", | ||
| "license": "Apache-2.0", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Byndyusoft/ui.git" | ||
| }, | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "rimraf dist", | ||
| "lint": "eslint src --config ../../eslint.config.js", | ||
| "test": "jest --config ../../jest.config.js" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/Byndyusoft/ui/issues" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
This file contains hidden or 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,2 @@ | ||
| import useDebounce from './useDebounce'; | ||
| export default useDebounce; |
This file contains hidden or 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,10 @@ | ||
| .container { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 8px; | ||
| } | ||
|
|
||
| input{ | ||
| max-height: 40px; | ||
| width: 100px; | ||
| } |
|
Oustinger marked this conversation as resolved.
|
This file contains hidden or 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,7 @@ | ||
| import { Meta } from '@storybook/addon-docs'; | ||
| import Readme from '../README.md'; | ||
| import { Markdown } from '@storybook/blocks'; | ||
|
|
||
| <Meta title="hooks/useDebounce" /> | ||
|
|
||
| <Markdown>{Readme}</Markdown> |
This file contains hidden or 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,31 @@ | ||
| import React from 'react'; | ||
| import useDebounce from './useDebounce'; | ||
| import './useDebounce.stories.css'; | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
|
|
||
| type Story = StoryObj<typeof Template>; | ||
|
|
||
| const Template = () => { | ||
| const [debouncedValue, setDebouncedValue] = useDebounce('', 2000); | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <span> | ||
| Debounced value: <span>{debouncedValue}</span> | ||
| </span> | ||
| <input onChange={e => setDebouncedValue(e.target.value)} /> | ||
| <button onClick={() => setDebouncedValue('10000')}>set debounced value</button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const HookStory: Story = { | ||
| decorators: [() => <Template />] | ||
| }; | ||
|
|
||
| const meta: Meta<typeof Template> = { | ||
| title: 'hooks/useDebounce', | ||
| component: Template | ||
| }; | ||
|
|
||
| export default meta; |
This file contains hidden or 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,36 @@ | ||
| import React from 'react'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import useDebounce from './useDebounce'; | ||
|
|
||
| const debouncedValueLabel = 'debounced value'; | ||
| const inputValue = '1111'; | ||
|
|
||
| const Setup = (): JSX.Element => { | ||
| const [debouncedValue, setDebouncedValue] = useDebounce('', 2000); | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <span> | ||
| Debounced value: <span aria-label={debouncedValueLabel}>{debouncedValue}</span> | ||
| </span> | ||
| <input onChange={e => setDebouncedValue(e.target.value)} /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| describe('hooks/useDebounce', () => { | ||
| test('useDebounce works', async () => { | ||
| render(<Setup />); | ||
|
|
||
| const input = await screen.findByRole('textbox'); | ||
| userEvent.type(input, inputValue); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| expect(screen.getByLabelText(debouncedValueLabel)).toHaveTextContent(inputValue); | ||
| }, | ||
| { timeout: 2100 } | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,10 @@ | ||
| import { useState } from 'react'; | ||
| import useDebounceCallback from '@byndyusoft-ui/use-debounce-callback'; | ||
|
|
||
| export default function useDebounce(value: string, delay = 300): [string, (value: string) => void] { | ||
|
VaseninaNastya marked this conversation as resolved.
Outdated
|
||
| const [debouncedValue, setValue] = useState(value); | ||
|
|
||
| const setDebounceValue = useDebounceCallback(setValue, delay); | ||
|
|
||
| return [debouncedValue, setDebounceValue]; | ||
| } | ||
This file contains hidden or 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,12 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "declaration": true, | ||
| "declarationDir": "dist", | ||
| "outDir": "dist", | ||
| "module": "commonjs", | ||
| "target": "es6" | ||
| }, | ||
| "include": ["src"], | ||
| "exclude": ["node_modules", "src/*.tests.ts"] | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.