shelving 1.295.0 → 1.295.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.295.0",
3
+ "version": "1.295.2",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -40,11 +40,12 @@ function _closeOnBackdropClick({ currentTarget, target }) {
40
40
  if (target instanceof Element && target.closest("a:any-link, nav button:enabled"))
41
41
  _requestClose(currentTarget);
42
42
  }
43
- /** Ask a `<dialog>` to close. `requestClose()` fires a cancelable `cancel` event, so `<Dialog>` can run `onClose` in a transition. Older browsers close at once. */
43
+ /**
44
+ * Ask a `<dialog>` to close: fire a cancelable `cancel` event, so `<Dialog>` can run `onClose` in a transition. Close it at once only when nothing cancels the event.
45
+ * - Not `requestClose()`: some browsers lack it, and Safari fires its `cancel` event as not cancelable. Both close the dialog at once, so it vanishes with no fade.
46
+ */
44
47
  function _requestClose(dialog) {
45
- if (typeof dialog.requestClose === "function")
46
- dialog.requestClose();
47
- else
48
+ if (dialog.dispatchEvent(new Event("cancel", { cancelable: true })))
48
49
  dialog.close();
49
50
  }
50
51
  /**
@@ -74,10 +74,12 @@ function _closeOnBackdropClick({ currentTarget, target }: MouseEvent<HTMLDialogE
74
74
  if (target instanceof Element && target.closest("a:any-link, nav button:enabled")) _requestClose(currentTarget);
75
75
  }
76
76
 
77
- /** Ask a `<dialog>` to close. `requestClose()` fires a cancelable `cancel` event, so `<Dialog>` can run `onClose` in a transition. Older browsers close at once. */
77
+ /**
78
+ * Ask a `<dialog>` to close: fire a cancelable `cancel` event, so `<Dialog>` can run `onClose` in a transition. Close it at once only when nothing cancels the event.
79
+ * - Not `requestClose()`: some browsers lack it, and Safari fires its `cancel` event as not cancelable. Both close the dialog at once, so it vanishes with no fade.
80
+ */
78
81
  function _requestClose(dialog: HTMLDialogElement): void {
79
- if (typeof dialog.requestClose === "function") dialog.requestClose();
80
- else dialog.close();
82
+ if (dialog.dispatchEvent(new Event("cancel", { cancelable: true }))) dialog.close();
81
83
  }
82
84
 
83
85
  /**
@@ -23,5 +23,5 @@ required = false, disabled = false, message = "", value, onValue, min, max, inpu
23
23
  onValue?.(e.currentTarget.value ?? undefined);
24
24
  };
25
25
  const dateToString = _DATE_TO_STRING[input];
26
- return (_jsx("input", { name: name, type: input, min: dateToString(min), max: dateToString(max), disabled: disabled, required: required, defaultValue: dateToString(value), placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onChange: onChange, onInput: onChange, title: message, "aria-invalid": !!message, step: step }));
26
+ return (_jsx("input", { name: name, type: input, min: dateToString(min), max: dateToString(max), disabled: disabled, required: required, defaultValue: dateToString(value), placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onChange: onChange, title: message, "aria-invalid": !!message, step: step }));
27
27
  }
@@ -66,7 +66,6 @@ export function DateInput({
66
66
  placeholder={placeholder || " "}
67
67
  className={getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className)}
68
68
  onChange={onChange}
69
- onInput={onChange}
70
69
  title={message}
71
70
  aria-invalid={!!message}
72
71
  step={step}
@@ -18,5 +18,5 @@ onValue, types = {}, className, ...variants }) {
18
18
  for (const file of files)
19
19
  onValue(file);
20
20
  };
21
- return (_jsx("input", { name: name, type: "file", accept: Object.values(types).join(","), required: required, disabled: disabled, placeholder: placeholder, className: getClass(getInputClass(variants), className), onChange: onChange, onInput: onChange, multiple: false, title: message, "aria-invalid": !!message }));
21
+ return (_jsx("input", { name: name, type: "file", accept: Object.values(types).join(","), required: required, disabled: disabled, placeholder: placeholder, className: getClass(getInputClass(variants), className), onChange: onChange, multiple: false, title: message, "aria-invalid": !!message }));
22
22
  }
@@ -48,7 +48,6 @@ export function FileInput({
48
48
  placeholder={placeholder}
49
49
  className={getClass(getInputClass(variants), className)}
50
50
  onChange={onChange}
51
- onInput={onChange}
52
51
  multiple={false}
53
52
  title={message}
54
53
  aria-invalid={!!message}
@@ -0,0 +1,38 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { ReactElement } from "react";
3
+ import { DateInput, FileInput, NumberInput, TextInput } from "shelving/ui";
4
+ import { PASSTHROUGH } from "shelving/util/function";
5
+
6
+ /** The props of the rendered element that the tests read. */
7
+ interface _ElementProps {
8
+ readonly onInput?: unknown;
9
+ readonly onChange?: unknown;
10
+ }
11
+
12
+ describe("value inputs", () => {
13
+ // React's `onChange` already fires for each native `input` event, so a second `onInput` handler would call `onValue()` twice.
14
+ const elements: [string, ReactElement][] = [
15
+ ["TextInput", TextInput({ name: "x", onValue: PASSTHROUGH })],
16
+ ["TextInput (multiline)", TextInput({ name: "x", rows: 3, onValue: PASSTHROUGH })],
17
+ ["NumberInput", NumberInput({ name: "x", onValue: PASSTHROUGH })],
18
+ ["DateInput", DateInput({ name: "x", onValue: PASSTHROUGH })],
19
+ ["FileInput", FileInput({ name: "x", onValue: PASSTHROUGH })],
20
+ ];
21
+ for (const [label, element] of elements) {
22
+ test(`${label} listens to change only, so it calls onValue once per edit`, () => {
23
+ const props = element.props as _ElementProps;
24
+ expect(props.onChange).toBeFunction();
25
+ expect(props.onInput).toBeUndefined();
26
+ });
27
+ }
28
+ });
29
+
30
+ describe("FileInput", () => {
31
+ test("calls onValue once for the picked file", () => {
32
+ const values: (string | File | undefined)[] = [];
33
+ const file = new File(["abc"], "a.txt");
34
+ const element = FileInput({ name: "x", onValue: v => void values.push(v) });
35
+ (element.props as { onChange(e: { currentTarget: { files: File[] } }): void }).onChange({ currentTarget: { files: [file] } });
36
+ expect(values).toEqual([file]);
37
+ });
38
+ });
@@ -24,5 +24,5 @@ formatter = formatNumber, className, ...variants }) {
24
24
  const num = getNumber(currentTarget.value);
25
25
  currentTarget.value = num !== undefined ? formatter(num) : "";
26
26
  };
