Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
44fe1f8
feat: add useDebounce
Feb 18, 2024
687b9c1
feat: add use-debounce-callback
Mar 21, 2024
865b3e8
feat: add useDebounse
Apr 10, 2024
51cf537
feat: resolve conflicts
Apr 10, 2024
256b0de
resolve conflicts
Apr 10, 2024
b04673f
fix: fix name
Apr 17, 2024
ecdb5be
fix: delete wrong changes
Apr 17, 2024
4565f11
fix: fix test
Apr 17, 2024
6f85519
fix: fix tests
Apr 17, 2024
0fe4443
chore: fix package.json
sadcitizen Sep 29, 2024
ef01236
refactor(usedebounce): рефакторинг хуков
Nov 29, 2024
458ed3e
Merge branch 'master' into feature/useDebounce
Nov 29, 2024
66f4185
feat(useDebounce): добавил tsconfig.build.json
Dec 1, 2024
a577238
feat(useDebounce): добавил tsconfig.build.json
Dec 1, 2024
610f581
test(hooks/usedebounce): тесты и ридми
Dec 2, 2024
c19db26
feat(hooks/usedebounce): рефактор и истории
Dec 2, 2024
02e2257
feat(hooks/useDebouncedValue): разобрался со storybook и mdx
Dec 3, 2024
4934f14
feat(hooks/useDebouncedCallback): истории
SmorodinVik Dec 4, 2024
26c5f8d
feat(usedebounce): убрал story из документации
SmorodinVik Dec 4, 2024
39661b7
Merge branch 'master' into feature/useDebounce
SmorodinVik Dec 4, 2024
e358b55
feat(usedebounce): обновил истории
SmorodinVik Dec 4, 2024
157d3e4
feat(useDebounce): поправил стили
Dec 5, 2024
40feece
feat(useDebounce): поправил стили
Dec 5, 2024
6fce773
fix(useDebounce): фиксы после ревью
Dec 5, 2024
ef4b293
feat(useDebounce): css модули и обновления шаблонов
Dec 5, 2024
377bf15
feat(codeOfConduct): обновление имени файла стилей историй
Dec 5, 2024
6ddc8c2
Merge branch 'master' into feature/useDebounce
AMZhukov Jan 7, 2025
b7a1a1b
feat(useDebounce): add tests for useDebouncedCallback hook
AMZhukov Jan 8, 2025
f0665de
feat(useDebounce): refactor useDebouncedCallback to use useTimeout
AMZhukov Jan 8, 2025
afc67f2
feat(useDebounce): refactor` index.ts` to export directly
AMZhukov Jan 8, 2025
1554c4f
fix(useDebounce): fix dependencies for `useDebounce`
AMZhukov Jan 22, 2025
22daa71
refactor(useDebounce): fix markdown code `useDebounced`
AMZhukov Jan 22, 2025
328d771
feat(useDebounce): improve type for `useDebouncedValue`
AMZhukov Jan 22, 2025
757042a
fix(useDebounce): fix test for `useDebouncedCallback`
AMZhukov Jan 22, 2025
51f20d4
feat(useArray): добавил css modules в истории
Jan 23, 2025
ad7093f
Merge branch 'master' into feature/useDebounce
sadcitizen Mar 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hooks/use-debounce-callback/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
9 changes: 9 additions & 0 deletions hooks/use-debounce-callback/README.md
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
```
33 changes: 33 additions & 0 deletions hooks/use-debounce-callback/package.json
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"
Comment thread
VaseninaNastya marked this conversation as resolved.
Outdated
},
"bugs": {
"url": "https://github.com/Byndyusoft/ui/issues"
},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions hooks/use-debounce-callback/src/index.ts
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 hooks/use-debounce-callback/src/useDebounceCallback.stories.css
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;
}
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 hooks/use-debounce-callback/src/useDebounceCallback.stories.tsx
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 hooks/use-debounce-callback/src/useDebounceCallback.tests.tsx
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 }
);
});
});
30 changes: 30 additions & 0 deletions hooks/use-debounce-callback/src/useDebounceCallback.ts
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;
}
12 changes: 12 additions & 0 deletions hooks/use-debounce-callback/tsconfig.json
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"]
}
1 change: 1 addition & 0 deletions hooks/use-debounce/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
9 changes: 9 additions & 0 deletions hooks/use-debounce/README.md
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
```
33 changes: 33 additions & 0 deletions hooks/use-debounce/package.json
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"
}
}
2 changes: 2 additions & 0 deletions hooks/use-debounce/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import useDebounce from './useDebounce';
export default useDebounce;
10 changes: 10 additions & 0 deletions hooks/use-debounce/src/useDebounce.stories.css
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 hooks/use-debounce/src/useDebounce.stories.mdx
Comment thread
Oustinger marked this conversation as resolved.
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>
31 changes: 31 additions & 0 deletions hooks/use-debounce/src/useDebounce.stories.tsx
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;
36 changes: 36 additions & 0 deletions hooks/use-debounce/src/useDebounce.tests.tsx
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 }
);
});
});
10 changes: 10 additions & 0 deletions hooks/use-debounce/src/useDebounce.ts
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] {
Comment thread
VaseninaNastya marked this conversation as resolved.
Outdated
const [debouncedValue, setValue] = useState(value);

const setDebounceValue = useDebounceCallback(setValue, delay);

return [debouncedValue, setDebounceValue];
}
12 changes: 12 additions & 0 deletions hooks/use-debounce/tsconfig.json
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"]
}
2 changes: 1 addition & 1 deletion hooks/use-window-size/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"window",
"size"
],
"author": "Nastya Vasenina <vasenina@byndyusoft.com>",
Comment thread
VaseninaNastya marked this conversation as resolved.
"author": "Anastasia Vasenina <vasenina@byndyusoft.com>",
"homepage": "https://github.com/Byndyusoft/ui/tree/master/hooks/use-window-size#readme",
"license": "Apache-2.0",
"main": "dist/index.js",
Expand Down
Loading