feat(input): add updateWhenDefaultValueChanges property

This commit is contained in:
xzdry 2022-05-16 09:58:50 +08:00
parent 96b3c98cad
commit ebf9f073a1
4 changed files with 36 additions and 3 deletions

View File

@ -19,6 +19,7 @@ const exampleProperties: Static<typeof InputPropsSpec> = {
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<RefInputType | null>(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);

View File

@ -24,6 +24,7 @@ const exampleProperties: Static<typeof TextAreaPropsSpec> = {
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<RefInputType | null>(null);
@ -60,6 +67,13 @@ export const TextArea = implementRuntimeComponent(options)(props => {
}
}, [getElement, ref]);
useEffect(() => {
if (updateWhenDefaultValueChanges) {
setValue(defaultValue);
mergeState({ value: defaultValue });
}
}, [defaultValue, updateWhenDefaultValueChanges]);
return (
<BaseTextArea
ref={ref}

View File

@ -15,7 +15,7 @@ export const InputPropsSpec = {
}),
updateWhenDefaultValueChanges: Type.Boolean({
title: 'Update When Default Value Changes',
category: 'Basic',
category: Category.Basic,
}),
allowClear: Type.Boolean({
title: 'Allow Clear',

View File

@ -19,6 +19,10 @@ export const TextAreaPropsSpec = {
title: 'Disabled',
category: Category.Basic,
}),
updateWhenDefaultValueChanges: Type.Boolean({
title: 'Update When Default Value Changes',
category: Category.Basic,
}),
size: StringUnion(['default', 'mini', 'small', 'large'], {
title: 'Size',
category: Category.Style,