27
- return (_jsx("input", { name: name, type: "text", inputMode: "decimal", defaultValue: value !== undefined ? formatter(value) : "", required: required, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onChange: onChange, onInput: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
27
+ return (_jsx("input", { name: name, type: "text", inputMode: "decimal", defaultValue: value !== undefined ? formatter(value) : "", required: required, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
28
28
  }
@@ -62,7 +62,6 @@ export function NumberInput({
62
62
  placeholder={placeholder || " "}
63
63
  className={getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className)}
64
64
  onChange={onChange}
65
- onInput={onChange}
66
65
  onBlur={onBlur}
67
66
  title={message}
68
67
  aria-invalid={!!message}
@@ -25,7 +25,7 @@ function _getInputProps(schema: StringSchema, onValue: (value: string | undefine
25
25
  interface _InputProps {
26
26
  readonly type?: string;
27
27
  readonly defaultValue?: string;
28
- onInput(e: { currentTarget: { value: string } }): void;
28
+ onChange(e: { currentTarget: { value: string } }): void;
29
29
  onBlur(e: { currentTarget: { value: string } }): void;
30
30
  }
31
31
 
@@ -58,7 +58,7 @@ describe("StringSchemaInput", () => {
58
58
  // `format()` is for display only — the store must receive a value that re-validates.
59
59
  const values: (string | undefined)[] = [];
60
60
  const input = _getInputProps(new BracketSchema({}), v => void values.push(v));
61
- input.onInput({ currentTarget: { value: " abc " } });
61
+ input.onChange({ currentTarget: { value: " abc " } });
62
62
 
63
63
  expect(values).toEqual(["abc"]);
64
64
  });
@@ -77,7 +77,7 @@ describe("StringSchemaInput", () => {
77
77
  // A `PasswordSchema` field must submit the typed password; masking is the `type="password"` input's job.
78
78
  const values: (string | undefined)[] = [];
79
79
  const input = _getInputProps(PASSWORD, v => void values.push(v), "hunter2");
80
- input.onInput({ currentTarget: { value: "hunter22" } });
80
+ input.onChange({ currentTarget: { value: "hunter22" } });
81
81
  const currentTarget = { value: "hunter22" };
82
82
  input.onBlur({ currentTarget });
83
83
 
@@ -91,7 +91,7 @@ describe("StringSchemaInput", () => {
91
91
  // `URLSchema.format()` strips the scheme for display; publishing that form would fail re-validation.
92
92
  const values: (string | undefined)[] = [];
93
93
  const input = _getInputProps(URL_SCHEMA, v => void values.push(v));
94
- input.onInput({ currentTarget: { value: " https://example.com/path " } });
94
+ input.onChange({ currentTarget: { value: " https://example.com/path " } });
95
95
  const currentTarget = { value: "https://example.com/path" };
96
96
  input.onBlur({ currentTarget });
97
97
 
@@ -22,12 +22,12 @@ required = false, disabled = false, message = "", value, onValue, input = "text"
22
22
  onValue?.(e.currentTarget.value);
23
23
  _resize(e.currentTarget, rows);
24
24
  };
25
- return (_jsx("textarea", { ref: el => void (el && _resize(el, rows)), name: name, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, rows: rows, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), getModuleClass(INPUT_CSS, "multiline"), className), onInput: onChange, onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
25
+ return (_jsx("textarea", { ref: el => void (el && _resize(el, rows)), name: name, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, rows: rows, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), getModuleClass(INPUT_CSS, "multiline"), className), onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
26
26
  }
27
27
  const onChange = (e) => {
28
28
  onValue?.(e.currentTarget.value);
29
29
  };
30
- return (_jsx("input", { name: name, type: input, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onInput: onChange, onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
30
+ return (_jsx("input", { name: name, type: input, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className), onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
31
31
  }
32
32
  /**
33
33
  * @todo This can be removed when Firefox supports `field-sizing:` in CSS.
@@ -70,7 +70,6 @@ export function TextInput({
70
70
  disabled={disabled}
71
71
  placeholder={placeholder || " "}
72
72
  className={getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), getModuleClass(INPUT_CSS, "multiline"), className)}
73
- onInput={onChange}
74
73
  onChange={onChange}
75
74
  onBlur={onBlur}
76
75
  title={message}
@@ -94,7 +93,6 @@ export function TextInput({
94
93
  disabled={disabled}
95
94
  placeholder={placeholder || " "}
96
95
  className={getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), className)}
97
- onInput={onChange}
98
96
  onChange={onChange}
99
97
  onBlur={onBlur}
100
98
  title={message}