Skip to content

Commit

Permalink
fix(form): after setting validateTrigger, trigger should be excluded (
Browse files Browse the repository at this point in the history
  • Loading branch information
oasis-cloud authored Mar 1, 2024
1 parent cf15911 commit 6e40c53
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 6 deletions.
46 changes: 46 additions & 0 deletions src/packages/form/__tests__/form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,52 @@ test('form set initialValues', () => {
)
})

test('form validateTrigger', async () => {
const { container, rerender } = render(
<Form>
<Form.Item
label="字段A"
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: '请输入字段A' }]}
>
<Input placeholder="请输入字段A" type="text" />
</Form.Item>
</Form>
)
const input = container.querySelector('.nut-input-native')
if (input) {
fireEvent.focus(input)
fireEvent.blur(input)
await waitFor(() => {
const errorMessage = container.querySelector('.nut-form-item-body-tips')
expect(errorMessage).toBeTruthy()
})
}
rerender(
<Form initialValues={{ username: 'NutUI-React' }}>
<Form.Item
label="字段A"
name="username"
required
validateTrigger={['onBlur']}
rules={[{ required: true, message: '请输入字段A' }]}
>
<Input placeholder="请输入字段A" type="text" />
</Form.Item>
</Form>
)
if (input) {
fireEvent.focus(input)
fireEvent.blur(input)
await waitFor(() => {
const errorMessage = container.querySelector('.nut-form-item-body-tips')
expect(errorMessage).toBeTruthy()
})
}
})

test('useForm', () => {
const App = () => {
const [form] = Form.useForm()
Expand Down
15 changes: 15 additions & 0 deletions src/packages/form/demo.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const FormDemo = () => {
radioOption: '选项',
picker: '选择器',
select: '请选择',
validateTrigger: '校验触发时机',
},
'en-US': {
basic: 'Basic Usage',
Expand Down Expand Up @@ -137,6 +138,7 @@ const FormDemo = () => {
radioOption: 'radio',
picker: 'Picker',
select: 'Please select',
validateTrigger: 'Validate Trigger',
},
})
const [state, SetState] = useState({
Expand Down Expand Up @@ -475,6 +477,19 @@ const FormDemo = () => {
</Form.Item>
</Form>

<h2>{translated.validateTrigger}</h2>
<Form>
<Form.Item
label={translated.name}
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: translated.nameTip }]}
>
<Input placeholder={translated.nameTip1} type="text" />
</Form.Item>
</Form>

<h2>{translated.title5}</h2>
<Form
style={{ '--nutui-form-item-label-width': '120px' }}
Expand Down
15 changes: 15 additions & 0 deletions src/packages/form/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const FormDemo = () => {
radioOption: '选项',
picker: '选择器',
select: '请选择',
validateTrigger: '校验触发时机',
},
'en-US': {
basic: 'Basic Usage',
Expand Down Expand Up @@ -133,6 +134,7 @@ const FormDemo = () => {
radioOption: 'radio',
picker: 'Picker',
select: 'Please select',
validateTrigger: 'Validate Trigger',
},
})

Expand Down Expand Up @@ -431,6 +433,19 @@ const FormDemo = () => {
</Form.Item>
</Form>

<h2>{translated.validateTrigger}</h2>
<Form>
<Form.Item
label={translated.name}
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: translated.nameTip }]}
>
<Input placeholder={translated.nameTip1} type="text" />
</Form.Item>
</Form>

<h2>{translated.title5}</h2>
<Form
style={{ '--nutui-form-item-label-width': '120px' }}
Expand Down
33 changes: 32 additions & 1 deletion src/packages/form/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,38 @@ export default App;

:::

### form type
### Validate Trigger

:::demo

```tsx
import React from "react";
import { Form, Input } from '@nutui/nutui-react';

const App = () => {
return (
<>
<Form>
<Form.Item
label="Field A"
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: "Place Input Field A" }]}
>
<Input placeholder="Place Input Field A" type="text" />
</Form.Item>
</Form>
</>
)
}

export default App;
```

:::

### Form Type

:::demo

Expand Down
31 changes: 31 additions & 0 deletions src/packages/form/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,37 @@ export default App;

:::

### 校验触发时机

:::demo

```tsx
import React from "react";
import { Form, Input } from '@nutui/nutui-react';

const App = () => {
return (
<>
<Form>
<Form.Item
label="字段A"
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: "请输入字段A" }]}
>
<Input placeholder="请输入字段A" type="text" />
</Form.Item>
</Form>
</>
)
}

export default App;
```

:::

### 表单类型

:::demo
Expand Down
31 changes: 31 additions & 0 deletions src/packages/form/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,37 @@ export default App;

:::

### 校验触发时机

:::demo

```tsx
import React from "react";
import { Form, Input } from '@nutui/nutui-react-taro';

const App = () => {
return (
<>
<Form>
<Form.Item
label="字段A"
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: "请输入字段A" }]}
>
<Input placeholder="请输入字段A" type="text" />
</Form.Item>
</Form>
</>
)
}

export default App;
```

:::

### 表单类型

:::demo
Expand Down
33 changes: 32 additions & 1 deletion src/packages/form/doc.zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,38 @@ export default App;

:::

### 錶單類型
### 校驗觸發時機

:::demo

```tsx
import React from "react";
import { Form, Input } from '@nutui/nutui-react';

const App = () => {
return (
<>
<Form>
<Form.Item
label="欄位A"
name="username"
required
validateTrigger="onBlur"
rules={[{ required: true, message: "請輸入欄位A" }]}
>
<Input placeholder="請輸入欄位A" type="text" />
</Form.Item>
</Form>
</>
)
}

export default App;
```

:::

### 表單類型

:::demo

Expand Down
4 changes: 2 additions & 2 deletions src/packages/formitem/formitem.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export class FormItem extends React.Component<
if (validateTrigger) {
validateTriggers =
typeof validateTrigger === 'string'
? [...validateTriggers, validateTrigger]
: [...validateTriggers, ...validateTrigger]
? [validateTrigger]
: [...validateTrigger]
validateTriggers.forEach((trigger) => {
const originTrigger = controlled[trigger]
controlled[trigger] = (...args: any) => {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/formitem/formitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export class FormItem extends React.Component<
if (validateTrigger) {
validateTriggers =
typeof validateTrigger === 'string'
? [...validateTriggers, validateTrigger]
: [...validateTriggers, ...validateTrigger]
? [validateTrigger]
: [...validateTrigger]
validateTriggers.forEach((trigger) => {
const originTrigger = controlled[trigger]
controlled[trigger] = (...args: any) => {
Expand Down

0 comments on commit 6e40c53

Please sign in to comment.