Skip to content

Commit 95476fe

Browse files
committed
fix: initialValue should also be validate
1 parent 819b054 commit 95476fe

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-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

0 commit comments

Comments
 (0)