Allow for pre-set Enum changes (#7803)

This commit is contained in:
Jaco 2022-06-27 09:32:08 +02:00 committed by GitHub
parent b7f4335e58
commit aee491d4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,7 @@ interface Options {
interface Initial {
initialEnum: string | null;
initialValues: RawParam[] | undefined;
initialParams: RawParam[] | undefined | null;
}
function getSubTypes (registry: Registry, type: TypeDef): TypeDef[] {
@ -53,7 +53,7 @@ function getInitial (defaultValue: RawParam): Initial {
? defaultValue.value.type
: Object.keys(defaultValue.value as Record<string, unknown>)[0]
: null,
initialValues: defaultValue && defaultValue.value
initialParams: defaultValue && defaultValue.value
? defaultValue.value instanceof Enum
? [{ isValid: true, value: defaultValue.value.inner }]
: undefined
@ -73,7 +73,7 @@ function EnumParam (props: Props): React.ReactElement<Props> {
const { className = '', defaultValue, isDisabled, isError, label, onChange, overrides, registry, type, withLabel } = props;
const [{ options, subTypes }] = useState<Options>(() => getOptions(registry, type));
const [current, setCurrent] = useState<ParamDef[] | null>(() => getCurrent(registry, type, defaultValue, subTypes));
const [{ initialEnum, initialValues }] = useState<Initial>(() => getInitial(defaultValue));
const [{ initialEnum, initialParams }, setInitial] = useState<Initial>(() => getInitial(defaultValue));
const _onChange = useCallback(
(value: string): void => {
@ -84,6 +84,16 @@ function EnumParam (props: Props): React.ReactElement<Props> {
? [{ name: newType.name, type: newType }]
: null
);
if (newType) {
// if the enum changes, we want to discard the original initParams,
// these are not applicable anymore, rather use empty defaults
setInitial((prev) =>
newType.name === prev.initialEnum
? prev
: { initialEnum: prev.initialEnum, initialParams: null }
);
}
},
[subTypes]
);
@ -121,7 +131,7 @@ function EnumParam (props: Props): React.ReactElement<Props> {
overrides={overrides}
params={current}
registry={registry}
values={initialValues}
values={initialParams}
/>
)}
</Bare>