feat: make fields key of form deconstructable (#1663)
Co-authored-by: liheng.wu <liheng.wu@tcl.com>
This commit is contained in:
parent
927220933e
commit
79eb909c66
|
|
@ -11,6 +11,43 @@ interface UseFormValuesContext {
|
|||
getProps: ComputedRef<FormProps>;
|
||||
formModel: Recordable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @desription deconstruct array-link key. This method will mutate the target.
|
||||
*/
|
||||
function tryDeconstructArray(key: string, value: any, target: Recordable) {
|
||||
const pattern = /^\[(.+)\]$/;
|
||||
if (pattern.test(key)) {
|
||||
const match = key.match(pattern);
|
||||
if (match && match[1]) {
|
||||
const keys = match[1].split(',');
|
||||
value = Array.isArray(value) ? value : [value];
|
||||
keys.forEach((k, index) => {
|
||||
set(target, k.trim(), value[index]);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @desription deconstruct object-link key. This method will mutate the target.
|
||||
*/
|
||||
function tryDeconstructObject(key: string, value: any, target: Recordable) {
|
||||
const pattern = /^\{(.+)\}$/;
|
||||
if (pattern.test(key)) {
|
||||
const match = key.match(pattern);
|
||||
if (match && match[1]) {
|
||||
const keys = match[1].split(',');
|
||||
value = isObject(value) ? value : {};
|
||||
keys.forEach((k) => {
|
||||
set(target, k.trim(), value[k.trim()]);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function useFormValues({
|
||||
defaultValueRef,
|
||||
getSchema,
|
||||
|
|
@ -41,7 +78,10 @@ export function useFormValues({
|
|||
if (isString(value)) {
|
||||
value = value.trim();
|
||||
}
|
||||
set(res, key, value);
|
||||
if (!tryDeconstructArray(key, value, res) && !tryDeconstructObject(key, value, res)) {
|
||||
// 没有解构成功的,按原样赋值
|
||||
set(res, key, value);
|
||||
}
|
||||
}
|
||||
return handleRangeTimeValue(res);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -530,6 +530,22 @@
|
|||
span: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'divider-deconstruct',
|
||||
component: 'Divider',
|
||||
label: '字段解构',
|
||||
helpMessage: ['如果组件的值是 array 或者 object', '可以根据 ES6 的解构语法分别取值'],
|
||||
},
|
||||
{
|
||||
field: '[startTime, endTime]',
|
||||
label: '时间范围',
|
||||
component: 'RangePicker',
|
||||
componentProps: {
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
placeholder: ['开始时间', '结束时间'],
|
||||
showTime: { format: 'HH:mm:ss' },
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'divider-others',
|
||||
component: 'Divider',
|
||||
|
|
@ -602,6 +618,7 @@
|
|||
keyword.value = '';
|
||||
},
|
||||
handleSubmit: (values: any) => {
|
||||
console.log('values', values);
|
||||
createMessage.success('click search,values:' + JSON.stringify(values));
|
||||
},
|
||||
check,
|
||||
|
|
|
|||
Loading…
Reference in New Issue