diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f9bf89..2cfc1f0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -92,3 +92,10 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} flags: Autosizer files: ./packages/autosizer/coverage/coverage-final.json + + - name: Upload `useDebounce` coverage reports to Codecov + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + flags: useDebounce + files: ./packages/use-debounce/coverage/coverage-final.json diff --git a/apps/docs/package.json b/apps/docs/package.json index 3ce37b2..f65bbee 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -10,7 +10,7 @@ "clean": "rimraf .turbo && rimraf node_modules && rimraf .next" }, "dependencies": { - "@react-awesome/components": "1.0.22", + "@react-awesome/components": "1.0.23", "@vercel/speed-insights": "^1.0.10", "classnames": "^2.5.1", "lodash": "^4.17.21", diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index e9506d5..31a0351 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -1,5 +1,12 @@ # @react-awesome/components +## 1.0.23 + +### Patch Changes + +- Updated dependencies + - @react-awesome/use-debounce@1.0.0 + ## 1.0.22 ### Patch Changes diff --git a/packages/components/package.json b/packages/components/package.json index 0e6217f..679947f 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -1,6 +1,6 @@ { "name": "@react-awesome/components", - "version": "1.0.22", + "version": "1.0.23", "main": "./dist/index.cjs", "module": "./dist/index.js", "types": "./dist/index.d.ts", @@ -46,6 +46,7 @@ "@react-awesome/use-previous": "0.0.3", "@react-awesome/use-toggle": "1.0.0", "@react-awesome/use-breakpoint": "1.0.0", + "@react-awesome/use-debounce": "1.0.0", "@react-awesome/autosizer": "0.0.0" } } diff --git a/packages/use-debounce/.eslintrc.js b/packages/use-debounce/.eslintrc.js new file mode 100644 index 0000000..19f3ed1 --- /dev/null +++ b/packages/use-debounce/.eslintrc.js @@ -0,0 +1,8 @@ +module.exports = { + root: true, + extends: ["@react-awesome/eslint-config/library.js"], + parser: "@typescript-eslint/parser", + parserOptions: { + project: true, + }, +}; diff --git a/packages/use-debounce/CHANGELOG.md b/packages/use-debounce/CHANGELOG.md new file mode 100644 index 0000000..8d69e2a --- /dev/null +++ b/packages/use-debounce/CHANGELOG.md @@ -0,0 +1,7 @@ +# @react-awesome/use-debounce + +## 1.0.0 + +### Major Changes + +- Release first version diff --git a/packages/use-debounce/README.md b/packages/use-debounce/README.md new file mode 100644 index 0000000..2c22c32 --- /dev/null +++ b/packages/use-debounce/README.md @@ -0,0 +1,32 @@ +# @react-awesome/use-debounce + +
+ + + + + +
+ +**use-debounce** tracks the previous value of a variable. + +Please refer to the [documentation](https://react-awesome-components.vercel.app/docs/use-debounce) for more information. + +## Installation + +```sh +yarn add @react-awesome/use-debounce +# or +npm i @react-awesome/use-debounce +``` + +## Contribution + +Yes please! See the +[contributing guidelines](https://github.com/trinhthinh388/react-awesome-components/blob/master/CONTRIBUTING.md) +for details. + +## Licence + +This project is licensed under the terms of the +[MIT license](https://github.com/trinhthinh388/react-awesome-components/blob/master/LICENSE). diff --git a/packages/use-debounce/package.json b/packages/use-debounce/package.json new file mode 100644 index 0000000..c522e13 --- /dev/null +++ b/packages/use-debounce/package.json @@ -0,0 +1,46 @@ +{ + "name": "@react-awesome/use-debounce", + "version": "1.0.0", + "main": "./dist/index.cjs", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", + "sideEffects": false, + "license": "MIT", + "description": "useDebounce debounces the state value.", + "keywords": [ + "react", + "debounce", + "state", + "react", + "react", + "hooks", + "useDebounce" + ], + "repository": { + "url": "https://github.com/trinhthinh388/react-awesome-components" + }, + "files": [ + "dist/**" + ], + "scripts": { + "build": "vite --config ../../vite.config.ts build", + "dev": "vite --watch --config ../../vite.config.ts build", + "lint": "eslint \"src/**/*.ts*\"", + "clean": "rimraf .turbo && rimraf node_modules && rimraf dist", + "test": "vitest --config ../../vite.config.ts --coverage --run", + "test:ui": "vitest --config ../../vite.config.ts --coverage --ui", + "typecheck": "tsc --noEmit" + }, + "devDependencies": { + "@react-awesome/eslint-config": "*", + "@react-awesome/tsconfig": "*", + "@types/react": "^18.2.46", + "@types/react-dom": "^18.2.18", + "eslint": "^8.56.0", + "react": "^18.2.0", + "typescript": "^5.3.3" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/use-debounce/src/index.ts b/packages/use-debounce/src/index.ts new file mode 100644 index 0000000..38ef774 --- /dev/null +++ b/packages/use-debounce/src/index.ts @@ -0,0 +1 @@ +export * from './use-debounce' diff --git a/packages/use-debounce/src/use-debounce.spec.tsx b/packages/use-debounce/src/use-debounce.spec.tsx new file mode 100644 index 0000000..8facd64 --- /dev/null +++ b/packages/use-debounce/src/use-debounce.spec.tsx @@ -0,0 +1,22 @@ +import { useDebounce } from './use-debounce' +import { renderHook } from '@testing-library/react' + +describe('useDebounce', () => { + beforeEach(() => { + // tell vitest we use mocked time + vi.useFakeTimers() + }) + + afterEach(() => { + // restoring date after each test run + vi.useRealTimers() + }) + + it('Should debounce the state update', () => { + const { result, rerender } = renderHook(() => useDebounce(1, 3_000)) + expect(result.current).toBe(undefined) + vi.runAllTimers() + rerender() + expect(result.current).toBe(1) + }) +}) diff --git a/packages/use-debounce/src/use-debounce.tsx b/packages/use-debounce/src/use-debounce.tsx new file mode 100644 index 0000000..8a27f64 --- /dev/null +++ b/packages/use-debounce/src/use-debounce.tsx @@ -0,0 +1,23 @@ +import { useEffect, useRef, useState } from 'react' + +export function useDebounce