@mdbase-dev/ui 0.2.5 → 0.2.7

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.
@@ -1,20 +1,23 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useMemo, useRef, useState } from "react";
3
3
  import { Combo } from "../Combo.js";
4
4
  import { Glyph } from "../glyphs.js";
5
5
  import { useLocaleRevision } from "../i18n.js";
6
6
  import { propertyLabels } from "./labels.js";
7
- import { describeProperty, isEmptyValue, isGroup, isPropertyName, isScalar, propertyKind, valueLine } from "./model.js";
7
+ import { blankValue, describeProperty, isEmptyValue, isGroup, isPropertyName, propertiesInUse, propertyKind, valueCount, valueLine } from "./model.js";
8
8
  import { PropertyValue } from "./PropertyValue.js";
9
9
  const kindGlyphs = {
10
10
  choice: "choice",
11
11
  boolean: "boolean",
12
12
  date: "date",
13
+ datetime: "date",
13
14
  number: "number",
14
15
  tags: "list",
15
16
  links: "link",
16
17
  text: "text",
17
18
  long: "text",
19
+ group: "group",
20
+ items: "list",
18
21
  other: "text"
19
22
  };
20
23
  /** The small drawing that says what kind of value a property holds, as the list's rows show it. */
@@ -28,6 +31,8 @@ export function PropertyKindIcon({
28
31
  });
29
32
  }
30
33
  const showEmptyKey = "mdbase:show-empty-properties";
