From 175b2f49ae5206d719f143d019020fb6e940926f Mon Sep 17 00:00:00 2001 From: quirkyshop Date: Sun, 19 Jan 2020 10:16:11 +0800 Subject: [PATCH 1/3] fix: fix 616 --- packages/next/README.zh-cn.md | 23 +++++++++++++++++++++++ packages/react/src/hooks/useFormSpy.ts | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/next/README.zh-cn.md b/packages/next/README.zh-cn.md index 7ae06f0d622..a488c2d7c17 100644 --- a/packages/next/README.zh-cn.md +++ b/packages/next/README.zh-cn.md @@ -6,6 +6,29 @@ npm install --save @uform/next ``` +```jsx +import React, { useState } from "react"; +import { SchemaForm, Submit } from '@uform/next'; + +function App() { + const [visible, setVisible] = useState(true) + return ( +
+ { visible ? { + return new Promise(resolve => { + setVisible(false) + setTimeout(resolve, 1000) + }) + }}> + + : null } +
+ ); +} + +ReactDOM.render(, document.getElementById('root')) +``` + ### 目录 diff --git a/packages/react/src/hooks/useFormSpy.ts b/packages/react/src/hooks/useFormSpy.ts index 0c6778e7d32..c6b365d4d75 100644 --- a/packages/react/src/hooks/useFormSpy.ts +++ b/packages/react/src/hooks/useFormSpy.ts @@ -16,15 +16,17 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => { const broadcast = useContext(BroadcastContext) const form = useContext(FormContext) const initializedRef = useRef(false) + const unmountRef = useRef(false) const subscriberId = useRef() const [type, setType] = useState(LifeCycleTypes.ON_FORM_INIT) const [state, dispatch] = useReducer( (state, action) => props.reducer(state, action, form), {} ) - const subscriber = useCallback(({ type, payload }) => { + const subscriber = useCallback(({ type, payload }) => { if (initializedRef.current) return setTimeout(() => { + if (unmountRef.current) return if (isStr(props.selector) && FormPath.parse(props.selector).match(type)) { setType(type) dispatch({ @@ -56,6 +58,7 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => { } else if (broadcast) { broadcast.unsubscribe(subscriberId.current) } + unmountRef.current = true } }, []) const formApi: IForm = form ? form : broadcast && broadcast.getContext() From 3c01c1b3b12481dcee12378f8db225a7745ab887 Mon Sep 17 00:00:00 2001 From: quirkyshop Date: Sun, 19 Jan 2020 10:23:32 +0800 Subject: [PATCH 2/3] fix: remove document demo --- packages/next/README.zh-cn.md | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/packages/next/README.zh-cn.md b/packages/next/README.zh-cn.md index a488c2d7c17..7ae06f0d622 100644 --- a/packages/next/README.zh-cn.md +++ b/packages/next/README.zh-cn.md @@ -6,29 +6,6 @@ npm install --save @uform/next ``` -```jsx -import React, { useState } from "react"; -import { SchemaForm, Submit } from '@uform/next'; - -function App() { - const [visible, setVisible] = useState(true) - return ( -
- { visible ? { - return new Promise(resolve => { - setVisible(false) - setTimeout(resolve, 1000) - }) - }}> - - : null } -
- ); -} - -ReactDOM.render(, document.getElementById('root')) -``` - ### 目录 From ab41263f23ea2e3f36133c7a8e13c2d4c45907c0 Mon Sep 17 00:00:00 2001 From: quirkyshop Date: Sun, 19 Jan 2020 14:24:24 +0800 Subject: [PATCH 3/3] fix: add onSubmit condition --- packages/react/src/__tests__/form.spec.tsx | 80 +++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/packages/react/src/__tests__/form.spec.tsx b/packages/react/src/__tests__/form.spec.tsx index bdbe939fd92..525b391f743 100644 --- a/packages/react/src/__tests__/form.spec.tsx +++ b/packages/react/src/__tests__/form.spec.tsx @@ -1,12 +1,14 @@ -import React from 'react' +import React, { useState } from 'react' import { Form, Field, createFormActions, FormEffectHooks, - IFieldStateUIProps + IFieldStateUIProps, + FormSpy, } from '../index' -import { render } from '@testing-library/react' +import { render, fireEvent, waitForElement } from '@testing-library/react' +import { LifeCycleTypes } from '@uform/core' const Input: React.FC = props => ( @@ -60,6 +62,78 @@ describe('test all apis', () => { await actions.submit() expect(onSubmitHandler).toBeCalledWith({ aaa: 'hello world' }) }) + + test('onSubmit async', async () => { + const actions = createFormActions() + const onSubmitEndHandler = jest.fn() + const App = () => { + return
+
{ + return new Promise((resolve) => { + setTimeout(resolve) + }) + }} + actions={actions} + > + + + {({ type }) => { + return + }} + +
+
+ } + const { getByTestId, queryByTestId } = render() + + expect(onSubmitEndHandler).toBeCalledTimes(0) + fireEvent.click(queryByTestId('submit')) + const submitEle = await waitForElement( + () => getByTestId('submit') + ) + + expect(submitEle.textContent).not.toEqual(LifeCycleTypes.ON_FORM_INIT) + }) + + test('onSubmit unmount promise', async () => { + const actions = createFormActions() + const onSubmitEndHandler = jest.fn() + const App = () => { + const [visible, setVisible] = useState(true) + return
+ {visible ?
{ + return new Promise((resolve) => { + setVisible(false) + setTimeout(resolve) + }) + }} + actions={actions} + > + + + {({ type }) => { + return + }} + +
: null } +
+ } + const { getByTestId, queryByTestId } = render() + + expect(onSubmitEndHandler).toBeCalledTimes(0) + fireEvent.click(queryByTestId('submit')) + const submitEle = await waitForElement( + () => getByTestId('submit') + ) + + expect(submitEle.textContent).toEqual(LifeCycleTypes.ON_FORM_INIT) + }) }) describe('major scenes', () => {