| 1 | import React, { useEffect, useId, useRef, useState } from "react"; |
| 2 | import { createPortal } from "react-dom"; |
| 3 | import { Check, ChevronDown } from "lucide-react"; |
| 4 | import { useSelectPopup } from "../select-popup"; |
| 5 | |
| 6 | |
| 7 | export function InlineSelect({ label, value, options, onChange, onLongPress, footer, disabled, icon }) { |
| 8 | const id = useId(); |
| 9 | const root = useRef(null); |
| 10 | const trigger = useRef(null); |
| 11 | const list = useRef(null); |
| 12 | const hold = useRef(null); |
| 13 | const held = useRef(false); |
| 14 | const origin = useRef(null); |
| 15 | const [open, setOpen] = useState(false); |
| 16 | const [cursor, setCursor] = useState(value); |
| 17 | const selected = options.find(item => item.value === value); |
| 18 | const visible = open && !disabled; |
| 19 | const position = useSelectPopup(visible, trigger, list, options.length); |
| 20 | const cancelHold = () => { clearTimeout(hold.current); hold.current = null; }; |
| 21 | useEffect(() => { |
| 22 | if (!visible) return; |
| 23 | const outside = event => { if (!root.current.contains(event.target) && !list.current?.contains(event.target)) setOpen(false); }; |
| 24 | document.addEventListener("click", outside, true); |
| 25 | return () => document.removeEventListener("click", outside, true); |
| 26 | }, [visible]); |
| 27 | useEffect(() => () => clearTimeout(hold.current), []); |
| 28 | const choose = item => { |
| 29 | setOpen(false); |
| 30 | trigger.current.focus(); |
| 31 | onChange(item.value); |
| 32 | }; |
| 33 | const requestDisconnect = item => { |
| 34 | if (!onLongPress) return; |
| 35 | held.current = true; |
| 36 | setOpen(false); |
| 37 | onLongPress(item.value); |
| 38 | }; |
| 39 | const press = (event, item) => { |
| 40 | cancelHold(); |
| 41 | held.current = false; |
| 42 | origin.current = { x: event.clientX, y: event.clientY }; |
| 43 | if (onLongPress && event.button === 0) hold.current = setTimeout(() => requestDisconnect(item), 550); |
| 44 | }; |
| 45 | const keys = event => { |
| 46 | if (event.nativeEvent.isComposing) return; |
| 47 | const index = Math.max(0, options.findIndex(item => item.value === cursor)); |
| 48 | if (event.key === "Escape") {event.preventDefault(); setOpen(false);} |
| 49 | else if (event.key === "Tab") setOpen(false); |
| 50 | else if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) { |
| 51 | event.preventDefault(); |
| 52 | const next = event.key === "Home" ? 0 : event.key === "End" ? options.length - 1 |
| 53 | : Math.max(0, Math.min(options.length - 1, index + (visible ? (event.key === "ArrowUp" ? -1 : 1) : 0))); |
| 54 | setCursor(options[next]?.value); setOpen(true); |
| 55 | } else if (event.key === "Enter" || event.key === " ") { |
| 56 | event.preventDefault(); |
| 57 | if (visible) {const item = options.find(item => item.value === cursor); if (item) choose(item);} |
| 58 | else {setCursor(value); setOpen(true);} |
| 59 | } else if (onLongPress && event.key === "F10" && event.shiftKey) { |
| 60 | event.preventDefault(); |
| 61 | const item = options.find(item => item.value === (visible ? cursor : value)); |
| 62 | if (item) requestDisconnect(item); |
| 63 | } |
| 64 | }; |
| 65 | const pointerHandlers = item => ({ |
| 66 | onPointerDown: event => press(event, item), |
| 67 | onPointerUp: cancelHold, |
| 68 | onPointerCancel: cancelHold, |
| 69 | onPointerLeave: cancelHold, |
| 70 | onPointerMove: event => { |
| 71 | if (origin.current && Math.hypot(event.clientX - origin.current.x, event.clientY - origin.current.y) > 10) cancelHold(); |
| 72 | }, |
| 73 | onContextMenu: event => { |
| 74 | if (onLongPress) {event.preventDefault(); cancelHold(); if (!held.current) requestDisconnect(item);} |
| 75 | }, |
| 76 | }); |
| 77 | return <div className="inline-select" ref={root}> |
| 78 | <button type="button" className="inline-select-trigger" ref={trigger} role="combobox" aria-label={label} |
| 79 | aria-haspopup="listbox" aria-expanded={visible} aria-controls={visible ? id : undefined} |
| 80 | aria-activedescendant={visible ? id + "-" + options.findIndex(item => item.value === cursor) : undefined} |
| 81 | aria-describedby={footer ? id + "-hint" : undefined} disabled={disabled} onKeyDown={keys} |
| 82 | {...(selected ? pointerHandlers(selected) : {})} |
| 83 | onClick={() => {if (held.current) {held.current = false; return;} setCursor(value); setOpen(!visible);}}> |
| 84 | {icon}<span>{selected?.label || label}</span><ChevronDown size={14} aria-hidden="true" /> |
| 85 | </button> |
| 86 | {visible && createPortal(<div className="inline-select-popover" ref={list} style={position}> |
| 87 | <div className="inline-select-options" role="listbox" id={id} aria-label={label + "选项"}> |
| 88 | {options.map((item, index) => <div key={item.value} role="option" id={id + "-" + index} |
| 89 | aria-selected={item.value === value} data-active={item.value === cursor || undefined} |
| 90 | {...pointerHandlers(item)} onMouseDown={event => event.preventDefault()} |
| 91 | onClick={() => {if (held.current) {held.current = false; return;} choose(item);}}> |
| 92 | <span>{item.optionLabel || item.label}</span>{item.value === value && <Check size={15} aria-hidden="true" />} |
| 93 | </div>)} |
| 94 | </div> |
| 95 | {footer && <p className="inline-select-hint" id={id + "-hint"}>{footer}</p>} |
| 96 | </div>, document.body)} |
| 97 | </div>; |
| 98 | } |