34
+ /** A value with more values in it than this starts folded away. */
35
+ const openAtMost = 8;
31
36
  function readShowEmpty() {
32
37
  try {
33
38
  return localStorage.getItem(showEmptyKey) === "1";
@@ -36,10 +41,15 @@ function readShowEmpty() {
36
41
  }
37
42
  }
38
43
  const shownAlways = () => false;
44
+ const noErrors = {};
45
+ const noProperties = [];
46
+ const noValues = [];
47
+ const noParts = {};
39
48
  /**
40
49
  * A record's properties, each edited in place with the control its value calls for. Properties
41
50
  * that hold nothing stay out of the way until they are asked for: one at a time through "Add
42
- * property", or all at once.
51
+ * property", or all at once. A value with parts of its own, or a list of them, is shown part by
52
+ * part under its property and can be folded away.
43
53
  */
44
54
  export function PropertyList(props) {
45
55
  useLocaleRevision();
@@ -47,14 +57,46 @@ export function PropertyList(props) {
47
57
  ...propertyLabels(),
48
58
  ...props.labels
49
59
  };
60
+ const [folds, setFolds] = useState(() => new Map());
61
+ const shared = {
62
+ labels,
63
+ records: props.records,
64
+ errors: props.errors ?? noErrors,
65
+ folds,
66
+ fold: (path, open) => setFolds(current => new Map(current).set(path, open))
67
+ };
68
+ return _jsx("div", {
69
+ className: "mdbase-properties",
70
+ children: _jsx(Rows, {
71
+ shared: shared,
72
+ parent: undefined,
73
+ values: props.values,
74
+ declared: props.declared,
75
+ inUse: props.inUse ?? noProperties,
76
+ hidden: props.hidden ?? shownAlways,
77
+ rest: undefined,
78
+ onChange: props.onChange
79
+ })
80
+ });
81
+ }
82
+ /** Named values: a record's properties at the top, a group's parts further in. */
83
+ function Rows(props) {
50
84
  const {
85
+ shared,
86
+ parent,
51
87
  values,
52
88
  declared,
89
+ inUse,
90
+ hidden,
91
+ rest,
53
92
  onChange
54
93
  } = props;
55
- const inUse = props.inUse ?? [];
56
- const hidden = props.hidden ?? shownAlways;
57
- const [adding, setAdding] = useState(false);
94
+ const {
95
+ labels
96
+ } = shared;
97
+ const [addingHere, setAddingHere] = useState(false);
98
+ const adding = parent ? Boolean(props.adding) : addingHere;
99
+ const setAdding = next => parent ? props.onAdding?.(next) : setAddingHere(next);
58
100
  const [showEmpty, setShowEmpty] = useState(readShowEmpty);
59
101
  /** Properties brought into view to be filled in. */
60
102
  const [revealed, setRevealed] = useState([]);
@@ -67,9 +109,11 @@ export function PropertyList(props) {
67
109
  for (const name of names) {
68
110
  if (!isEmptyValue(values[name])) held.current.add(name);
69
111
  }
70
- const lasting = name => held.current.has(name) || revealed.includes(name);
71
- const shown = names.filter(name => showEmpty || lasting(name));
72
- const empties = names.filter(name => !lasting(name)).length;
112
+ const must = name => declared.some(field => field.name === name && field.required);
113
+ // A part of a value is there to be seen once the value holds it; a record's own empty properties wait to be asked for.
114
+ const lasting = name => held.current.has(name) || revealed.includes(name) || must(name) || parent !== undefined && name in values;
115
+ const shown = names.filter(name => !parent && showEmpty || lasting(name));
116
+ const empties = parent ? 0 : names.filter(name => !lasting(name)).length;
73
117
  const toggleEmpty = () => {
74
118
  const next = !showEmpty;
75
119
  setShowEmpty(next);
@@ -103,115 +147,69 @@ export function PropertyList(props) {
103
147
  },
104
148
  // eslint-disable-next-line react-hooks/exhaustive-deps -- the names are compared by what they say.
105
149
  [namesKey, shownKey, inUse, declared]);
106
- return _jsxs("div", {
107
- className: "mdbase-properties",
150
+ // A group that names all its parts takes no others.
151
+ const open = rest !== false;
152
+ // A record's properties are added to from the foot of its list.
153
+ const addable = !parent && onChange !== undefined;
154
+ return _jsxs(_Fragment, {
108
155
  children: [shown.map(name => {
109
156
  const {
110
157
  info,
111
158
  suggestions,
112
159
  own
113
- } = describeProperty(name, declared, inUse);
160
+ } = describeProperty(name, declared, inUse, rest);
114
161
  const value = values[name];
115
- if (isGroup(value) && Object.keys(value).length) {
116
- // A value with parts of its own is shown part by part. Changing one is the work of the app that keeps it.
117
- return Object.entries(value).map(([part, inner]) => {
118
- const text = valueLine(inner, labels) ?? (isScalar(inner) && inner !== null ? String(inner) : undefined);
119
- return text === undefined ? null : _jsxs("div", {
120
- className: "mdbase-property",
121
- "data-field": `${name}.${part}`,
122
- "data-kind": "other",
123
- children: [_jsxs("span", {
124
- className: "mdbase-property-name",
125
- title: `${name}.${part}`,
126
- children: [_jsx(Glyph, {
127
- name: typeof inner === "number" ? "number" : "text"
128
- }), _jsxs("span", {
129
- children: [name, " ", _jsx("i", {
130
- children: part
131
- })]
132
- })]
133
- }), _jsx("div", {
134
- className: "mdbase-property-value",
135
- children: _jsx("span", {
136
- className: "mdbase-property-static",
137
- children: text
138
- })
139
- })]
140
- }, `${name}.${part}`);
141
- });
142
- }
143
- const kind = propertyKind(value, info);
144
162
  const set = !isEmptyValue(value);
145
163
  // A property the record does not have and no type asks for is only a row until it is given a value.
146
164
  const passing = !set && !own && !(name in values);
147
- return _jsxs("div", {
148
- className: "mdbase-property",
149
- "data-field": name,
150
- "data-kind": kind,
151
- "data-empty": set ? undefined : "",
152
- children: [_jsxs("span", {
153
- className: "mdbase-property-name",
154
- title: name,
155
- children: [_jsx(PropertyKindIcon, {
156
- kind: kind
157
- }), _jsx("span", {
158
- children: name
159
- })]
160
- }), _jsx("div", {
161
- className: "mdbase-property-value",
162
- children: _jsx(PropertyValue, {
163
- name: name,
164
- value: value,
165
- info: info,
166
- kind: kind,
167
- suggestions: suggestions,
168
- labels: labels,
169
- records: props.records,
170
- autoFocus: focus === name,
171
- // What is typed for a property nothing describes is read for what it most plainly is.
172
- loose: passing,
173
- onChange: onChange ? next => {
174
- written.current.add(name);
175
- onChange(name, next);
176
- } : undefined,
177
- onDone: () => {
178
- if (focus === name) setFocus(null);
179
- if (passing && !written.current.has(name)) setRevealed(current => current.filter(other => other !== name));
180
- }
181
- })
182
- }), onChange && (set || passing) && !own ? _jsx("button", {
183
- type: "button",
184
- className: "mdbase-property-clear",
185
- "aria-label": labels.removeProperty(name),
186
- title: labels.removeProperty(name),
187
- onClick: () => {
188
- held.current.delete(name);
189
- setRevealed(current => current.filter(other => other !== name));
190
- if (name in values) onChange(name, null);
191
- },
192
- children: _jsx(Glyph, {
193
- name: "close",
194
- size: 12
195
- })
196
- }) : null]
165
+ const kind = propertyKind(value, info);
166
+ // A switch cannot be emptied and a value with parts has no one control to empty it with: those come off by the row.
167
+ const unclearable = kind === "boolean" || kind === "group" || kind === "items";
168
+ const removable = onChange !== undefined && (set || passing || parent !== undefined && name in values) && (!own || !info?.required && unclearable && name in values);
169
+ return _jsx(Row, {
170
+ shared: shared,
171
+ path: parent ? `${parent.path}.${name}` : name,
172
+ name: name,
173
+ label: parent ? `${parent.label} ${name}` : name,
174
+ value: value,
175
+ info: info,
176
+ suggestions: suggestions,
177
+ focus: focus === name,
178
+ // What is typed for a property nothing describes is read for what it most plainly is.
179
+ loose: passing,
180
+ startOpen: revealed.includes(name),
181
+ onChange: onChange ? next => {
182
+ written.current.add(name);
183
+ onChange(name, next);
184
+ } : undefined,
185
+ onDone: () => {
186
+ if (focus === name) setFocus(null);
187
+ if (passing && !written.current.has(name)) setRevealed(current => current.filter(other => other !== name));
188
+ },
189
+ removeLabel: labels.removeProperty(parent ? `${parent.label} ${name}` : name),
190
+ onRemove: removable ? () => {
191
+ held.current.delete(name);
192
+ setRevealed(current => current.filter(other => other !== name));
193
+ if (name in values) onChange(name, null);
194
+ } : undefined
197
195
  }, name);
198
- }), !shown.length && !onChange && !empties ? _jsx("p", {
196
+ }), !parent && !shown.length && !onChange && !empties ? _jsx("p", {
199
197
  className: "mdbase-properties-none",
200
198
  children: labels.noProperties
201
199
  }) : null, adding && onChange ? _jsx(Combo, {
202
200
  className: "mdbase-property-add",
203
- label: labels.addProperty,
204
- placeholder: labels.findProperty,
201
+ label: parent ? labels.addPropertyTo(parent.label) : labels.addProperty,
202
+ placeholder: open ? labels.findProperty : labels.findDeclaredProperty,
205
203
  options: candidates,
206
204
  newLabel: labels.newProperty,
207
- accepts: text => isPropertyName(text) && !hidden(text),
208
- emptyMessage: labels.propertyNameInvalid,
205
+ accepts: text => open && isPropertyName(text) && !hidden(text),
206
+ emptyMessage: open ? labels.propertyNameInvalid : labels.noMatches,
209
207
  autoFocus: true,
210
208
  onCommit: name => reveal(name),
211
209
  onCancel: () => setAdding(false)
212
- }) : null, onChange && !adding || empties ? _jsxs("div", {
210
+ }) : null, addable && !adding || empties ? _jsxs("div", {
213
211
  className: "mdbase-properties-foot",
214
- children: [onChange && !adding ? _jsxs("button", {
212
+ children: [addable && !adding ? _jsxs("button", {
215
213
  type: "button",
216
214
  className: "mdbase-properties-action",
217
215
  onClick: () => setAdding(true),
@@ -231,3 +229,214 @@ export function PropertyList(props) {
231
229
  }) : null]
232
230
  });
233
231
  }
232
+ /** One value and its name: a control for a value that stands alone, or the parts of one that has them, under it. */
233
+ function Row(props) {
234
+ const {
235
+ shared,
236
+ path,
237
+ name,
238
+ label,
239
+ value,
240
+ info,
241
+ onChange
242
+ } = props;
243
+ const {
244
+ labels
245
+ } = shared;
246
+ /** A part of this value is being named: at once in a value added with no parts. */
247
+ const [adding, setAdding] = useState(() => Boolean(props.focus) && isGroup(value) && Object.keys(value).length === 0);
248
+ /** The place of a value just added to this list, which takes the keyboard. */
249
+ const [added, setAdded] = useState(null);
250
+ const kind = propertyKind(value, info);
251
+ const set = !isEmptyValue(value);
252
+ const error = shared.errors[path];
253
+ const parted = kind === "group" || kind === "items";
254
+ const list = Array.isArray(value) ? value : noValues;
255
+ const group = isGroup(value) ? value : noParts;
256
+ const count = Array.isArray(value) ? value.length : Object.keys(group).length;
257
+ const open = parted && (shared.folds.get(path) ?? (props.startOpen || valueCount(value, openAtMost + 1) <= openAtMost));
258
+ // One of several values with parts is told from the others by the first thing it says in its own words: not a choice from a few.
259
+ const chosen = part => Boolean(info?.parts?.find(other => other.name === part)?.options?.length);
260
+ const lead = props.counted ? Object.entries(group).find(([part, inner]) => typeof inner === "string" && !chosen(part) && valueLine(inner, labels) !== undefined)?.[1] : undefined;
261
+ const summary = count === 0 ? labels.empty : typeof lead === "string" ? valueLine(lead, labels) : kind === "items" ? labels.itemCount(count) : labels.partCount(count);
262
+ const parts = `mdbase-parts-${path}`;
263
+ // A value that names all its parts and holds them all has no room for another.
264
+ const roomy = kind === "items" || kind === "group" && (info?.rest !== false || (info.parts ?? noProperties).some(part => !(part.name in group)));
265
+ const add = () => {
266
+ shared.fold(path, true);
267
+ if (kind === "group") setAdding(true);else {
268
+ setAdded(list.length);
269
+ // Without a type to say, a new value is like the ones the list has.
270
+ onChange?.([...list, info?.items ? blankValue(info.items) : list.length && list.every(isGroup) ? {} : list.length && list.every(Array.isArray) ? [] : null]);
271
+ }
272
+ };
273
+ return _jsxs(_Fragment, {
274
+ children: [_jsxs("div", {
275
+ className: "mdbase-property",
276
+ "data-field": path,
277
+ "data-kind": kind,
278
+ "data-empty": set ? undefined : "",
279
+ "data-invalid": error ? "" : undefined,
280
+ children: [_jsxs("span", {
281
+ className: "mdbase-property-name",
282
+ "data-counted": props.counted ? "" : undefined,
283
+ title: info?.description ? `${name}: ${info.description}` : name,
284
+ children: [_jsx(PropertyKindIcon, {
285
+ kind: kind
286
+ }), _jsx("span", {
287
+ children: name
288
+ }), info?.required ? _jsx("abbr", {
289
+ className: "mdbase-property-required",
290
+ title: labels.required,
291
+ children: "*"
292
+ }) : null]
293
+ }), _jsx("div", {
294
+ className: "mdbase-property-value",
295
+ children: parted ?
296
+ // Nothing to open in a value that holds nothing and cannot be added to.
297
+ count === 0 && !onChange ? _jsx("span", {
298
+ className: "mdbase-property-static is-empty",
299
+ children: labels.empty
300
+ }) : _jsxs("button", {
301
+ type: "button",
302
+ className: "mdbase-property-fold",
303
+ "aria-expanded": open,
304
+ "aria-label": `${label}: ${summary}`,
305
+ onClick: () => shared.fold(path, !open),
306
+ children: [_jsx("span", {
307
+ children: summary
308
+ }), _jsx(Glyph, {
309
+ name: open ? "up" : "down",
310
+ size: 12
311
+ })]
312
+ }) : _jsx(PropertyValue, {
313
+ name: label,
314
+ value: value,
315
+ info: info,
316
+ kind: kind,
317
+ suggestions: props.suggestions,
318
+ labels: labels,
319
+ records: shared.records,
320
+ autoFocus: props.focus,
321
+ loose: props.loose,
322
+ onChange: onChange,
323
+ onDone: props.onDone
324
+ })
325
+ }), onChange && roomy ? _jsx("button", {
326
+ type: "button",
327
+ className: "mdbase-property-more",
328
+ "aria-label": kind === "items" ? labels.addItemTo(label) : labels.addPropertyTo(label),
329
+ title: kind === "items" ? labels.addItem : labels.addProperty,
330
+ onClick: add,
331
+ children: _jsx(Glyph, {
332
+ name: "plus",
333
+ size: 12
334
+ })
335
+ }) : null, props.onRemove ? _jsx("button", {
336
+ type: "button",
337
+ className: "mdbase-property-clear",
338
+ "aria-label": props.removeLabel,
339
+ title: props.removeLabel,
340
+ onClick: props.onRemove,
341
+ children: _jsx(Glyph, {
342
+ name: "close",
343
+ size: 12
344
+ })
345
+ }) : null, error ? _jsx("p", {
346
+ className: "mdbase-property-error",
347
+ role: "alert",
348
+ children: error
349
+ }) : null]
350
+ }), open && (count > 0 || kind === "group" && onChange !== undefined) ? _jsx("div", {
351
+ className: "mdbase-property-parts",
352
+ id: parts,
353
+ role: "group",
354
+ "aria-label": label,
355
+ children: kind === "items" ? _jsx(Items, {
356
+ shared: shared,
357
+ parent: {
358
+ path,
359
+ label
360
+ },
361
+ list: list,
362
+ info: info?.items,
363
+ added: added,
364
+ onAdded: setAdded,
365
+ onChange: onChange
366
+ }) : _jsx(Rows, {
367
+ shared: shared,
368
+ parent: {
369
+ path,
370
+ label
371
+ },
372
+ values: group,
373
+ declared: info?.parts ?? noProperties,
374
+ inUse: props.inUse ?? noProperties,
375
+ hidden: shownAlways,
376
+ rest: info?.rest,
377
+ adding: adding,
378
+ onAdding: setAdding,
379
+ onChange: onChange ? (part, next) => {
380
+ const {
381
+ [part]: _was,
382
+ ...others
383
+ } = group;
384
+ onChange(next === null ? others : {
385
+ ...group,
386
+ [part]: next
387
+ });
388
+ } : undefined
389
+ })
390
+ }) : null]
391
+ });
392
+ }
393
+ /** A list's values one under another, each by its place: for values that are more than short text. */
394
+ function Items(props) {
395
+ const {
396
+ shared,
397
+ parent,
398
+ list,
399
+ info,
400
+ added,
401
+ onChange
402
+ } = props;
403
+ // Values with parts and no type to name them are offered the parts the others have: their names and
404
+ // what they hold, not a choice among the few values one record happens to hold for them.
405
+ const inUse = useMemo(() => info ? noProperties : propertiesInUse(list.filter(isGroup)).map(({
406
+ options: _few,
407
+ ...part
408
+ }) => part), [info, list]);
409
+ return _jsx(_Fragment, {
410
+ children: list.map((item, index) => {
411
+ const place = String(index + 1);
412
+ return _jsx(Row
413
+ // A list has nothing but places to tell its values apart by.
414
+ // eslint-disable-next-line react/no-array-index-key
415
+ , {
416
+ shared: shared,
417
+ path: `${parent.path}.${String(index)}`,
418
+ name: place,
419
+ label: `${parent.label} ${place}`,
420
+ counted: true,
421
+ value: item,
422
+ info: info ? {
423
+ ...info,
424
+ name: place
425
+ } : undefined,
426
+ focus: added === index,
427
+ inUse: inUse,
428
+ loose: !info,
429
+ startOpen: added === index,
430
+ onChange: onChange ? next => onChange(list.map((other, at) => at === index ? next : other)) : undefined,
431
+ onDone: () => {
432
+ if (added === index) props.onAdded(null);
433
+ },
434
+ removeLabel: shared.labels.removeItem(parent.label, index + 1),
435
+ onRemove: onChange ? () => {
436
+ props.onAdded(null);
437
+ onChange(list.filter((_other, at) => at !== index));
438
+ } : undefined
439
+ }, index);
440
+ })
441
+ });
442
+ }
@@ -1,21 +1,50 @@
1
- import { type JSX } from "react";
1
+ import { type JSX, type ReactNode } from "react";
2
2
  import { type PropertyLabels } from "./labels.js";
3
3
  import { type PropertyInfo, type PropertyKind } from "./model.js";
4
+ /** A record as a link names it: its title, what kind of record it is, and a drawing for that kind. */
5
+ export interface PropertyRecord {
6
+ readonly title: string;
7
+ readonly detail?: string | undefined;
8
+ readonly icon?: ReactNode | undefined;
9
+ }
10
+ /** Where a record is being made from a property's picker, and how the link to it is written. */
11
+ export interface PropertyLinkCreation {
12
+ /** The property the link is for. */
13
+ readonly property: string;
14
+ /** Where the property is on screen; nothing once it has gone. */
15
+ anchor(): DOMRect | undefined;
16
+ /** Writes the link to the record made. */
17
+ link(path: string, title: string): void;
18
+ /** Hands the keyboard back to the property. */
19
+ restoreFocus(): void;
20
+ }
4
21
  /** What the editors need to know about the other records, for properties that link to them. */
5
22
  export interface PropertyRecords {
6
23
  /** A record's title and kind, when it is known. */
7
- describe(path: string): {
8
- readonly title: string;
9
- readonly detail?: string | undefined;
10
- } | undefined;
11
- /** Records whose title matches what was typed, best first. */
12
- search(query: string): readonly {
24
+ describe(path: string): PropertyRecord | undefined;
25
+ /**
26
+ * Records whose title matches what was typed, best first. `property` is the one being filled
27
+ * in, for a host that offers only the records its type allows there.
28
+ */
29
+ search(query: string, property?: string): readonly (PropertyRecord & {
13
30
  readonly path: string;
14
- readonly title: string;
15
- readonly detail?: string | undefined;
16
- }[];
31
+ })[];
17
32
  /** Show the record. Without it a link is only named. */
18
33
  readonly open?: ((path: string) => void) | undefined;
34
+ /**
35
+ * The record a link's text leads to, for a host that asks the collection rather than reading
36
+ * the text: undefined until it is known, null when the link leads nowhere.
37
+ */
38
+ readonly resolve?: ((link: string) => string | null | undefined) | undefined;
39
+ /** The text to write for a link to a record. Without it the link is a wikilink. */
40
+ readonly link?: ((path: string, title: string, property: string) => string) | undefined;
41
+ /** The kinds of record that can be made from a property's picker, such as one for each type it may link to. */
42
+ readonly creates?: ((property: string) => readonly {
43
+ readonly id: string;
44
+ readonly label: string;
45
+ }[]) | undefined;
46
+ /** Makes a record of a kind `creates` offered, titled as typed, and links it through `creation.link`. */
47
+ readonly create?: ((kind: string, title: string, creation: PropertyLinkCreation) => void) | undefined;
19
48
  }
20
49
  export interface PropertyValueProps {
21
50
  /** The property's name, which also names its control. */