Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions packages/components/form/_example/customized-form-controls.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Form, Input, Button, MessagePlugin, Space, Select } from 'tdesign-react';
import { Button, Form, Input, Select, Space } from 'tdesign-react';
import type { FormProps } from 'tdesign-react';

interface ICourseSelect {
Expand Down Expand Up @@ -30,10 +30,17 @@ function CourseSelect(props: ICourseSelect) {
]}
value={value?.type}
onChange={(v) => {
// type 改变时,清空 name
onChange?.({
...value,
type: v as string,
name: '',
});

// type 改变时,保留 name
// onChange?.({
// ...value,
// type: v as string,
// });
}}
placeholder="请选择课程类型"
/>
Expand All @@ -54,20 +61,29 @@ function CourseSelect(props: ICourseSelect) {
export default function BaseForm() {
const [form] = Form.useForm();

const onSubmit: FormProps['onSubmit'] = (e) => {
console.log(e);
if (e.validateResult === true) {
MessagePlugin.info('提交成功');
}
const handleSubmit: FormProps['onSubmit'] = () => {
console.log('form.getFieldsValue', form.getFieldsValue(true));
};

return (
<Form form={form} onSubmit={onSubmit} colon labelWidth={100}>
<Form form={form} colon labelWidth={100} onSubmit={handleSubmit}>
<FormItem label="课程" name="course">
<CourseSelect />
</FormItem>
<FormItem style={{ marginLeft: 100 }}>
<Button type="submit" theme="primary">
<Button
onClick={() => {
form.setFieldsValue({
course: {
type: 'math',
name: '线性代数',
},
});
}}
>
设置数据
</Button>
<Button type="submit" theme="primary" style={{ marginLeft: 12 }}>
提交
</Button>
</FormItem>
Expand Down
31 changes: 21 additions & 10 deletions packages/components/form/hooks/useInstance.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cloneDeep, get, isEmpty, isFunction, merge, set } from 'lodash-es';
import { cloneDeep, isArray, isEmpty, isFunction, isObject, merge, set, get } from 'lodash-es';
import log from '@tdesign/common-js/log/index';
import useConfig from '../../hooks/useConfig';
import { calcFieldValue, findFormItem, objectToArray, travelMapFromObject } from '../utils';
import { calcFieldValue, findFormItem, travelMapFromObject } from '../utils';

import type { FormItemInstance } from '../FormItem';
import type {
Expand Down Expand Up @@ -164,18 +164,29 @@ export default function useInstance(
* 对外方法,设置对应 FormItem 的值
*/
function setFieldsValue(fields = {}) {
const nameLists = objectToArray(fields);
nameLists.forEach((nameList) => {
const fieldValue = get(fields, nameList);
const formItemRef = findFormItem(nameList, formMapRef);
const setValueByPath = (value: any, path: (string | number)[] = []) => {
// 当前路径对应的 FormItem 存在,直接设置
const formItemRef = findFormItem(path, formMapRef);
if (formItemRef?.current) {
formItemRef.current.setValue?.(fieldValue);
formItemRef.current.setValue?.(value);
return;
}

// 递归处理对象
// { user: { name: '' } } => [['user', 'name']]
// { user: [{ name: '' }]} => [['user']]
if (isObject(value) && !isArray(value) && !isEmpty(value)) {
Object.keys(value).forEach((key) => {
setValueByPath(value[key], [...path, key]);
});
} else {
set(floatingFormDataRef.current, nameList, fieldValue);
set(floatingFormDataRef.current, path, value);
// 确保 store 始终包含所有被 set 的字段
set(form?.store, nameList, fieldValue);
set(form?.store, path, value);
}
});
};

setValueByPath(fields);
}

/**
Expand Down
22 changes: 1 addition & 21 deletions packages/components/form/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, has, isArray, isEmpty, isObject } from 'lodash-es';
import { get, has } from 'lodash-es';
import type { FormItemInstance } from '../FormItem';
import type { NamePath } from '../type';

Expand Down Expand Up @@ -62,26 +62,6 @@ function findFormItemDeep(
}
}

// { user: { name: '' } } => [['user', 'name']]
// 不处理数组类型
// { user: [{ name: '' }]} => [['user']]
export function objectToArray(obj: Record<string | number, any>) {
const result: (string | number)[][] = [];

function traverse(current: any, path: string[] = []) {
if (isObject(current) && !isArray(current) && !isEmpty(current)) {
Object.keys(current).forEach((key) => {
traverse(current[key], [...path, key]);
});
} else {
result.push(path);
}
}

traverse(obj);
return result;
}

export function calcFieldValue(name: NamePath, value: any, numericKeyAsIndex = true) {
if (!Array.isArray(name)) return { [name]: value };
let result: any = value;
Expand Down
Loading
Loading