@tristan2828/ui-foundation 4.87.0 → 4.88.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.
Files changed (31) hide show
  1. package/README.md +1 -1
  2. package/conventions/docs/add-an-entity.md +38 -23
  3. package/conventions/docs/cell-patterns.md +49 -24
  4. package/dist/components/app/choice-buttons.d.ts +25 -0
  5. package/dist/components/app/choice-buttons.js +17 -0
  6. package/dist/components/app/date-field.d.ts +28 -9
  7. package/dist/components/app/date-field.js +103 -10
  8. package/dist/components/app/edit-in-place-controls.d.ts +1 -3
  9. package/dist/components/app/edit-in-place-controls.js +4 -58
  10. package/dist/components/app/edit-in-place-presets.d.ts +38 -4
  11. package/dist/components/app/edit-in-place-presets.js +48 -27
  12. package/dist/components/app/edit-row.d.ts +7 -0
  13. package/dist/components/app/edit-row.js +23 -0
  14. package/dist/components/app/editable-value.d.ts +8 -1
  15. package/dist/components/app/editable-value.js +36 -4
  16. package/dist/components/app/entity-view.js +5 -1
  17. package/dist/components/app/multi-pick.d.ts +8 -1
  18. package/dist/components/app/multi-pick.js +19 -5
  19. package/dist/components/app/multi-reference.d.ts +8 -1
  20. package/dist/components/app/multi-reference.js +44 -6
  21. package/dist/components/app/reference-combobox.d.ts +37 -0
  22. package/dist/components/app/reference-combobox.js +96 -0
  23. package/dist/components/ui/field.d.ts +2 -2
  24. package/dist/components/ui/field.js +2 -1
  25. package/dist/index.d.ts +4 -1
  26. package/dist/index.js +4 -1
  27. package/dist/lib/names-record.d.ts +4 -0
  28. package/dist/lib/names-record.js +7 -0
  29. package/dist/lib/plain-date.d.ts +20 -0
  30. package/dist/lib/plain-date.js +72 -0
  31. package/package.json +1 -1
