@tristan2828/ui-foundation 4.70.0 → 4.72.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/conventions/docs/add-an-entity.md +107 -21
- package/conventions/docs/cell-patterns.md +210 -37
- package/conventions/docs/design-language.md +52 -24
- package/dist/components/app/edit-in-place-controls.d.ts +34 -0
- package/dist/components/app/edit-in-place-controls.js +92 -0
- package/dist/components/app/edit-in-place-presets.d.ts +187 -0
- package/dist/components/app/edit-in-place-presets.js +409 -0
- package/dist/components/app/edit-in-place-store.d.ts +9 -0
- package/dist/components/app/edit-in-place-store.js +14 -7
- package/dist/components/app/edit-in-place.d.ts +101 -4
- package/dist/components/app/editable-list.js +158 -23
- package/dist/components/app/editable-value.d.ts +2 -2
- package/dist/components/app/editable-value.js +7 -3
- package/dist/components/app/entity-view.d.ts +6 -2
- package/dist/components/app/entity-view.js +11 -2
- package/dist/components/app/record-dialog.d.ts +10 -1
- package/dist/components/app/record-dialog.js +32 -4
- package/dist/components/app/row-edit-cells.d.ts +9 -0
- package/dist/components/app/row-edit-cells.js +12 -16
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/lib/link-href.d.ts +28 -0
- package/dist/lib/link-href.js +40 -0
- package/package.json +1 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// The controls behind the `inPlace` presets that hold state or lay out
|
|
3
|
+
// several parts (edit-in-place-presets.tsx), kept in a file of their own
|
|
4
|
+
// so the presets' file exports no components.
|
|
5
|
+
import { useEffect, useRef, useState } from 'react';
|
|
6
|
+
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from '../ui/combobox.js';
|
|
7
|
+
import { FieldError } from '../ui/field.js';
|
|
8
|
+
/** A select's and a combobox's items are strings: the option's position, or this for "Not set". */
|
|
9
|
+
export const NONE = 'none';
|
|
10
|
+
function controlAria(props) {
|
|
11
|
+
return {
|
|
12
|
+
'aria-label': props.label,
|
|
13
|
+
'aria-describedby': props.describedBy,
|
|
14
|
+
'aria-invalid': props.invalid || undefined,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
// How long typing pauses before the search runs.
|
|
18
|
+
const SEARCH_DELAY_MS = 200;
|
|
19
|
+
// The reference's combobox: what's typed searches (through the app's
|
|
20
|
+
// `search`), a pick is the record's id. Names come from the searches and
|
|
21
|
+
// from the saved record's `label`, so a pick the current search no longer
|
|
22
|
+
// returns still shows its name.
|
|
23
|
+
export function ReferencePicker({ props, alone, config }) {
|
|
24
|
+
const [text, setText] = useState('');
|
|
25
|
+
const [found, setFound] = useState([]);
|
|
26
|
+
const [names] = useState(() => new Map());
|
|
27
|
+
if (props.value != null && config.label !== undefined && !names.has(String(props.value)))
|
|
28
|
+
names.set(String(props.value), { id: props.value, label: config.label });
|
|
29
|
+
for (const record of found)
|
|
30
|
+
names.set(String(record.id), record);
|
|
31
|
+
// The search is the app's, often a new function each render: the
|
|
32
|
+
// latest is called, and only what's typed starts a search.
|
|
33
|
+
const search = useRef(config.search);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
search.current = config.search;
|
|
36
|
+
});
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
let live = true;
|
|
39
|
+
const timer = setTimeout(() => {
|
|
40
|
+
search.current(text).then((records) => {
|
|
41
|
+
if (live)
|
|
42
|
+
setFound(records);
|
|
43
|
+
}, () => {
|
|
44
|
+
if (live)
|
|
45
|
+
setFound([]);
|
|
46
|
+
});
|
|
47
|
+
}, text === '' ? 0 : SEARCH_DELAY_MS);
|
|
48
|
+
return () => {
|
|
49
|
+
live = false;
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
};
|
|
52
|
+
}, [text]);
|
|
53
|
+
const picked = props.value == null ? null : String(props.value);
|
|
54
|
+
const items = [...(config.required ? [] : [NONE]), ...found.map((record) => String(record.id))];
|
|
55
|
+
const emptyLabel = config.emptyLabel ?? 'Not set';
|
|
56
|
+
const nameOf = (key) => (key === NONE ? '' : (names.get(key)?.label ?? ''));
|
|
57
|
+
return (_jsxs(Combobox, { items: items, value: picked, onValueChange: (key) => {
|
|
58
|
+
if (key == null)
|
|
59
|
+
return;
|
|
60
|
+
const id = key === NONE ? null : (names.get(key)?.id ?? null);
|
|
61
|
+
if (alone)
|
|
62
|
+
props.commit(id);
|
|
63
|
+
else
|
|
64
|
+
props.onChange(id);
|
|
65
|
+
},
|
|
66
|
+
// The box showing the pick's own name is no search: the list then
|
|
67
|
+
// offers everything, not only the one already picked.
|
|
68
|
+
onInputValueChange: (typed) => setText(picked !== null && typed === nameOf(picked) ? '' : typed), itemToStringLabel: nameOf, filter: null, readOnly: props.disabled, defaultOpen: alone, children: [_jsx(ComboboxInput, { id: props.id, placeholder: config.placeholder ?? 'Search', ...controlAria(props) }), _jsxs(ComboboxContent, { children: [_jsx(ComboboxEmpty, { children: config.emptyText ?? 'No matches.' }), _jsx(ComboboxList, { children: items.map((key) => (_jsx(ComboboxItem, { value: key, children: key === NONE ? emptyLabel : nameOf(key) }, key))) })] })] }));
|
|
69
|
+
}
|
|
70
|
+
// The parts side by side (stacked when the field is narrow), each with its
|
|
71
|
+
// own label over its control and its own refusal under it. A refusal
|
|
72
|
+
// about none of them (the rule across them) is the field's, under them all.
|
|
73
|
+
export function Parts({ props, keys, parts, kinds }) {
|
|
74
|
+
const errors = props.errors;
|
|
75
|
+
return (_jsx("span", { className: "flex flex-wrap items-start gap-3", children: keys.map((key) => {
|
|
76
|
+
const part = parts[key];
|
|
77
|
+
const partId = `${props.id}-${key}`;
|
|
78
|
+
const errorId = `${partId}-error`;
|
|
79
|
+
const error = errors?.[key];
|
|
80
|
+
return (_jsxs("span", { "data-part": key, className: "flex min-w-40 flex-1 flex-col gap-1", children: [_jsx("span", { "aria-hidden": "true", className: "type-caption text-muted-foreground", children: part.label }), kinds[key].control({
|
|
81
|
+
...props,
|
|
82
|
+
// The first part takes the field's id, so its label points at it.
|
|
83
|
+
id: key === keys[0] ? props.id : partId,
|
|
84
|
+
label: part.label,
|
|
85
|
+
value: props.value[key],
|
|
86
|
+
onChange: (next) => props.onChange({ ...props.value, [key]: next }),
|
|
87
|
+
invalid: Boolean(error) || (props.invalid && !errors),
|
|
88
|
+
describedBy: error ? `${errorId} ${props.describedBy}` : props.describedBy,
|
|
89
|
+
errors: undefined,
|
|
90
|
+
}, false), error && (_jsx(FieldError, { id: errorId, className: "type-caption", children: error }))] }, key));
|
|
91
|
+
}) }));
|
|
92
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { type PlainDate } from '../../lib/plain-date.js';
|
|
3
|
+
import type { EditInPlace } from './edit-in-place.js';
|
|
4
|
+
/** What the value saves as: with `required: true` never `null` (a blank is refused). */
|
|
5
|
+
export type InPlaceSaved<TValue, TRequired extends boolean> = TRequired extends true ? TValue : TValue | null;
|
|
6
|
+
type Common<TValue, TSaved> = {
|
|
7
|
+
/** The saved value. */
|
|
8
|
+
value: TValue;
|
|
9
|
+
/** Saves the checked value (the entity's single-field save, `mutateAsync`). Rejects with the AppError. */
|
|
10
|
+
save: (value: TSaved) => Promise<unknown>;
|
|
11
|
+
/** How the value reads at rest, in place of the preset's own (a badge, a link to the record). */
|
|
12
|
+
shown?: ReactNode;
|
|
13
|
+
};
|
|
14
|
+
/** One line of text. Blank saves `null` unless `required`. */
|
|
15
|
+
export type InPlaceTextConfig<TRequired extends boolean = boolean> = {
|
|
16
|
+
/** A blank is refused ("Required"). */
|
|
17
|
+
required?: TRequired;
|
|
18
|
+
/** The most characters it takes. */
|
|
19
|
+
maxLength?: number;
|
|
20
|
+
placeholder?: string;
|
|
21
|
+
};
|
|
22
|
+
/** A number typed in one line. Blank saves `null` unless `required`. */
|
|
23
|
+
export type InPlaceNumberConfig<TRequired extends boolean = boolean> = {
|
|
24
|
+
required?: TRequired;
|
|
25
|
+
/** The smallest it may be ("At least 0"). */
|
|
26
|
+
min?: number;
|
|
27
|
+
/** The largest it may be ("At most 10"). */
|
|
28
|
+
max?: number;
|
|
29
|
+
/** Whole numbers only. */
|
|
30
|
+
integer?: boolean;
|
|
31
|
+
/** Shown after the number, at rest and beside the box ("kg", "min"). */
|
|
32
|
+
unit?: string;
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
locale?: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* An amount of money, two decimals, as the API writes a decimal: a string
|
|
38
|
+
* (`"1234.50"`). It reads as the currency formats it ("$1,234.50"); `$`
|
|
39
|
+
* and thousands separators typed in are fine. Blank saves `null` unless `required`.
|
|
40
|
+
*/
|
|
41
|
+
export type InPlaceMoneyConfig<TRequired extends boolean = boolean> = {
|
|
42
|
+
required?: TRequired;
|
|
43
|
+
/** ISO 4217 code. Defaults to `USD`. */
|
|
44
|
+
currency?: string;
|
|
45
|
+
/** For the symbol and the separators. Defaults to `en-US`. */
|
|
46
|
+
locale?: string;
|
|
47
|
+
/** The smallest amount. Defaults to 0: no negative amounts. */
|
|
48
|
+
min?: number;
|
|
49
|
+
/** The largest amount. */
|
|
50
|
+
max?: number;
|
|
51
|
+
placeholder?: string;
|
|
52
|
+
};
|
|
53
|
+
/** A plain day, `'YYYY-MM-DD'` (DateField). Not `required`: Clear saves `null`. */
|
|
54
|
+
export type InPlaceDateConfig<TRequired extends boolean = boolean> = {
|
|
55
|
+
required?: TRequired;
|
|
56
|
+
/** How the day reads. Defaults to `{ dateStyle: 'medium' }`. */
|
|
57
|
+
format?: Intl.DateTimeFormatOptions;
|
|
58
|
+
locale?: string;
|
|
59
|
+
placeholder?: string;
|
|
60
|
+
};
|
|
61
|
+
/** One option of a select: its value and its words. */
|
|
62
|
+
export type InPlaceOption<T> = {
|
|
63
|
+
value: T;
|
|
64
|
+
label: string;
|
|
65
|
+
};
|
|
66
|
+
/** One of a fixed list. Not `required`: the list starts with "Not set", which saves `null`. */
|
|
67
|
+
export type InPlaceSelectConfig<T extends string | number, TRequired extends boolean = boolean> = {
|
|
68
|
+
required?: TRequired;
|
|
69
|
+
/** Every option, in the order the list shows them. */
|
|
70
|
+
options: readonly InPlaceOption<T>[];
|
|
71
|
+
/** How an option reads, at rest, on the trigger and in the list (a badge). Defaults to its label. */
|
|
72
|
+
render?: (value: T, label: string) => ReactNode;
|
|
73
|
+
/** The "Not set" option's words. */
|
|
74
|
+
emptyLabel?: string;
|
|
75
|
+
placeholder?: string;
|
|
76
|
+
};
|
|
77
|
+
/** A record found by search, as `search` returns it. */
|
|
78
|
+
export type InPlaceRecord<Id> = {
|
|
79
|
+
id: Id;
|
|
80
|
+
label: string;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Another record, picked from a list searched as you type. The package
|
|
84
|
+
* knows no endpoint: `search` is the app's (its gateway's search, by
|
|
85
|
+
* name), called with what's typed (`''` when the list opens).
|
|
86
|
+
*/
|
|
87
|
+
export type InPlaceReferenceConfig<Id extends string | number, TRequired extends boolean = boolean> = {
|
|
88
|
+
required?: TRequired;
|
|
89
|
+
/** Finds records by what's typed. Rejects to show nothing found. */
|
|
90
|
+
search: (text: string) => Promise<readonly InPlaceRecord<Id>[]>;
|
|
91
|
+
/** The saved record's name (the view's lookup by id), shown at rest and in the box. */
|
|
92
|
+
label?: string;
|
|
93
|
+
/** The "Not set" option's words, for an optional reference. */
|
|
94
|
+
emptyLabel?: string;
|
|
95
|
+
placeholder?: string;
|
|
96
|
+
/** What the list says when nothing matches. Defaults to "No matches." */
|
|
97
|
+
emptyText?: string;
|
|
98
|
+
};
|
|
99
|
+
/** A link: its address and an optional label, as the app maps them to its own fields. */
|
|
100
|
+
export type InPlaceLinkValue = {
|
|
101
|
+
url: string | null;
|
|
102
|
+
label: string | null;
|
|
103
|
+
};
|
|
104
|
+
export type InPlaceLinkConfig = {
|
|
105
|
+
/** The address box's placeholder. Defaults to `https://`. */
|
|
106
|
+
placeholder?: string;
|
|
107
|
+
/** What reads at rest with no address. Defaults to the field's "Not set". */
|
|
108
|
+
emptyLabel?: string;
|
|
109
|
+
};
|
|
110
|
+
/** One part of `inPlace.fields`: its label and its kind's options. */
|
|
111
|
+
export type InPlacePart = ({
|
|
112
|
+
kind: 'text';
|
|
113
|
+
label: string;
|
|
114
|
+
} & InPlaceTextConfig) | ({
|
|
115
|
+
kind: 'number';
|
|
116
|
+
label: string;
|
|
117
|
+
} & InPlaceNumberConfig) | ({
|
|
118
|
+
kind: 'money';
|
|
119
|
+
label: string;
|
|
120
|
+
} & InPlaceMoneyConfig) | ({
|
|
121
|
+
kind: 'date';
|
|
122
|
+
label: string;
|
|
123
|
+
} & InPlaceDateConfig) | ({
|
|
124
|
+
kind: 'select';
|
|
125
|
+
label: string;
|
|
126
|
+
} & InPlaceSelectConfig<any>) | ({
|
|
127
|
+
kind: 'reference';
|
|
128
|
+
label: string;
|
|
129
|
+
} & InPlaceReferenceConfig<any>);
|
|
130
|
+
/**
|
|
131
|
+
* Two or more of the record's fields edited as one value and saved
|
|
132
|
+
* together, with a rule across them (a model and an effort that must agree).
|
|
133
|
+
*/
|
|
134
|
+
export type InPlaceFieldsOptions<T extends Record<string, unknown>> = {
|
|
135
|
+
/** The saved values, keyed by the record's field names (`{ model, effort }`). */
|
|
136
|
+
value: T;
|
|
137
|
+
/** Each value's part, in the order they show: its label and kind. */
|
|
138
|
+
fields: {
|
|
139
|
+
[K in keyof T]: InPlacePart;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* The rule across them, run once every part's own check passes: the
|
|
143
|
+
* message to show on the pair, or `null` when they agree.
|
|
144
|
+
*/
|
|
145
|
+
rule?: (value: T) => string | null;
|
|
146
|
+
/** Saves them together (one PATCH of every field). A 422 about one of them shows on its part. */
|
|
147
|
+
save: (value: T) => Promise<unknown>;
|
|
148
|
+
/** How the pair reads at rest. Defaults to each part's value, joined by " · ". */
|
|
149
|
+
shown?: ReactNode;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Ready-made fields for editing in place, one per kind of control. Each
|
|
153
|
+
* returns an `EditInPlace` carrying its control, its check and how it
|
|
154
|
+
* reads at rest (`shown`), so a field is one line:
|
|
155
|
+
* `{ label: 'Due', edit: inPlace.date({ value: task.due, save: (due) => save({ due }) }) }`.
|
|
156
|
+
*/
|
|
157
|
+
export declare const inPlace: {
|
|
158
|
+
/** One line of text; Enter saves. Trimmed; blank saves `null` unless `required`. */
|
|
159
|
+
text<R extends boolean = false>(options: Common<string | null | undefined, InPlaceSaved<string, R>> & InPlaceTextConfig<R>): EditInPlace;
|
|
160
|
+
/** A number in one line, checked against `min`, `max` and `integer`; `unit` reads after it. */
|
|
161
|
+
number<R extends boolean = false>(options: Common<number | null | undefined, InPlaceSaved<number, R>> & InPlaceNumberConfig<R>): EditInPlace;
|
|
162
|
+
/** Money, two decimals, saved as the decimal string (`"1234.50"`), shown as "$1,234.50". */
|
|
163
|
+
money<R extends boolean = false>(options: Common<string | number | null | undefined, InPlaceSaved<string, R>> & InPlaceMoneyConfig<R>): EditInPlace;
|
|
164
|
+
/** A plain day (`'YYYY-MM-DD'`) in DateField's calendar, which opens with the field; a pick saves. */
|
|
165
|
+
date<R extends boolean = false>(options: Common<PlainDate | null | undefined, InPlaceSaved<PlainDate, R>> & InPlaceDateConfig<R>): EditInPlace;
|
|
166
|
+
/** One of a fixed list; its list opens with the field and a pick saves. */
|
|
167
|
+
select<T extends string | number, R extends boolean = false>(options: Common<T | null | undefined, InPlaceSaved<T, R>> & InPlaceSelectConfig<T, R>): EditInPlace;
|
|
168
|
+
/** Another record, searched as you type through the app's `search`; a pick saves its id. */
|
|
169
|
+
reference<Id extends string | number, R extends boolean = false>(options: Common<Id | null | undefined, InPlaceSaved<Id, R>> & InPlaceReferenceConfig<Id, R>): EditInPlace;
|
|
170
|
+
/**
|
|
171
|
+
* A link: at rest its label (or the address's host when it has none),
|
|
172
|
+
* opening in a new tab; edited as two boxes, URL and an optional Label,
|
|
173
|
+
* saved together as `{ url, label }`. The address is checked by
|
|
174
|
+
* `linkRule` (LinkCell's): `example.com` gains https://, another scheme
|
|
175
|
+
* is refused. A blank address with no label saves `{ url: null, label: null }`.
|
|
176
|
+
*/
|
|
177
|
+
link(options: Common<InPlaceLinkValue, InPlaceLinkValue> & InPlaceLinkConfig): EditInPlace;
|
|
178
|
+
/**
|
|
179
|
+
* Two or more fields edited as one value and saved together, with a
|
|
180
|
+
* `rule` across them: each part its own control and label, a part's
|
|
181
|
+
* refusal (its own check, or a 422 naming its field) under it, the
|
|
182
|
+
* rule's under them all. Saves on leaving it or Ctrl/Cmd+Enter (and on
|
|
183
|
+
* Enter when every part is typed into).
|
|
184
|
+
*/
|
|
185
|
+
fields<T extends Record<string, unknown>>(options: InPlaceFieldsOptions<T>): EditInPlace;
|
|
186
|
+
};
|
|
187
|
+
export {};
|