@newtone-dev/ui-react-web 0.48.0 → 0.50.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.
@@ -0,0 +1,138 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useId, useRef, useState } from 'react';
4
+ import { Icon } from '../icon/index.js';
5
+ import { periodExactness, readPeriod, stepPeriod, writePeriod, } from './period.js';
6
+ /**
7
+ * A date as exact as it is known: a day, a month, a quarter or a year, typed as words (`2028`, `Q1 2027`, `Dec 2026`,
8
+ * `Dec 5, 2026`) and kept as it is typed. Blueprint components/when-field.md; contracts/when-field.md.
9
+ */
10
+ export function WhenField({ label, value, onChange, onCommit, exactness, invalidLabel, placeholder, disabled, className, ...rest }) {
11
+ const shown = (v) => (v === null ? '' : writePeriod(v));
12
+ const read = (raw) => readPeriod(raw, exactness);
13
+ // What the box shows: its value as words; what is being typed, as it is typed; or words left that do not read as
14
+ // a date, kept and marked.
15
+ const [mode, setMode] = useState('value');
16
+ const [text, setText] = useState('');
17
+ // A value from outside takes the place of words that did not read. While the person types, theirs stay.
18
+ const [seen, setSeen] = useState(value);
19
+ if (seen !== value) {
20
+ setSeen(value);
21
+ if (mode === 'unread')
22
+ setMode('value');
23
+ }
24
+ // What this change has told so far, for `onCommit`, and so the same reading is never told twice in a row; and the
25
+ // value it started from, given back where the change ends on words that do not read.
26
+ const told = useRef(null);
27
+ // Whether the box holds the focus: words that arrive without it (a script's fill, a drop) are never left, so they
28
+ // settle as they arrive.
29
+ const here = useRef(false);
30
+ // Escape is leaving the field: the blur it causes settles nothing.
31
+ const dropping = useRef(false);
32
+ const markId = useId();
33
+ const unread = mode === 'unread';
34
+ const tell = (next) => {
35
+ // Never what it already is: the value, or what this change last told where the consumer has not followed yet.
36
+ if (next === (told.current ? told.current.value : value))
37
+ return;
38
+ told.current = { value: next, from: told.current ? told.current.from : value };
39
+ onChange?.(next);
40
+ };
41
+ // A change ended: said once, with what it ended on.
42
+ const commit = () => {
43
+ const last = told.current;
44
+ told.current = null;
45
+ if (last)
46
+ onCommit?.(last.value);
47
+ };
48
+ // A change that ends on words that do not read changes nothing: a date told on the way (2027, typing 2027-Q1) is
49
+ // taken back, the value it started from told again, and nothing is ended. The field takes that value as its own,
50
+ // so it does not take the place of the marked words.
51
+ const giveBack = () => {
52
+ const last = told.current;
53
+ told.current = null;
54
+ if (!last || last.value === last.from)
55
+ return;
56
+ setSeen(last.from);
57
+ onChange?.(last.from);
58
+ };
59
+ // How words left in the box stand: read (or none), the box shows its value again, so what was typed is tidied
60
+ // (dec 2026 → Dec 2026); not read, they stay as they are, marked.
61
+ const left = (raw) => (raw.trim() !== '' && read(raw) === null ? 'unread' : 'value');
62
+ // Words settled: read, they end the change; not read, they are marked and change nothing.
63
+ const end = (raw) => {
64
+ if (left(raw) === 'unread') {
65
+ setMode('unread');
66
+ giveBack();
67
+ }
68
+ else {
69
+ setMode('value');
70
+ commit();
71
+ }
72
+ };
73
+ // Left, or Enter. Only what was typed is judged: a value is shown as it is.
74
+ const settle = (raw) => {
75
+ if (dropping.current)
76
+ return;
77
+ if (mode === 'typing')
78
+ end(raw);
79
+ else
80
+ commit();
81
+ };
82
+ const keys = (e) => {
83
+ if (e.key === 'Enter') {
84
+ settle(e.currentTarget.value);
85
+ // Once the tidied words are in the box, they are selected, to type over.
86
+ const box = e.currentTarget;
87
+ requestAnimationFrame(() => box.select());
88
+ }
89
+ else if (e.key === 'Escape') {
90
+ // Words that were not kept go; a date already told stays the value.
91
+ setMode('value');
92
+ commit();
93
+ dropping.current = true;
94
+ e.currentTarget.blur();
95
+ dropping.current = false;
96
+ }
97
+ else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
98
+ // One of its own unit, as NumberField steps a number (Shift: ten). With no date there is no unit to step,
99
+ // and a value of an exactness this field does not take is no date here.
100
+ e.preventDefault();
101
+ const kept = value === null ? null : periodExactness(value);
102
+ const from = read(e.currentTarget.value) ??
103
+ (kept !== null && (!exactness || exactness.includes(kept)) ? value : null);
104
+ if (from === null)
105
+ return;
106
+ setMode('value');
107
+ tell(stepPeriod(from, (e.key === 'ArrowUp' ? 1 : -1) * (e.shiftKey ? 10 : 1)));
108
+ commit();
109
+ }
110
+ };
111
+ return (_jsxs("div", { className: ['newtone-when-field', className].filter(Boolean).join(' '), "data-disabled": disabled ? '' : undefined, "data-invalid": unread ? '' : undefined, ...rest, children: [_jsx("input", { className: "newtone-when-field-input", type: "text", "aria-label": label, "aria-invalid": unread || undefined, "aria-describedby": unread ? markId : undefined, autoComplete: "off", spellCheck: false, enterKeyHint: "done", value: mode === 'value' ? shown(value) : text, placeholder: placeholder, disabled: disabled, onChange: (e) => {
112
+ const box = e.currentTarget;
113
+ const raw = box.value;
114
+ setText(raw);
115
+ // Kept as it is typed: every text that reads as a date is told at once, and an empty one clears it.
116
+ if (raw.trim() === '')
117
+ tell(null);
118
+ else {
119
+ const period = read(raw);
120
+ if (period !== null)
121
+ tell(period);
122
+ }
123
+ // Typed into, the words are the person's until the box is left. Arriving without the focus, they
124
+ // are settled as they arrive: nobody will leave the box to settle them.
125
+ if (here.current || box.ownerDocument.activeElement === box)
126
+ setMode('typing');
127
+ else
128
+ end(raw);
129
+ }, onBlur: (e) => {
130
+ here.current = false;
131
+ settle(e.currentTarget.value);
132
+ }, onKeyDown: keys, onFocus: (e) => {
133
+ here.current = true;
134
+ const box = e.currentTarget;
135
+ requestAnimationFrame(() => box.select());
136
+ } }), unread ? (_jsx("span", { id: markId, className: "newtone-when-field-mark", role: "img", "aria-label": invalidLabel, title: invalidLabel, children: _jsx(Icon, { name: "error", size: "sm" }) })) : null] }));
137
+ }
138
+ //# sourceMappingURL=when-field.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"when-field.js","sourceRoot":"","sources":["../../src/when-field/when-field.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,Q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IAAI,CAAA;oBACnB,MAAM,GAAG,GAAG,CAAC,CAAC,aAAa,CAAA;oBAC3B,qBAAqB,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAA;gBAC7C,CAAC,GACH,EACD,MAAM,CAAC,CAAC,CAAC,CACN,eACI,EAAE,EAAE,MAAM,EACV,SAAS,EAAC,yBAAyB,EACnC,IAAI,EAAC,KAAK,gBACE,YAAY,EACxB,KAAK,EAAE,YAAY,YAEnB,KAAC,IAAI,IAAC,IAAI,EAAC,OAAO,EAAC,IAAI,EAAC,IAAI,GAAG,GAC5B,CACV,CAAC,CAAC,CAAC,IAAI,IACN,CACT,CAAA;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newtone-dev/ui-react-web",
3
- "version": "0.48.0",
3
+ "version": "0.50.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -422,6 +422,14 @@
422
422
  "types": "./dist/file-grid/index.d.ts",
423
423
  "default": "./dist/file-grid/index.js"
424
424
  },
425
+ "./when-field": {
426
+ "types": "./dist/when-field/index.d.ts",
427
+ "default": "./dist/when-field/index.js"
428
+ },
429
+ "./outline": {
430
+ "types": "./dist/outline/index.d.ts",
431
+ "default": "./dist/outline/index.js"
432
+ },
425
433
  "./crumbs": {
426
434
  "types": "./dist/crumbs/index.d.ts",
427
435
  "default": "./dist/crumbs/index.js"
@@ -560,6 +568,8 @@
560
568
  "./node-map.css": "./dist/node-map/node-map.css",
561
569
  "./diagram.css": "./dist/diagram/diagram.css",
562
570
  "./file-grid.css": "./dist/file-grid/file-grid.css",
571
+ "./when-field.css": "./dist/when-field/when-field.css",
572
+ "./outline.css": "./dist/outline/outline.css",
563
573
  "./crumbs.css": "./dist/crumbs/crumbs.css",
564
574
  "./drop-zone.css": "./dist/drop-zone/drop-zone.css",
565
575
  "./file-view.css": "./dist/file-view/file-view.css",