Skip to content

Commit

Permalink
feat(useEventTarget): return setValue for direct update state value
Browse files Browse the repository at this point in the history
  • Loading branch information
coolzjy committed Jun 19, 2024
1 parent d02d146 commit cf299f5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventTarget/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ describe('useEventTarget', () => {
hook.result.current[1].onChange({ target: { value: 'def' } });
});
expect(hook.result.current[0]).toBe('def');
act(() => {
hook.result.current[1].setValue('foo');
});
expect(hook.result.current[0]).toBe('foo');
act(() => {
hook.result.current[1].reset();
});
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks/src/useEventTarget/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A hook that encapsulates `onChange` and `value` logic for form controls that obt
## API

```typescript
const [value, { onChange, reset }] = useEventTarget<T, U>(Options<T, U>);
const [value, { onChange, reset, setValue }] = useEventTarget<T, U>(Options<T, U>);
```

### Result
Expand All @@ -30,6 +30,7 @@ const [value, { onChange, reset }] = useEventTarget<T, U>(Options<T, U>);
| value | component value | `T` |
| onChange | callback when value changes | `(e: { target: { value: T } }) => void` |
| reset | function to reset the value to initialValue | `() => void` |
| setValue | function to set value directly | `(value: T) => void` |

### Options

Expand Down
1 change: 1 addition & 0 deletions packages/hooks/src/useEventTarget/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function useEventTarget<T, U = T>(options?: Options<T, U>) {
{
onChange,
reset,
setValue,
},
] as const;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks/src/useEventTarget/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nav:
## API

```typescript
const [value, { onChange, reset }] = useEventTarget<T, U>(Options<T, U>);
const [value, { onChange, reset, setValue }] = useEventTarget<T, U>(Options<T, U>);
```

### Result
Expand All @@ -30,6 +30,7 @@ const [value, { onChange, reset }] = useEventTarget<T, U>(Options<T, U>);
| value | 表单控件的值 | `T` |
| onChange | 表单控件值发生变化时候的回调 | `(e: { target: { value: T } }) => void` |
| reset | 重置函数 | `() => void` |
| setValue | 直接设置 state 值的函数 | `(value: T) => void` |

### Options

Expand Down

0 comments on commit cf299f5

Please sign in to comment.