From ebf9f073a1d6c11a0ecfd2dd6a08a450f59589ab Mon Sep 17 00:00:00 2001 From: xzdry Date: Mon, 16 May 2022 09:58:50 +0800 Subject: [PATCH] feat(input): add updateWhenDefaultValueChanges property --- packages/arco-lib/src/components/Input.tsx | 17 ++++++++++++++++- packages/arco-lib/src/components/TextArea.tsx | 16 +++++++++++++++- packages/arco-lib/src/generated/types/Input.ts | 2 +- .../arco-lib/src/generated/types/TextArea.ts | 4 ++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/arco-lib/src/components/Input.tsx b/packages/arco-lib/src/components/Input.tsx index fad2060b..3f37e700 100644 --- a/packages/arco-lib/src/components/Input.tsx +++ b/packages/arco-lib/src/components/Input.tsx @@ -19,6 +19,7 @@ const exampleProperties: Static = { disabled: false, readOnly: false, defaultValue: '', + updateWhenDefaultValueChanges: false, placeholder: 'please input', error: false, size: 'default', @@ -46,7 +47,14 @@ const options = { }; export const Input = implementRuntimeComponent(options)(props => { - const { getElement, slotsElements, customStyle, callbackMap, mergeState } = props; + const { + getElement, + updateWhenDefaultValueChanges, + slotsElements, + customStyle, + callbackMap, + mergeState, + } = props; const { defaultValue, ...cProps } = getComponentProps(props); const ref = useRef(null); const [value, setValue] = useState(defaultValue); @@ -58,6 +66,13 @@ export const Input = implementRuntimeComponent(options)(props => { } }, [getElement, ref]); + useEffect(() => { + if (updateWhenDefaultValueChanges) { + setValue(defaultValue); + mergeState({ value: defaultValue }); + } + }, [defaultValue, updateWhenDefaultValueChanges]); + const onChange = useCallback( value => { setValue(value); diff --git a/packages/arco-lib/src/components/TextArea.tsx b/packages/arco-lib/src/components/TextArea.tsx index 248cb824..eb4a340d 100644 --- a/packages/arco-lib/src/components/TextArea.tsx +++ b/packages/arco-lib/src/components/TextArea.tsx @@ -24,6 +24,7 @@ const exampleProperties: Static = { error: false, size: 'default', autoSize: true, + updateWhenDefaultValueChanges:false }; const options = { @@ -48,7 +49,13 @@ const options = { }; export const TextArea = implementRuntimeComponent(options)(props => { - const { getElement, customStyle, callbackMap, mergeState } = props; + const { + getElement, + updateWhenDefaultValueChanges, + customStyle, + callbackMap, + mergeState, + } = props; const { defaultValue, ...cProps } = getComponentProps(props); const [value, setValue] = useState(defaultValue); const ref = useRef(null); @@ -60,6 +67,13 @@ export const TextArea = implementRuntimeComponent(options)(props => { } }, [getElement, ref]); + useEffect(() => { + if (updateWhenDefaultValueChanges) { + setValue(defaultValue); + mergeState({ value: defaultValue }); + } + }, [defaultValue, updateWhenDefaultValueChanges]); + return (