Skip to content

Commit

Permalink
feat(form): resetFields 增加 namepath 参数,用于重置指定的字段
Browse files Browse the repository at this point in the history
  • Loading branch information
oasis-cloud committed Jan 20, 2025
1 parent 16fb6e5 commit c547da8
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 16 deletions.
53 changes: 53 additions & 0 deletions src/packages/form/__tests__/form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,56 @@ test('no-style and render function', async () => {
expect(relatedInput).toBeTruthy()
})
})

test('reset usename filed', async () => {
const Demo1 = () => {
const [form] = Form.useForm()
return (
<>
<Form
form={form}
labelPosition="right"
footer={
<>
<div
id="reset"
onClick={() => {
form.resetFields(['username'])
}}
>
Reset
</div>
</>
}
>
<Form.Item
align="center"
required
label="字段A"
name="username"
rules={[{ max: 5, message: '字段A不能超过5个字' }]}
>
<Input placeholder="请输入字段A" type="text" />
</Form.Item>
</Form>
</>
)
}
const { container } = render(<Demo1 />)
const input = container.querySelector('input')
const reset = container.querySelector('#reset')
if (input) {
fireEvent.change(input, { target: { value: 'NutUI React Taro' } })
await waitFor(() => {
expect(
container.querySelector('.nut-form-item-body-tips')
).toHaveTextContent('字段A不能超过5个字')
})
}
if (reset) {
fireEvent.click(reset)
await waitFor(() => {
expect(container.querySelector('.nut-form-item-body-tips')).toBeNull()
})
}
})
32 changes: 24 additions & 8 deletions src/packages/form/useform.taro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class FormStore {

validateFields = async (nameList?: NamePath[]) => {
let filterEntities = []
this.errors.length = 0
// this.errors.length = 0
if (!nameList || nameList.length === 0) {
filterEntities = this.fieldEntities
} else {
Expand All @@ -200,13 +200,29 @@ class FormStore {
}
}

resetFields = () => {
this.errors.length = 0
const nextStore = merge({}, this.initialValues)
this.updateStore(nextStore)
this.fieldEntities.forEach((entity: FormFieldEntity) => {
entity.onStoreChange('reset')
})
resetFields = (namePaths?: NamePath[]) => {
if (namePaths) {
namePaths.forEach((path) => {
this.errors[path] = null
this.fieldEntities.forEach((entity: FormFieldEntity) => {
const name = entity.props.name
if (name === path) {
if (path in this.initialValues) {
this.updateStore({ [path]: this.initialValues[path] })
} else {
delete this.store[path]
}
entity.onStoreChange('reset')
}
})
})
} else {
const nextStore = merge({}, this.initialValues)
this.updateStore(nextStore)
this.fieldEntities.forEach((entity: FormFieldEntity) => {
entity.onStoreChange('reset')
})
}
}

// 监听事件
Expand Down
32 changes: 24 additions & 8 deletions src/packages/form/useform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class FormStore {

validateFields = async (nameList?: NamePath[]) => {
let filterEntities = []
this.errors.length = 0
// this.errors.length = 0
if (!nameList || nameList.length === 0) {
filterEntities = this.fieldEntities
} else {
Expand All @@ -200,13 +200,29 @@ class FormStore {
}
}

resetFields = () => {
this.errors.length = 0
const nextStore = merge({}, this.initialValues)
this.updateStore(nextStore)
this.fieldEntities.forEach((entity: FormFieldEntity) => {
entity.onStoreChange('reset')
})
resetFields = (namePaths?: NamePath[]) => {
if (namePaths) {
namePaths.forEach((path) => {
this.errors[path] = null
this.fieldEntities.forEach((entity: FormFieldEntity) => {
const name = entity.props.name
if (name === path) {
if (path in this.initialValues) {
this.updateStore({ [path]: this.initialValues[path] })

Check warning on line 211 in src/packages/form/useform.ts

View check run for this annotation

Codecov / codecov/patch

src/packages/form/useform.ts#L211

Added line #L211 was not covered by tests
} else {
delete this.store[path]
}
entity.onStoreChange('reset')
}
})
})
} else {
const nextStore = merge({}, this.initialValues)
this.updateStore(nextStore)
this.fieldEntities.forEach((entity: FormFieldEntity) => {
entity.onStoreChange('reset')
})
}
}

// 监听事件
Expand Down

0 comments on commit c547da8

Please sign in to comment.