| 1 | import React, { Children, useEffect, useId, useRef, useState } from "react"; |
| 2 | import { createPortal } from "react-dom"; |
| 3 | import { useSelectPopup } from "./select-popup"; |
| 4 | import { Check, ChevronDown } from "lucide-react"; |
| 5 | import "./inline-select.css"; |
| 6 | |
| 7 | |
| 8 | export function InlineSelect({ |
| 9 | children, |
| 10 | value, |
| 11 | onValueChange, |
| 12 | disabled, |
| 13 | editable = false, |
| 14 | displayLabel, |
| 15 | ...props |
| 16 | }) { |
| 17 | const id = useId(); |
| 18 | const root = useRef(null); |
| 19 | const control = useRef(null); |
| 20 | const list = useRef(null); |
| 21 | const [open, setOpen] = useState(false); |
| 22 | const [cursor, setCursor] = useState(null); |
| 23 | const options = Children.toArray(children).flatMap((child) => { |
| 24 | const group = child.type === "optgroup" ? child.props.label : null; |
| 25 | return (group ? Children.toArray(child.props.children) : [child]).map( |
| 26 | ({ props: option }) => ({ |
| 27 | value: String(option.value), |
| 28 | label: |
| 29 | Children.toArray(option.children).join("") || String(option.value), |
| 30 | disabled: option.disabled, |
| 31 | group, |
| 32 | }), |
| 33 | ); |
| 34 | }); |
| 35 | const selected = options.find((option) => option.value === String(value)); |
| 36 | const filtered = |
| 37 | editable && !selected && value |
| 38 | ? options.filter((option) => |
| 39 | (option.label + " " + option.value) |
| 40 | .toLowerCase() |
| 41 | .includes(value.toLowerCase()), |
| 42 | ) |
| 43 | : options; |
| 44 | const visible = open && !disabled && filtered.length > 0; |
| 45 | const active = filtered.findIndex( |
| 46 | (item) => item.value === cursor && !item.disabled, |
| 47 | ); |
| 48 | const position = useSelectPopup(visible, control, list, filtered.length); |
| 49 | useEffect(() => { |
| 50 | if (visible && active >= 0) { |
| 51 | const row = list.current.querySelector(`[data-index="${active}"]`); |
| 52 | if (row.offsetTop < list.current.scrollTop) |
| 53 | list.current.scrollTop = row.offsetTop; |
| 54 | else if ( |
| 55 | row.offsetTop + row.offsetHeight > |
| 56 | list.current.scrollTop + list.current.clientHeight |
| 57 | ) |
| 58 | list.current.scrollTop = |
| 59 | row.offsetTop + row.offsetHeight - list.current.clientHeight; |
| 60 | } |
| 61 | }, [visible, active]); |
| 62 | useEffect(() => { |
| 63 | if (!open) return; |
| 64 | const outside = (event) => { |
| 65 | if (!root.current.contains(event.target) && !list.current?.contains(event.target)) setOpen(false); |
| 66 | }; |
| 67 | document.addEventListener("click", outside, true); |
| 68 | return () => document.removeEventListener("click", outside, true); |
| 69 | }, [open]); |
| 70 | const show = () => { |
| 71 | setCursor(selected?.value ?? null); |
| 72 | setOpen(true); |
| 73 | }; |
| 74 | const choose = (item) => { |
| 75 | if (item.disabled) return; |
| 76 | control.current.focus(); |
| 77 | setOpen(false); |
| 78 | onValueChange(item.value); |
| 79 | }; |
| 80 | const keydown = (event) => { |
| 81 | const key = event.key; |
| 82 | if (event.nativeEvent.isComposing) return; |
| 83 | if (key === "Tab") { |
| 84 | setOpen(false); |
| 85 | } else if (key === "Escape" && open) { |
| 86 | event.preventDefault(); |
| 87 | event.stopPropagation(); |
| 88 | setOpen(false); |
| 89 | } else if (key === "Enter" || (!editable && key === " ")) { |
| 90 | if (editable && (!visible || active < 0)) return; |
| 91 | event.preventDefault(); |
| 92 | if (!visible) show(); |
| 93 | else if (active >= 0) choose(filtered[active]); |
| 94 | else setOpen(false); |
| 95 | } else if ( |
| 96 | ["ArrowUp", "ArrowDown"].includes(key) || |
| 97 | (!editable && ["Home", "End"].includes(key)) |
| 98 | ) { |
| 99 | event.preventDefault(); |
| 100 | const enabled = filtered.filter((item) => !item.disabled); |
| 101 | const index = enabled.findIndex((item) => item.value === cursor); |
| 102 | let next = |
| 103 | key === "Home" |
| 104 | ? 0 |
| 105 | : key === "End" |
| 106 | ? enabled.length - 1 |
| 107 | : Math.min( |
| 108 | enabled.length - 1, |
| 109 | Math.max(0, index + (key === "ArrowUp" ? -1 : 1)), |
| 110 | ); |
| 111 | if (!visible && selected && key.startsWith("Arrow")) |
| 112 | next = enabled.indexOf(selected); |
| 113 | setCursor(enabled[next]?.value ?? null); |
| 114 | setOpen(true); |
| 115 | } |
| 116 | }; |
| 117 | const common = { |
| 118 | ...props, |
| 119 | ref: control, |
| 120 | disabled, |
| 121 | role: "combobox", |
| 122 | "aria-expanded": visible, |
| 123 | "aria-controls": visible ? id : undefined, |
| 124 | "aria-activedescendant": |
| 125 | visible && active >= 0 ? id + "-" + active : undefined, |
| 126 | onKeyDown: keydown, |
| 127 | }; |
| 128 | return ( |
| 129 | <span ref={root} className="inline-select"> |
| 130 | {editable ? ( |
| 131 | <input |
| 132 | {...common} |
| 133 | aria-autocomplete="list" |
| 134 | autoComplete="off" |
| 135 | value={value} |
| 136 | onClick={show} |
| 137 | onChange={(event) => { |
| 138 | onValueChange(event.target.value); |
| 139 | setCursor(null); |
| 140 | setOpen(true); |
| 141 | }} |
| 142 | /> |
| 143 | ) : ( |
| 144 | <button |
| 145 | {...common} |
| 146 | type="button" |
| 147 | value={value} |
| 148 | className="inline-select-trigger" |
| 149 | onClick={() => { |
| 150 | control.current.focus(); |
| 151 | open ? setOpen(false) : show(); |
| 152 | }} |
| 153 | > |
| 154 | <span>{displayLabel ?? selected?.label ?? value}</span> |
| 155 | <ChevronDown size={14} aria-hidden="true" /> |
| 156 | </button> |
| 157 | )} |
| 158 | {visible && createPortal( |
| 159 | <span |
| 160 | ref={list} |
| 161 | id={id} |
| 162 | role="listbox" |
| 163 | aria-label={(props["aria-label"] || "") + "选项"} |
| 164 | className="inline-select-list" |
| 165 | style={position} |
| 166 | onMouseDown={(event) => event.preventDefault()} |
| 167 | onClick={(event) => event.preventDefault()} |
| 168 | > |
| 169 | {filtered.map((item, index) => ( |
| 170 | <React.Fragment key={item.value}> |
| 171 | {item.group && item.group !== filtered[index - 1]?.group && ( |
| 172 | <span className="inline-select-group" aria-hidden="true"> |
| 173 | {item.group} |
| 174 | </span> |
| 175 | )} |
| 176 | <span |
| 177 | role="option" |
| 178 | id={id + "-" + index} |
| 179 | data-index={index} |
| 180 | data-value={item.value} |
| 181 | data-active={index === active || undefined} |
| 182 | aria-disabled={Boolean(item.disabled)} |
| 183 | aria-selected={item === selected} |
| 184 | onClick={() => choose(item)} |
| 185 | > |
| 186 | <span> |
| 187 | {editable && item.label !== item.value |
| 188 | ? item.value + " · " |
| 189 | : ""} |
| 190 | {item.label} |
| 191 | </span> |
| 192 | {item === selected && <Check size={14} aria-hidden="true" />} |
| 193 | </span> |
| 194 | </React.Fragment> |
| 195 | ))} |
| 196 | </span>, document.body |
| 197 | )} |
| 198 | </span> |
| 199 | ); |
| 200 | } |