From 097547c57a996d3dc47763ab71a119ee480e4667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 30 Apr 2024 13:43:13 +0800 Subject: [PATCH 1/5] chore: rm defaultProps --- src/Field.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Field.tsx b/src/Field.tsx index 10c3ad3b..20af7499 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -562,11 +562,11 @@ class Field extends React.Component implements F public getControlled = (childProps: ChildProps = {}) => { const { name, - trigger, + trigger = 'onChange', validateTrigger, getValueFromEvent, normalize, - valuePropName, + valuePropName = 'value', getValueProps, fieldContext, } = this.props; @@ -581,7 +581,7 @@ class Field extends React.Component implements F const mergedGetValueProps = getValueProps || ((val: StoreValue) => ({ [valuePropName]: val })); // eslint-disable-next-line @typescript-eslint/no-explicit-any - const originTriggerFunc: any = childProps[trigger]; + const originTriggerFunc = childProps[trigger]; const valueProps = name !== undefined ? mergedGetValueProps(value) : {}; From a7605022fe1be097cc57e3aa59482b094004b091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 30 Apr 2024 13:44:08 +0800 Subject: [PATCH 2/5] chore: rm defaultProps --- src/Field.tsx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Field.tsx b/src/Field.tsx index 20af7499..58c7cca5 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -111,11 +111,6 @@ export interface FieldState { class Field extends React.Component implements FieldEntity { public static contextType = FieldContext; - public static defaultProps = { - trigger: 'onChange', - valuePropName: 'value', - }; - public state = { resetCount: 0, }; From 1226d7fe08a41aa6751aaf4c09430f1168e28f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 30 Apr 2024 15:25:08 +0800 Subject: [PATCH 3/5] fix: fix --- src/Field.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Field.tsx b/src/Field.tsx index 58c7cca5..d7e73afd 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -575,7 +575,6 @@ class Field extends React.Component implements F const value = this.getValue(); const mergedGetValueProps = getValueProps || ((val: StoreValue) => ({ [valuePropName]: val })); - // eslint-disable-next-line @typescript-eslint/no-explicit-any const originTriggerFunc = childProps[trigger]; const valueProps = name !== undefined ? mergedGetValueProps(value) : {}; From dfd8a4447d703f4bb3651baf2ce12927ce2a6952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 30 Apr 2024 15:59:31 +0800 Subject: [PATCH 4/5] fix: fix --- src/Field.tsx | 9 +++++++-- tests/validate.test.tsx | 28 ++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Field.tsx b/src/Field.tsx index d7e73afd..1b9d1cba 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -111,6 +111,11 @@ export interface FieldState { class Field extends React.Component implements FieldEntity { public static contextType = FieldContext; + public static defaultProps = { + trigger: 'onChange', + valuePropName: 'value', + }; + public state = { resetCount: 0, }; @@ -557,11 +562,11 @@ class Field extends React.Component implements F public getControlled = (childProps: ChildProps = {}) => { const { name, - trigger = 'onChange', + trigger, validateTrigger, getValueFromEvent, normalize, - valuePropName = 'value', + valuePropName, getValueProps, fieldContext, } = this.props; diff --git a/tests/validate.test.tsx b/tests/validate.test.tsx index 20cf7c01..23ef93e2 100644 --- a/tests/validate.test.tsx +++ b/tests/validate.test.tsx @@ -1075,11 +1075,8 @@ describe('Form.Validate', () => { matchError(container.querySelectorAll('.field')[1], `validate`); matchError(container.querySelectorAll('.field')[2], false); - // Revalidate - rerender( - , - ); + rerender(); formRef.current.validateFields({ dirty: true }); await waitFakeTime(); @@ -1090,3 +1087,26 @@ describe('Form.Validate', () => { jest.useRealTimers(); }); }); + +const useDefaultProps = (originalProps: T, defaultProps: Record): T => { + const mergedProps = React.useMemo(() => { + const props = Object.assign({}, originalProps); + Object.keys(defaultProps).forEach(key => { + // https://github.com/facebook/react/blob/main/packages/react/src/jsx/ReactJSXElement.js#L388-L394 + if (props[key] === undefined) { + props[key] = defaultProps[key]; + } + }); + return props; + }, [originalProps, defaultProps]); + return mergedProps; +}; + +export default useDefaultProps; + +const props = (originalProps, defaultProps); + +const App: React.FC = originalProps => { + const props = useDefaultProps(originalProps, defaultProps); + return
xxx
; +}; From 6459cdd014568208c3850ffef18f2331ac77c9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Tue, 30 Apr 2024 16:00:08 +0800 Subject: [PATCH 5/5] fix: fix --- tests/validate.test.tsx | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/tests/validate.test.tsx b/tests/validate.test.tsx index 23ef93e2..660b1431 100644 --- a/tests/validate.test.tsx +++ b/tests/validate.test.tsx @@ -1087,26 +1087,3 @@ describe('Form.Validate', () => { jest.useRealTimers(); }); }); - -const useDefaultProps = (originalProps: T, defaultProps: Record): T => { - const mergedProps = React.useMemo(() => { - const props = Object.assign({}, originalProps); - Object.keys(defaultProps).forEach(key => { - // https://github.com/facebook/react/blob/main/packages/react/src/jsx/ReactJSXElement.js#L388-L394 - if (props[key] === undefined) { - props[key] = defaultProps[key]; - } - }); - return props; - }, [originalProps, defaultProps]); - return mergedProps; -}; - -export default useDefaultProps; - -const props = (originalProps, defaultProps); - -const App: React.FC = originalProps => { - const props = useDefaultProps(originalProps, defaultProps); - return
xxx
; -};