@mccaskie-labs/workbench-ui 0.3.0 → 0.3.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @mccaskie-labs/workbench-ui
2
2
 
3
+ ## 0.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - renderForm draws a form's labels, controls, hints, messages, summary and config block from its definition in one stacked layout, matching FORM_SCRIPT, so apps no longer write form markup by hand. The generator adds the layout's words (ui.form.optional, ui.form.choose, ui.form.none). workbench-ui: a visible "none of these" box counts only when ticked, and STYLE styles the layout's classes.
8
+
9
+ ## 0.3.1
10
+
11
+ ### Patch Changes
12
+
13
+ - workbench-forms: `show: { when, is }` on any field shows it only while an earlier field holds one of some values. A field that isn't shown is neither checked nor stored (null), and `validate` returns it in the new `hidden` list. The kit's own form gains a conditional field, and tests 2, 4, 6 and 7 cover it. workbench-ui: `FORM_SCRIPT` hides a field's `data-wb-field` wrapper and disables its controls while its condition doesn't hold, updating as the person types.
14
+
3
15
  ## 0.3.0
4
16
 
5
17
  - `FORM_SCRIPT`: the screen behaviour for forms checked by `workbench-forms` (Robert's form-validation standard §7 and §8): a problem shows when the person leaves the field and clears as they fix it, a failed submit focuses the first bad field and announces the count, an optional summary links each problem to its field, a date, amount or phone number that was read differently is shown back before it is stored, and reset clears everything. Styles for field errors, the summary, counters, text areas and the input width tokens (`wb-w-xs` to `wb-w-full`).
@@ -15,6 +15,10 @@
15
15
  * - When a date, an amount or a phone number was read as something other than
16
16
  * what was typed, the first submit writes it back and stops, so the person
17
17
  * sees it before it is stored.
18
+ * - A field with a \`show\` condition is hidden while the condition doesn't
19
+ * hold: its wrapper (the element with \`data-wb-field="<name>"\`, or the
20
+ * control itself) gets \`hidden\`, its controls are disabled so they aren't
21
+ * sent, and its message clears. This updates as the person types.
18
22
  * - Reset clears every message, the summary and the counters.
19
23
  *
20
24
  * Messages come only from the config (already in the visitor's language) and
@@ -15,6 +15,10 @@
15
15
  * - When a date, an amount or a phone number was read as something other than
16
16
  * what was typed, the first submit writes it back and stops, so the person
17
17
  * sees it before it is stored.
18
+ * - A field with a \`show\` condition is hidden while the condition doesn't
19
+ * hold: its wrapper (the element with \`data-wb-field="<name>"\`, or the
20
+ * control itself) gets \`hidden\`, its controls are disabled so they aren't
21
+ * sent, and its message clears. This updates as the person types.
18
22
  * - Reset clears every message, the summary and the counters.
19
23
  *
20
24
  * Messages come only from the config (already in the visitor's language) and
@@ -55,7 +59,9 @@ export const FORM_SCRIPT = `(() => {
55
59
  else if (el.checked) picked.push(el.value);
56
60
  }
57
61
  if (picked.length) return picked;
58
- return form.elements.namedItem(n + "__none") ? [] : null;
62
+ // "None of these" is a ticked box (renderForm) or a hidden input the page always sends.
63
+ const none = form.elements.namedItem(n + "__none");
64
+ return none && (none.type !== "checkbox" || none.checked) ? [] : null;
59
65
  }
60
66
  const radios = els.filter((el) => el.type === "radio");
61
67
  if (radios.length) { const on = radios.find((el) => el.checked); return on ? on.value : undefined; }
@@ -102,6 +108,8 @@ export const FORM_SCRIPT = `(() => {
102
108
  if (el) el.textContent = "";
103
109
  for (const c of controls(n)) c.removeAttribute("aria-invalid");
104
110
  };
111
+ // A "none of these" box belongs to its field: its events are the field's.
112
+ const field = (n) => (n && n.endsWith("__none") ? n.slice(0, -6) : n);
105
113
  const showing = (n) => controls(n).some((c) => c.getAttribute("aria-invalid") === "true");
106
114
  const check = () => {
107
115
  const input = {};
@@ -111,6 +119,8 @@ export const FORM_SCRIPT = `(() => {
111
119
  let live = form.querySelector(".wb-live");
112
120
  if (!live) { live = document.createElement("p"); live.className = "wb-live"; live.setAttribute("role", "status"); live.setAttribute("aria-live", "polite"); form.prepend(live); }
113
121
  const summary = form.hasAttribute("data-wb-summary") ? (form.querySelector(".wb-summary") || form.insertBefore(Object.assign(document.createElement("div"), { className: "wb-summary", tabIndex: -1 }), form.firstChild)) : null;
122
+ // A summary the server drew after a failed submit takes focus, so it is read out once.
123
+ if (summary && !summary.hidden && summary.textContent.trim()) summary.focus();
114
124
  const say = (key, count) => { const text = Object.prototype.hasOwnProperty.call(messages, key) ? fill(messages[key], { count }) : ""; live.textContent = ""; setTimeout(() => { live.textContent = text; }, 50); return text; };
115
125
  const counters = () => {
116
126
  for (const el of form.querySelectorAll("[data-wb-max]")) {
@@ -124,6 +134,28 @@ export const FORM_SCRIPT = `(() => {
124
134
  c.classList.toggle("over", n > Number(el.getAttribute("data-wb-max")));
125
135
  }
126
136
  };
137
+ const wrapper = (n) => form.querySelector('[data-wb-field="' + n + '"]');
138
+ const conditional = names.filter((n) => fields[n].show);
139
+ const visibility = (r) => {
140
+ if (!r || !Array.isArray(r.hidden)) return;
141
+ for (const n of conditional) {
142
+ const off = r.hidden.includes(n);
143
+ const w = wrapper(n);
144
+ if (w) w.hidden = off;
145
+ for (const c of controls(n)) {
146
+ // Only undo what this script did: a control the app rendered disabled stays disabled.
147
+ if (off && !c.disabled) { c.disabled = true; c.setAttribute("data-wb-hid", ""); }
148
+ else if (!off && c.hasAttribute("data-wb-hid")) { c.disabled = false; c.removeAttribute("data-wb-hid"); }
149
+ if (!w) c.hidden = off;
150
+ }
151
+ if (off) clear(n);
152
+ }
153
+ };
154
+ if (conditional.length) {
155
+ visibility(check());
156
+ // The browser may restore field values after this ran (back, forward, reload) without an input event.
157
+ addEventListener("pageshow", () => visibility(check()));
158
+ }
127
159
  counters();
128
160
  // Pressing a submit button leaves the field too, but the submit shows every problem itself: a message
129
161
  // appearing on the way would move the button out from under the pointer, and the press would be lost.
@@ -134,13 +166,14 @@ export const FORM_SCRIPT = `(() => {
134
166
  if (isSubmit(b)) { pressing = true; setTimeout(() => { pressing = false; }, 1000); }
135
167
  });
136
168
  form.addEventListener("focusout", (e) => {
137
- const n = e.target && e.target.name;
169
+ const n = field(e.target && e.target.name);
138
170
  if (!n || !names.includes(n)) return;
139
171
  // Leaving one box of a group for another of the same field isn't leaving the field.
140
- if (e.relatedTarget && e.relatedTarget.name === n) return;
172
+ if (e.relatedTarget && field(e.relatedTarget.name) === n) return;
141
173
  if (pressing || isSubmit(e.relatedTarget)) return;
142
174
  const r = check();
143
175
  if (!r) return;
176
+ visibility(r);
144
177
  const p = r.problems.find((x) => x.field === n);
145
178
  if (p) show(p);
146
179
  else { clear(n); writeBack(n, r.shown[n]); counters(); }
@@ -148,11 +181,14 @@ export const FORM_SCRIPT = `(() => {
148
181
  const edited = (e) => {
149
182
  held = false;
150
183
  counters();
151
- const n = e.target && e.target.name;
184
+ const n = field(e.target && e.target.name);
152
185
  if (!n || !names.includes(n)) return;
186
+ // A condition may name this field: show or hide the fields that depend on it.
187
+ const r0 = conditional.length ? check() : null;
188
+ if (r0) visibility(r0);
153
189
  // Keystrokes only ever turn messages off: this field's, and any a condition on it no longer asks for.
154
190
  if (!names.some(showing)) return;
155
- const r = check();
191
+ const r = r0 || check();
156
192
  if (!r) return;
157
193
  for (const m of names) if (showing(m) && !r.problems.some((x) => x.field === m)) clear(m);
158
194
  };
@@ -162,6 +198,7 @@ export const FORM_SCRIPT = `(() => {
162
198
  pressing = false;
163
199
  const r = check();
164
200
  if (!r) return;
201
+ visibility(r);
165
202
  for (const n of names) if (!r.problems.some((x) => x.field === n)) writeBack(n, r.shown[n]);
166
203
  counters();
167
204
  if (r.problems.length) {
@@ -204,7 +241,7 @@ export const FORM_SCRIPT = `(() => {
204
241
  for (const n of names) clear(n);
205
242
  if (summary) { summary.textContent = ""; summary.hidden = true; }
206
243
  live.textContent = "";
207
- setTimeout(counters, 0);
244
+ setTimeout(() => { counters(); if (conditional.length) visibility(check()); }, 0);
208
245
  });
209
246
  }
210
247
  })();`;
package/dist/style.js CHANGED
@@ -166,6 +166,9 @@ input:not([type=hidden]):not([type=checkbox]):not([type=radio]){min-height:2.75r
166
166
  .wb-summary{margin:0 0 1rem;padding:.6rem .75rem;border-left:3px solid var(--fail);background:var(--fail-bg);border-radius:6px}
167
167
  .wb-summary[hidden],.wb-summary:empty{display:none}
168
168
  .wb-summary p{margin:0 0 .3rem;font-weight:600}.wb-summary ul{margin:0;padding-left:1.1rem}
169
+ .wb-field{margin:0 0 1rem}.wb-field>label,.wb-field legend{display:block;font-weight:600;margin:0 0 .3rem}
170
+ .wb-field fieldset{border:0;margin:0;padding:0;min-width:0}.wb-option{display:flex;gap:.5rem;align-items:center;min-height:2.75rem;font-weight:400}
171
+ .wb-optional{font-weight:400;color:var(--muted)}.wb-hint{margin:0 0 .35rem;color:var(--muted);font-size:.875rem}
169
172
  .wb-live{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
170
173
  @media (max-width:640px){nav.links{margin-left:0}th,td{padding:.55rem .7rem}table.kv th{width:auto}}
171
174
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-ui",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Workbench pages: one dark look, a top bar with the environment tag, cards, outcome pills, and one format for times, money and durations.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",