Skip to content

Commit 9218ce5

Browse files
committed
2 parents 819b054 + b581a34 commit 9218ce5

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

docs/examples/validate-perf.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable react/prop-types */
1+
/* eslint-disable react/prop-types, @typescript-eslint/consistent-type-imports */
22

33
import React from 'react';
44
import Form, { Field, FormInstance } from 'rc-field-form';
@@ -34,7 +34,7 @@ export default class Demo extends React.Component {
3434
console.log('Failed:', errorInfo);
3535
};
3636

37-
public onPasswordError = (errors: string[]) => {
37+
public onPasswordError = ({ errors }: { errors: string[] }) => {
3838
console.log('🐞 Password Error:', errors);
3939
};
4040

@@ -64,12 +64,13 @@ export default class Demo extends React.Component {
6464
},
6565
},
6666
]}
67-
onError={this.onPasswordError}
67+
onMetaChange={this.onPasswordError}
6868
>
6969
<Input placeholder="password" />
7070
</LabelField>
7171

7272
<LabelField
73+
initialValue="123"
7374
name="password2"
7475
dependencies={['password']}
7576
messageVariables={{ displayName: '密码2' }}

src/Field.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
126126
*/
127127
private touched: boolean = false;
128128

129-
/** Mark when touched & validated. Currently only used for `dependencies` */
129+
/**
130+
* Mark when touched & validated. Currently only used for `dependencies`.
131+
* Note that we do not think field with `initialValue` is dirty
132+
* but this will be by `isFieldDirty` func.
133+
*/
130134
private dirty: boolean = false;
131135

132136
private validatePromise: Promise<string[]> | null = null;
@@ -418,7 +422,21 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
418422

419423
public isFieldTouched = () => this.touched;
420424

421-
public isFieldDirty = () => this.dirty;
425+
public isFieldDirty = () => {
426+
// Touched or validate or has initialValue
427+
if (this.dirty || this.props.initialValue !== undefined) {
428+
return true;
429+
}
430+
431+
// Form set initialValue
432+
const { fieldContext } = this.props;
433+
const { getInitialValue } = fieldContext.getInternalHooks(HOOK_MARK);
434+
if (getInitialValue(this.getNamePath()) !== undefined) {
435+
return true;
436+
}
437+
438+
return false;
439+
};
422440

423441
public getErrors = () => this.errors;
424442

src/FieldContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const Context = React.createContext<InternalFormInstance>({
3838
getFields: warningFunc,
3939
setValidateMessages: warningFunc,
4040
setPreserve: warningFunc,
41+
getInitialValue: warningFunc,
4142
};
4243
},
4344
});

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export interface InternalHooks {
198198
getFields: (namePathList?: InternalNamePath[]) => FieldData[];
199199
setValidateMessages: (validateMessages: ValidateMessages) => void;
200200
setPreserve: (preserve?: boolean) => void;
201+
getInitialValue: (namePath: InternalNamePath) => StoreValue;
201202
}
202203

203204
/** Only return partial when type is not any */

src/useForm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class FormStore {
111111
setValidateMessages: this.setValidateMessages,
112112
getFields: this.getFields,
113113
setPreserve: this.setPreserve,
114+
getInitialValue: this.getInitialValue,
114115
};
115116
}
116117

tests/dependencies.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ describe('Form.Dependencies', () => {
3232
matchError(getField(wrapper, 1), true);
3333
});
3434

35+
describe('initialValue', () => {
36+
function test(name, formProps, fieldProps) {
37+
it(name, async () => {
38+
let validated = false;
39+
40+
const wrapper = mount(
41+
<div>
42+
<Form {...formProps}>
43+
<InfoField name="field_1" />
44+
<InfoField
45+
name="field_2"
46+
rules={[
47+
{
48+
validator: async () => {
49+
validated = true;
50+
},
51+
},
52+
]}
53+
dependencies={['field_1']}
54+
{...fieldProps}
55+
/>
56+
</Form>
57+
</div>,
58+
);
59+
60+
// Not trigger if not touched
61+
await changeValue(getField(wrapper, 0), '');
62+
expect(validated).toBeTruthy();
63+
});
64+
}
65+
66+
test('form level', { initialValues: { field_2: 'bamboo' } });
67+
test('field level', null, { initialValue: 'little' });
68+
});
69+
3570
it('nest dependencies', async () => {
3671
let form = null;
3772
let rendered = false;

0 commit comments

Comments
 (0)