Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified db/TDesign.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export const formItemDefaultProps: TdFormItemProps = {
arrow: false,
label: '',
requiredMark: undefined,
shouldUpdate: false,
showErrorMessage: undefined,
};
11 changes: 11 additions & 0 deletions packages/products/tdesign-mobile-react/src/form/form.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ colon | Boolean | false | \- | N
contentAlign | String | left | options: left/right | N
disabled | Boolean | undefined | \- | N
errorMessage | Object | - | Typescript: `FormErrorMessage` | N
form | Object | - | Typescript: `FormInstanceFunctions` | N
id | String | undefined | native id attribute of the form,which supports being used in conjunction with non-form buttons through the form attribute to trigger form events | N
initialData | Object | - | \- | N
labelAlign | String | right | options: left/right/top | N
labelWidth | String / Number | '81px' | \- | N
preventSubmitDefault | Boolean | true | \- | N
Expand All @@ -36,7 +38,14 @@ name | params | return | description
className | String | - | className of component | N
style | Object | - | CSS(Cascading Style Sheets),Typescript: `React.CSSProperties` | N
clearValidate | `(fields?: Array<keyof FormData>)` | \- | required
currentElement | \- | `HTMLFormElement` | \-
getCurrentElement | \- | `HTMLFormElement` | \-
getFieldValue | `(field: NamePath) ` | `unknown` | required
getFieldsValue | `(nameList: string[] \| boolean)` | `getFieldsValue<FormData>` | required。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface getFieldsValue<T>{ (nameList: true): T; (nameList: any[]): Record<keyof T, unknown>;}`<br/>
getValidateMessage | `(fields?: Array<keyof FormData>)` | `Array<FormRule> \| void` | required
reset | `(params?: FormResetParams<FormData>)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
setFields | `(fields: FieldData[])` | \- | required。Typescript: `(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)
setFieldsValue | `(field: Data)` | \- | required
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`<br/>
submit | `(params?: { showErrorMessage?: boolean })` | \- | required
validate | `(params?: FormValidateParams)` | `Promise<FormValidateResult<FormData>>` | required。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface FormValidateParams { fields?: Array<string>; showErrorMessage?: boolean; trigger?: ValidateTriggerType }`<br/><br/>`type ValidateTriggerType = 'blur' \| 'change' \| 'submit' \| 'all'`<br/>
Expand All @@ -53,12 +62,14 @@ arrow | Boolean | false | \- | N
contentAlign | String | - | options: left/right | N
for | String | - | \- | N
help | TNode | - | Typescript: `string \| TNode`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/blob/develop/src/common.ts) | N
initialData | String / Number / Object / Array | - | Typescript: `InitialData` `type InitialData = any`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts) | N
label | TNode | '' | Typescript: `string \| TNode`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/blob/develop/src/common.ts) | N
labelAlign | String | - | options: left/right/top | N
labelWidth | String / Number | - | \- | N
name | String / Number / Array | - | Typescript: `NamePath` `type NamePath = string \| number \| Array<string \| number>`。[see more ts definition](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts) | N
requiredMark | Boolean | undefined | \- | N
rules | Array | - | Typescript: `Array<FormRule>` | N
shouldUpdate | Boolean / Function | false | Typescript: `boolean \| ((prevValue, curValue) => boolean)` | N
showErrorMessage | Boolean | undefined | \- | N

### FormRule
Expand Down
11 changes: 11 additions & 0 deletions packages/products/tdesign-mobile-react/src/form/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ colon | Boolean | false | 是否在表单标签字段右侧显示冒号 | N
contentAlign | String | left | 表单内容对齐方式:左对齐、右对齐。可选项:left/right | N
disabled | Boolean | undefined | 是否禁用整个表单 | N
errorMessage | Object | - | 表单错误信息配置,示例:`{ idcard: '请输入正确的身份证号码', max: '字符长度不能超过 ${max}' }`。TS 类型:`FormErrorMessage` | N
form | Object | - | 经 `Form.useForm()` 创建的 form 控制实例。TS 类型:`FormInstanceFunctions` | N
id | String | undefined | 表单原生的id属性,支持用于配合非表单内的按钮通过form属性来触发表单事件 | N
initialData | Object | - | 表单初始数据,重置时所需初始数据,优先级小于 FormItem 设置的 initialData | N
labelAlign | String | right | 表单字段标签对齐方式:左对齐、右对齐、顶部对齐。可选项:left/right/top | N
labelWidth | String / Number | '81px' | 可以整体设置label标签宽度,默认为81px | N
preventSubmitDefault | Boolean | true | 是否阻止表单提交默认事件(表单提交默认事件会刷新页面),设置为 `true` 可以避免刷新 | N
Expand All @@ -36,7 +38,14 @@ onValuesChange | Function | | TS 类型:`(changedValues: Record<string, unkno
className | String | - | 类名 | N
style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
clearValidate | `(fields?: Array<keyof FormData>)` | \- | 必需。清空校验结果。可使用 fields 指定清除部分字段的校验结果,fields 值为空则表示清除所有字段校验结果。清除邮箱校验结果示例:`clearValidate(['email'])`
currentElement | \- | `HTMLFormElement` | 获取 form dom 元素
getCurrentElement | \- | `HTMLFormElement` | 获取 form dom 元素
getFieldValue | `(field: NamePath) ` | `unknown` | 必需。获取单个字段值
getFieldsValue | `(nameList: string[] \| boolean)` | `getFieldsValue<FormData>` | 必需。获取一组字段名对应的值,当调用 getFieldsValue(true) 时返回所有表单数据。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface getFieldsValue<T>{ (nameList: true): T; (nameList: any[]): Record<keyof T, unknown>;}`<br/>
getValidateMessage | `(fields?: Array<keyof FormData>)` | `Array<FormRule> \| void` | 必需。获取校验结果,当调用 getValidateMessage() 时返回所有校验结果
reset | `(params?: FormResetParams<FormData>)` | \- | 必需。重置表单,表单里面没有重置按钮`<button type=\"reset\" />`时可以使用该方法,默认重置全部字段为空,该方法会触发 `reset` 事件。<br />如果表单属性 `resetType='empty'` 或者 `reset.type='empty'` 会重置为空;<br />如果表单属性 `resetType='initial'` 或者 `reset.type='initial'` 会重置为表单初始值。<br />`reset.fields` 用于设置具体重置哪些字段,示例:`reset({ type: 'initial', fields: ['name', 'age'] })`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
setFields | `(fields: FieldData[])` | \- | 必需。设置多组字段状态。TS 类型:`(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)
setFieldsValue | `(field: Data)` | \- | 必需。设置表单字段值
setValidateMessage | `(message: FormValidateMessage<FormData>)` | \- | 必需。设置自定义校验结果,如远程校验信息直接呈现。注意需要在组件挂载结束后使用该方法。`FormData` 指表单数据泛型。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] }`<br/><br/>`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`<br/>
submit | `(params?: { showErrorMessage?: boolean })` | \- | 必需。提交表单,表单里面没有提交按钮`<button type=\"submit\" />`时可以使用该方法。`showErrorMessage` 表示是否在提交校验不通过时显示校验不通过的原因,默认显示。该方法会触发 `submit` 事件
validate | `(params?: FormValidateParams)` | `Promise<FormValidateResult<FormData>>` | 必需。校验函数,包含错误文本提示等功能。泛型 `FormData` 表示表单数据 TS 类型。<br/>【关于参数】`params.fields` 表示校验字段,如果设置了 `fields`,本次校验将仅对这些字段进行校验。`params.trigger` 表示本次触发校验的范围,'params.trigger = blur' 表示只触发校验规则设定为 trigger='blur' 的字段,'params.trigger = change' 表示只触发校验规则设定为 trigger='change' 的字段,默认触发全范围校验。`params.showErrorMessage` 表示校验结束后是否显示错误文本提示,默认显示。<br />【关于返回值】返回值为 true 表示校验通过;如果校验不通过,返回值为校验结果列表。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts)。<br/>`interface FormValidateParams { fields?: Array<string>; showErrorMessage?: boolean; trigger?: ValidateTriggerType }`<br/><br/>`type ValidateTriggerType = 'blur' \| 'change' \| 'submit' \| 'all'`<br/>
Expand All @@ -53,12 +62,14 @@ arrow | Boolean | false | 是否显示右侧箭头 | N
contentAlign | String | - | 表单内容对齐方式,优先级高于 Form.contentAlign。可选项:left/right | N
for | String | - | label 原生属性 | N
help | TNode | - | 表单项说明内容。TS 类型:`string \| TNode`。[通用类型定义](https://github.com/Tencent/tdesign-mobile-react/blob/develop/src/common.ts) | N
initialData | String / Number / Object / Array | - | 表单初始数据,重置时所需初始数据。TS 类型:`InitialData` `type InitialData = any`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts) | N
label | TNode | '' | 字段标签名称。TS 类型:`string \| TNode`。[通用类型定义](https://github.com/Tencent/tdesign-mobile-react/blob/develop/src/common.ts) | N
labelAlign | String | - | 表单字段标签对齐方式:左对齐、右对齐、顶部对齐。默认使用 Form 的对齐方式,优先级高于 Form.labelAlign。可选项:left/right/top | N
labelWidth | String / Number | - | 可以整体设置标签宽度,优先级高于 Form.labelWidth | N
name | String / Number / Array | - | 表单字段名称。TS 类型:`NamePath` `type NamePath = string \| number \| Array<string \| number>`。[详细类型定义](https://github.com/Tencent/tdesign-mobile-react/tree/develop/src/form/type.ts) | N
requiredMark | Boolean | undefined | 是否显示必填符号(*),优先级高于 Form.requiredMark | N
rules | Array | - | 表单字段校验规则。TS 类型:`Array<FormRule>` | N
shouldUpdate | Boolean / Function | false | TS 类型:`boolean \| ((prevValue, curValue) => boolean)` | N
showErrorMessage | Boolean | undefined | 校验不通过时,是否显示错误提示信息,优先级高于 `Form.showErrorMessage` | N

### FormRule
Expand Down
59 changes: 59 additions & 0 deletions packages/products/tdesign-mobile-react/src/form/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ export interface TdFormProps<FormData extends Data = Data> {
* 表单错误信息配置,示例:`{ idcard: '请输入正确的身份证号码', max: '字符长度不能超过 ${max}' }`
*/
errorMessage?: FormErrorMessage;
/**
* 经 `Form.useForm()` 创建的 form 控制实例
*/
form?: FormInstanceFunctions;
/**
* 表单原生的id属性,支持用于配合非表单内的按钮通过form属性来触发表单事件
*/
id?: string;
/**
* 表单初始数据,重置时所需初始数据,优先级小于 FormItem 设置的 initialData
*/
initialData?: object;
/**
* 表单字段标签对齐方式:左对齐、右对齐、顶部对齐
* @default right
Expand Down Expand Up @@ -105,10 +113,38 @@ export interface FormInstanceFunctions<FormData extends Data = Data> {
* 清空校验结果。可使用 fields 指定清除部分字段的校验结果,fields 值为空则表示清除所有字段校验结果。清除邮箱校验结果示例:`clearValidate(['email'])`
*/
clearValidate: (fields?: Array<keyof FormData>) => void;
/**
* 获取 form dom 元素
*/
currentElement?: () => HTMLFormElement;
/**
* 获取 form dom 元素
*/
getCurrentElement?: () => HTMLFormElement;
/**
* 获取单个字段值
*/
getFieldValue: (field: NamePath) => unknown;
/**
* 获取一组字段名对应的值,当调用 getFieldsValue(true) 时返回所有表单数据
*/
getFieldsValue: (nameList: string[] | boolean) => getFieldsValue<FormData>;
/**
* 获取校验结果,当调用 getValidateMessage() 时返回所有校验结果
*/
getValidateMessage: (fields?: Array<keyof FormData>) => Array<FormRule> | void;
/**
* 重置表单,表单里面没有重置按钮`<button type=\"reset\" />`时可以使用该方法,默认重置全部字段为空,该方法会触发 `reset` 事件。<br />如果表单属性 `resetType='empty'` 或者 `reset.type='empty'` 会重置为空;<br />如果表单属性 `resetType='initial'` 或者 `reset.type='initial'` 会重置为表单初始值。<br />`reset.fields` 用于设置具体重置哪些字段,示例:`reset({ type: 'initial', fields: ['name', 'age'] })`
*/
reset: (params?: FormResetParams<FormData>) => void;
/**
* 设置多组字段状态
*/
setFields: (fields: FieldData[]) => void;
/**
* 设置表单字段值
*/
setFieldsValue: (field: Data) => void;
/**
* 设置自定义校验结果,如远程校验信息直接呈现。注意需要在组件挂载结束后使用该方法。`FormData` 指表单数据泛型
*/
Expand Down Expand Up @@ -146,6 +182,10 @@ export interface TdFormItemProps {
* 表单项说明内容
*/
help?: TNode;
/**
* 表单初始数据,重置时所需初始数据
*/
initialData?: InitialData;
/**
* 字段标签名称
* @default ''
Expand All @@ -171,6 +211,11 @@ export interface TdFormItemProps {
* 表单字段校验规则
*/
rules?: Array<FormRule>;
/**
* null
* @default false
*/
shouldUpdate?: boolean | ((prevValue, curValue) => boolean);
/**
* 校验不通过时,是否显示错误提示信息,优先级高于 `Form.showErrorMessage`
*/
Expand Down Expand Up @@ -355,11 +400,23 @@ export type ErrorList = Array<FormRule>;

export type ValidateResultContext<T extends Data> = Omit<SubmitContext<T>, 'e'>;

export interface getFieldsValue<T> {
(nameList: true): T;
(nameList: any[]): Record<keyof T, unknown>;
}

export interface FormResetParams<FormData> {
type?: 'initial' | 'empty';
fields?: Array<keyof FormData>;
}

export interface FieldData {
name: NamePath;
value?: unknown;
status?: string;
validateMessage?: { type?: string; message?: string };
}

export type FormValidateMessage<FormData> = { [field in keyof FormData]: FormItemValidateMessage[] };

export interface FormItemValidateMessage {
Expand All @@ -377,6 +434,8 @@ export type ValidateTriggerType = 'blur' | 'change' | 'submit' | 'all';

export type Data = { [key: string]: any };

export type InitialData = any;

export type NamePath = string | number | Array<string | number>;

export interface IsDateOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ currentElement | \- | `HTMLFormElement` | \-
getCurrentElement | \- | `HTMLFormElement` | \-
getFieldValue | `(field: NamePath) ` | `unknown` | required
getFieldsValue | `(nameList: string[] \| boolean)` | `getFieldsValue<FormData>` | required。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)。<br/>`interface getFieldsValue<T>{ (nameList: true): T; (nameList: any[]): Record<keyof T, unknown>;}`<br/>
getValidateMessage | `(fields?: Array<keyof FormData>)` | `Array<FormRule> \| voi` | required
reset | `(params?: FormResetParams<FormData>)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
setFields | `(fields: FieldData[])` | \- | required。Typescript: `(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)
setFieldsValue | `(field: Data)` | \- | required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ currentElement | \- | `HTMLFormElement` | 获取 form dom 元素
getCurrentElement | \- | `HTMLFormElement` | 获取 form dom 元素
getFieldValue | `(field: NamePath) ` | `unknown` | 必需。获取单个字段值
getFieldsValue | `(nameList: string[] \| boolean)` | `getFieldsValue<FormData>` | 必需。获取一组字段名对应的值,当调用 getFieldsValue(true) 时返回所有表单数据。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)。<br/>`interface getFieldsValue<T>{ (nameList: true): T; (nameList: any[]): Record<keyof T, unknown>;}`<br/>
getValidateMessage | `(fields?: Array<keyof FormData>)` | `Array<FormRule> \| voi` | 必需。获取校验结果,当调用 getValidateMessage() 时返回所有校验结果
reset | `(params?: FormResetParams<FormData>)` | \- | 必需。重置表单,表单里面没有重置按钮`<button type=\"reset\" />`时可以使用该方法,默认重置全部字段为空,该方法会触发 `reset` 事件。<br />如果表单属性 `resetType='empty'` 或者 `reset.type='empty'` 会重置为空;<br />如果表单属性 `resetType='initial'` 或者 `reset.type='initial'` 会重置为表单初始值。<br />`reset.fields` 用于设置具体重置哪些字段,示例:`reset({ type: 'initial', fields: ['name', 'age'] })`。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)。<br/>`interface FormResetParams<FormData> { type?: 'initial' \| 'empty'; fields?: Array<keyof FormData> }`<br/>
setFields | `(fields: FieldData[])` | \- | 必需。设置多组字段状态。TS 类型:`(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/form/type.ts)
setFieldsValue | `(field: Data)` | \- | 必需。设置表单字段值
Expand Down
Loading