Skip to content

Commit 6ea8f07

Browse files
committed
update vc-form to 2.4.8
1 parent b6a7f49 commit 6ea8f07

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

components/vc-form/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// based on rc-form 2.4.1
1+
// based on rc-form 2.4.8
22
import { createForm, createFormField } from './src/';
33
export { createForm, createFormField };

components/vc-form/src/createBaseForm.jsx

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AsyncValidator from 'async-validator';
22
import warning from 'warning';
33
import get from 'lodash/get';
44
import set from 'lodash/set';
5+
import eq from 'lodash/eq';
56
import omit from 'lodash/omit';
67
import createFieldsStore from './createFieldsStore';
78
import { cloneElement } from '../../_util/vnode';
@@ -126,7 +127,14 @@ function createBaseForm(option = {}, mixins = []) {
126127
const valuesAllSet = {};
127128
valuesAll[name] = value;
128129
Object.keys(valuesAll).forEach(key => set(valuesAllSet, key, valuesAll[key]));
129-
onValuesChange(this, set({}, name, value), valuesAllSet);
130+
onValuesChange(
131+
{
132+
[formPropName]: this.getForm(),
133+
...this.$props,
134+
},
135+
set({}, name, value),
136+
valuesAllSet,
137+
);
130138
}
131139
const field = this.fieldsStore.getField(name);
132140
return { name, field: { ...field, value, touched: true }, fieldMeta };
@@ -135,6 +143,7 @@ function createBaseForm(option = {}, mixins = []) {
135143
onCollect(name_, action, ...args) {
136144
const { name, field, fieldMeta } = this.onCollectCommon(name_, action, args);
137145
const { validate } = fieldMeta;
146+
this.fieldsStore.setFieldsAsDirty();
138147
const newField = {
139148
...field,
140149
dirty: hasRules(validate),
@@ -150,6 +159,7 @@ function createBaseForm(option = {}, mixins = []) {
150159
...field,
151160
dirty: true,
152161
};
162+
this.fieldsStore.setFieldsAsDirty();
153163
this.validateFieldsInternal([newField], {
154164
action,
155165
options: {
@@ -243,7 +253,7 @@ function createBaseForm(option = {}, mixins = []) {
243253
if (process.env.NODE_ENV !== 'production') {
244254
warning(
245255
this.fieldsStore.isValidNestedFieldName(name),
246-
'One field name cannot be part of another, e.g. `a` and `a.b`.',
256+
`One field name cannot be part of another, e.g. \`a\` and \`a.b\`. Check field: ${name}`,
247257
);
248258
warning(
249259
!('exclusive' in usersFieldOption),
@@ -346,7 +356,14 @@ function createBaseForm(option = {}, mixins = []) {
346356
(acc, name) => set(acc, name, this.fieldsStore.getField(name)),
347357
{},
348358
);
349-
onFieldsChange(this, changedFields, this.fieldsStore.getNestedAllFields());
359+
onFieldsChange(
360+
{
361+
[formPropName]: this.getForm(),
362+
...this.$props,
363+
},
364+
changedFields,
365+
this.fieldsStore.getNestedAllFields(),
366+
);
350367
}
351368
if (templateContext) {
352369
templateContext.$forceUpdate();
@@ -381,7 +398,14 @@ function createBaseForm(option = {}, mixins = []) {
381398
this.setFields(newFields, callback);
382399
if (onValuesChange) {
383400
const allValues = this.fieldsStore.getAllValues();
384-
onValuesChange(this, changedValues, allValues);
401+
onValuesChange(
402+
{
403+
[formPropName]: this.getForm(),
404+
...this.$props,
405+
},
406+
changedValues,
407+
allValues,
408+
);
385409
}
386410
},
387411

@@ -501,7 +525,38 @@ function createBaseForm(option = {}, mixins = []) {
501525
};
502526
if (errors && errors.length) {
503527
errors.forEach(e => {
504-
const fieldName = e.field;
528+
const errorFieldName = e.field;
529+
let fieldName = errorFieldName;
530+
531+
// Handle using array validation rule.
532+
// ref: https://github.com/ant-design/ant-design/issues/14275
533+
Object.keys(allRules).some(ruleFieldName => {
534+
const rules = allRules[ruleFieldName] || [];
535+
536+
// Exist if match rule
537+
if (ruleFieldName === errorFieldName) {
538+
fieldName = ruleFieldName;
539+
return true;
540+
}
541+
542+
// Skip if not match array type
543+
if (
544+
rules.every(({ type }) => type !== 'array') &&
545+
errorFieldName.indexOf(ruleFieldName) !== 0
546+
) {
547+
return false;
548+
}
549+
550+
// Exist if match the field name
551+
const restPath = errorFieldName.slice(ruleFieldName.length + 1);
552+
if (/^\d+$/.test(restPath)) {
553+
fieldName = ruleFieldName;
554+
return true;
555+
}
556+
557+
return false;
558+
});
559+
505560
const field = get(errorsGroup, fieldName);
506561
if (typeof field !== 'object' || Array.isArray(field)) {
507562
set(errorsGroup, fieldName, { errors: [] });
@@ -516,7 +571,7 @@ function createBaseForm(option = {}, mixins = []) {
516571
const fieldErrors = get(errorsGroup, name);
517572
const nowField = this.fieldsStore.getField(name);
518573
// avoid concurrency problems
519-
if (nowField.value !== allValues[name]) {
574+
if (!eq(nowField.value, allValues[name])) {
520575
expired.push({
521576
name,
522577
});
@@ -583,9 +638,7 @@ function createBaseForm(option = {}, mixins = []) {
583638
return field;
584639
});
585640
if (!fields.length) {
586-
if (callback) {
587-
callback(null, this.fieldsStore.getFieldsValue(fieldNames));
588-
}
641+
callback(null, this.fieldsStore.getFieldsValue(fieldNames));
589642
return;
590643
}
591644
if (!('firstFields' in options)) {
@@ -604,7 +657,7 @@ function createBaseForm(option = {}, mixins = []) {
604657
);
605658
});
606659
pending.catch(e => {
607-
if (console.error) {
660+
if (console.error && process.env.NODE_ENV !== 'production') {
608661
console.error(e);
609662
}
610663
return e;
@@ -627,7 +680,7 @@ function createBaseForm(option = {}, mixins = []) {
627680
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
628681
warning(
629682
false,
630-
'`submit` is deprecated.' +
683+
'`submit` is deprecated. ' +
631684
"Actually, it's more convenient to handle submitting status by yourself.",
632685
);
633686
}

components/vc-form/src/createFieldsStore.jsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import set from 'lodash/set';
22
import createFormField, { isFormField } from './createFormField';
3-
import { flattenFields, getErrorStrs, startsWith } from './utils';
3+
import { hasRules, flattenFields, getErrorStrs, startsWith } from './utils';
44

55
function partOf(a, b) {
66
return b.indexOf(a) === 0 && ['.', '['].indexOf(b[a.length]) !== -1;
@@ -92,6 +92,19 @@ class FieldsStore {
9292
this.fieldsMeta[name] = meta;
9393
}
9494

95+
setFieldsAsDirty() {
96+
Object.keys(this.fields).forEach(name => {
97+
const field = this.fields[name];
98+
const fieldMeta = this.fieldsMeta[name];
99+
if (field && fieldMeta && hasRules(fieldMeta.validate)) {
100+
this.fields[name] = {
101+
...field,
102+
dirty: true,
103+
};
104+
}
105+
});
106+
}
107+
95108
getFieldMeta(name) {
96109
this.fieldsMeta[name] = this.fieldsMeta[name] || {};
97110
return this.fieldsMeta[name];

0 commit comments

Comments
 (0)