Skip to content

Commit

Permalink
fix(BasicForm): setFieldsValue not work in form when use date comp (#…
Browse files Browse the repository at this point in the history
…3819)

* fix(BasicForm): setFieldsValue not work in form when use date comp

* fix(BasicForm): add lost time component
  • Loading branch information
electroluxcode committed May 9, 2024
1 parent 88e77db commit 7538c57
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/Form/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createPlaceholderMessage(component: ComponentType) {
const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'];

function genType() {
return [...DATE_TYPE, 'RangePicker'];
return [...DATE_TYPE, 'RangePicker',"TimeRangePicker"];
}

export function setComponentRuleType(
Expand Down
49 changes: 36 additions & 13 deletions src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isArray, isFunction, isObject, isString, isNil } from '@/utils/is';
import { deepMerge } from '@/utils';
import { dateItemType, defaultValueComponents, isIncludeSimpleComponents } from '../helper';
import { dateUtil } from '@/utils/dateUtil';
import { cloneDeep, has, uniqBy, get } from 'lodash-es';
import { cloneDeep, has, uniqBy, get, set } from 'lodash-es';
import { error } from '@/utils/log';

interface UseFormActionContext {
Expand All @@ -20,6 +20,23 @@ interface UseFormActionContext {
handleFormValues: Fn;
}

function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined {
const pattern = /^\[(.+)\]$/;
if (pattern.test(field)) {
const match = field.match(pattern);
if (match && match[1]) {
const keys = match[1].split(',');
if (!keys.length) {
return undefined;
}
const result = [];
keys.forEach((k, index) => {
set(result, index, values[k.trim()]);
});
return result.filter(Boolean).length ? result : undefined;
}
}
}
export function useFormEvents({
emit,
getProps,
Expand Down Expand Up @@ -83,28 +100,36 @@ export function useFormEvents({
});
}

const constructValue = get(value, key);
let constructValue
const setDateFieldValue = (v) => {
return v ? (_props?.valueFormat ? v : dateUtil(v)) : null;
};

// 0| '' is allow
if (hasKey || !!constructValue) {
const fieldValue = constructValue || value;
// time type
if (itemIsDateType(key)) {

// Adapt date component
if(itemIsDateComponent(schema?.component)){
constructValue = tryConstructArray(key, values) ;
if(!!constructValue){
const fieldValue = constructValue || value;
if (Array.isArray(fieldValue)) {
const arr: any[] = [];
for (const ele of fieldValue) {
arr.push(setDateFieldValue(ele));
}
unref(formModel)[key] = arr;
validKeys.push(key);
} else {
unref(formModel)[key] = setDateFieldValue(fieldValue);
validKeys.push(key);
}
} else {
unref(formModel)[key] = fieldValue;
}
}

// Adapt common component
if (hasKey) {
constructValue = get(value, key);
const fieldValue = constructValue || value;
unref(formModel)[key] = fieldValue;
if (_props?.onChange) {
_props?.onChange(fieldValue);
}
Expand Down Expand Up @@ -295,10 +320,8 @@ export function useFormEvents({
/**
* @description: Is it time
*/
function itemIsDateType(key: string) {
return unref(getSchema).some((item) => {
return item.field === key && item.component ? dateItemType.includes(item.component) : false;
});
function itemIsDateComponent(key: string) {
return dateItemType.includes(key);
}

async function validateFields(nameList?: NamePath[] | undefined) {
Expand Down

0 comments on commit 7538c57

Please sign in to comment.