Merge pull request #1307 from 2betop/autoFill

整理 autoFill
This commit is contained in:
RickCole 2021-01-04 12:14:48 +08:00 committed by GitHub
commit 365d970a22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 78 additions and 79 deletions

View File

@ -42,13 +42,6 @@ export default class ButtonGroupControl extends React.Component<
@autobind
handleToggle(option: Option) {
const {onToggle, multiple, autoFill, onBulkChange} = this.props;
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, option as Option);
sendTo && onBulkChange(sendTo);
onToggle(option);
}

View File

@ -97,7 +97,8 @@ export default class FormControl extends React.PureComponent<
labelField,
joinValues,
extractValue,
selectFirst
selectFirst,
autoFill
}
} = this.props;
@ -136,7 +137,8 @@ export default class FormControl extends React.PureComponent<
labelField,
joinValues,
extractValue,
selectFirst
selectFirst,
autoFill
});
if (this.model.unique && form.parentStore?.storeType === ComboStore.name) {
@ -243,7 +245,9 @@ export default class FormControl extends React.PureComponent<
'valueField',
'labelField',
'joinValues',
'extractValue'
'extractValue',
'selectFirst',
'autoFill'
],
props.control,
nextProps.control
@ -261,7 +265,9 @@ export default class FormControl extends React.PureComponent<
labelField: nextProps.control.labelField,
joinValues: nextProps.control.joinValues,
extractValue: nextProps.control.extractValue,
messages: nextProps.control.validationErrors
messages: nextProps.control.validationErrors,
selectFirst: nextProps.control.selectFirst,
autoFill: nextProps.control.autoFill
});
}
}

View File

@ -595,6 +595,7 @@ export default class FileControl extends React.Component<FileProps, FileState> {
files: files
},
() => {
// todo 这个逻辑应该移到 onChange 里面去,因为这个时候并不一定修改了表单项的值。
const sendTo =
!multiple &&
autoFill &&

View File

@ -600,6 +600,7 @@ export default class ImageControl extends React.Component<
env.notify('error', error || __('图片上传失败,请重试'));
} else {
newFile = {
name: file.name,
...obj,
preview: file.preview
} as FileValue;
@ -611,11 +612,12 @@ export default class ImageControl extends React.Component<
files: (this.files = files)
},
() => {
// todo 这个逻辑应该移到 onChange 里面去,因为这个时候并不一定修改了表单项的值。
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, obj || {});
dataMapping(autoFill, newFile || {});
sendTo && onBulkChange(sendTo);
this.tick();

View File

@ -57,14 +57,7 @@ export default class ListControl extends React.Component<ListProps, any> {
return;
}
const {onToggle, multiple, autoFill, onBulkChange} = this.props;
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, option as Option);
sendTo && onBulkChange(sendTo);
const {onToggle} = this.props;
onToggle(option);
}

View File

@ -184,9 +184,7 @@ export default class NestedSelectControl extends React.Component<
onChange,
joinValues,
extractValue,
valueField,
autoFill,
onBulkChange
valueField
} = this.props;
if (multiple) {
@ -195,13 +193,6 @@ export default class NestedSelectControl extends React.Component<
e.stopPropagation();
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, option);
sendTo && onBulkChange(sendTo);
onChange(
joinValues
? option[valueField || 'value']

View File

@ -249,9 +249,7 @@ export default class PickerControl extends React.PureComponent<
multiple,
options,
setOptions,
onChange,
autoFill,
onBulkChange
onChange
} = this.props;
let value: any = items;
@ -281,12 +279,6 @@ export default class PickerControl extends React.PureComponent<
});
additionalOptions.length && setOptions(options.concat(additionalOptions));
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, value as Option);
sendTo && onBulkChange(sendTo);
onChange(value);
}

View File

