| 14 | } |
| 15 | |
| 16 | export function SelectDialog<T>(props: SelectDialogProps<T>) { |
| 17 | const [dialog, others] = splitProps(props, ["trigger", "onOpenChange", "defaultOpen"]) |
| 18 | let closeButton!: HTMLButtonElement |
| 19 | let inputRef: HTMLInputElement | undefined |
| 20 | const [filter, setFilter] = createSignal("") |
| 21 | let listRef: ListRef | undefined |
| 22 | |
| 23 | createEffect(() => { |
| 24 | if (!props.current) return |
| 25 | const key = props.key(props.current) |
| 26 | requestAnimationFrame(() => { |
| 27 | const element = document.querySelector(`[data-key="${key}"]`) |
| 28 | element?.scrollIntoView({ block: "center" }) |
| 29 | }) |
| 30 | }) |
| 31 | |
| 32 | const handleSelect = (item: T | undefined, index: number) => { |
| 33 | others.onSelect?.(item, index) |
| 34 | closeButton.click() |
| 35 | } |
| 36 | |
| 37 | const handleKey = (e: KeyboardEvent) => { |
| 38 | if (e.key === "Escape") return |
| 39 | listRef?.onKeyDown(e) |
| 40 | } |
| 41 | |
| 42 | const handleOpenChange = (open: boolean) => { |
| 43 | if (!open) setFilter("") |
| 44 | props.onOpenChange?.(open) |
| 45 | } |
| 46 | |
| 47 | return ( |
| 48 | <Dialog modal {...dialog} onOpenChange={handleOpenChange}> |
| 49 | <Dialog.Header> |
| 50 | <Dialog.Title>{others.title}</Dialog.Title> |
| 51 | <Show when={others.actions}>{others.actions}</Show> |
| 52 | <Dialog.CloseButton ref={closeButton} tabIndex={-1} style={{ display: others.actions ? "none" : undefined }} /> |
| 53 | </Dialog.Header> |
| 54 | <div data-slot="select-dialog-content"> |
| 55 | <div data-component="select-dialog-input"> |
| 56 | <div data-slot="select-dialog-input-container"> |
| 57 | <Icon name="magnifying-glass" /> |
| 58 | <TextField |
| 59 | ref={inputRef} |
| 60 | autofocus |
| 61 | variant="ghost" |
| 62 | data-slot="select-dialog-input" |
| 63 | type="text" |
| 64 | value={filter()} |
| 65 | onChange={setFilter} |
| 66 | onKeyDown={handleKey} |
| 67 | placeholder={others.placeholder} |
| 68 | spellcheck={false} |
| 69 | autocorrect="off" |
| 70 | autocomplete="off" |
| 71 | autocapitalize="off" |
| 72 | /> |
| 73 | </div> |