Skip to content

Commit

Permalink
feat: 添加显示声明类型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Nov 30, 2023
1 parent 88e7b31 commit 4800c58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/useWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ function useWatch<TForm extends FormInstance>(
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>;

// ------- selector type -------
function useWatch<TForm extends FormInstance, TSelected = unknown>(
selector: (values: GetGeneric<TForm>) => TSelected,
form?: TForm | WatchOptions<TForm>,
): TSelected;

function useWatch<ValueType = Store, TSelected = unknown>(
selector: (values: ValueType) => TSelected,
form?: FormInstance | WatchOptions<FormInstance>,
): TSelected;
// ------- selector type end -------

function useWatch<TForm extends FormInstance>(
dependencies: NamePath,
form?: TForm | WatchOptions<TForm>,
Expand Down
23 changes: 22 additions & 1 deletion tests/useWatch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,29 @@ describe('useWatch', () => {
const demo5 = Form.useWatch(['demo1', 'demo2', 'demo3', 'demo4', 'demo5'], form);
const more = Form.useWatch(['age', 'name', 'gender'], form);
const demo = Form.useWatch<string>(['demo']);

const values2 = Form.useWatch(values => ({ newName: values.name, newAge: values.age }), form);
const values3 = Form.useWatch<FieldType, { newName?: string }>(values => ({
newName: values.name,
}));

return (
<>{JSON.stringify({ values, main, age, demo1, demo2, demo3, demo4, demo5, more, demo })}</>
<>
{JSON.stringify({
values,
main,
age,
demo1,
demo2,
demo3,
demo4,
demo5,
more,
demo,
values2,
values3,
})}
</>
);
};

Expand Down

0 comments on commit 4800c58

Please sign in to comment.