Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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-debounced-callback/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
9 changes: 9 additions & 0 deletions hooks/use-debounced-callback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@byndyusoft-ui/use-debounced-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-debounced-callback
```
33 changes: 33 additions & 0 deletions hooks/use-debounced-callback/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@byndyusoft-ui/use-debounced-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-debounced-callback#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 --roots hooks/use-debounced-callback/src"
},
"bugs": {
"url": "https://github.com/Byndyusoft/ui/issues"
},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions hooks/use-debounced-callback/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import useDebouncedCallback from './useDebouncedCallback';
export default useDebouncedCallback;
10 changes: 10 additions & 0 deletions hooks/use-debounced-callback/src/useDebouncedCallback.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/useDebouncedCallback" />

<Markdown>{Readme}</Markdown>
35 changes: 35 additions & 0 deletions hooks/use-debounced-callback/src/useDebouncedCallback.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useState } from 'react';
import useDebouncedCallback from './useDebouncedCallback';
import './useDebouncedCallback.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 = useDebouncedCallback(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/useDebouncedCallback',
component: Template
};

export default meta;
27 changes: 27 additions & 0 deletions hooks/use-debounced-callback/src/useDebouncedCallback.tests.tsx
Comment thread
Oustinger marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { waitFor } from '@testing-library/react';
import { renderHook, act } from '@testing-library/react-hooks';
import useDebouncedCallback from './useDebouncedCallback';

const oldValue = 'old value';
const newValue = 'new value';
const timeout = 2000;

describe('hooks/useDebouncedCallback', () => {
test('useDebouncedCallback works', () => {
let value = oldValue;
const handle = () => (value = newValue);
const { result } = renderHook(() => useDebouncedCallback(handle, timeout));
const setDebounceValue = result.current;
act(() => {
setDebounceValue();
});

expect(value).toEqual(oldValue);
waitFor(
() => {
expect(value).toEqual(newValue);
},
{ timeout: 2500 }
);
});
});
30 changes: 30 additions & 0 deletions hooks/use-debounced-callback/src/useDebouncedCallback.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-debounced-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-debounced-value/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
9 changes: 9 additions & 0 deletions hooks/use-debounced-value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@byndyusoft-ui/use-debounced-value`
---
> 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-debounced-value
```
33 changes: 33 additions & 0 deletions hooks/use-debounced-value/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@byndyusoft-ui/use-debounced-value",
"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-debounced-value#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 --roots hooks/use-debounced-value/src"
},
"bugs": {
"url": "https://github.com/Byndyusoft/ui/issues"
},
"publishConfig": {
"access": "public"
}
}
2 changes: 2 additions & 0 deletions hooks/use-debounced-value/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import useDebouncedValue from './useDebouncedValue';
export default useDebouncedValue;
10 changes: 10 additions & 0 deletions hooks/use-debounced-value/src/useDebouncedValue.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-debounced-value/src/useDebouncedValue.stories.mdx
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/useDebouncedValue" />

<Markdown>{Readme}</Markdown>
31 changes: 31 additions & 0 deletions hooks/use-debounced-value/src/useDebouncedValue.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import useDebouncedValue from './useDebouncedValue';
import './useDebouncedValue.stories.css';
import type { Meta, StoryObj } from '@storybook/react';

type Story = StoryObj<typeof Template>;

const Template = () => {
const [debouncedValue, setDebouncedValue] = useDebouncedValue('', 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/useDebouncedValue',
component: Template
};

export default meta;
25 changes: 25 additions & 0 deletions hooks/use-debounced-value/src/useDebouncedValue.tests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { waitFor } from '@testing-library/react';
import { renderHook, act } from '@testing-library/react-hooks';
import useDebouncedValue from './useDebouncedValue';

const oldValue = 'old value';
const newValue = 'new value';
const timeout = 2000;

describe('hooks/useDebouncedValue', () => {
test('useDebouncedValue works', () => {
const { result } = renderHook(() => useDebouncedValue(oldValue, timeout));
const [debouncedValue, setDebouncedValue] = result.current;
expect(debouncedValue).toEqual(oldValue);
act(() => {
setDebouncedValue(newValue);
});
expect(debouncedValue).toEqual(oldValue);
waitFor(
() => {
expect(debouncedValue).toEqual(newValue);
},
{ timeout: 2500 }
);
});
});
10 changes: 10 additions & 0 deletions hooks/use-debounced-value/src/useDebouncedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from 'react';
import useDebouncedCallback from '@byndyusoft-ui/use-debounced-callback';

export default function useDebouncedValue(value: string, delay = 300): [string, (value: string) => void] {
const [debouncedValue, setValue] = useState(value);

const setDebounceValue = useDebouncedCallback(setValue, delay);

return [debouncedValue, setDebounceValue];
}
12 changes: 12 additions & 0 deletions hooks/use-debounced-value/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"]
}
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading