表单项默认总是写入一个值到 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(); 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); const origin = getVariable(self.data, name, false);
if (value === origin) {
return;
}
const prev = self.data; const prev = self.data;
const data = cloneObject(self.data); const data = cloneObject(self.data);
if (prev.__prev) { if (value !== origin) {
// 基于之前的 __prev 改 if (prev.__prev) {
const prevData = cloneObject(prev.__prev); // 基于之前的 __prev 改
setVariable(prevData, name, origin); const prevData = cloneObject(prev.__prev);
Object.defineProperty(data, '__prev', { setVariable(prevData, name, origin);
value: prevData, Object.defineProperty(data, '__prev', {
enumerable: false, value: prevData,
configurable: false, enumerable: false,
writable: false, configurable: false,
}); writable: false,
} else { });
Object.defineProperty(data, '__prev', { } else {
value: {...prev}, Object.defineProperty(data, '__prev', {
enumerable: false, value: {...prev},
configurable: false, enumerable: false,
writable: false, configurable: false,
}); writable: false,
});
}
} else if (!force) {
return;
} }
setVariable(data, name, value); setVariable(data, name, value);
@ -361,7 +362,7 @@ export const FormStore = ServiceStore
item = self.items[self.items.length - 1] as IFormItemStore; 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); 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) 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]; : 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> = []; const selectedOptions:Array<any> = [];
self.filteredOptions.forEach((item:any) => { self.filteredOptions.forEach((item:any) => {
@ -230,7 +238,7 @@ export const FormItemStore = types
type?: string; type?: string;
id?: string; id?: string;
}) { }) {
const form = self.form; const form = self.form as IFormStore;
if (typeof rules === 'string') { if (typeof rules === 'string') {
rules = str2rules(rules); rules = str2rules(rules);
@ -404,6 +412,13 @@ export const FormItemStore = types
: value === void 0 ? [] : value === void 0 ? []
: [value && value.hasOwnProperty(self.valueField || 'value') ? value[self.valueField || 'value'] : value]; : [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 expressionsInOptions = false;
let filteredOptions = self.options let filteredOptions = self.options
.filter((item:any) => { .filter((item:any) => {