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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
import { createEffect, Show, For, type JSX, splitProps } from "solid-js"
import { Dialog, DialogProps, Icon, IconButton, Input } from "@opencode-ai/ui"
import { createStore } from "solid-js/store"
import { FilteredListProps, useFilteredList } from "@opencode-ai/ui/hooks"
interface SelectDialogProps<T>
extends FilteredListProps<T>,
Pick<DialogProps, "trigger" | "onOpenChange" | "defaultOpen"> {
title: string
placeholder?: string
emptyMessage?: string
children: (item: T) => JSX.Element
onSelect?: (value: T | undefined) => void
}
export function SelectDialog<T>(props: SelectDialogProps<T>) {
const [dialog, others] = splitProps(props, ["trigger", "onOpenChange", "defaultOpen"])
let closeButton!: HTMLButtonElement
let scrollRef: HTMLDivElement | undefined
const [store, setStore] = createStore({
mouseActive: false,
})
const { filter, grouped, flat, reset, clear, active, setActive, onKeyDown, onInput } = useFilteredList<T>({
items: others.items,
key: others.key,
filterKeys: others.filterKeys,
current: others.current,
groupBy: others.groupBy,
sortBy: others.sortBy,
sortGroupsBy: others.sortGroupsBy,
})
createEffect(() => {
filter()
scrollRef?.scrollTo(0, 0)
reset()
})
createEffect(() => {
const all = flat()
if (store.mouseActive || all.length === 0) return
if (active() === others.key(all[0])) {
scrollRef?.scrollTo(0, 0)
return
}
const element = scrollRef?.querySelector(`[data-key="${active()}"]`)
element?.scrollIntoView({ block: "nearest", behavior: "smooth" })
})
const handleInput = (value: string) => {
onInput(value)
reset()
}
const handleSelect = (item: T | undefined) => {
others.onSelect?.(item)
closeButton.click()
}
const handleKey = (e: KeyboardEvent) => {
setStore("mouseActive", false)
if (e.key === "Escape") return
if (e.key === "Enter") {
e.preventDefault()
const selected = flat().find((x) => others.key(x) === active())
if (selected) handleSelect(selected)
} else {
onKeyDown(e)
}
}
const handleOpenChange = (open: boolean) => {
if (!open) clear()
props.onOpenChange?.(open)
}
return (
<Dialog modal {...dialog} onOpenChange={handleOpenChange}>
<Dialog.Header>
<Dialog.Title>{others.title}</Dialog.Title>
<Dialog.CloseButton ref={closeButton} style={{ display: "none" }} />
</Dialog.Header>
<div data-component="select-dialog-input">
<div data-slot="input-container">
<Icon data-slot="icon" name="magnifying-glass" />
<Input
data-slot="input"
type="text"
value={filter()}
onChange={(value) => handleInput(value)}
onKeyDown={handleKey}
placeholder={others.placeholder}
autofocus
spellcheck={false}
autocorrect="off"
autocomplete="off"
autocapitalize="off"
/>
</div>
<Show when={filter()}>
<IconButton
data-slot="clear-button"
icon="circle-x"
variant="ghost"
onClick={() => {
onInput("")
reset()
}}
/>
</Show>
</div>
<Dialog.Body ref={scrollRef} data-component="select-dialog" class="no-scrollbar">
<Show
when={flat().length > 0}
fallback={
<div data-slot="empty-state">
<div data-slot="message">
{props.emptyMessage ?? "No search results"} for <span data-slot="filter">"{filter()}"</span>
</div>
</div>
}
>
<For each={grouped()}>
{(group) => (
<div data-slot="group">
<Show when={group.category}>
<div data-slot="header">{group.category}</div>
</Show>
<div data-slot="list">
<For each={group.items}>
{(item) => (
<button
data-slot="item"
data-key={others.key(item)}
data-active={others.key(item) === active()}
onClick={() => handleSelect(item)}
onMouseMove={() => {
setStore("mouseActive", true)
setActive(others.key(item))
}}
>
{others.children(item)}
</button>
)}
</For>
</div>
</div>
)}
</For>
</Show>
</Dialog.Body>
</Dialog>
)
}
|