From a98c247bf957d0cee53d81e4b4580c35e7305383 Mon Sep 17 00:00:00 2001 From: janryWang Date: Sun, 28 Apr 2019 18:25:28 +0800 Subject: [PATCH] feat(@uform/react): Optimize package size and fixing onFieldChange initialization trigger twice --- packages/core/src/form.js | 8 ++++-- packages/react/package.json | 3 ++- packages/react/src/__tests__/effects.spec.js | 27 +++++++++++++++++--- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/core/src/form.js b/packages/core/src/form.js index 8537fbb60f0..f3a96f57c0b 100644 --- a/packages/core/src/form.js +++ b/packages/core/src/form.js @@ -22,8 +22,9 @@ import { } from './utils' import { Field } from './field' import { runValidation, format } from '@uform/validator' -import { Subject } from 'rxjs' -import { filter } from 'rxjs/operators' +import { Subject } from 'rxjs/internal/Subject' +import { filter } from 'rxjs/internal/operators/filter' +import { debounceTime } from 'rxjs/internal/operators/debounceTime' import { FormPath } from './path' const defaults = opts => ({ @@ -98,6 +99,9 @@ export class Form { if (!this.subscribes[eventName]) { this.subscribes[eventName] = new Subject() } + this.subscribes[eventName] = this.subscribes[eventName].pipe( + debounceTime(0) + ) if (isStr($filter) || isFn($filter)) { return this.subscribes[eventName].pipe( filter(isStr($filter) ? FormPath.match($filter) : $filter) diff --git a/packages/react/package.json b/packages/react/package.json index 1640fc25c4d..63810be328a 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -24,7 +24,8 @@ "@uform/utils": "0.1.3", "@uform/validator": "0.1.3", "pascal-case": "^2.0.1", - "react-eva": "^1.0.0" + "react-eva": "^1.0.0", + "rxjs-compat": "^6.5.1" }, "publishConfig": { "access": "public" diff --git a/packages/react/src/__tests__/effects.spec.js b/packages/react/src/__tests__/effects.spec.js index 54d421adb08..80484f34f16 100644 --- a/packages/react/src/__tests__/effects.spec.js +++ b/packages/react/src/__tests__/effects.spec.js @@ -74,6 +74,28 @@ test('onFormInit setFieldState', async () => { expect(queryByText('field is required')).toBeNull() }) +test('init triggers', async () => { + const callback = jest.fn() + const TestComponent = () => { + return ( + { + $('onFieldChange', 'aaa').subscribe(callback) + }} + > + + + + ) + } + + render() + await sleep(33) + expect(callback).toHaveBeenCalledTimes(1) +}) + test('onFieldChange will trigger with initialValues', async () => { const callback = jest.fn() const TestComponent = () => { @@ -104,8 +126,7 @@ test('onFieldChange will trigger with initialValues', async () => { render() await sleep(33) - expect(callback).toHaveBeenCalledTimes(3) + expect(callback).toHaveBeenCalledTimes(2) expect(callback.mock.calls[0][0].value).toBe(undefined) - expect(callback.mock.calls[1][0].value).toBe(undefined) - expect(callback.mock.calls[2][0].value).toBe(123) + expect(callback.mock.calls[1][0].value).toBe(123) })