Buttons, fields, drop-downs, check boxes, tabs, dialogs and notices are Ant Design components; their tokens come from the theme variables (web/src/ui). Every factory board column has its own vertical scrollbar. ChatView rendered fewer hooks once its chat was deleted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
78 lines
2.9 KiB
TypeScript
78 lines
2.9 KiB
TypeScript
// A drop-down with the interface of a native <select> (value, onChange with `target.value`, <option> and
|
|
// <optgroup> children) drawn by Ant Design.
|
|
import { Select as AntSelect } from "antd";
|
|
import { Children, Fragment, isValidElement, useState, type CSSProperties, type ReactNode } from "react";
|
|
|
|
export interface SelectChange {
|
|
target: { value: string };
|
|
currentTarget: { value: string };
|
|
}
|
|
|
|
interface Props {
|
|
value?: string | number;
|
|
defaultValue?: string | number;
|
|
onChange?: (e: SelectChange) => void;
|
|
children?: ReactNode;
|
|
className?: string;
|
|
style?: CSSProperties;
|
|
title?: string;
|
|
id?: string;
|
|
disabled?: boolean;
|
|
autoFocus?: boolean;
|
|
required?: boolean;
|
|
size?: "small" | "middle" | "large";
|
|
"aria-label"?: string;
|
|
onClick?: (e: React.MouseEvent) => void;
|
|
}
|
|
|
|
type Option = { value: string; label: ReactNode; text: string; disabled?: boolean; title?: string };
|
|
type Entry = Option | { label: ReactNode; title?: string; options: Option[] };
|
|
|
|
const textOf = (node: ReactNode): string => {
|
|
if (typeof node === "string" || typeof node === "number") return String(node);
|
|
if (Array.isArray(node)) return node.map(textOf).join("");
|
|
return isValidElement<{ children?: ReactNode }>(node) ? textOf(node.props.children) : "";
|
|
};
|
|
|
|
function entries(children: ReactNode): Entry[] {
|
|
const out: Entry[] = [];
|
|
Children.forEach(children, (child) => {
|
|
if (!isValidElement<any>(child)) return;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const p = child.props as any;
|
|
if (child.type === Fragment) out.push(...entries(p.children));
|
|
else if (child.type === "optgroup") out.push({ label: p.label, options: entries(p.children).flatMap((e) => ("options" in e ? e.options : [e])) });
|
|
else if (child.type === "option") {
|
|
const text = textOf(p.children);
|
|
out.push({ value: String(p.value ?? text), label: p.children, text, disabled: p.disabled, title: p.title ?? text });
|
|
}
|
|
});
|
|
return out;
|
|
}
|
|
|
|
const SEARCH_FROM = 12;
|
|
|
|
export function Select({ value, defaultValue, onChange, children, className, style, size, ...rest }: Props) {
|
|
const options = entries(children);
|
|
const flat = options.flatMap((e) => ("options" in e ? e.options : [e]));
|
|
// Like a native select, an uncontrolled one starts with its first option.
|
|
const [own, setOwn] = useState(() => String(defaultValue ?? flat[0]?.value ?? ""));
|
|
const current = value === undefined ? own : String(value);
|
|
return (
|
|
<AntSelect<string, Entry>
|
|
{...rest}
|
|
size={size}
|
|
className={className}
|
|
style={style}
|
|
value={flat.some((o) => o.value === current) ? current : undefined}
|
|
options={options}
|
|
popupMatchSelectWidth={false}
|
|
showSearch={flat.length >= SEARCH_FROM ? { filterOption: (q, o) => String((o as Option | undefined)?.text ?? "").toLowerCase().includes(q.toLowerCase()) } : false}
|
|
onChange={(v) => {
|
|
setOwn(v);
|
|
onChange?.({ target: { value: v }, currentTarget: { value: v } });
|
|
}}
|
|
/>
|
|
);
|
|
}
|