@tristan2828/ui-foundation 4.71.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 +21 -15
- 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/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,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 {};
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Input } from '../ui/input.js';
|
|
3
|
+
import { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from '../ui/input-group.js';
|
|
4
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select.js';
|
|
5
|
+
import { linkHost, linkRule } from '../../lib/link-href.js';
|
|
6
|
+
import { formatPlainDate } from '../../lib/plain-date.js';
|
|
7
|
+
import { DateField } from './date-field.js';
|
|
8
|
+
import { NONE, Parts, ReferencePicker } from './edit-in-place-controls.js';
|
|
9
|
+
import { LinkOut } from './row-edit-cells.js';
|
|
10
|
+
const REQUIRED = 'Required';
|
|
11
|
+
// The props every control spreads onto its element.
|
|
12
|
+
function controlAria(props) {
|
|
13
|
+
return {
|
|
14
|
+
'aria-label': props.label,
|
|
15
|
+
'aria-describedby': props.describedBy,
|
|
16
|
+
'aria-invalid': props.invalid || undefined,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// ─── The kinds ──────────────────────────────────────────────────────────
|
|
20
|
+
const blank = (text) => text.trim() === '';
|
|
21
|
+
function textKind(config) {
|
|
22
|
+
return {
|
|
23
|
+
editKind: 'text',
|
|
24
|
+
toDraft: (value) => value ?? '',
|
|
25
|
+
check: (draft) => {
|
|
26
|
+
const text = draft.trim();
|
|
27
|
+
if (text === '')
|
|
28
|
+
return config.required ? { ok: false, message: REQUIRED } : { ok: true, value: null };
|
|
29
|
+
if (config.maxLength !== undefined && text.length > config.maxLength)
|
|
30
|
+
return { ok: false, message: `At most ${config.maxLength} characters` };
|
|
31
|
+
return { ok: true, value: text };
|
|
32
|
+
},
|
|
33
|
+
show: (value) => (value ? value : null),
|
|
34
|
+
control: (props) => (_jsx(Input, { id: props.id, value: props.value, placeholder: config.placeholder, readOnly: props.disabled, onChange: (event) => props.onChange(event.target.value), ...controlAria(props) })),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function numberKind(config) {
|
|
38
|
+
const format = new Intl.NumberFormat(config.locale);
|
|
39
|
+
return {
|
|
40
|
+
editKind: 'text',
|
|
41
|
+
toDraft: (value) => (value == null ? '' : String(value)),
|
|
42
|
+
check: (draft) => {
|
|
43
|
+
if (blank(draft))
|
|
44
|
+
return config.required ? { ok: false, message: REQUIRED } : { ok: true, value: null };
|
|
45
|
+
const typed = draft.trim().replace(/,/g, '');
|
|
46
|
+
const number = /^[-+]?(\d+\.?\d*|\.\d+)$/.test(typed) ? Number(typed) : Number.NaN;
|
|
47
|
+
if (!Number.isFinite(number))
|
|
48
|
+
return { ok: false, message: 'Enter a number' };
|
|
49
|
+
if (config.integer && !Number.isInteger(number))
|
|
50
|
+
return { ok: false, message: 'Enter a whole number' };
|
|
51
|
+
if (config.min !== undefined && number < config.min)
|
|
52
|
+
return { ok: false, message: `At least ${format.format(config.min)}` };
|
|
53
|
+
if (config.max !== undefined && number > config.max)
|
|
54
|
+
return { ok: false, message: `At most ${format.format(config.max)}` };
|
|
55
|
+
return { ok: true, value: number };
|
|
56
|
+
},
|
|
57
|
+
show: (value) => (value == null ? null : config.unit ? `${format.format(value)} ${config.unit}` : format.format(value)),
|
|
58
|
+
control: (props) => {
|
|
59
|
+
const input = {
|
|
60
|
+
id: props.id,
|
|
61
|
+
inputMode: config.integer && (config.min ?? 0) >= 0 ? 'numeric' : 'decimal',
|
|
62
|
+
value: props.value,
|
|
63
|
+
placeholder: config.placeholder,
|
|
64
|
+
readOnly: props.disabled,
|
|
65
|
+
onChange: (event) => props.onChange(event.target.value),
|
|
66
|
+
...controlAria(props),
|
|
67
|
+
};
|
|
68
|
+
if (!config.unit)
|
|
69
|
+
return _jsx(Input, { ...input });
|
|
70
|
+
return (_jsxs(InputGroup, { children: [_jsx(InputGroupInput, { ...input }), _jsx(InputGroupAddon, { align: "inline-end", children: _jsx(InputGroupText, { children: config.unit }) })] }));
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const MONEY = /^(-)?(\d+)(?:\.(\d{0,2}))?$/;
|
|
75
|
+
function moneyKind(config) {
|
|
76
|
+
const locale = config.locale ?? 'en-US';
|
|
77
|
+
const currency = config.currency ?? 'USD';
|
|
78
|
+
const money = new Intl.NumberFormat(locale, { style: 'currency', currency });
|
|
79
|
+
const symbol = money.formatToParts(0).find((part) => part.type === 'currency')?.value ?? currency;
|
|
80
|
+
const min = config.min ?? 0;
|
|
81
|
+
return {
|
|
82
|
+
editKind: 'text',
|
|
83
|
+
toDraft: (value) => {
|
|
84
|
+
if (value == null || value === '')
|
|
85
|
+
return '';
|
|
86
|
+
const number = Number(value);
|
|
87
|
+
return Number.isFinite(number) ? number.toFixed(2) : String(value);
|
|
88
|
+
},
|
|
89
|
+
check: (draft) => {
|
|
90
|
+
if (blank(draft))
|
|
91
|
+
return config.required ? { ok: false, message: REQUIRED } : { ok: true, value: null };
|
|
92
|
+
// `$1,234.5` is 1234.50: the symbol, spaces and separators go.
|
|
93
|
+
const typed = draft.replaceAll(symbol, '').replace(/[\s,]/g, '');
|
|
94
|
+
const match = MONEY.exec(typed);
|
|
95
|
+
if (!match)
|
|
96
|
+
return { ok: false, message: 'Enter an amount, like 12.50' };
|
|
97
|
+
// Built from the digits, never through a float: 0.1 + 0.2 stays 0.30.
|
|
98
|
+
const amount = `${match[1] ?? ''}${String(Number(match[2]))}.${(match[3] ?? '').padEnd(2, '0')}`;
|
|
99
|
+
const number = Number(amount);
|
|
100
|
+
if (number < min)
|
|
101
|
+
return { ok: false, message: `At least ${money.format(min)}` };
|
|
102
|
+
if (config.max !== undefined && number > config.max)
|
|
103
|
+
return { ok: false, message: `At most ${money.format(config.max)}` };
|
|
104
|
+
return { ok: true, value: amount === '-0.00' ? '0.00' : amount };
|
|
105
|
+
},
|
|
106
|
+
show: (value) => {
|
|
107
|
+
if (value == null || value === '')
|
|
108
|
+
return null;
|
|
109
|
+
const number = Number(value);
|
|
110
|
+
return Number.isFinite(number) ? money.format(number) : String(value);
|
|
111
|
+
},
|
|
112
|
+
control: (props) => (_jsxs(InputGroup, { children: [_jsx(InputGroupAddon, { children: _jsx(InputGroupText, { children: symbol }) }), _jsx(InputGroupInput, { id: props.id, inputMode: "decimal", value: props.value, placeholder: config.placeholder ?? '0.00', readOnly: props.disabled, onChange: (event) => props.onChange(event.target.value), ...controlAria(props) })] })),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function dateKind(config) {
|
|
116
|
+
return {
|
|
117
|
+
editKind: 'choice',
|
|
118
|
+
toDraft: (value) => value ?? '',
|
|
119
|
+
check: (draft) => {
|
|
120
|
+
if (draft === '')
|
|
121
|
+
return config.required ? { ok: false, message: REQUIRED } : { ok: true, value: null };
|
|
122
|
+
return formatPlainDate(draft) === '' ? { ok: false, message: 'Pick a date' } : { ok: true, value: draft };
|
|
123
|
+
},
|
|
124
|
+
show: (value) => (value ? formatPlainDate(value, config.format, config.locale) || value : null),
|
|
125
|
+
control: (props, alone) => (_jsx(DateField, { id: props.id, value: props.value,
|
|
126
|
+
// Alone, a pick saves and the calendar opens with the field; closing
|
|
127
|
+
// it with nothing picked gives up (a pick has already saved by then).
|
|
128
|
+
onChange: alone ? (day) => props.commit(day) : props.onChange, clearable: !config.required, placeholder: config.placeholder, format: config.format, locale: config.locale, invalid: props.invalid, readOnly: props.disabled, defaultOpen: alone, modal: alone ? false : undefined, onOpenChange: alone
|
|
129
|
+
? (open) => {
|
|
130
|
+
if (!open)
|
|
131
|
+
props.cancel();
|
|
132
|
+
}
|
|
133
|
+
: undefined, "aria-label": props.label, "aria-describedby": props.describedBy })),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function selectKind(config) {
|
|
137
|
+
const labelOf = (value) => config.options.find((option) => option.value === value)?.label ?? String(value);
|
|
138
|
+
const shownOf = (value) => (config.render ? config.render(value, labelOf(value)) : labelOf(value));
|
|
139
|
+
const keyOf = (value) => {
|
|
140
|
+
const at = value == null ? -1 : config.options.findIndex((option) => option.value === value);
|
|
141
|
+
return at === -1 ? null : String(at);
|
|
142
|
+
};
|
|
143
|
+
const valueOf = (key) => (key === null || key === NONE ? null : (config.options[Number(key)]?.value ?? null));
|
|
144
|
+
const emptyLabel = config.emptyLabel ?? 'Not set';
|
|
145
|
+
return {
|
|
146
|
+
editKind: 'choice',
|
|
147
|
+
toDraft: (value) => value,
|
|
148
|
+
check: (draft) => (draft == null && config.required ? { ok: false, message: REQUIRED } : { ok: true, value: draft }),
|
|
149
|
+
show: (value) => (value == null ? null : shownOf(value)),
|
|
150
|
+
control: (props, alone) => (_jsxs(Select, { value: keyOf(props.value) ?? (config.required ? null : NONE), onValueChange: (key) => {
|
|
151
|
+
if (key === null)
|
|
152
|
+
return;
|
|
153
|
+
const picked = valueOf(key);
|
|
154
|
+
if (alone)
|
|
155
|
+
props.commit(picked);
|
|
156
|
+
else
|
|
157
|
+
props.onChange(picked);
|
|
158
|
+
}, disabled: props.disabled, defaultOpen: alone, modal: false, onOpenChange: alone
|
|
159
|
+
? (open) => {
|
|
160
|
+
if (!open)
|
|
161
|
+
props.cancel();
|
|
162
|
+
}
|
|
163
|
+
: undefined, children: [_jsx(SelectTrigger, { id: props.id, className: "min-w-40", ...controlAria(props), children: _jsx(SelectValue, { placeholder: config.placeholder ?? 'Pick one', children: (key) => {
|
|
164
|
+
const value = valueOf(key);
|
|
165
|
+
return value == null ? (key === NONE ? emptyLabel : (config.placeholder ?? 'Pick one')) : shownOf(value);
|
|
166
|
+
} }) }), _jsxs(SelectContent, { children: [!config.required && _jsx(SelectItem, { value: NONE, children: emptyLabel }), config.options.map((option, index) => (_jsx(SelectItem, { value: String(index), children: shownOf(option.value) }, String(option.value))))] })] })),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function referenceKind(config) {
|
|
170
|
+
return {
|
|
171
|
+
editKind: 'choice',
|
|
172
|
+
toDraft: (value) => value,
|
|
173
|
+
check: (draft) => (draft == null && config.required ? { ok: false, message: REQUIRED } : { ok: true, value: draft }),
|
|
174
|
+
show: (value) => (value == null ? null : (config.label ?? String(value))),
|
|
175
|
+
control: (props, alone) => _jsx(ReferencePicker, { props: props, alone: alone, config: config }),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
// A link's address: `linkRule` (LinkCell's), which also says what's refused.
|
|
179
|
+
function urlKind(config) {
|
|
180
|
+
return {
|
|
181
|
+
editKind: 'text',
|
|
182
|
+
toDraft: (value) => value ?? '',
|
|
183
|
+
check: (draft) => {
|
|
184
|
+
const result = linkRule.safeParse(draft);
|
|
185
|
+
return result.success ? { ok: true, value: result.data } : { ok: false, message: result.error.issues[0].message };
|
|
186
|
+
},
|
|
187
|
+
show: (value) => value,
|
|
188
|
+
control: (props) => (_jsx(Input, { id: props.id, type: "url", inputMode: "url", value: props.value, placeholder: config.placeholder ?? 'https://', readOnly: props.disabled, onChange: (event) => props.onChange(event.target.value), ...controlAria(props) })),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function kindOf(part) {
|
|
192
|
+
switch (part.kind) {
|
|
193
|
+
case 'text':
|
|
194
|
+
return textKind(part);
|
|
195
|
+
case 'number':
|
|
196
|
+
return numberKind(part);
|
|
197
|
+
case 'money':
|
|
198
|
+
return moneyKind(part);
|
|
199
|
+
case 'date':
|
|
200
|
+
return dateKind(part);
|
|
201
|
+
case 'select':
|
|
202
|
+
return selectKind(part);
|
|
203
|
+
case 'reference':
|
|
204
|
+
return referenceKind(part);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// ─── A field of one kind ────────────────────────────────────────────────
|
|
208
|
+
// Two drafts are the same value when both check to the same thing (`12`
|
|
209
|
+
// and `12.0`, `$5` and `5.00`), else when they're the same text.
|
|
210
|
+
function sameValue(check) {
|
|
211
|
+
return (a, b) => {
|
|
212
|
+
const [left, right] = [check(a), check(b)];
|
|
213
|
+
if (left.ok && right.ok)
|
|
214
|
+
return JSON.stringify(left.value) === JSON.stringify(right.value);
|
|
215
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function single(kind, options) {
|
|
219
|
+
return {
|
|
220
|
+
kind: kind.editKind,
|
|
221
|
+
value: kind.toDraft(options.value),
|
|
222
|
+
control: (props) => kind.control(props, true),
|
|
223
|
+
schema: {
|
|
224
|
+
safeParse: (draft) => {
|
|
225
|
+
const checked = kind.check(draft);
|
|
226
|
+
return checked.ok ? { success: true, data: checked.value } : { success: false, error: { issues: [{ message: checked.message }] } };
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
save: options.save,
|
|
230
|
+
isEqual: sameValue(kind.check),
|
|
231
|
+
shown: options.shown !== undefined ? options.shown : kind.show(options.value),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
// Which parts a refusal names: the check's issues (each part's own, by
|
|
235
|
+
// its key), or a 422's field errors (`model`, `effort.level`).
|
|
236
|
+
function partErrors(keys, cause) {
|
|
237
|
+
const errors = {};
|
|
238
|
+
if (cause && typeof cause === 'object' && 'issues' in cause) {
|
|
239
|
+
for (const issue of cause.issues) {
|
|
240
|
+
const key = issue.path?.[0];
|
|
241
|
+
if (typeof key === 'string' && keys.includes(key) && !(key in errors))
|
|
242
|
+
errors[key] = issue.message;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (cause && typeof cause === 'object' && 'kind' in cause) {
|
|
246
|
+
for (const [field, messages] of Object.entries(cause.fieldErrors ?? {})) {
|
|
247
|
+
const key = keys.find((name) => field === name || field.startsWith(`${name}.`));
|
|
248
|
+
if (key && !(key in errors) && messages[0])
|
|
249
|
+
errors[key] = messages[0];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return Object.keys(errors).length > 0 ? errors : null;
|
|
253
|
+
}
|
|
254
|
+
function fieldsInPlace(options) {
|
|
255
|
+
const keys = Object.keys(options.fields);
|
|
256
|
+
const parts = options.fields;
|
|
257
|
+
const kinds = Object.fromEntries(keys.map((key) => [key, kindOf(parts[key])]));
|
|
258
|
+
const value = options.value;
|
|
259
|
+
const check = (draft) => {
|
|
260
|
+
const issues = [];
|
|
261
|
+
const checked = {};
|
|
262
|
+
for (const key of keys) {
|
|
263
|
+
const result = kinds[key].check(draft[key]);
|
|
264
|
+
if (result.ok)
|
|
265
|
+
checked[key] = result.value;
|
|
266
|
+
else
|
|
267
|
+
issues.push({ message: `${parts[key].label}: ${result.message}`, path: [key] });
|
|
268
|
+
}
|
|
269
|
+
if (issues.length > 0)
|
|
270
|
+
return { ok: false, message: issues[0].message, issues };
|
|
271
|
+
const message = options.rule?.(checked) ?? null;
|
|
272
|
+
return message === null ? { ok: true, value: checked } : { ok: false, message, issues: [{ message }] };
|
|
273
|
+
};
|
|
274
|
+
// Enter saves when every part is typed into; with a list among them,
|
|
275
|
+
// Enter is the list's, and leaving or Ctrl/Cmd+Enter saves.
|
|
276
|
+
const typed = keys.every((key) => kinds[key].editKind === 'text');
|
|
277
|
+
const shownParts = keys.map((key) => kinds[key].show(value[key])).filter((shown) => shown !== null && shown !== '');
|
|
278
|
+
return {
|
|
279
|
+
kind: typed ? 'text' : 'multi',
|
|
280
|
+
value: Object.fromEntries(keys.map((key) => [key, kinds[key].toDraft(value[key])])),
|
|
281
|
+
control: (props) => (_jsx(Parts, { props: props, keys: keys, parts: parts, kinds: kinds })),
|
|
282
|
+
schema: {
|
|
283
|
+
safeParse: (draft) => {
|
|
284
|
+
const result = check(draft);
|
|
285
|
+
return result.ok ? { success: true, data: result.value } : { success: false, error: { issues: result.issues ?? [] } };
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
save: options.save,
|
|
289
|
+
isEqual: sameValue((draft) => {
|
|
290
|
+
const checked = {};
|
|
291
|
+
for (const key of keys) {
|
|
292
|
+
const result = kinds[key].check(draft[key]);
|
|
293
|
+
if (!result.ok)
|
|
294
|
+
return result;
|
|
295
|
+
checked[key] = result.value;
|
|
296
|
+
}
|
|
297
|
+
return { ok: true, value: checked };
|
|
298
|
+
}),
|
|
299
|
+
fieldErrors: (cause) => partErrors(keys, cause),
|
|
300
|
+
errorsOnFields: true,
|
|
301
|
+
shown: options.shown !== undefined
|
|
302
|
+
? options.shown
|
|
303
|
+
: shownParts.length === 0
|
|
304
|
+
? null
|
|
305
|
+
: shownParts.map((shown, index) => (_jsxs("span", { children: [index > 0 && ' · ', shown] }, index))),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
// ─── A link ─────────────────────────────────────────────────────────────
|
|
309
|
+
function linkInPlace(options) {
|
|
310
|
+
const { url, label } = options.value;
|
|
311
|
+
const href = url ? linkRule.safeParse(url) : null;
|
|
312
|
+
// Only an address the box would have saved is a link: a stored
|
|
313
|
+
// `javascript:` reads as text, never as something to click.
|
|
314
|
+
const shown = options.shown !== undefined
|
|
315
|
+
? options.shown
|
|
316
|
+
: !url
|
|
317
|
+
? (options.emptyLabel ? _jsx("span", { className: "text-muted-foreground", children: options.emptyLabel }) : null)
|
|
318
|
+
: href?.success && href.data
|
|
319
|
+
? _jsx(LinkOut, { href: href.data, children: label || linkHost(href.data) })
|
|
320
|
+
: url;
|
|
321
|
+
const urls = urlKind(options);
|
|
322
|
+
const labels = textKind({ placeholder: 'Label (optional)' });
|
|
323
|
+
const kinds = { url: urls, label: labels };
|
|
324
|
+
const parts = {
|
|
325
|
+
url: { kind: 'text', label: 'URL' },
|
|
326
|
+
label: { kind: 'text', label: 'Label' },
|
|
327
|
+
};
|
|
328
|
+
const check = (draft) => {
|
|
329
|
+
const address = urls.check(draft.url);
|
|
330
|
+
if (!address.ok)
|
|
331
|
+
return { success: false, error: { issues: [{ message: address.message, path: ['url'] }] } };
|
|
332
|
+
const words = labels.check(draft.label);
|
|
333
|
+
if (!words.ok)
|
|
334
|
+
return { success: false, error: { issues: [{ message: words.message, path: ['label'] }] } };
|
|
335
|
+
if (address.value === null && words.value !== null)
|
|
336
|
+
return { success: false, error: { issues: [{ message: 'Add the address, or clear the label', path: ['url'] }] } };
|
|
337
|
+
return { success: true, data: { url: address.value, label: words.value } };
|
|
338
|
+
};
|
|
339
|
+
return {
|
|
340
|
+
// Enter in either box saves, as a one-line field does.
|
|
341
|
+
kind: 'text',
|
|
342
|
+
value: { url: url ?? '', label: label ?? '' },
|
|
343
|
+
control: (props) => (_jsx(Parts, { props: props, keys: ['url', 'label'], parts: parts, kinds: kinds })),
|
|
344
|
+
save: options.save,
|
|
345
|
+
fieldErrors: (cause) => partErrors(['url', 'label'], cause),
|
|
346
|
+
errorsOnFields: true,
|
|
347
|
+
schema: { safeParse: (draft) => check(draft) },
|
|
348
|
+
isEqual: (a, b) => {
|
|
349
|
+
const [left, right] = [check(a), check(b)];
|
|
350
|
+
if (left.success && right.success)
|
|
351
|
+
return JSON.stringify(left.data) === JSON.stringify(right.data);
|
|
352
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
353
|
+
},
|
|
354
|
+
shown,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
// ─── The presets ────────────────────────────────────────────────────────
|
|
358
|
+
/**
|
|
359
|
+
* Ready-made fields for editing in place, one per kind of control. Each
|
|
360
|
+
* returns an `EditInPlace` carrying its control, its check and how it
|
|
361
|
+
* reads at rest (`shown`), so a field is one line:
|
|
362
|
+
* `{ label: 'Due', edit: inPlace.date({ value: task.due, save: (due) => save({ due }) }) }`.
|
|
363
|
+
*/
|
|
364
|
+
export const inPlace = {
|
|
365
|
+
/** One line of text; Enter saves. Trimmed; blank saves `null` unless `required`. */
|
|
366
|
+
text(options) {
|
|
367
|
+
return single(textKind(options), { ...options, value: options.value ?? null });
|
|
368
|
+
},
|
|
369
|
+
/** A number in one line, checked against `min`, `max` and `integer`; `unit` reads after it. */
|
|
370
|
+
number(options) {
|
|
371
|
+
return single(numberKind(options), { ...options, value: options.value ?? null });
|
|
372
|
+
},
|
|
373
|
+
/** Money, two decimals, saved as the decimal string (`"1234.50"`), shown as "$1,234.50". */
|
|
374
|
+
money(options) {
|
|
375
|
+
return single(moneyKind(options), { ...options, value: options.value ?? null });
|
|
376
|
+
},
|
|
377
|
+
/** A plain day (`'YYYY-MM-DD'`) in DateField's calendar, which opens with the field; a pick saves. */
|
|
378
|
+
date(options) {
|
|
379
|
+
return single(dateKind(options), { ...options, value: options.value || null });
|
|
380
|
+
},
|
|
381
|
+
/** One of a fixed list; its list opens with the field and a pick saves. */
|
|
382
|
+
select(options) {
|
|
383
|
+
return single(selectKind(options), { ...options, value: options.value ?? null });
|
|
384
|
+
},
|
|
385
|
+
/** Another record, searched as you type through the app's `search`; a pick saves its id. */
|
|
386
|
+
reference(options) {
|
|
387
|
+
return single(referenceKind(options), { ...options, value: options.value ?? null });
|
|
388
|
+
},
|
|
389
|
+
/**
|
|
390
|
+
* A link: at rest its label (or the address's host when it has none),
|
|
391
|
+
* opening in a new tab; edited as two boxes, URL and an optional Label,
|
|
392
|
+
* saved together as `{ url, label }`. The address is checked by
|
|
393
|
+
* `linkRule` (LinkCell's): `example.com` gains https://, another scheme
|
|
394
|
+
* is refused. A blank address with no label saves `{ url: null, label: null }`.
|
|
395
|
+
*/
|
|
396
|
+
link(options) {
|
|
397
|
+
return linkInPlace(options);
|
|
398
|
+
},
|
|
399
|
+
/**
|
|
400
|
+
* Two or more fields edited as one value and saved together, with a
|
|
401
|
+
* `rule` across them: each part its own control and label, a part's
|
|
402
|
+
* refusal (its own check, or a 422 naming its field) under it, the
|
|
403
|
+
* rule's under them all. Saves on leaving it or Ctrl/Cmd+Enter (and on
|
|
404
|
+
* Enter when every part is typed into).
|
|
405
|
+
*/
|
|
406
|
+
fields(options) {
|
|
407
|
+
return fieldsInPlace(options);
|
|
408
|
+
},
|
|
409
|
+
};
|
|
@@ -25,6 +25,13 @@ export type EditFieldConfig = {
|
|
|
25
25
|
autosave?: boolean;
|
|
26
26
|
/** Always open: the control is the value, with no click to open. Autosaves. */
|
|
27
27
|
alwaysEditable?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Which of the value's own fields a refusal is about: given the schema's
|
|
30
|
+
* error (`{ issues }`) or the save's rejection (the AppError), the
|
|
31
|
+
* message for each field it names, keyed by the field's path inside the
|
|
32
|
+
* value (`text`, `effort`). `null` or `{}` when it names none.
|
|
33
|
+
*/
|
|
34
|
+
fieldErrors?: (cause: unknown) => Record<string, string> | null;
|
|
28
35
|
};
|
|
29
36
|
export type EditFieldState = {
|
|
30
37
|
status: 'closed';
|
|
@@ -39,6 +46,8 @@ export type OpenFieldState = {
|
|
|
39
46
|
error: string | null;
|
|
40
47
|
/** A problem the control reports itself (rich text that can't save as it stands). */
|
|
41
48
|
problem: string | null;
|
|
49
|
+
/** The refusal's messages for the value's own fields (`fieldErrors`), shown on those fields. */
|
|
50
|
+
fieldErrors?: Record<string, string> | null;
|
|
42
51
|
/**
|
|
43
52
|
* An autosaving field's save: in flight (the control stays editable),
|
|
44
53
|
* or done with nothing typed since. Absent otherwise.
|