Skip to content

Commit

Permalink
feat: api
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Nov 29, 2023
1 parent 983df60 commit f7e543a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/demo/useWatch-selector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## useWatch-selector

<code src="../examples/useWatch-selector.tsx"></code>
28 changes: 28 additions & 0 deletions docs/examples/useWatch-selector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import Form, { Field } from 'rc-field-form';
import Input from './components/Input';

type FieldType = {
name?: string;
age?: number;
};

export default () => {
const [form] = Form.useForm<FieldType>();
const values = Form.useWatch(values => ({ newName: values.name }), form);
console.log('values', values);
return (
<>
<Form form={form}>
name
<Field name="name">
<Input />
</Field>
age
<Field name="age">
<Input />
</Field>
</Form>
</>
);
};
17 changes: 14 additions & 3 deletions src/useWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function useWatch<TForm extends FormInstance>(
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>;

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

function useWatch<TForm extends FormInstance>(
dependencies: NamePath,
form?: TForm | WatchOptions<TForm>,
Expand All @@ -86,8 +91,10 @@ function useWatch<ValueType = Store>(
form?: FormInstance | WatchOptions<FormInstance>,
): ValueType;

function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>]) {
const [dependencies = [], _form = {}] = args;
function useWatch(
...args: [NamePath | ((values: Store) => any), FormInstance | WatchOptions<FormInstance>]
) {
const [dependencies, _form = {}] = args;
const options = isFormInstance(_form) ? { form: _form } : _form;
const form = options.form;

Expand Down Expand Up @@ -126,7 +133,11 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>]
const { registerWatch } = getInternalHooks(HOOK_MARK);

const cancelRegister = registerWatch((values, allValues) => {
const newValue = getValue(options.preserve ? allValues : values, namePathRef.current);
const _values = options.preserve ? allValues : values;
const newValue =
typeof dependencies === 'function'
? dependencies(_values)

Check warning on line 139 in src/useWatch.ts

View check run for this annotation

Codecov / codecov/patch

src/useWatch.ts#L139

Added line #L139 was not covered by tests
: getValue(_values, namePathRef.current);
const nextValueStr = stringify(newValue);

// Compare stringify in case it's nest object
Expand Down

0 comments on commit f7e543a

Please sign in to comment.