@@ -0,0 +1,96 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // Another record, picked from a list searched as you type: the one control
3
+ // for a single reference, on a form and edited in place (`inPlace.reference`
4
+ // draws it). The package knows no endpoint: `search` is the app's (its
5
+ // gateway's search by name), called with what's typed (`''` when the
6
+ // list opens). Names come from the searches and from the saved record's
7
+ // `label`, so a pick the current search no longer returns still shows its
8
+ // name.
9
+ //
10
+ // `onCreate` (4.88, the form standard's F14): when what's typed matches no
11
+ // record's name, Create "<typed>" is the list's last item. Picking it
12
+ // calls the app's `onCreate(name)`, which opens its small create form (a
13
+ // FormDialog) with the name filled in and resolves with the new record
14
+ // once it's saved: that record is then picked. While that form is open,
15
+ // an edit in place holding this control stays open (`data-edit-hold`).
16
+ import { useEffect, useRef, useState } from 'react';
17
+ import { namesRecord } from '../../lib/names-record.js';
18
+ import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from '../ui/combobox.js';
19
+ /** A select's and a combobox's items are strings: a record's id, or this for "Not set". */
20
+ const NONE = '__none__';
21
+ /** The Create "<typed>" item. */
22
+ const CREATE = '__create__';
23
+ // How long typing pauses before the search runs.
24
+ const SEARCH_DELAY_MS = 200;
25
+ export function ReferenceCombobox({ id, value, onValueChange, search, label, required = false, emptyLabel = 'Not set', placeholder = 'Search', emptyText = 'No matches.', onCreate, readOnly = false, defaultOpen = false, invalid = false, ...aria }) {
26
+ const [text, setText] = useState('');
27
+ const [found, setFound] = useState([]);
28
+ const [creating, setCreating] = useState(false);
29
+ const [names] = useState(() => new Map());
30
+ if (value != null && label !== undefined && !names.has(String(value)))
31
+ names.set(String(value), { id: value, label });
32
+ for (const record of found)
33
+ names.set(String(record.id), record);
34
+ // The search is the app's, often a new function each render: the
35
+ // latest is called, and only what's typed starts a search.
36
+ const latestSearch = useRef(search);
37
+ useEffect(() => {
38
+ latestSearch.current = search;
39
+ });
40
+ useEffect(() => {
41
+ let live = true;
42
+ const timer = setTimeout(() => {
43
+ latestSearch.current(text).then((records) => {
44
+ if (live)
45
+ setFound(records);
46
+ }, () => {
47
+ if (live)
48
+ setFound([]);
49
+ });
50
+ }, text === '' ? 0 : SEARCH_DELAY_MS);
51
+ return () => {
52
+ live = false;
53
+ clearTimeout(timer);
54
+ };
55
+ }, [text]);
56
+ const mounted = useRef(true);
57
+ useEffect(() => {
58
+ mounted.current = true;
59
+ return () => {
60
+ mounted.current = false;
61
+ };
62
+ }, []);
63
+ const typed = text.trim();
64
+ const offerCreate = Boolean(onCreate) && typed !== '' && !namesRecord(found, typed);
65
+ const picked = value == null ? null : String(value);
66
+ const items = [...(required ? [] : [NONE]), ...found.map((record) => String(record.id)), ...(offerCreate ? [CREATE] : [])];
67
+ const nameOf = (key) => (key === NONE || key === CREATE ? '' : (names.get(key)?.label ?? ''));
68
+ const create = (name) => {
69
+ if (!onCreate)
70
+ return;
71
+ setCreating(true);
72
+ onCreate(name).then((record) => {
73
+ if (mounted.current)
74
+ setCreating(false);
75
+ if (!record)
76
+ return;
77
+ names.set(String(record.id), record);
78
+ onValueChange(record.id);
79
+ }, () => {
80
+ if (mounted.current)
81
+ setCreating(false);
82
+ });
83
+ };
84
+ return (_jsxs(Combobox, { items: items, value: picked, onValueChange: (key) => {
85
+ if (key == null)
86
+ return;
87
+ if (key === CREATE) {
88
+ create(typed);
89
+ return;
90
+ }
91
+ onValueChange(key === NONE ? null : (names.get(key)?.id ?? null));
92
+ },
93
+ // The box showing the pick's own name is no search: the list then
94
+ // offers everything, not only the one already picked.
95
+ onInputValueChange: (next) => setText(picked !== null && next === nameOf(picked) ? '' : next), itemToStringLabel: nameOf, filter: null, readOnly: readOnly || creating, defaultOpen: defaultOpen, children: [_jsx(ComboboxInput, { id: id, placeholder: placeholder, "aria-invalid": invalid || undefined, "aria-busy": creating || undefined, "data-edit-hold": creating ? '' : undefined, ...aria }), _jsxs(ComboboxContent, { children: [_jsx(ComboboxEmpty, { children: emptyText }), _jsx(ComboboxList, { children: items.map((key) => (_jsx(ComboboxItem, { value: key, "data-create": key === CREATE ? '' : undefined, children: key === NONE ? emptyLabel : key === CREATE ? `Create "${typed}"` : nameOf(key) }, key))) })] })] }));
96
+ }
@@ -21,8 +21,8 @@ declare function Field({ className, orientation, size, solo, kind, ...props }: R
21
21
  solo?: boolean;
22
22
  /**
23
23
  * A date or a number: two of a kind stay side by side on a phone.
24
- * Found by itself for a date or number `<input>`; say it for a
25
- * control of your own (DateField's button).
24
+ * Found by itself for a date or number `<input>` and for DateField
25
+ * (which marks itself, 4.88); say it for a control of your own.
26
26
  */
27
27
  kind?: FieldKind;
28
28
  }): import("react").JSX.Element;
@@ -27,7 +27,8 @@ function FieldLegend({ className, variant = "legend", ...props }) {
27
27
  }
28
28
  // What a Field holds, when it doesn't say (`kind`): a date or a number
29
29
  // box keeps its partner on a phone.
30
- const DATE_CONTROL = "input[type=date]";
30
+ // DateField marks its own box (`data-kind="date"`, 4.88).
31
+ const DATE_CONTROL = "input[type=date], [data-kind=date]";
31
32
  const NUMBER_CONTROL = "input[type=number], input[inputmode=numeric], input[inputmode=decimal]";
