-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComboboxField.tsx
More file actions
95 lines (89 loc) · 2.76 KB
/
ComboboxField.tsx
File metadata and controls
95 lines (89 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@workspace/ui/components";
import { Button } from "@workspace/ui/components/shadcn/button";
import { Label } from "@workspace/ui/components/shadcn/label";
import { RefreshCw } from "@workspace/ui/icons";
import { useEffect, useState } from "react";
import type { BranchesFetchState } from "../../hooks/useBranches";
import type { FieldProps } from "../../types/common/settings";
export const ComboboxField = ({
field,
value,
onChange,
loading,
fetchState,
}: FieldProps<string> & { fetchState?: BranchesFetchState }) => {
const predefined = field.options ?? [];
const items =
value && !predefined.includes(value) ? [value, ...predefined] : predefined;
const [inputValue, setInputValue] = useState(value ?? "");
const [selectedValue, setSelectedValue] = useState<string | null>(value ?? null);
// Sync when value prop changes (e.g. after external save)
useEffect(() => {
setInputValue(value ?? "");
setSelectedValue(value ?? null);
}, [value]);
const commitInput = () => {
if (inputValue !== value) onChange(inputValue);
};
return (
<div className="w-70 space-y-2">
<div className="flex items-center gap-2">
<Label>{field.label}</Label>
{fetchState && (
<Button
variant="ghost"
size="sm"
onClick={fetchState.refetch}
disabled={loading}
className={`h-5 w-5 p-0 ${fetchState.error ? "text-destructive hover:text-destructive" : ""}`}
>
<RefreshCw className={`h-3 w-3 ${loading ? "animate-spin" : ""}`} />
</Button>
)}
</div>
<Combobox
items={items}
value={selectedValue}
onValueChange={(v) => {
setSelectedValue(v);
setInputValue(v ?? "");
onChange(v ?? "");
}}
>
<ComboboxInput
placeholder={
loading ? "Loading..." : (field.placeholder ?? "Choose or type...")
}
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value);
// Clear selection so base-ui doesn't reset the input to the selected value
setSelectedValue(null);
}}
onBlur={commitInput}
onKeyDown={(e) => {
if (e.key === "Enter") commitInput();
}}
/>
<ComboboxContent>
<ComboboxEmpty>No branches found</ComboboxEmpty>
<ComboboxList>
{(item) => (
<ComboboxItem key={item} value={item}>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
);
};
export default ComboboxField;