8000 fix: handle empty strings for Select component (#18553) · coder/coder@25f1b76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25f1b76

Browse files
authored
fix: handle empty strings for Select component (#18553)
resolve #18361 Its possible for a dynamic parameter option value to be an empty string which will cause the following error in the Radix Select component. The solution is to handle empty strings so that they are not set directly in the component. `Uncaught Error: A <Select.Item /> must have a value prop that is not an empty string. This is because the Select value can be set to an empty string to clear the selection and show the placeholder.` ``` data "coder_parameter" "radio" { name = "radio" display_name = "An example of a radio input" description = "The next parameter supports a single value." type = "string" form_type = "dropdown" order = 1 default = "" option { name = "Empty" value = "" } } ```
1 parent 6ed2204 commit 25f1b76

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

site/src/modules/workspaces/DynamicParameter/DynamicParameter.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,17 @@ const ParameterField: FC<ParameterFieldProps> = ({
379379
id,
380380
}) => {
381381
switch (parameter.form_type) {
382-
case "dropdown":
382+
case "dropdown": {
383+
const EMPTY_VALUE_PLACEHOLDER = "__EMPTY_STRING__";
384+
const selectValue = value === "" ? EMPTY_VALUE_PLACEHOLDER : value;
385+
const handleSelectChange = (newValue: string) => {
386+
onChange(newValue === EMPTY_VALUE_PLACEHOLDER ? "" : newValue);
387+
};
388+
383389
return (
384390
<Select
385-
onValueChange={onChange}
386-
value={value}
391+
onValueChange={handleSelectChange}
392+
value={selectValue}
387393
disabled={disabled}
388394
required={parameter.required}
389395
>
@@ -393,14 +399,26 @@ const ParameterField: FC<ParameterFieldProps> = ({
393399
/>
394400
</SelectTrigger>
395401
<SelectContent>
396-
{parameter.options.map((option) => (
397-
<SelectItem key={option.value.value} value={option.value.value}>
398-
<OptionDisplay option={option} />
399-
</SelectItem>
400-
))}
402+
{parameter.options.map((option, index) => {
403+
const optionValue =
404+
option.value.value === ""
405+
? EMPTY_VALUE_PLACEHOLDER
406+
: option.value.value;
407+
return (
408+
<SelectItem
409+
key={
410+
option.value.value || `${EMPTY_VALUE_PLACEHOLDER}:${index}`
411+
}
412+
value={optionValue}
413+
>
414+
<OptionDisplay option={option} />
415+
</SelectItem>
416+
);
417+
})}
401418
</SelectContent>
402419
</Select>
403420
);
421+
}
404422

405423
case "multi-select": {
406424
const parsedValues = parseStringArrayValue(value ?? "");

0 commit comments

Comments
 (0)
0