Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useEventTarget): return setValue for direct update state value #2556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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