Skip to content

Commit

Permalink
added callback argument
Browse files Browse the repository at this point in the history
  • Loading branch information
droganov committed Dec 22, 2024
1 parent 9929294 commit 46ae3a1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.0.3

- Added callback

## 0.0.2

- Added cache control to useAsyncValidator hook
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ React hooks for [@yobta/validator](https://github.com/yobta/validator)

## Installation

```
```sh
npm i @yobta/validator
npm i @yobta/validator-react
```
Expand All @@ -25,13 +25,13 @@ const validator = createAsyncValidator(rule(async value => somePromise(value)))
By default, the hook caches the validate function based on the validator provided. This means that unless the validator changes, the same validate function instance is used, optimizing performance by preventing unnecessary re-creations.

```ts
const [validate, isValidating] = useAsyncValidator(validator)
const [validate, isValidating] = useAsyncValidator(validator, conosle.log)
```

#### Cache Control with Dependencies

By providing a dependency list, you can control when the validate function is recreated. This is useful for optimizing performance and ensuring that the validator only updates when specific dependencies change.

```ts
const [validate, isValidating] = useAsyncValidator(validator, [dep1, dep2])
const [validate, isValidating] = useAsyncValidator(validator, conosle.log, [dep1, dep2])
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@
"import": {
"lib/index.js": "{ useAsyncValidator }"
},
"limit": "2644 B"
"limit": "2653 B"
},
{
"name": "*",
"import": {
"lib/index.js": "*"
},
"limit": "2691 B"
"limit": "2696 B"
}
],
"dependencies": {
Expand Down
12 changes: 9 additions & 3 deletions src/useAsyncValidator/useAsyncValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { jest } from '@jest/globals'
import { act, renderHook } from '@testing-library/react'
import { createAsyncValidator, rule } from '@yobta/validator'

import { useAsyncValidator } from './useAsyncValidator.js'

test('return value', () => {
const validator = createAsyncValidator(rule(() => {}))
const { result } = renderHook(() => useAsyncValidator(validator))
const callback = jest.fn()
const { result } = renderHook(() => useAsyncValidator(validator, callback))
const [validate, busy] = result.current

expect(typeof validate).toBe('function')
Expand All @@ -14,6 +16,8 @@ test('return value', () => {

test('promise resolve', async () => {
let resolvePromise: (value: unknown) => void
const callback = jest.fn()

const validator = createAsyncValidator(
rule(
() =>
Expand All @@ -23,7 +27,7 @@ test('promise resolve', async () => {
),
)

const { result } = renderHook(() => useAsyncValidator(validator))
const { result } = renderHook(() => useAsyncValidator(validator, callback))
const [validate] = result.current

expect(result.current[1]).toBe(false)
Expand All @@ -37,14 +41,16 @@ test('promise resolve', async () => {
resolvePromise({})
})
expect(result.current[1]).toBe(false)
expect(callback).toHaveBeenCalledWith({}, null)
})

test('deps', async () => {
let deps = [1, 2, 3]

let validator = createAsyncValidator(rule(() => null))
const callback = jest.fn()

const hook = renderHook(() => useAsyncValidator(validator, deps))
const hook = renderHook(() => useAsyncValidator(validator, callback, deps))
const result1 = hook.result.current

validator = createAsyncValidator(rule(() => null))
Expand Down
6 changes: 5 additions & 1 deletion src/useAsyncValidator/useAsyncValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ type Validator<Data> = (event: unknown) => YobtaAsyncValidatorResult<Data>
*
* @template Data - The type of data returned by the validator.
* @param {YobtaAsyncValidator<unknown, Data>} validator - The asynchronous validator function.
* @param {(...result: Awaited<YobtaAsyncValidatorResult<Data>>) => void} callback - The callback function to be called with the result of the validation.
* @param {DependencyList} [deps=[validator]] - The dependency list for the validator function.
* @returns {[Validator<Data>, boolean]} A tuple containing the validate function and a boolean indicating if validation is in progress.
*/
export const useAsyncValidator = <Data>(
validator: YobtaAsyncValidator<unknown, Data>,
callback: (...result: Awaited<YobtaAsyncValidatorResult<Data>>) => void,
deps: DependencyList = [validator],
): [Validator<Data>, boolean] => {
const [busy, setBusy] = useState(false)

const validate: Validator<Data> = useCallback(async (event: unknown) => {
setBusy(true)
try {
return await validator(event)
const result = await validator(event)
callback(...result)
return result
} finally {
setBusy(false)
}
Expand Down

0 comments on commit 46ae3a1

Please sign in to comment.