表单项默认总是写入一个值到 form.data

This commit is contained in:
liaoxuezhi 2019-09-02 14:54:42 +08:00
parent 26685cad5e
commit a7b9354614
2 changed files with 40 additions and 24 deletions

View File

@ -107,34 +107,35 @@ export const FormStore = ServiceStore
syncOptions();
}
function setValueByName(name:string, value:any, isPristine:boolean = false) {
function setValueByName(name:string, value:any, isPristine:boolean = false, force:boolean = false) {
// 没有变化就不跑了。
const origin = getVariable(self.data, name, false);
if (value === origin) {
return;
}
const prev = self.data;
const data = cloneObject(self.data);
if (prev.__prev) {
// 基于之前的 __prev 改
const prevData = cloneObject(prev.__prev);
setVariable(prevData, name, origin);
Object.defineProperty(data, '__prev', {
value: prevData,
enumerable: false,
configurable: false,
writable: false,
});
} else {
Object.defineProperty(data, '__prev', {
value: {...prev},
enumerable: false,
configurable: false,
writable: false,
});
if (value !== origin) {
if (prev.__prev) {
// 基于之前的 __prev 改
const prevData = cloneObject(prev.__prev);
setVariable(prevData, name, origin);
Object.defineProperty(data, '__prev', {
value: prevData,
enumerable: false,
configurable: false,
writable: false,
});
} else {
Object.defineProperty(data, '__prev', {
value: {...prev},
enumerable: false,
configurable: false,
writable: false,
});
}
} else if (!force) {
return;
}
setVariable(data, name, value);
@ -361,7 +362,7 @@ export const FormStore = ServiceStore
item = self.items[self.items.length - 1] as IFormItemStore;
// 默认值可能在原型上,把他挪到当前对象上。
setValueByName(item.name, item.value);
setValueByName(item.name, item.value, false, true);
options && item.config(options);

View File

@ -169,6 +169,14 @@ export const FormItemStore = types
const selected = Array.isArray(value) ? value.map(item=>item && item.hasOwnProperty(self.valueField || 'value') ? item[self.valueField || 'value'] : item)
: typeof value === 'string' ? value.split(self.delimiter || ',') : [value && value.hasOwnProperty(self.valueField || 'value') ? value[self.valueField || 'value'] : value];
if (value && value.hasOwnProperty(self.labelField || 'label')) {
selected[0]= {
[self.labelField || 'label']: value[self.labelField || 'label'],
[self.valueField || 'value']: value[self.valueField || 'value'],
};
}
const selectedOptions:Array<any> = [];
self.filteredOptions.forEach((item:any) => {
@ -230,7 +238,7 @@ export const FormItemStore = types
type?: string;
id?: string;
}) {
const form = self.form;
const form = self.form as IFormStore;
if (typeof rules === 'string') {
rules = str2rules(rules);
@ -404,6 +412,13 @@ export const FormItemStore = types
: value === void 0 ? []
: [value && value.hasOwnProperty(self.valueField || 'value') ? value[self.valueField || 'value'] : value];
if (value && value.hasOwnProperty(self.labelField || 'label')) {
selected[0]= {
[self.labelField || 'label']: value[self.labelField || 'label'],
[self.valueField || 'value']: value[self.valueField || 'value'],
};
}
let expressionsInOptions = false;
let filteredOptions = self.options
.filter((item:any) => {