Skip to content

Commit

Permalink
Merge pull request #11 from nighca/debug-on-change
Browse files Browse the repository at this point in the history
修复携带相同值的 `onChange` 触发后不能正确 validate 的 bug
  • Loading branch information
nighca authored Dec 7, 2019
2 parents 9a0b02a + 9fc27e4 commit a1ac6b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formstate-x",
"version": "1.0.5",
"version": "1.0.6",
"description": "Extended alternative for formstate",
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions src/fieldState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ describe('FieldState validation', () => {
state.dispose()
})

it('should work well with onChange of same value', async () => {
const state = createFieldState(1).validators(
() => null
)
await state.validate()
expect(state.validated).toBe(true)
expect(state.validating).toBe(false)
expect(state.hasError).toBe(false)

state.onChange(1)
await delay()
expect(state.validated).toBe(true)
expect(state.validating).toBe(false)
expect(state.hasError).toBe(false)
})

it('should work well with validate()', async () => {
const state = createFieldState('').validators(val => !val && 'empty')
state.validate()
Expand Down
4 changes: 3 additions & 1 deletion src/fieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export default class FieldState<TValue> extends Disposable implements Composible
* Set `_value` on change event.
*/
@action onChange(value: TValue) {
this.setValue(value)
if (value !== this._value) {
this.setValue(value)
}
}

/**
Expand Down

0 comments on commit a1ac6b4

Please sign in to comment.