fix(client): fix error on clear value in variable input (#1723)

This commit is contained in:
Junyi 2023-04-18 20:44:53 +07:00 committed by GitHub
parent 2d2e949452
commit a3a91965c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,14 +98,12 @@ const ConstantTypes = {
function getTypedConstantOption(type, props) { function getTypedConstantOption(type) {
const { t } = useTranslation();
return { return {
value: '', value: '',
label: t('Constant'), label: '{{t("Constant")}}',
children: Object.values(ConstantTypes), children: Object.values(ConstantTypes),
content: ConstantTypes[type]?.component({ value: props.value, onChange: props.onChange }) component: ConstantTypes[type]?.component
}; };
} }
@ -116,33 +114,34 @@ type VariableOptions = {
}; };
export function Input(props) { export function Input(props) {
const compile = useCompile();
const form = useForm();
const { value = '', scope, onChange, children, button, useTypedConstant } = props; const { value = '', scope, onChange, children, button, useTypedConstant } = props;
const parsed = parseValue(value); const parsed = parseValue(value);
const isConstant = typeof parsed === 'string'; const isConstant = typeof parsed === 'string';
const type = isConstant ? parsed : ''; const type = isConstant ? parsed : '';
const variable = isConstant ? null : parsed; const variable = isConstant ? null : parsed;
const compile = useCompile(); const varialbeOptions = typeof scope === 'function' ? scope() : scope ?? [];
const { content, ...constantOption }: VariableOptions & { content: React.ReactNode } = children
const { component: ConstantComponent, ...constantOption }: VariableOptions & { component?: React.FC<any> } = children
? { ? {
value: '', value: '',
label: '{{t("Constant")}}', label: '{{t("Constant")}}'
content: children
} }
: (useTypedConstant : (useTypedConstant
? getTypedConstantOption(type, props) ? getTypedConstantOption(type)
: { : {
value: '', value: '',
label: '{{t("Null")}}', label: '{{t("Null")}}',
content: ConstantTypes.null.component() component: ConstantTypes.null.component
} }
); );
const options: VariableOptions[] = compile([ const options: VariableOptions[] = compile([
constantOption, constantOption,
...(typeof scope === 'function' ? scope() : scope ?? []), ...varialbeOptions,
]); ]);
const form = useForm();
function onSwitch(next) { function onSwitch(next) {
if (next[0] === '') { if (next[0] === '') {
if (next[1]) { if (next[1]) {
@ -247,7 +246,7 @@ export function Input(props) {
</span> </span>
) : null} ) : null}
</div> </div>
) : content} ) : children ?? <ConstantComponent value={value} onChange={onChange} />}
{options.length > 1 ? ( {options.length > 1 ? (
<Cascader <Cascader
options={options} options={options}