From fc8d339629695eaf1538b9aaa5d5b0005780caf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=20Cai?= Date: Tue, 17 Dec 2024 11:37:43 +0800 Subject: [PATCH 1/2] fix(form): `value: [{ min: 0 }]` invalid and add test --- src/form/__tests__/form.test.tsx | 57 ++++++++++++++++++++++++++++++++ src/form/formModel.ts | 4 +-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/form/__tests__/form.test.tsx b/src/form/__tests__/form.test.tsx index f516f5f0aa..5bc6481f1c 100644 --- a/src/form/__tests__/form.test.tsx +++ b/src/form/__tests__/form.test.tsx @@ -7,6 +7,7 @@ import Input from '../../input'; import Button from '../../button'; import Radio from '../../radio'; import { HelpCircleIcon } from 'tdesign-icons-react'; +import InputNumber from '../../input-number'; const { FormItem, FormList } = Form; @@ -423,6 +424,62 @@ describe('Form 组件测试', () => { expect(container.querySelector('.t-input__extra').innerHTML).toBe('please input username'); }); + test('FormItem rules min max', async () => { + const TestForm = () => { + const initialValues = { + year1: -2, + year2: 1, + year3: 4, + year4: -4, + year5: -1, + year6: 2, + }; + return ( +
+ + + + + + + + + + + + + + + + + + + + + +
+ ); + }; + const { container, getByText, getByPlaceholderText } = render(); + fireEvent.click(getByText('提交')); + await mockDelay(); + expect(container.querySelector('.t-input__extra')).toBeNull(); + + // 错误验证 + fireEvent.change(getByPlaceholderText('year1'), { target: { value: -4 } }); + fireEvent.change(getByPlaceholderText('year2'), { target: { value: -1 } }); + fireEvent.change(getByPlaceholderText('year3'), { target: { value: 2 } }); + fireEvent.change(getByPlaceholderText('year4'), { target: { value: -2 } }); + fireEvent.change(getByPlaceholderText('year5'), { target: { value: 1 } }); + fireEvent.change(getByPlaceholderText('year6'), { target: { value: 4 } }); + fireEvent.click(getByText('提交')); + await mockDelay(); + const input__extraList = container.querySelectorAll('.t-input__extra'); + input__extraList.forEach((item: { innerHTML: string }, index: number) => { + expect(item.innerHTML).toBe(`year${index + 1} error`); + }); + }); + test('动态渲染并初始赋值', () => { const TestForm = () => { const [form] = Form.useForm(); diff --git a/src/form/formModel.ts b/src/form/formModel.ts index 42fe38a684..dc05ce7385 100644 --- a/src/form/formModel.ts +++ b/src/form/formModel.ts @@ -54,7 +54,7 @@ const VALIDATE_MAP = { validator: (val: ValueType, validate: CustomValidator): ReturnType => validate(val), }; -export type ValidateFuncType = typeof VALIDATE_MAP[keyof typeof VALIDATE_MAP]; +export type ValidateFuncType = (typeof VALIDATE_MAP)[keyof typeof VALIDATE_MAP]; /** * 校验某一条数据的某一条规则,一种校验规则不满足则不再进行校验。 @@ -75,7 +75,7 @@ export async function validateOneRule(value: ValueType, rule: FormRule): Promise } const validateRule: ValidateFuncType = VALIDATE_MAP[key]; // 找到一个校验规则,则无需再找,因为参数只允许对一个规则进行校验 - if (validateRule && rule[key]) { + if (validateRule && rule[key] !== undefined && rule[key] !== null) { // rule 值为 true 则表示没有校验参数,只是对值进行默认规则校验 vOptions = rule[key] === true ? undefined : rule[key]; vValidateFun = validateRule; From 8569ad259322723b1f15eedd68cff205c98c2175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?w=C5=AB=20y=C4=81ng?= Date: Wed, 18 Dec 2024 11:43:24 +0800 Subject: [PATCH 2/2] chore: optimize --- src/form/formModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/form/formModel.ts b/src/form/formModel.ts index dc05ce7385..3dc8fc65ab 100644 --- a/src/form/formModel.ts +++ b/src/form/formModel.ts @@ -75,7 +75,7 @@ export async function validateOneRule(value: ValueType, rule: FormRule): Promise } const validateRule: ValidateFuncType = VALIDATE_MAP[key]; // 找到一个校验规则,则无需再找,因为参数只允许对一个规则进行校验 - if (validateRule && rule[key] !== undefined && rule[key] !== null) { + if (validateRule && ![undefined, null].includes(rule[key])) { // rule 值为 true 则表示没有校验参数,只是对值进行默认规则校验 vOptions = rule[key] === true ? undefined : rule[key]; vValidateFun = validateRule;