32
33
  function kindOf(field) {
33
34
  const said = field.dataset.kind;
package/dist/index.d.ts CHANGED
@@ -18,6 +18,8 @@ export { EntityForm, type EntityFormProps } from './components/app/entity-form.j
18
18
  export { countFormErrors } from './lib/form-errors.js';
19
19
  export { FormSection, type FormSectionProps } from './components/app/form-section.js';
20
20
  export { OptionSelect, type OptionSelectOption, type OptionSelectProps } from './components/app/option-select.js';
21
+ export { ChoiceButtons, type ChoiceButtonsOption, type ChoiceButtonsProps } from './components/app/choice-buttons.js';
22
+ export { ReferenceCombobox, type ReferenceComboboxProps, type ReferenceRecord } from './components/app/reference-combobox.js';
21
23
  export { fieldRows, type FieldKind, type FieldRowItem, type FieldRowPlace, type FieldSize } from './lib/field-rows.js';
22
24
  export { KeyStats, type KeyStat, type KeyStatsProps } from './components/app/key-stats.js';
23
25
  export { IconAction, type IconActionProps } from './components/app/icon-action.js';
@@ -32,6 +34,7 @@ export { inPlace, type InPlaceDateConfig, type InPlaceFieldsOptions, type InPlac
32
34
  export { linkHost, linkHref, linkRule } from './lib/link-href.js';
33
35
  export { DataEnvironmentBanner } from './components/app/data-environment-banner.js';
34
36
  export { EditableValue, type EditableValueProps } from './components/app/editable-value.js';
37
+ export { editRowClass, onEditRowClick, SAVED_FOR_MS } from './components/app/edit-row.js';
35
38
  export { ErrorState } from './components/app/error-state.js';
36
39
  export { InlineCreate, type InlineCreateProps } from './components/app/inline-create.js';
37
40
  export { FoundationProviders } from './components/app/foundation-providers.js';
@@ -76,7 +79,7 @@ export { EmojiField, type EmojiFieldProps } from './components/app/emoji-field.j
76
79
  export { MonthDayField, type MonthDayFieldProps } from './components/app/month-day-field.js';
77
80
  export { ImageField, type ImageFieldProps } from './components/app/image-field.js';
78
81
  export { DEFAULT_IMAGE_TYPES, type ImageUpload, type ImageUploadResult } from './lib/image-file.js';
79
- export { formatPlainDate, type PlainDate } from './lib/plain-date.js';
82
+ export { formatPlainDate, parsePlainDate, plainDateExamples, unreadableDateMessage, type ParsePlainDateOptions, type PlainDate, } from './lib/plain-date.js';
80
83
  export { formatMonthDay, isMonthDay, monthDayProblem, type MonthDay } from './lib/month-day.js';
81
84
  export { ChoiceCell, LinkCell, LinkOut, NameCell, type ChoiceCellProps, type LinkCellProps, type NameCellProps, } from './components/app/row-edit-cells.js';
82
85
  export { ModuleCard, ModuleStrip, type CategorySlot, type ModuleCardLink, type ModuleCardPlace, type ModuleCardProps, type ModuleStripMobile, type ModuleStripProps, } from './components/app/module-card.js';
package/dist/index.js CHANGED
@@ -22,6 +22,8 @@ export { EntityForm } from './components/app/entity-form.js';
22
22
  export { countFormErrors } from './lib/form-errors.js';
23
23
  export { FormSection } from './components/app/form-section.js';
24
24
  export { OptionSelect } from './components/app/option-select.js';
25
+ export { ChoiceButtons } from './components/app/choice-buttons.js';
26
+ export { ReferenceCombobox } from './components/app/reference-combobox.js';
25
27
  export { fieldRows } from './lib/field-rows.js';
26
28
  export { KeyStats } from './components/app/key-stats.js';
27
29
  export { IconAction } from './components/app/icon-action.js';
@@ -36,6 +38,7 @@ export { inPlace, } from './components/app/edit-in-place-presets.js';
36
38
  export { linkHost, linkHref, linkRule } from './lib/link-href.js';
37
39
  export { DataEnvironmentBanner } from './components/app/data-environment-banner.js';
38
40
  export { EditableValue } from './components/app/editable-value.js';
41
+ export { editRowClass, onEditRowClick, SAVED_FOR_MS } from './components/app/edit-row.js';
39
42
  export { ErrorState } from './components/app/error-state.js';
40
43
  export { InlineCreate } from './components/app/inline-create.js';
41
44
  export { FoundationProviders } from './components/app/foundation-providers.js';
@@ -78,7 +81,7 @@ export { EmojiField } from './components/app/emoji-field.js';
78
81
  export { MonthDayField } from './components/app/month-day-field.js';
79
82
  export { ImageField } from './components/app/image-field.js';
80
83
  export { DEFAULT_IMAGE_TYPES } from './lib/image-file.js';
81
- export { formatPlainDate } from './lib/plain-date.js';
84
+ export { formatPlainDate, parsePlainDate, plainDateExamples, unreadableDateMessage, } from './lib/plain-date.js';
82
85
  export { formatMonthDay, isMonthDay, monthDayProblem } from './lib/month-day.js';
83
86
  export { ChoiceCell, LinkCell, LinkOut, NameCell, } from './components/app/row-edit-cells.js';
84
87
  export { ModuleCard, ModuleStrip, } from './components/app/module-card.js';
@@ -0,0 +1,4 @@
1
+ /** Whether `name` is one of the records' names, ignoring case and spaces at the ends. */
2
+ export declare function namesRecord(records: readonly {
3
+ label: string;
4
+ }[], name: string): boolean;
@@ -0,0 +1,7 @@
1
+ // Whether a typed name is already one of the records (a reference picker's
2
+ // Create "<typed>" is offered only when it isn't).
3
+ /** Whether `name` is one of the records' names, ignoring case and spaces at the ends. */
4
+ export function namesRecord(records, name) {
5
+ const wanted = name.trim().toLowerCase();
6
+ return records.some((record) => record.label.trim().toLowerCase() === wanted);
7
+ }
@@ -10,3 +10,23 @@ export declare function formatPlainDate(value: PlainDate, options?: Intl.DateTim
10
10
  export declare function plainDateToLocal(value: PlainDate): Date | undefined;
11
11
  /** A local Date's own calendar day, as `'YYYY-MM-DD'`. */
12
12
  export declare function localToPlainDate(date: Date): PlainDate;
13
+ /** Whether the locale writes the day before the month (3/14 in en-US, 14/3 in en-GB). */
14
+ export declare function dayFirst(locale?: string): boolean;
15
+ /** What `parsePlainDate` is told: today's day (for "today" and a day with no year) and the locale. */
16
+ export type ParsePlainDateOptions = {
17
+ /** Today, `'YYYY-MM-DD'`. Defaults to the browser's own today. */
18
+ today?: PlainDate;
19
+ /** Decides `3/4`'s order: month first in en-US, day first in en-GB. */
20
+ locale?: string;
21
+ };
22
+ /**
23
+ * Reads a day typed by hand: `3/14`, `3/14/26`, `Mar 14`, `March 14 2026`,
24
+ * `14 Mar`, `2026-03-14`, `today`, `yesterday`, `tomorrow`. A day with no
25
+ * year is in today's year. Gives `'YYYY-MM-DD'`, `''` for blank, or
26
+ * `null` for something that isn't a day (`Feb 30`, `soon`).
27
+ */
28
+ export declare function parsePlainDate(text: string, options?: ParsePlainDateOptions): PlainDate | null;
29
+ /** Two days typed the way the locale writes them, for an empty box: "3/14 or Mar 14" (en-US), "14/3 or 14 Mar" (en-GB). */
30
+ export declare function plainDateExamples(locale?: string): string;
31
+ /** What a typed day says when it can't be read: "Try a date like 3/14 or Mar 14". */
32
+ export declare function unreadableDateMessage(locale?: string): string;
@@ -35,3 +35,75 @@ export function localToPlainDate(date) {
35
35
  const pad = (n) => String(n).padStart(2, '0');
36
36
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
37
37
  }
38
+ const MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
39
+ /** Whether the locale writes the day before the month (3/14 in en-US, 14/3 in en-GB). */
40
+ export function dayFirst(locale) {
41
+ const order = new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'numeric', timeZone: 'UTC' })
42
+ .formatToParts(Date.UTC(2026, 2, 14))
43
+ .filter((part) => part.type === 'day' || part.type === 'month');
44
+ return order[0]?.type === 'day';
45
+ }
46
+ function dayOf(year, month, day) {
47
+ const pad = (n) => String(n).padStart(2, '0');
48
+ const value = `${String(year).padStart(4, '0')}-${pad(month)}-${pad(day)}`;
49
+ return parts(value) ? value : null;
50
+ }
51
+ const fullYear = (typed, fallback) => {
52
+ if (typed === undefined)
53
+ return fallback;
54
+ return typed.length <= 2 ? 2000 + Number(typed) : Number(typed);
55
+ };
56
+ /**
57
+ * Reads a day typed by hand: `3/14`, `3/14/26`, `Mar 14`, `March 14 2026`,
58
+ * `14 Mar`, `2026-03-14`, `today`, `yesterday`, `tomorrow`. A day with no
59
+ * year is in today's year. Gives `'YYYY-MM-DD'`, `''` for blank, or
60
+ * `null` for something that isn't a day (`Feb 30`, `soon`).
61
+ */
62
+ export function parsePlainDate(text, options = {}) {
63
+ const typed = text.trim().toLowerCase().replace(/\s+/g, ' ');
64
+ if (typed === '')
65
+ return '';
66
+ const today = options.today && parts(options.today) ? options.today : localToPlainDate(new Date());
67
+ const [year, month, day] = parts(today);
68
+ const shift = (days) => localToPlainDate(new Date(year, month - 1, day + days));
69
+ if (typed === 'today')
70
+ return today;
71
+ if (typed === 'yesterday')
72
+ return shift(-1);
73
+ if (typed === 'tomorrow')
74
+ return shift(1);
75
+ let match = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(typed);
76
+ if (match)
77
+ return dayOf(Number(match[1]), Number(match[2]), Number(match[3]));
78
+ match = /^(\d{1,2})[/.-](\d{1,2})(?:[/.-](\d{2}|\d{4}))?$/.exec(typed);
79
+ if (match) {
80
+ const [a, b] = [Number(match[1]), Number(match[2])];
81
+ const [m, d] = dayFirst(options.locale) ? [b, a] : [a, b];
82
+ return dayOf(fullYear(match[3], year), m, d);
83
+ }
84
+ const monthOf = (word) => {
85
+ const at = MONTHS.indexOf(word.slice(0, 3));
86
+ return at !== -1 && word.length >= 3 ? at + 1 : null;
87
+ };
88
+ // Mar 14, March 14 2026, Mar 14, 2026
89
+ match = /^([a-z]+)\.? (\d{1,2})(?:st|nd|rd|th)?(?:,? (\d{4}))?$/.exec(typed);
90
+ if (match) {
91
+ const m = monthOf(match[1]);
92
+ return m ? dayOf(fullYear(match[3], year), m, Number(match[2])) : null;
93
+ }
94
+ // 14 Mar, 14 March 2026
95
+ match = /^(\d{1,2})(?:st|nd|rd|th)? ([a-z]+)\.?(?:,? (\d{4}))?$/.exec(typed);
96
+ if (match) {
97
+ const m = monthOf(match[2]);
98
+ return m ? dayOf(fullYear(match[3], year), m, Number(match[1])) : null;
99
+ }
100
+ return null;
101
+ }
102
+ /** Two days typed the way the locale writes them, for an empty box: "3/14 or Mar 14" (en-US), "14/3 or 14 Mar" (en-GB). */
103
+ export function plainDateExamples(locale) {
104
+ return dayFirst(locale) ? '14/3 or 14 Mar' : '3/14 or Mar 14';
105
+ }
106
+ /** What a typed day says when it can't be read: "Try a date like 3/14 or Mar 14". */
107
+ export function unreadableDateMessage(locale) {
108
+ return `Try a date like ${plainDateExamples(locale)}`;
109
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tristan2828/ui-foundation",
3
- "version": "4.87.0",
3
+ "version": "4.88.0",
4
4
  "description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
5
5
  "license": "MIT",
6
6
  "repository": {