Skip to content

Commit c2c6378

Browse files
fix the multi type
1 parent ea34922 commit c2c6378

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*!
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { Textarea } from "@chakra-ui/react";
20+
21+
import { paramPlaceholder, useParamStore } from "src/queries/useParamStore";
22+
23+
import type { FlexibleFormElementProps } from ".";
24+
25+
export const FieldMultiType = ({ name, namespace = "default", onUpdate }: FlexibleFormElementProps) => {
26+
const { disabled, paramsDict, setParamsDict } = useParamStore(namespace);
27+
const param = paramsDict[name] ?? paramPlaceholder;
28+
29+
// Display objects as pretty-printed JSON, plain strings as-is.
30+
const displayValue =
31+
param.value !== null && typeof param.value === "object"
32+
? JSON.stringify(param.value, undefined, 2)
33+
: String(param.value ?? "");
34+
35+
const handleChange = (value: string) => {
36+
if (paramsDict[name]) {
37+
if (value === "") {
38+
// "undefined" values are removed from params, so we set it to null to avoid falling back to DAG defaults.
39+
paramsDict[name].value = null;
40+
} else {
41+
try {
42+
paramsDict[name].value = JSON.parse(value) as JSON;
43+
} catch {
44+
paramsDict[name].value = value;
45+
}
46+
}
47+
}
48+
49+
setParamsDict(paramsDict);
50+
onUpdate(value);
51+
};
52+
53+
return (
54+
<Textarea
55+
disabled={disabled}
56+
id={`element_${name}`}
57+
name={`element_${name}`}
58+
onChange={(event) => handleChange(event.target.value)}
59+
rows={6}
60+
size="sm"
61+
value={displayValue}
62+
/>
63+
);
64+
};

airflow-core/src/airflow/ui/src/components/FlexibleForm/FieldSelector.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { FieldDateTime } from "./FieldDateTime";
2626
import { FieldDropdown } from "./FieldDropdown";
2727
import { FieldMultiSelect } from "./FieldMultiSelect";
2828
import { FieldMultilineText } from "./FieldMultilineText";
29+
import { FieldMultiType } from "./FieldMultiType";
2930
import { FieldNumber } from "./FieldNumber";
3031
import { FieldObject } from "./FieldObject";
3132
import { FieldString } from "./FieldString";
@@ -88,6 +89,10 @@ const isFieldObject = (fieldType: string) => fieldType === "object";
8889
const isFieldStringArray = (fieldType: string, fieldSchema: ParamSchema) =>
8990
fieldType === "array" && (fieldSchema.items?.type === undefined || fieldSchema.items.type === "string");
9091

92+
const isFieldMultiType = (fieldSchema: ParamSchema) =>
93+
Array.isArray(fieldSchema.type) &&
94+
fieldSchema.type.filter((type) => type !== "null").length > 1;
95+
9196
const isFieldTime = (fieldType: string, fieldSchema: ParamSchema) =>
9297
fieldType === "string" && fieldSchema.format === "time";
9398

@@ -105,6 +110,12 @@ export const FieldSelector = ({ name, namespace = "default", onUpdate }: Flexibl
105110
value: currentParam?.value ?? initialParam.value,
106111
};
107112

113+
// Check for multiple non-null types before inferring a single type —
114+
// inferType picks only the first, discarding the others.
115+
if (isFieldMultiType(param.schema)) {
116+
return <FieldMultiType name={name} namespace={namespace} onUpdate={onUpdate} />;
117+
}
118+
108119
const fieldType = inferType(param);
109120

110121
if (isFieldBool(fieldType)) {

0 commit comments

Comments
 (0)