未验证 提交 79eb909c 编写于 作者: L lee 提交者: GitHub

feat: make fields key of form deconstructable (#1663)

Co-authored-by: Nliheng.wu <liheng.wu@tcl.com>
上级 92722093
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册