menhir 0.1.11 → 0.1.13
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/README.md +1 -1
- package/dist/checkbox/root/CheckboxRoot.js +10 -61
- package/dist/core/hiddenInput.js +94 -0
- package/dist/core/rovingFocus.js +1 -1
- package/dist/number-field/decrement/NumberFieldDecrement.js +11 -0
- package/dist/number-field/increment/NumberFieldIncrement.js +11 -0
- package/dist/number-field/index.js +2 -0
- package/dist/number-field/index.parts.js +5 -0
- package/dist/number-field/input/NumberFieldInput.js +87 -0
- package/dist/number-field/parse.js +28 -0
- package/dist/number-field/root/NumberFieldRoot.js +169 -0
- package/dist/number-field/root/NumberFieldRootContext.js +10 -0
- package/dist/number-field/stepButton.js +60 -0
- package/dist/radio/index.js +2 -0
- package/dist/radio/index.parts.js +4 -0
- package/dist/radio/indicator/RadioIndicator.js +20 -0
- package/dist/radio/item/RadioItem.js +94 -0
- package/dist/radio/item/RadioItemContext.js +10 -0
- package/dist/radio/root/RadioRoot.js +96 -0
- package/dist/radio/root/RadioRootContext.js +10 -0
- package/dist/source/checkbox/root/CheckboxRoot.jsx +5 -57
- package/dist/source/core/hiddenInput.d.ts +46 -0
- package/dist/source/core/hiddenInput.jsx +93 -0
- package/dist/source/core/rovingFocus.d.ts +1 -1
- package/dist/source/core/rovingFocus.js +1 -1
- package/dist/source/number-field/decrement/NumberFieldDecrement.d.ts +8 -0
- package/dist/source/number-field/decrement/NumberFieldDecrement.jsx +9 -0
- package/dist/source/number-field/increment/NumberFieldIncrement.d.ts +8 -0
- package/dist/source/number-field/increment/NumberFieldIncrement.jsx +9 -0
- package/dist/source/number-field/index.d.ts +1 -0
- package/dist/source/number-field/index.js +1 -0
- package/dist/source/number-field/index.parts.d.ts +4 -0
- package/dist/source/number-field/index.parts.js +4 -0
- package/dist/source/number-field/input/NumberFieldInput.d.ts +14 -0
- package/dist/source/number-field/input/NumberFieldInput.jsx +103 -0
- package/dist/source/number-field/parse.d.ts +11 -0
- package/dist/source/number-field/parse.js +46 -0
- package/dist/source/number-field/root/NumberFieldRoot.d.ts +49 -0
- package/dist/source/number-field/root/NumberFieldRoot.jsx +175 -0
- package/dist/source/number-field/root/NumberFieldRootContext.d.ts +35 -0
- package/dist/source/number-field/root/NumberFieldRootContext.js +13 -0
- package/dist/source/number-field/stepButton.d.ts +12 -0
- package/dist/source/number-field/stepButton.jsx +69 -0
- package/dist/source/radio/index.d.ts +1 -0
- package/dist/source/radio/index.js +1 -0
- package/dist/source/radio/index.parts.d.ts +3 -0
- package/dist/source/radio/index.parts.js +3 -0
- package/dist/source/radio/indicator/RadioIndicator.d.ts +10 -0
- package/dist/source/radio/indicator/RadioIndicator.jsx +12 -0
- package/dist/source/radio/item/RadioItem.d.ts +14 -0
- package/dist/source/radio/item/RadioItem.jsx +96 -0
- package/dist/source/radio/item/RadioItemContext.d.ts +12 -0
- package/dist/source/radio/item/RadioItemContext.js +12 -0
- package/dist/source/radio/root/RadioRoot.d.ts +28 -0
- package/dist/source/radio/root/RadioRoot.jsx +112 -0
- package/dist/source/radio/root/RadioRootContext.d.ts +40 -0
- package/dist/source/radio/root/RadioRootContext.js +13 -0
- package/dist/source/switch/root/SwitchRoot.jsx +5 -57
- package/dist/switch/root/SwitchRoot.js +10 -61
- package/package.json +11 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ For everything else, go to the docs:
|
|
|
62
62
|
|
|
63
63
|
Early 0.x versions: the API may change between minor versions. The form components come next; the plan and roadmap are in the [repository](https://github.com/j13n/menhir). Built so far:
|
|
64
64
|
|
|
65
|
-
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Popover, Progress, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
|
|
65
|
+
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Number Field, Popover, Progress, Radio, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
|
|
66
66
|
- Utilities: color scheme and theme, each with local-storage and cookie storage.
|
|
67
67
|
|
|
68
68
|
## Licence
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { warnUnlessButton } from "../../core/button.js";
|
|
2
2
|
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
3
|
+
import { createHiddenInput } from "../../core/hiddenInput.js";
|
|
3
4
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
5
|
import { renderElement } from "../../core/renderElement.js";
|
|
5
6
|
import { CheckboxRootContext } from "./CheckboxRootContext.js";
|
|
6
|
-
import { createComponent,
|
|
7
|
-
import { merge, omit
|
|
7
|
+
import { createComponent, isDev, memo, spread, template } from "@solidjs/web";
|
|
8
|
+
import { merge, omit } from "solid-js";
|
|
8
9
|
// src/checkbox/root/CheckboxRoot.tsx
|
|
9
|
-
var _tmpl$ = /* @__PURE__ */ template(`<
|
|
10
|
-
var _tmpl$2 = /* @__PURE__ */ template(`<button>`);
|
|
10
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
11
11
|
/**
|
|
12
12
|
* The checkbox itself: a `<button role="checkbox">`.
|
|
13
13
|
*/
|
|
@@ -57,7 +57,7 @@ function CheckboxRoot(props) {
|
|
|
57
57
|
get "aria-required"() {
|
|
58
58
|
return props.required ? "true" : void 0;
|
|
59
59
|
},
|
|
60
|
-
onClick: () => input
|
|
60
|
+
onClick: () => input.click(),
|
|
61
61
|
onKeyDown: (event) => {
|
|
62
62
|
if (event.key === "Enter") event.preventDefault();
|
|
63
63
|
},
|
|
@@ -67,69 +67,18 @@ function CheckboxRoot(props) {
|
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
let button;
|
|
70
|
-
|
|
71
|
-
const syncInput = () => {
|
|
72
|
-
if (input) input.checked = untrack(checked);
|
|
73
|
-
};
|
|
74
|
-
const onInputChange = () => {
|
|
75
|
-
if (!input) return;
|
|
76
|
-
setChecked(input.checked);
|
|
77
|
-
onFlush(syncInput);
|
|
78
|
-
};
|
|
79
|
-
const onInputClick = (event) => {
|
|
80
|
-
event.stopPropagation();
|
|
81
|
-
if (props.readOnly) event.preventDefault();
|
|
82
|
-
};
|
|
83
|
-
onSettled(() => {
|
|
84
|
-
const form = input?.form;
|
|
85
|
-
if (!form) return;
|
|
86
|
-
const onReset = () => {
|
|
87
|
-
setChecked(props.defaultChecked ?? false);
|
|
88
|
-
onFlush(syncInput);
|
|
89
|
-
};
|
|
90
|
-
form.addEventListener("reset", onReset);
|
|
91
|
-
return () => form.removeEventListener("reset", onReset);
|
|
92
|
-
});
|
|
70
|
+
const input = createHiddenInput("checkbox", props, checked, setChecked, () => button, () => setChecked(props.defaultChecked ?? false));
|
|
93
71
|
const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "checked", "defaultChecked", "onCheckedChange", "indeterminate", "name", "value", "disabled", "readOnly", "required", "form"));
|
|
94
72
|
return createComponent(CheckboxRootContext, {
|
|
95
73
|
value: stateAttributes,
|
|
96
74
|
get children() {
|
|
97
75
|
return [memo(() => {
|
|
98
76
|
return renderElement(props, elementProps, (p) => (() => {
|
|
99
|
-
var _el$
|
|
100
|
-
spread(_el
|
|
101
|
-
return _el
|
|
77
|
+
var _el$ = _tmpl$();
|
|
78
|
+
spread(_el$, p, false);
|
|
79
|
+
return _el$;
|
|
102
80
|
})());
|
|
103
|
-
}),
|
|
104
|
-
var _el$ = _tmpl$();
|
|
105
|
-
_el$.addEventListener("focus", () => button?.focus());
|
|
106
|
-
_el$.addEventListener("change", onInputChange);
|
|
107
|
-
ref(() => {
|
|
108
|
-
return (element) => {
|
|
109
|
-
input = element;
|
|
110
|
-
element.addEventListener("click", onInputClick);
|
|
111
|
-
};
|
|
112
|
-
}, _el$);
|
|
113
|
-
effect(() => {
|
|
114
|
-
return {
|
|
115
|
-
e: props.name,
|
|
116
|
-
t: props.value,
|
|
117
|
-
a: props.disabled,
|
|
118
|
-
o: props.required,
|
|
119
|
-
i: props.form,
|
|
120
|
-
n: checked(),
|
|
121
|
-
s: heldRead()
|
|
122
|
-
};
|
|
123
|
-
}, ({ e, t, a, o, i, n, s }, _p$) => {
|
|
124
|
-
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
125
|
-
(t !== _p$?.t || !s) && setProperty(_el$, "value", t);
|
|
126
|
-
a !== _p$?.a && setAttribute(_el$, "disabled", a);
|
|
127
|
-
o !== _p$?.o && setAttribute(_el$, "required", o);
|
|
128
|
-
i !== _p$?.i && setAttribute(_el$, "form", i);
|
|
129
|
-
(n !== _p$?.n || !s) && (_el$.checked = n);
|
|
130
|
-
});
|
|
131
|
-
return _el$;
|
|
132
|
-
})()];
|
|
81
|
+
}), input];
|
|
133
82
|
}
|
|
134
83
|
});
|
|
135
84
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { effect, heldRead, ref, setAttribute, setProperty, template } from "@solidjs/web";
|
|
2
|
+
import { onFlush, onSettled, untrack } from "solid-js";
|
|
3
|
+
// src/core/hiddenInput.tsx
|
|
4
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input aria-hidden=true tabindex=-1 style=position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none>`);
|
|
5
|
+
/**
|
|
6
|
+
* The form's view of a control whose element is a native button (PLAN.md
|
|
7
|
+
* section 8, "The hidden input is the form's view of the control"): a
|
|
8
|
+
* visually hidden `<input type="checkbox">` or `<input type="radio">`,
|
|
9
|
+
* rendered after the button, for Switch, Checkbox and a Radio's Items.
|
|
10
|
+
* Returns the input, which the button clicks, so the form sees the input's
|
|
11
|
+
* native `input` and `change` events:
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const input = createHiddenInput(
|
|
15
|
+
* "checkbox",
|
|
16
|
+
* props,
|
|
17
|
+
* checked,
|
|
18
|
+
* setChecked,
|
|
19
|
+
* () => button,
|
|
20
|
+
* () => setChecked(props.defaultChecked ?? false),
|
|
21
|
+
* );
|
|
22
|
+
* // The button's own props: onClick: () => input.click()
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* - `props` are the control's own, read where they are used.
|
|
26
|
+
* - `checked` is the control's state, which the input follows, and
|
|
27
|
+
* `setChecked` asks for the input's checkedness once the user changed it.
|
|
28
|
+
* - `button` is where focus goes when the browser focuses the input, as it
|
|
29
|
+
* does when it blocks a submission.
|
|
30
|
+
* - `reset` asks for the control's default on a form reset, through the
|
|
31
|
+
* setter, so that a controlled control asks. A Radio's Items each ask
|
|
32
|
+
* the group, which asks once.
|
|
33
|
+
*
|
|
34
|
+
* A plain function with positional parameters, not a component with props:
|
|
35
|
+
* a component would wrap each prop in a getter, and an options object
|
|
36
|
+
* would spell out each name. Measured on 2026-11-24, Switch and Checkbox
|
|
37
|
+
* are 30 B larger each than with a copy of their own (src/radio/README.md,
|
|
38
|
+
* "Size").
|
|
39
|
+
*/
|
|
40
|
+
function createHiddenInput(type, props, checked, setChecked, button, reset) {
|
|
41
|
+
let input;
|
|
42
|
+
const sync = () => {
|
|
43
|
+
if (input) input.checked = untrack(checked);
|
|
44
|
+
};
|
|
45
|
+
const onClick = (event) => {
|
|
46
|
+
event.stopPropagation();
|
|
47
|
+
if (props.readOnly) event.preventDefault();
|
|
48
|
+
};
|
|
49
|
+
onSettled(() => {
|
|
50
|
+
const form = input?.form;
|
|
51
|
+
if (!form) return;
|
|
52
|
+
const onReset = () => {
|
|
53
|
+
reset();
|
|
54
|
+
onFlush(sync);
|
|
55
|
+
};
|
|
56
|
+
form.addEventListener("reset", onReset);
|
|
57
|
+
return () => form.removeEventListener("reset", onReset);
|
|
58
|
+
});
|
|
59
|
+
return (() => {
|
|
60
|
+
var _el$ = _tmpl$();
|
|
61
|
+
_el$.addEventListener("focus", () => button()?.focus());
|
|
62
|
+
_el$.addEventListener("change", () => {
|
|
63
|
+
setChecked(input.checked);
|
|
64
|
+
onFlush(sync);
|
|
65
|
+
});
|
|
66
|
+
setAttribute(_el$, "type", type);
|
|
67
|
+
ref(() => {
|
|
68
|
+
return (element) => {
|
|
69
|
+
input = element;
|
|
70
|
+
element.addEventListener("click", onClick);
|
|
71
|
+
};
|
|
72
|
+
}, _el$);
|
|
73
|
+
effect(() => {
|
|
74
|
+
return {
|
|
75
|
+
e: props.name,
|
|
76
|
+
t: props.value,
|
|
77
|
+
a: props.disabled,
|
|
78
|
+
o: props.required,
|
|
79
|
+
i: props.form,
|
|
80
|
+
n: checked(),
|
|
81
|
+
s: heldRead()
|
|
82
|
+
};
|
|
83
|
+
}, ({ e, t, a, o, i, n, s }, _p$) => {
|
|
84
|
+
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
85
|
+
(t !== _p$?.t || !s) && setProperty(_el$, "value", t);
|
|
86
|
+
a !== _p$?.a && setAttribute(_el$, "disabled", a);
|
|
87
|
+
o !== _p$?.o && setAttribute(_el$, "required", o);
|
|
88
|
+
i !== _p$?.i && setAttribute(_el$, "form", i);
|
|
89
|
+
(n !== _p$?.n || !s) && (_el$.checked = n);
|
|
90
|
+
});
|
|
91
|
+
return _el$;
|
|
92
|
+
})();
|
|
93
|
+
}
|
|
94
|
+
export { createHiddenInput };
|
package/dist/core/rovingFocus.js
CHANGED
|
@@ -4,7 +4,7 @@ var byPosition = (a, b) => a.element.compareDocumentPosition(b.element) & Node.D
|
|
|
4
4
|
/**
|
|
5
5
|
* One tab stop among a group's items, with arrow keys between them (roving
|
|
6
6
|
* tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
|
|
7
|
-
* Group's items, Tabs' tabs. Pass the options as getters and functions, so
|
|
7
|
+
* Group's items, Tabs' tabs, a Radio's radios. Pass the options as getters and functions, so
|
|
8
8
|
* they are read where they are used:
|
|
9
9
|
*
|
|
10
10
|
* ```ts
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { renderStepButton } from "../stepButton.js";
|
|
2
|
+
// src/number-field/decrement/NumberFieldDecrement.tsx
|
|
3
|
+
/**
|
|
4
|
+
* The button that decreases the number by `step`: a `<button type="button"
|
|
5
|
+
* tabindex="-1">`, repeating while held, disabled at `min`. Give it a
|
|
6
|
+
* name, such as `aria-label="Decrease"`. Place it inside `NumberField.Root`.
|
|
7
|
+
*/
|
|
8
|
+
function NumberFieldDecrement(props) {
|
|
9
|
+
return renderStepButton(props, -1, "NumberField.Decrement");
|
|
10
|
+
}
|
|
11
|
+
export { NumberFieldDecrement };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { renderStepButton } from "../stepButton.js";
|
|
2
|
+
// src/number-field/increment/NumberFieldIncrement.tsx
|
|
3
|
+
/**
|
|
4
|
+
* The button that increases the number by `step`: a `<button type="button"
|
|
5
|
+
* tabindex="-1">`, repeating while held, disabled at `max`. Give it a
|
|
6
|
+
* name, such as `aria-label="Increase"`. Place it inside `NumberField.Root`.
|
|
7
|
+
*/
|
|
8
|
+
function NumberFieldIncrement(props) {
|
|
9
|
+
return renderStepButton(props, 1, "NumberField.Increment");
|
|
10
|
+
}
|
|
11
|
+
export { NumberFieldIncrement };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NumberFieldRoot } from "./root/NumberFieldRoot.js";
|
|
2
|
+
import { NumberFieldInput } from "./input/NumberFieldInput.js";
|
|
3
|
+
import { NumberFieldIncrement } from "./increment/NumberFieldIncrement.js";
|
|
4
|
+
import { NumberFieldDecrement } from "./decrement/NumberFieldDecrement.js";
|
|
5
|
+
export { NumberFieldDecrement as Decrement, NumberFieldIncrement as Increment, NumberFieldInput as Input, NumberFieldRoot as Root };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useNumberFieldRootContext } from "../root/NumberFieldRootContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { createEffect, merge, omit } from "solid-js";
|
|
6
|
+
// src/number-field/input/NumberFieldInput.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input>`);
|
|
8
|
+
/**
|
|
9
|
+
* The spinbutton: an `<input type="text" role="spinbutton">` that shows the
|
|
10
|
+
* number formatted, keeps text as it is typed, and reads it back on blur
|
|
11
|
+
* and on Enter. Up, Down, Page Up, Page Down, Home and End step the number.
|
|
12
|
+
* Takes the Root's `id`; place it inside `NumberField.Root`.
|
|
13
|
+
*/
|
|
14
|
+
function NumberFieldInput(props) {
|
|
15
|
+
const root = useNumberFieldRootContext();
|
|
16
|
+
const ownProps = merge(root.attributes, {
|
|
17
|
+
type: "text",
|
|
18
|
+
role: "spinbutton",
|
|
19
|
+
inputmode: "decimal",
|
|
20
|
+
autocomplete: "off",
|
|
21
|
+
get id() {
|
|
22
|
+
return root.inputId();
|
|
23
|
+
},
|
|
24
|
+
get defaultValue() {
|
|
25
|
+
return root.text();
|
|
26
|
+
},
|
|
27
|
+
get disabled() {
|
|
28
|
+
return root.props.disabled;
|
|
29
|
+
},
|
|
30
|
+
get readonly() {
|
|
31
|
+
return root.props.readOnly;
|
|
32
|
+
},
|
|
33
|
+
get "aria-valuenow"() {
|
|
34
|
+
return root.value() ?? void 0;
|
|
35
|
+
},
|
|
36
|
+
get "aria-valuetext"() {
|
|
37
|
+
return root.value() === null ? void 0 : root.text();
|
|
38
|
+
},
|
|
39
|
+
get "aria-valuemin"() {
|
|
40
|
+
return root.props.min;
|
|
41
|
+
},
|
|
42
|
+
get "aria-valuemax"() {
|
|
43
|
+
return root.props.max;
|
|
44
|
+
},
|
|
45
|
+
get "aria-required"() {
|
|
46
|
+
return root.props.required ? "true" : void 0;
|
|
47
|
+
},
|
|
48
|
+
get "aria-describedby"() {
|
|
49
|
+
return root.props["aria-describedby"];
|
|
50
|
+
},
|
|
51
|
+
get "aria-invalid"() {
|
|
52
|
+
return root.props["aria-invalid"];
|
|
53
|
+
},
|
|
54
|
+
onInput: () => {
|
|
55
|
+
root.edited = true;
|
|
56
|
+
},
|
|
57
|
+
onBlur: () => root.commit(),
|
|
58
|
+
onKeyDown: (event) => {
|
|
59
|
+
const { key } = event;
|
|
60
|
+
if (key === "Enter") {
|
|
61
|
+
if (!event.isComposing) root.commit();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (!root.enabled() || event.ctrlKey || event.altKey || event.metaKey) return;
|
|
65
|
+
const limit = key === "Home" ? "min" : key === "End" ? "max" : void 0;
|
|
66
|
+
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
67
|
+
if (event.shiftKey) return;
|
|
68
|
+
root.step(key === "ArrowUp" ? 1 : -1);
|
|
69
|
+
} else if (key === "PageUp" || key === "PageDown") root.step(key === "PageUp" ? 1 : -1, true);
|
|
70
|
+
else if (limit && root.props[limit] !== void 0) root.setLimit(limit);
|
|
71
|
+
else return;
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
},
|
|
74
|
+
ref: (element) => {
|
|
75
|
+
root.input = element;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
createEffect(() => root.text(), () => root.sync());
|
|
79
|
+
const consumerProps = omit(props, "render");
|
|
80
|
+
const elementProps = mergeElementProps(ownProps, consumerProps);
|
|
81
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
82
|
+
var _el$ = _tmpl$();
|
|
83
|
+
spread(_el$, p, false);
|
|
84
|
+
return _el$;
|
|
85
|
+
})());
|
|
86
|
+
}
|
|
87
|
+
export { NumberFieldInput };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/number-field/parse.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reads text written in a formatter's locale and format back to a number,
|
|
4
|
+
* `NaN` when it is not one (src/number-field/README.md, "Decisions"). Reads
|
|
5
|
+
* the locale's digits, and ASCII digits, its decimal separator, its minus
|
|
6
|
+
* sign and `-`; drops its group separator, whitespace, apostrophes, and the
|
|
7
|
+
* currency, percent sign, unit and literal text the format writes. Any
|
|
8
|
+
* other character makes the text not a number. A percentage is read as the
|
|
9
|
+
* fraction the formatter writes as one: `25%` is `0.25`. Empty text is
|
|
10
|
+
* `NaN` too: the caller reads it as no value.
|
|
11
|
+
*/
|
|
12
|
+
function parseNumber(formatter, text) {
|
|
13
|
+
const { locale, numberingSystem, style } = formatter.resolvedOptions();
|
|
14
|
+
const plain = new Intl.NumberFormat(locale, { numberingSystem });
|
|
15
|
+
const meaning = { "-": "-" };
|
|
16
|
+
for (const { type, value } of plain.formatToParts(-12345.6)) {
|
|
17
|
+
if (type === "decimal") meaning[value] = ".";
|
|
18
|
+
if (type === "minusSign") meaning[value] = "-";
|
|
19
|
+
if (type === "group") meaning[value] = "";
|
|
20
|
+
}
|
|
21
|
+
for (let digit = 0; digit < 10; digit++) meaning[plain.format(digit)] = String(digit);
|
|
22
|
+
for (const { type, value } of [...formatter.formatToParts(-1), ...formatter.formatToParts(1)]) if (/^(currency|percentSign|unit|literal|plusSign)$/.test(type)) text = text.split(value).join("");
|
|
23
|
+
let number = "";
|
|
24
|
+
for (const char of text.replace(/[\s'’]/g, "")) number += /\d/.test(char) ? char : meaning[char] ?? "!";
|
|
25
|
+
if (!/^-?(\d+\.?\d*|\.\d+)$/.test(number)) return NaN;
|
|
26
|
+
return style === "percent" ? Number(number) / 100 : Number(number);
|
|
27
|
+
}
|
|
28
|
+
export { parseNumber };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
4
|
+
import { parseNumber } from "../parse.js";
|
|
5
|
+
import { NumberFieldRootContext } from "./NumberFieldRootContext.js";
|
|
6
|
+
import { createComponent, effect, heldRead, memo, ref, setAttribute, spread, template } from "@solidjs/web";
|
|
7
|
+
import { createMemo, createUniqueId, omit, onFlush, onSettled, untrack } from "solid-js";
|
|
8
|
+
// src/number-field/root/NumberFieldRoot.tsx
|
|
9
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input type=number aria-hidden=true tabindex=-1 style=position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none>`);
|
|
10
|
+
var _tmpl$2 = /* @__PURE__ */ template(`<div>`);
|
|
11
|
+
/**
|
|
12
|
+
* The number field: a `<div>` around its Input and buttons, which holds the
|
|
13
|
+
* value, then a visually hidden `<input type="number">` with the plain
|
|
14
|
+
* number, the form's view of it. Its `id`, `aria-describedby` and
|
|
15
|
+
* `aria-invalid` go on the Input, so it joins a Field as `Field.Control`'s
|
|
16
|
+
* `render`.
|
|
17
|
+
*/
|
|
18
|
+
function NumberFieldRoot(props) {
|
|
19
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
20
|
+
get value() {
|
|
21
|
+
return props.value;
|
|
22
|
+
},
|
|
23
|
+
get defaultValue() {
|
|
24
|
+
return props.defaultValue ?? null;
|
|
25
|
+
},
|
|
26
|
+
onChange: (next) => props.onValueChange?.(next)
|
|
27
|
+
});
|
|
28
|
+
const formatter = createMemo(() => new Intl.NumberFormat(props.locale, props.format));
|
|
29
|
+
const format = (number) => number === null ? "" : formatter().format(number);
|
|
30
|
+
const generatedId = createUniqueId();
|
|
31
|
+
let hidden;
|
|
32
|
+
const roundAndClamp = (number, digits) => Math.min(Math.max(Number(number.toFixed(digits)), props.min ?? -Infinity), props.max ?? Infinity);
|
|
33
|
+
const formValue = (number) => number === null ? "" : String(number);
|
|
34
|
+
const sync = () => {
|
|
35
|
+
context.edited = false;
|
|
36
|
+
const number = untrack(value);
|
|
37
|
+
const text = untrack(() => format(number));
|
|
38
|
+
if (context.input && context.input.value !== text) context.input.value = text;
|
|
39
|
+
if (hidden) hidden.value = formValue(number);
|
|
40
|
+
};
|
|
41
|
+
const change = (next) => {
|
|
42
|
+
const before = hidden?.value;
|
|
43
|
+
if (hidden) hidden.value = formValue(next);
|
|
44
|
+
setValue(next);
|
|
45
|
+
if (hidden && hidden.value !== before) {
|
|
46
|
+
hidden.dispatchEvent(new Event("input", { bubbles: true }));
|
|
47
|
+
hidden.dispatchEvent(new Event("change", { bubbles: true }));
|
|
48
|
+
}
|
|
49
|
+
onFlush(sync);
|
|
50
|
+
};
|
|
51
|
+
const current = () => {
|
|
52
|
+
if (context.edited && context.input) {
|
|
53
|
+
const parsed = parseNumber(formatter(), context.input.value);
|
|
54
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
55
|
+
}
|
|
56
|
+
return requested();
|
|
57
|
+
};
|
|
58
|
+
const places = (number = 0) => (String(number).split(".")[1] ?? "").length;
|
|
59
|
+
const context = {
|
|
60
|
+
props,
|
|
61
|
+
value,
|
|
62
|
+
text: () => format(value()),
|
|
63
|
+
inputId: () => props.id || generatedId,
|
|
64
|
+
attributes: {
|
|
65
|
+
get "data-disabled"() {
|
|
66
|
+
return props.disabled ? "" : void 0;
|
|
67
|
+
},
|
|
68
|
+
get "data-readonly"() {
|
|
69
|
+
return props.readOnly ? "" : void 0;
|
|
70
|
+
},
|
|
71
|
+
get "data-required"() {
|
|
72
|
+
return props.required ? "" : void 0;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
edited: false,
|
|
76
|
+
sync,
|
|
77
|
+
enabled: () => !props.disabled && !props.readOnly,
|
|
78
|
+
canStep: (direction) => {
|
|
79
|
+
const number = value();
|
|
80
|
+
const limit = direction > 0 ? props.max : props.min;
|
|
81
|
+
return context.enabled() && (number === null || limit === void 0 || (direction > 0 ? number < limit : number > limit));
|
|
82
|
+
},
|
|
83
|
+
step: (direction, large) => {
|
|
84
|
+
const size = props.step ?? 1;
|
|
85
|
+
const amount = large ? props.largeStep ?? size * 10 : size;
|
|
86
|
+
const origin = props.min ?? 0;
|
|
87
|
+
const steps = ((current() ?? 0) - origin) / size;
|
|
88
|
+
const next = origin + (direction > 0 ? Math.floor(steps + 1e-9) : Math.ceil(steps - 1e-9)) * size + direction * amount;
|
|
89
|
+
change(roundAndClamp(next, Math.max(places(size), places(origin), places(amount))));
|
|
90
|
+
},
|
|
91
|
+
setLimit: (limit) => {
|
|
92
|
+
const number = props[limit];
|
|
93
|
+
if (number !== void 0) change(number);
|
|
94
|
+
},
|
|
95
|
+
commit: () => {
|
|
96
|
+
if (!context.edited || !context.input) return;
|
|
97
|
+
const text = context.input.value.trim();
|
|
98
|
+
const parsed = text ? parseNumber(formatter(), text) : null;
|
|
99
|
+
if (Number.isNaN(parsed)) return sync();
|
|
100
|
+
if (parsed === null) return change(null);
|
|
101
|
+
const options = formatter().resolvedOptions();
|
|
102
|
+
change(roundAndClamp(parsed, options.maximumFractionDigits + (options.style === "percent" ? 2 : 0)));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
onSettled(() => {
|
|
106
|
+
const form = hidden?.form;
|
|
107
|
+
if (!form) return;
|
|
108
|
+
let disposed = false;
|
|
109
|
+
const onReset = (event) => {
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (disposed || event.defaultPrevented) return;
|
|
112
|
+
setValue(props.defaultValue ?? null);
|
|
113
|
+
onFlush(sync);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
form.addEventListener("reset", onReset);
|
|
117
|
+
return () => {
|
|
118
|
+
disposed = true;
|
|
119
|
+
form.removeEventListener("reset", onReset);
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
const elementProps = mergeElementProps(context.attributes, omit(props, "render", "value", "defaultValue", "onValueChange", "min", "max", "step", "largeStep", "format", "locale", "name", "form", "required", "disabled", "readOnly", "id", "aria-describedby", "aria-invalid"));
|
|
123
|
+
return createComponent(NumberFieldRootContext, {
|
|
124
|
+
value: context,
|
|
125
|
+
get children() {
|
|
126
|
+
return [memo(() => {
|
|
127
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
128
|
+
var _el$2 = _tmpl$2();
|
|
129
|
+
spread(_el$2, p, false);
|
|
130
|
+
return _el$2;
|
|
131
|
+
})());
|
|
132
|
+
}), (() => {
|
|
133
|
+
var _el$ = _tmpl$();
|
|
134
|
+
_el$.addEventListener("focus", () => context.input?.focus());
|
|
135
|
+
ref(() => {
|
|
136
|
+
return (element) => {
|
|
137
|
+
hidden = element;
|
|
138
|
+
};
|
|
139
|
+
}, _el$);
|
|
140
|
+
effect(() => {
|
|
141
|
+
return {
|
|
142
|
+
e: props.name,
|
|
143
|
+
t: props.form,
|
|
144
|
+
a: props.min,
|
|
145
|
+
o: props.max,
|
|
146
|
+
i: props.step ?? "any",
|
|
147
|
+
n: props.required,
|
|
148
|
+
s: props.disabled,
|
|
149
|
+
h: props.readOnly,
|
|
150
|
+
r: formValue(value()),
|
|
151
|
+
d: heldRead()
|
|
152
|
+
};
|
|
153
|
+
}, ({ e, t, a, o, i, n, s, h, r, d }, _p$) => {
|
|
154
|
+
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
155
|
+
t !== _p$?.t && setAttribute(_el$, "form", t);
|
|
156
|
+
a !== _p$?.a && setAttribute(_el$, "min", a);
|
|
157
|
+
o !== _p$?.o && setAttribute(_el$, "max", o);
|
|
158
|
+
i !== _p$?.i && setAttribute(_el$, "step", i);
|
|
159
|
+
n !== _p$?.n && setAttribute(_el$, "required", n);
|
|
160
|
+
s !== _p$?.s && setAttribute(_el$, "disabled", s);
|
|
161
|
+
h !== _p$?.h && setAttribute(_el$, "readonly", h);
|
|
162
|
+
(r !== _p$?.r || !d) && (_el$.value = r ?? "");
|
|
163
|
+
});
|
|
164
|
+
return _el$;
|
|
165
|
+
})()];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
export { NumberFieldRoot };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/number-field/root/NumberFieldRootContext.ts
|
|
4
|
+
var NumberFieldRootContext = createContext(null);
|
|
5
|
+
function useNumberFieldRootContext() {
|
|
6
|
+
const context = useContext(NumberFieldRootContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] NumberField parts must be placed within <NumberField.Root>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { NumberFieldRootContext, useNumberFieldRootContext };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { warnUnlessButton } from "../core/button.js";
|
|
2
|
+
import { mergeElementProps } from "../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../core/renderElement.js";
|
|
4
|
+
import { useNumberFieldRootContext } from "./root/NumberFieldRootContext.js";
|
|
5
|
+
import { isDev, spread, template } from "@solidjs/web";
|
|
6
|
+
import { merge, omit, onSettled } from "solid-js";
|
|
7
|
+
// src/number-field/stepButton.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
9
|
+
var holdDelay = 400;
|
|
10
|
+
var repeatInterval = 60;
|
|
11
|
+
/**
|
|
12
|
+
* Increment or Decrement: a `<button type="button" tabindex="-1">` that
|
|
13
|
+
* steps the value that way on a click, and again and again while a pointer
|
|
14
|
+
* holds it. It never takes focus, and is disabled at its limit.
|
|
15
|
+
*/
|
|
16
|
+
function renderStepButton(props, direction, part) {
|
|
17
|
+
const root = useNumberFieldRootContext();
|
|
18
|
+
let timer;
|
|
19
|
+
const stop = () => clearTimeout(timer);
|
|
20
|
+
const repeat = (delay) => {
|
|
21
|
+
root.step(direction);
|
|
22
|
+
timer = setTimeout(() => {
|
|
23
|
+
if (root.canStep(direction)) repeat(repeatInterval);
|
|
24
|
+
}, delay);
|
|
25
|
+
};
|
|
26
|
+
onSettled(() => stop);
|
|
27
|
+
const ownProps = merge(root.attributes, {
|
|
28
|
+
type: "button",
|
|
29
|
+
tabindex: -1,
|
|
30
|
+
get disabled() {
|
|
31
|
+
return !root.canStep(direction);
|
|
32
|
+
},
|
|
33
|
+
get "aria-controls"() {
|
|
34
|
+
return root.inputId();
|
|
35
|
+
},
|
|
36
|
+
onPointerDown: (event) => {
|
|
37
|
+
if (event.button !== 0) return;
|
|
38
|
+
event.preventDefault();
|
|
39
|
+
if (event.pointerType === "mouse") root.input?.focus();
|
|
40
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
41
|
+
repeat(holdDelay);
|
|
42
|
+
},
|
|
43
|
+
onPointerUp: stop,
|
|
44
|
+
onPointerCancel: stop,
|
|
45
|
+
onLostPointerCapture: stop,
|
|
46
|
+
onClick: (event) => {
|
|
47
|
+
if (event.detail === 0) root.step(direction);
|
|
48
|
+
},
|
|
49
|
+
ref: (element) => {
|
|
50
|
+
if (isDev) warnUnlessButton(element, part);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
54
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
55
|
+
var _el$ = _tmpl$();
|
|
56
|
+
spread(_el$, p, false);
|
|
57
|
+
return _el$;
|
|
58
|
+
})());
|
|
59
|
+
}
|
|
60
|
+
export { renderStepButton };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useRadioItemContext } from "../item/RadioItemContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { omit } from "solid-js";
|
|
6
|
+
// src/radio/indicator/RadioIndicator.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<span>`);
|
|
8
|
+
/**
|
|
9
|
+
* The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
|
|
10
|
+
* so that CSS shows it by state. Place it inside `Radio.Item`.
|
|
11
|
+
*/
|
|
12
|
+
function RadioIndicator(props) {
|
|
13
|
+
const elementProps = mergeElementProps(useRadioItemContext(), omit(props, "render"));
|
|
14
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
15
|
+
var _el$ = _tmpl$();
|
|
16
|
+
spread(_el$, p, false);
|
|
17
|
+
return _el$;
|
|
18
|
+
})());
|
|
19
|
+
}
|
|
20
|
+
export { RadioIndicator };
|