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
|
import { createEffect, Show, For, type JSX, createSignal } from "solid-js"
import { createStore } from "solid-js/store"
import { FilteredListProps, useFilteredList } from "@opencode-ai/ui/hooks"
import { Icon, IconProps } from "./icon"
export interface ListProps<T> extends FilteredListProps<T> {
class?: string
children: (item: T) => JSX.Element
emptyMessage?: string
onKeyEvent?: (event: KeyboardEvent, item: T | undefined) => void
activeIcon?: IconProps["name"]
filter?: string
}
export interface ListRef {
onKeyDown: (e: KeyboardEvent) => void
setScrollRef: (el: HTMLDivElement | undefined) => void
}
export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void }) {
const [scrollRef, setScrollRef] = createSignal<HTMLDivElement | undefined>(undefined)
const [store, setStore] = createStore({
mouseActive: false,
})
const { filter, grouped, flat, reset, active, setActive, onKeyDown, onInput } = useFilteredList<T>({
items: props.items,
key: props.key,
filterKeys: props.filterKeys,
current: props.current,
groupBy: props.groupBy,
sortBy: props.sortBy,
sortGroupsBy: props.sortGroupsBy,
})
createEffect(() => {
if (props.filter === undefined) return
onInput(props.filter)
})
createEffect(() => {
filter()
scrollRef()?.scrollTo(0, 0)
reset()
})
createEffect(() => {
if (!scrollRef()) return
if (!props.current) return
const key = props.key(props.current)
requestAnimationFrame(() => {
const element = scrollRef()!.querySelector(`[data-key="${key}"]`)
element?.scrollIntoView({ block: "center" })
})
})
createEffect(() => {
const all = flat()
if (store.mouseActive || all.length === 0) return
if (active() === props.key(all[0])) {
scrollRef()?.scrollTo(0, 0)
return
}
const element = scrollRef()?.querySelector(`[data-key="${active()}"]`)
element?.scrollIntoView({ block: "nearest", behavior: "smooth" })
})
const handleSelect = (item: T | undefined, index: number) => {
props.onSelect?.(item, index)
}
const handleKey = (e: KeyboardEvent) => {
setStore("mouseActive", false)
if (e.key === "Escape") return
const all = flat()
const selected = all.find((x) => props.key(x) === active())
const index = selected ? all.indexOf(selected) : -1
props.onKeyEvent?.(e, selected)
if (e.key === "Enter") {
e.preventDefault()
if (selected) handleSelect(selected, index)
} else {
onKeyDown(e)
}
}
props.ref?.({
onKeyDown: handleKey,
setScrollRef,
})
return (
<div ref={setScrollRef} data-component="list" classList={{ [props.class ?? ""]: !!props.class }}>
<Show
when={flat().length > 0}
fallback={
<div data-slot="list-empty-state">
<div data-slot="list-message">
{props.emptyMessage ?? "No results"} for <span data-slot="list-filter">"{filter()}"</span>
</div>
</div>
}
>
<For each={grouped()}>
{(group) => (
<div data-slot="list-group">
<Show when={group.category}>
<div data-slot="list-header">{group.category}</div>
</Show>
<div data-slot="list-items">
<For each={group.items}>
{(item, i) => (
<button
data-slot="list-item"
data-key={props.key(item)}
data-active={props.key(item) === active()}
data-selected={item === props.current}
onClick={() => handleSelect(item, i())}
onMouseMove={() => {
setStore("mouseActive", true)
setActive(props.key(item))
}}
>
{props.children(item)}
<Show when={item === props.current}>
<Icon data-slot="list-item-selected-icon" name="check-small" />
</Show>
<Show when={props.activeIcon}>
{(icon) => <Icon data-slot="list-item-active-icon" name={icon()} />}
</Show>
</button>
)}
</For>
</div>
</div>
)}
</For>
</Show>
</div>
)
}
|