@ -38,18 +38,7 @@ export default class RadiosControl extends React.Component<RadiosProps, any> {
@autobind
handleChange(option: Option) {
const {
joinValues,
extractValue,
valueField,
onChange,
autoFill,
onBulkChange
} = this.props;
const sendTo =
autoFill && !isEmpty(autoFill) && dataMapping(autoFill, option);
sendTo && onBulkChange && onBulkChange(sendTo);
const {joinValues, extractValue, valueField, onChange} = this.props;
if (option && (joinValues || extractValue)) {
option = option[valueField || 'value'];

View File

@ -83,9 +83,7 @@ export default class SelectControl extends React.Component<SelectProps, any> {
valueField,
onChange,
setOptions,
options,
autoFill,
onBulkChange
options
} = this.props;
let newValue: string | Option | Array<Option> | void = value;
@ -129,12 +127,6 @@ export default class SelectControl extends React.Component<SelectProps, any> {
// 不设置没法回显
additonalOptions.length && setOptions(options.concat(additonalOptions));
const sendTo =
!multiple &&
autoFill &&
!isEmpty(autoFill) &&
dataMapping(autoFill, value as Option);
sendTo && onBulkChange(sendTo);
onChange(newValue);
}

View File

@ -21,7 +21,8 @@ import {
isObjectShallowModified,
findTree,
findTreeIndex,
spliceTree
spliceTree,
isEmpty
} from '../utils/helper';
import {flattenTree} from '../utils/helper';
import {IRendererStore} from '.';
@ -31,6 +32,7 @@ import {SimpleMap} from '../utils/SimpleMap';
import memoize from 'lodash/memoize';
import {TranslateFn} from '../locale';
import {StoreNode} from './node';
import {dataMapping} from '../utils/tpl-builtin';
interface IOption {
value?: string | number | null;
@ -70,6 +72,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
options: types.optional(types.array(types.frozen()), []),
expressionsInOptions: false,
selectFirst: false,
autoFill: types.frozen(),
selectedOptions: types.optional(types.frozen(), []),
filteredOptions: types.optional(types.frozen(), []),
dialogSchema: types.frozen(),
@ -223,7 +226,8 @@ export const FormItemStore = StoreNode.named('FormItemStore')
extractValue,
type,
id,
selectFirst
selectFirst,
autoFill
}: {
required?: any;
unique?: any;
@ -239,6 +243,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
type?: string;
id?: string;
selectFirst?: boolean;
autoFill?: any;
}) {
if (typeof rules === 'string') {
rules = str2rules(rules);
@ -251,6 +256,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
typeof unique !== 'undefined' && (self.unique = !!unique);
typeof multiple !== 'undefined' && (self.multiple = !!multiple);
typeof selectFirst !== 'undefined' && (self.selectFirst = !!selectFirst);
typeof autoFill !== 'undefined' && (self.autoFill = autoFill);
typeof joinValues !== 'undefined' && (self.joinValues = !!joinValues);
typeof extractValue !== 'undefined' &&
(self.extractValue = !!extractValue);
@ -292,6 +298,8 @@ export const FormItemStore = StoreNode.named('FormItemStore')
} else {
self.form.setValueByName(self.name, value, isPrintine);
}
syncAutoFill(value, isPrintine);
}
const validate: (hook?: any) => Promise<boolean> = flow(function* validate(
@ -485,24 +493,27 @@ export const FormItemStore = StoreNode.named('FormItemStore')
onChange && onChange((json.data as any).value, false, true);
} else if (
self.selectFirst &&
self.options.length &&
!self.selectedOptions.length
self.filteredOptions.length &&
!self.selectedOptions.length &&
onChange
) {
onChange &&
onChange(
self.options
.slice(0, 1)
.map(item => {
if (self.extractValue || self.joinValues) {
return item.value;
}
const list = self.filteredOptions.slice(0, 1).map((item: any) => {
if (self.extractValue || self.joinValues) {
return item.value;
}
return item;
})
[self.joinValues ? 'join' : 'concat'](),
false,
true
);
return item;
});
onChange(
self.joinValues
? list.join(self.delimiter)
: self.multiple
? list
: list[0],
false,
true
);
} else if (clearValue) {
self.selectedOptions.some((item: any) => item.__unmatched) &&
onChange &&
@ -747,6 +758,34 @@ export const FormItemStore = StoreNode.named('FormItemStore')
}
}
function syncAutoFill(
value: any = self.value,
isPrintine: boolean = false
) {
if (
!self.multiple &&
self.autoFill &&
!isEmpty(self.autoFill) &&
self.options.length
) {
const selectedOptions = self.getSelectedOptions(value);
if (selectedOptions.length !== 1) {
return;
}
const toSync = dataMapping(self.autoFill, selectedOptions[0]);
Object.keys(toSync).forEach(key => {
const value = toSync[key];
if (typeof value === 'undefined' || value === '__undefined') {
self.form.deleteValueByName(key);
} else {
self.form.setValueByName(key, value, isPrintine);
}
});
}
}
return {
focus,
blur,
@ -764,7 +803,8 @@ export const FormItemStore = StoreNode.named('FormItemStore')
setSubStore,
reset,
openDialog,
closeDialog
closeDialog,
syncAutoFill
};
});