@caqophony/ui 0.6.5 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1119,6 +1119,22 @@ declare const RadioGroup: React.ForwardRefExoticComponent<RadioGroupProps & Reac
1119
1119
  * text-muted-fg -> colors.mutedFg (placeholder)
1120
1120
  * bg-primary -> colors.primary (selected tag)
1121
1121
  *
1122
+ * Search on the server (#6524): with `onSearchChange`, the typed text is sent
1123
+ * to the caller (debounced by `searchDelayMs`) and the options are shown as
1124
+ * given, never filtered here, so a picker can reach records past a list's first
1125
+ * page. A picked option's label is kept, so the field still names it after the
1126
+ * caller replaces `options` with another search's results.
1127
+ *
1128
+ * In a form (#6524, kit 0.8.0): with `name`, the picked value is carried by a
1129
+ * hidden native `<select>` (`multiple` in multi mode), so `new FormData(form)`
1130
+ * reads it exactly as a native select's: one entry, `""` when cleared, in
1131
+ * single mode; one entry per value, none when empty, in multi mode. A pick or a
1132
+ * clear fires a bubbling `change` on that select, so the form's `onChange` sees
1133
+ * it as an edit; the start value never does. Without `value` the picker is
1134
+ * uncontrolled: it starts at `defaultValue` and returns to it on the form's
1135
+ * reset. `defaultLabel` names a start value that is not among `options` (a
1136
+ * record past a list's first page, named from its own detail read).
1137
+ *
1122
1138
  * Issue #2633 — B-7: Advanced Form Family
1123
1139
  * Authored by Caqophony
1124
1140
  */
@@ -1127,13 +1143,36 @@ interface ComboboxOption {
1127
1143
  value: string;
1128
1144
  label: string;
1129
1145
  }
1130
- interface ComboboxProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
1146
+ interface ComboboxProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> {
1131
1147
  options: ComboboxOption[];
1148
+ /** The picked value, controlled. Without it the picker keeps its own (`defaultValue`). */
1132
1149
  value?: string | string[];
1133
1150
  onChange?: (value: string | string[]) => void;
1151
+ /**
1152
+ * The form field name: the value is submitted under it, as a native select's
1153
+ * (one entry, `""` when cleared; in multi mode one per value, none when empty).
1154
+ */
1155
+ name?: string;
1156
+ /** The uncontrolled start value, used when `value` is not given. */
1157
+ defaultValue?: string | string[];
1158
+ /**
1159
+ * The label of a start value that is not among `options`: one label in
1160
+ * single mode, labels by value in multi mode.
1161
+ */
1162
+ defaultLabel?: string | Readonly<Record<string, string>>;
1134
1163
  placeholder?: string;
1135
1164
  multi?: boolean;
1165
+ /** Filter `options` by the typed text here. Ignored with `onSearchChange`. */
1136
1166
  filterable?: boolean;
1167
+ /**
1168
+ * The typed text, sent to the caller once typing pauses for `searchDelayMs`.
1169
+ * When set, the search input always shows and `options` are drawn as given:
1170
+ * the caller searches (e.g. a list read with a search input) and passes the
1171
+ * results back.
1172
+ */
1173
+ onSearchChange?: (text: string) => void;
1174
+ /** How long typing pauses before `onSearchChange` is called (ms). */
1175
+ searchDelayMs?: number;
1137
1176
  }
1138
1177
  declare const Combobox: React.ForwardRefExoticComponent<ComboboxProps & React.RefAttributes<HTMLDivElement>>;
1139
1178
 
package/dist/index.js CHANGED
@@ -3329,38 +3329,96 @@ RadioGroup.displayName = "RadioGroup";
3329
3329
 
3330
3330
  // src/combobox.tsx
3331
3331
  import * as React23 from "react";
3332
- import { jsx as jsx34, jsxs as jsxs27 } from "react/jsx-runtime";
3332
+ import { Fragment as Fragment7, jsx as jsx34, jsxs as jsxs27 } from "react/jsx-runtime";
3333
+ var SEARCH_DELAY_MS = 250;
3333
3334
  var Combobox = React23.forwardRef(
3334
3335
  ({
3335
3336
  className,
3336
3337
  options,
3337
3338
  value,
3338
3339
  onChange,
3340
+ name,
3341
+ defaultValue,
3342
+ defaultLabel,
3339
3343
  placeholder = "Select...",
3340
3344
  multi = false,
3341
3345
  filterable = true,
3346
+ onSearchChange,
3347
+ searchDelayMs = SEARCH_DELAY_MS,
3342
3348
  ...props
3343
3349
  }, ref) => {
3344
3350
  const [open, setOpen] = React23.useState(false);
3345
3351
  const [search, setSearch] = React23.useState("");
3346
- const selected = multi ? Array.isArray(value) ? value : [] : typeof value === "string" ? value : "";
3347
- const filtered = filterable ? options.filter(
3352
+ const searchesOnServer = onSearchChange !== void 0;
3353
+ const showsSearch = searchesOnServer || filterable;
3354
+ const onSearchRef = React23.useRef(onSearchChange);
3355
+ onSearchRef.current = onSearchChange;
3356
+ const sentRef = React23.useRef("");
3357
+ React23.useEffect(() => {
3358
+ if (!searchesOnServer || search === sentRef.current) return;
3359
+ const timer = setTimeout(() => {
3360
+ sentRef.current = search;
3361
+ onSearchRef.current?.(search);
3362
+ }, searchDelayMs);
3363
+ return () => clearTimeout(timer);
3364
+ }, [search, searchesOnServer, searchDelayMs]);
3365
+ const controlled = value !== void 0;
3366
+ const [own, setOwn] = React23.useState(defaultValue);
3367
+ const current = controlled ? value : own;
3368
+ const interactive = !controlled || onChange !== void 0;
3369
+ const start = React23.useRef(controlled ? value : defaultValue);
3370
+ const pickedLabels = React23.useRef(/* @__PURE__ */ new Map());
3371
+ const startLabelOf = (v) => {
3372
+ if (defaultLabel === void 0) return void 0;
3373
+ if (typeof defaultLabel === "string")
3374
+ return v === start.current ? defaultLabel : void 0;
3375
+ return Object.prototype.hasOwnProperty.call(defaultLabel, v) ? defaultLabel[v] : void 0;
3376
+ };
3377
+ const labelOf = (v) => options.find((o) => o.value === v)?.label ?? pickedLabels.current.get(v) ?? startLabelOf(v);
3378
+ const selected = multi ? Array.isArray(current) ? current : [] : typeof current === "string" ? current : "";
3379
+ const picks = Array.isArray(selected) ? selected : [];
3380
+ const pick = typeof selected === "string" ? selected : "";
3381
+ const formControl = React23.useRef(null);
3382
+ const announce = React23.useRef(false);
3383
+ const selectedKey = JSON.stringify(selected);
3384
+ React23.useEffect(() => {
3385
+ if (!announce.current) return;
3386
+ announce.current = false;
3387
+ formControl.current?.dispatchEvent(
3388
+ new Event("change", { bubbles: true })
3389
+ );
3390
+ }, [selectedKey]);
3391
+ React23.useEffect(() => {
3392
+ const form = formControl.current?.form;
3393
+ if (controlled || !form) return;
3394
+ const onReset = () => setOwn(start.current);
3395
+ form.addEventListener("reset", onReset);
3396
+ return () => form.removeEventListener("reset", onReset);
3397
+ }, [controlled]);
3398
+ const commit = (next) => {
3399
+ announce.current = name !== void 0 && JSON.stringify(next) !== selectedKey;
3400
+ if (!controlled) setOwn(next);
3401
+ onChange?.(next);
3402
+ };
3403
+ const filtered = filterable && !searchesOnServer ? options.filter(
3348
3404
  (o) => o.label.toLowerCase().includes(search.toLowerCase())
3349
3405
  ) : options;
3350
3406
  const handleSelect = (optValue) => {
3351
- if (!onChange) return;
3407
+ if (!interactive) return;
3408
+ const picked = options.find((o) => o.value === optValue);
3409
+ if (picked) pickedLabels.current.set(picked.value, picked.label);
3352
3410
  if (multi) {
3353
3411
  const arr = Array.isArray(selected) ? selected : [];
3354
- onChange(
3412
+ commit(
3355
3413
  arr.includes(optValue) ? arr.filter((v) => v !== optValue) : [...arr, optValue]
3356
3414
  );
3357
3415
  } else {
3358
- onChange(optValue);
3416
+ commit(optValue);
3359
3417
  setOpen(false);
3360
3418
  setSearch("");
3361
3419
  }
3362
3420
  };
3363
- const displayValue = multi ? null : options.find((o) => o.value === selected)?.label;
3421
+ const displayValue = multi || typeof selected !== "string" ? null : labelOf(selected);
3364
3422
  return /* @__PURE__ */ jsxs27("div", { ref, className: cn("relative", className), ...props, children: [
3365
3423
  /* @__PURE__ */ jsxs27(
3366
3424
  "div",
@@ -3373,11 +3431,12 @@ var Combobox = React23.forwardRef(
3373
3431
  {
3374
3432
  className: "text-primary-text inline-flex items-center gap-1 rounded bg-primary/10 px-2 py-0.5 text-xs font-medium",
3375
3433
  children: [
3376
- options.find((o) => o.value === v)?.label,
3434
+ labelOf(v),
3377
3435
  /* @__PURE__ */ jsx34(
3378
3436
  "button",
3379
3437
  {
3380
3438
  type: "button",
3439
+ "aria-label": `Remove ${labelOf(v) ?? v}`,
3381
3440
  onClick: (e) => {
3382
3441
  e.stopPropagation();
3383
3442
  handleSelect(v);
@@ -3399,12 +3458,51 @@ var Combobox = React23.forwardRef(
3399
3458
  children: displayValue || placeholder
3400
3459
  }
3401
3460
  ),
3402
- /* @__PURE__ */ jsx34("span", { className: "ml-auto text-muted-foreground", children: "\u25BE" })
3461
+ !multi && interactive && selected !== "" && /* @__PURE__ */ jsx34(
3462
+ "button",
3463
+ {
3464
+ type: "button",
3465
+ "aria-label": "Clear",
3466
+ className: "ml-auto text-xs text-muted-foreground hover:text-foreground",
3467
+ onClick: (e) => {
3468
+ e.stopPropagation();
3469
+ commit("");
3470
+ },
3471
+ children: "\u2715"
3472
+ }
3473
+ ),
3474
+ /* @__PURE__ */ jsx34(
3475
+ "span",
3476
+ {
3477
+ className: cn(
3478
+ "text-muted-foreground",
3479
+ (multi || !interactive || selected === "") && "ml-auto"
3480
+ ),
3481
+ children: "\u25BE"
3482
+ }
3483
+ )
3403
3484
  ]
3404
3485
  }
3405
3486
  ),
3487
+ name !== void 0 && /* @__PURE__ */ jsx34(
3488
+ "select",
3489
+ {
3490
+ ref: formControl,
3491
+ name,
3492
+ multiple: multi,
3493
+ value: selected,
3494
+ onChange: () => void 0,
3495
+ hidden: true,
3496
+ "aria-hidden": true,
3497
+ tabIndex: -1,
3498
+ children: multi ? picks.map((v) => /* @__PURE__ */ jsx34("option", { value: v, children: v }, v)) : /* @__PURE__ */ jsxs27(Fragment7, { children: [
3499
+ /* @__PURE__ */ jsx34("option", { value: "" }),
3500
+ pick !== "" && /* @__PURE__ */ jsx34("option", { value: pick, children: pick }, pick)
3501
+ ] })
3502
+ }
3503
+ ),
3406
3504
  open && /* @__PURE__ */ jsxs27("div", { className: "z-dropdown absolute mt-1 w-full rounded-md border border-border bg-card shadow-lg", children: [
3407
- filterable && /* @__PURE__ */ jsx34("div", { className: "border-b border-border p-2", children: /* @__PURE__ */ jsx34(
3505
+ showsSearch && /* @__PURE__ */ jsx34("div", { className: "border-b border-border p-2", children: /* @__PURE__ */ jsx34(
3408
3506
  "input",
3409
3507
  {
3410
3508
  type: "text",
@@ -3412,6 +3510,9 @@ var Combobox = React23.forwardRef(
3412
3510
  placeholder: "Search...",
3413
3511
  value: search,
3414
3512
  onChange: (e) => setSearch(e.target.value),
3513
+ onKeyDown: (e) => {
3514
+ if (e.key === "Enter") e.preventDefault();
3515
+ },
3415
3516
  autoFocus: true
3416
3517
  }
3417
3518
  ) }),
@@ -4335,7 +4436,7 @@ function EmptyState({
4335
4436
 
4336
4437
  // src/action-panel.tsx
4337
4438
  import * as React34 from "react";
4338
- import { Fragment as Fragment7, jsx as jsx48, jsxs as jsxs38 } from "react/jsx-runtime";
4439
+ import { Fragment as Fragment8, jsx as jsx48, jsxs as jsxs38 } from "react/jsx-runtime";
4339
4440
  var BORDER_INTENT = {
4340
4441
  default: "border-l-primary",
4341
4442
  warning: "border-l-intent-warning",
@@ -4361,13 +4462,13 @@ var ActionPanel = React34.forwardRef(
4361
4462
  className
4362
4463
  ),
4363
4464
  ...props,
4364
- children: variant === "split" ? /* @__PURE__ */ jsxs38(Fragment7, { children: [
4465
+ children: variant === "split" ? /* @__PURE__ */ jsxs38(Fragment8, { children: [
4365
4466
  /* @__PURE__ */ jsxs38("div", { className: "min-w-0", children: [
4366
4467
  /* @__PURE__ */ jsx48("h3", { className: "font-semibold text-foreground", children: title }),
4367
4468
  description && /* @__PURE__ */ jsx48("p", { className: "mt-1 text-sm text-muted-foreground", children: description })
4368
4469
  ] }),
4369
4470
  action && /* @__PURE__ */ jsx48("div", { className: "flex-shrink-0", children: action })
4370
- ] }) : /* @__PURE__ */ jsxs38(Fragment7, { children: [
4471
+ ] }) : /* @__PURE__ */ jsxs38(Fragment8, { children: [
4371
4472
  /* @__PURE__ */ jsx48("h3", { className: "font-semibold text-foreground", children: title }),
4372
4473
  description && /* @__PURE__ */ jsx48("p", { className: "mt-1 text-sm text-muted-foreground", children: description }),
4373
4474
  action && /* @__PURE__ */ jsx48("div", { className: "mt-4", children: action })
@@ -4712,7 +4813,7 @@ SidePanelLayout.displayName = "SidePanelLayout";
4712
4813
 
4713
4814
  // src/overflow-menu.tsx
4714
4815
  import * as React39 from "react";
4715
- import { Fragment as Fragment8, jsx as jsx53, jsxs as jsxs42 } from "react/jsx-runtime";
4816
+ import { Fragment as Fragment9, jsx as jsx53, jsxs as jsxs42 } from "react/jsx-runtime";
4716
4817
  function OverflowMenu({
4717
4818
  label,
4718
4819
  items,
@@ -4743,7 +4844,7 @@ function OverflowMenu({
4743
4844
  children: /* @__PURE__ */ jsx53(Icon, { name: "ellipsis", size: "sm" })
4744
4845
  }
4745
4846
  ),
4746
- open && /* @__PURE__ */ jsxs42(Fragment8, { children: [
4847
+ open && /* @__PURE__ */ jsxs42(Fragment9, { children: [
4747
4848
  /* @__PURE__ */ jsx53(
4748
4849
  "div",
4749
4850
  {
@@ -5230,10 +5331,10 @@ var Progress = React45.forwardRef(
5230
5331
  Progress.displayName = "Progress";
5231
5332
 
5232
5333
  // src/hooks/use-debounce.ts
5233
- import { useEffect as useEffect7, useState as useState11 } from "react";
5334
+ import { useEffect as useEffect8, useState as useState11 } from "react";
5234
5335
  function useDebounce(value, ms) {
5235
5336
  const [debounced, setDebounced] = useState11(value);
5236
- useEffect7(() => {
5337
+ useEffect8(() => {
5237
5338
  const handle = setTimeout(() => setDebounced(value), ms);
5238
5339
  return () => clearTimeout(handle);
5239
5340
  }, [value, ms]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caqophony/ui",
3
- "version": "0.6.5",
3
+ "version": "0.8.0",
4
4
  "description": "Generic, optional React UI reuse library (Gossamer tokens). Centrally maintained by Caqophony.",
5
5
  "license": "MIT",
6
6
  "author": "Caqophony",