Skip to content

Commit

Permalink
feat(@uform/core): Improve noValidate reset logic
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jul 11, 2019
1 parent ab9c6e8 commit efaa75a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
9 changes: 8 additions & 1 deletion docs/Examples/next/Detail.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ const App = () => {
>
手动触发校验
</Button>
<Reset />
<Reset >值重置</Reset>
<Button
onClick={() => {
actions.reset(false,true)
}}
>
无错重置
</Button>
</FormButtonGroup>
</SchemaForm>
</Printer>
Expand Down
19 changes: 14 additions & 5 deletions packages/core/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,21 @@ export class Form {
each(this.fields, (field, name) => {
const value = this.getValue(name)
const initialValue = this.getInitialValue(name, field.path)
if (isEmpty(value) && isEmpty(initialValue)) {
return
if (noValidate) {
if (field.errors.length > 0) {
field.errors = []
field.dirty = true
}
if (field.effectErrors.length > 0) {
field.effectErrors = []
field.dirty = true
}
}
if (!isEmpty(value) || !isEmpty(initialValue)) {
field.updateState(state => {
state.value = forceClear ? undefined : initialValue
})
}
field.updateState(state => {
state.value = forceClear ? undefined : initialValue
})
if (field.dirty) {
raf(() => {
if (this.destructed) {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/state/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ export const StateForm = createHOC((options, Form) => {
return this.form.submit()
}

public reset = (forceClear?: boolean) => {
this.form.reset(forceClear)
public reset = (forceClear?: boolean, noValidate: boolean = false) => {
this.form.reset(forceClear, noValidate)
}

public validate = () => {
Expand Down

0 comments on commit efaa75a

Please sign in to comment.