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
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads text written in a formatter's locale and format back to a number,
|
|
3
|
+
* `NaN` when it is not one (src/number-field/README.md, "Decisions"). Reads
|
|
4
|
+
* the locale's digits, and ASCII digits, its decimal separator, its minus
|
|
5
|
+
* sign and `-`; drops its group separator, whitespace, apostrophes, and the
|
|
6
|
+
* currency, percent sign, unit and literal text the format writes. Any
|
|
7
|
+
* other character makes the text not a number. A percentage is read as the
|
|
8
|
+
* fraction the formatter writes as one: `25%` is `0.25`. Empty text is
|
|
9
|
+
* `NaN` too: the caller reads it as no value.
|
|
10
|
+
*/
|
|
11
|
+
export function parseNumber(formatter, text) {
|
|
12
|
+
const { locale, numberingSystem, style } = formatter.resolvedOptions();
|
|
13
|
+
const plain = new Intl.NumberFormat(locale, { numberingSystem });
|
|
14
|
+
// What each character the locale writes stands for.
|
|
15
|
+
const meaning = { "-": "-" };
|
|
16
|
+
for (const { type, value } of plain.formatToParts(-12345.6)) {
|
|
17
|
+
if (type === "decimal")
|
|
18
|
+
meaning[value] = ".";
|
|
19
|
+
if (type === "minusSign")
|
|
20
|
+
meaning[value] = "-";
|
|
21
|
+
if (type === "group")
|
|
22
|
+
meaning[value] = "";
|
|
23
|
+
}
|
|
24
|
+
// The locale's digits, such as Arabic-Indic ones.
|
|
25
|
+
for (let digit = 0; digit < 10; digit++) {
|
|
26
|
+
meaning[plain.format(digit)] = String(digit);
|
|
27
|
+
}
|
|
28
|
+
// The text the format writes around the number.
|
|
29
|
+
for (const { type, value } of [
|
|
30
|
+
...formatter.formatToParts(-1),
|
|
31
|
+
...formatter.formatToParts(1),
|
|
32
|
+
]) {
|
|
33
|
+
if (/^(currency|percentSign|unit|literal|plusSign)$/.test(type)) {
|
|
34
|
+
text = text.split(value).join("");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
let number = "";
|
|
38
|
+
// Whitespace, for a no-break space typed as a space, and either
|
|
39
|
+
// apostrophe, as ICU versions and keyboards differ on de-CH's.
|
|
40
|
+
for (const char of text.replace(/[\s'’]/g, "")) {
|
|
41
|
+
number += /\d/.test(char) ? char : (meaning[char] ?? "!");
|
|
42
|
+
}
|
|
43
|
+
if (!/^-?(\d+\.?\d*|\.\d+)$/.test(number))
|
|
44
|
+
return NaN;
|
|
45
|
+
return style === "percent" ? Number(number) / 100 : Number(number);
|
|
46
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface NumberFieldRootProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** The number, when controlled; `null` while the field is empty. */
|
|
5
|
+
value?: number | null;
|
|
6
|
+
/** The number to start with when uncontrolled, and the one a form reset puts back. */
|
|
7
|
+
defaultValue?: number | null;
|
|
8
|
+
/** Called with the new number when the user changes it, and on a form reset. */
|
|
9
|
+
onValueChange?: (value: number | null) => void;
|
|
10
|
+
/** The lowest number the user can set. */
|
|
11
|
+
min?: number;
|
|
12
|
+
/** The highest number the user can set. */
|
|
13
|
+
max?: number;
|
|
14
|
+
/** How much Up, Down and the buttons change the number. Default 1. */
|
|
15
|
+
step?: number;
|
|
16
|
+
/** How much Page Up and Page Down change the number. Default ten steps. */
|
|
17
|
+
largeStep?: number;
|
|
18
|
+
/**
|
|
19
|
+
* How the Input shows the number, as `Intl.NumberFormat` options, such as
|
|
20
|
+
* `{ style: "currency", currency: "EUR" }`. Typed text is read back in
|
|
21
|
+
* the same format.
|
|
22
|
+
*/
|
|
23
|
+
format?: Intl.NumberFormatOptions;
|
|
24
|
+
/**
|
|
25
|
+
* The language to format and parse in, else the runtime's default. Pass it
|
|
26
|
+
* when rendering on the server, so that the server and the browser agree.
|
|
27
|
+
*/
|
|
28
|
+
locale?: Intl.LocalesArgument;
|
|
29
|
+
/** The name the number is submitted under. */
|
|
30
|
+
name?: string;
|
|
31
|
+
/** The `id` of the form the number belongs to, when the field is not inside it. */
|
|
32
|
+
form?: string;
|
|
33
|
+
/** Whether the form can be submitted only with a number. */
|
|
34
|
+
required?: boolean;
|
|
35
|
+
/** Whether every part ignores the user, and the form submits nothing. */
|
|
36
|
+
disabled?: boolean;
|
|
37
|
+
/** Whether the user can see and select the number, but not change it. */
|
|
38
|
+
readOnly?: boolean;
|
|
39
|
+
/** Renders the Root as another element or component. Read once. */
|
|
40
|
+
render?: RenderProp;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The number field: a `<div>` around its Input and buttons, which holds the
|
|
44
|
+
* value, then a visually hidden `<input type="number">` with the plain
|
|
45
|
+
* number, the form's view of it. Its `id`, `aria-describedby` and
|
|
46
|
+
* `aria-invalid` go on the Input, so it joins a Field as `Field.Control`'s
|
|
47
|
+
* `render`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function NumberFieldRoot(props: NumberFieldRootProps): JSX.Element;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { createMemo, createUniqueId, omit, onFlush, onSettled, untrack, } from "solid-js";
|
|
2
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
3
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
5
|
+
import { parseNumber } from "../parse.js";
|
|
6
|
+
import { NumberFieldRootContext } from "./NumberFieldRootContext.js";
|
|
7
|
+
/**
|
|
8
|
+
* The number field: a `<div>` around its Input and buttons, which holds the
|
|
9
|
+
* value, then a visually hidden `<input type="number">` with the plain
|
|
10
|
+
* number, the form's view of it. Its `id`, `aria-describedby` and
|
|
11
|
+
* `aria-invalid` go on the Input, so it joins a Field as `Field.Control`'s
|
|
12
|
+
* `render`.
|
|
13
|
+
*/
|
|
14
|
+
export function NumberFieldRoot(props) {
|
|
15
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
16
|
+
get value() {
|
|
17
|
+
return props.value;
|
|
18
|
+
},
|
|
19
|
+
get defaultValue() {
|
|
20
|
+
return props.defaultValue ?? null;
|
|
21
|
+
},
|
|
22
|
+
onChange: (next) => props.onValueChange?.(next),
|
|
23
|
+
});
|
|
24
|
+
const formatter = createMemo(() => new Intl.NumberFormat(props.locale, props.format));
|
|
25
|
+
const format = (number) => number === null ? "" : formatter().format(number);
|
|
26
|
+
const generatedId = createUniqueId();
|
|
27
|
+
let hidden;
|
|
28
|
+
// The number rounded to `digits` decimals, to drop a float error or the
|
|
29
|
+
// digits the format does not show, then clamped to `min` and `max`.
|
|
30
|
+
const roundAndClamp = (number, digits) => Math.min(Math.max(Number(number.toFixed(digits)), props.min ?? -Infinity), props.max ?? Infinity);
|
|
31
|
+
// The number as the form writes it.
|
|
32
|
+
const formValue = (number) => number === null ? "" : String(number);
|
|
33
|
+
// Puts the Input and the hidden input back to the value, once it has
|
|
34
|
+
// settled: a controlled field's consumer may have declined the change,
|
|
35
|
+
// and a commit shows the value formatted, such as `1000` as `1,000`.
|
|
36
|
+
const sync = () => {
|
|
37
|
+
context.edited = false;
|
|
38
|
+
const number = untrack(value);
|
|
39
|
+
const text = untrack(() => format(number));
|
|
40
|
+
if (context.input && context.input.value !== text) {
|
|
41
|
+
context.input.value = text;
|
|
42
|
+
}
|
|
43
|
+
if (hidden)
|
|
44
|
+
hidden.value = formValue(number);
|
|
45
|
+
};
|
|
46
|
+
// A change by the user. The hidden input changes first and fires its
|
|
47
|
+
// native events, as a native control does, so the form's listeners, a
|
|
48
|
+
// Field and a Form see the new number.
|
|
49
|
+
const change = (next) => {
|
|
50
|
+
const before = hidden?.value;
|
|
51
|
+
if (hidden)
|
|
52
|
+
hidden.value = formValue(next);
|
|
53
|
+
setValue(next);
|
|
54
|
+
if (hidden && hidden.value !== before) {
|
|
55
|
+
hidden.dispatchEvent(new Event("input", { bubbles: true }));
|
|
56
|
+
hidden.dispatchEvent(new Event("change", { bubbles: true }));
|
|
57
|
+
}
|
|
58
|
+
onFlush(sync);
|
|
59
|
+
};
|
|
60
|
+
// The number the user's text says, if edited, else the value set last.
|
|
61
|
+
const current = () => {
|
|
62
|
+
if (context.edited && context.input) {
|
|
63
|
+
const parsed = parseNumber(formatter(), context.input.value);
|
|
64
|
+
if (!Number.isNaN(parsed))
|
|
65
|
+
return parsed;
|
|
66
|
+
}
|
|
67
|
+
return requested();
|
|
68
|
+
};
|
|
69
|
+
// Decimal places of a number, to round a step's float error away.
|
|
70
|
+
const places = (number = 0) => (String(number).split(".")[1] ?? "").length;
|
|
71
|
+
const context = {
|
|
72
|
+
props,
|
|
73
|
+
value,
|
|
74
|
+
text: () => format(value()),
|
|
75
|
+
inputId: () => props.id || generatedId,
|
|
76
|
+
attributes: {
|
|
77
|
+
get "data-disabled"() {
|
|
78
|
+
return props.disabled ? "" : undefined;
|
|
79
|
+
},
|
|
80
|
+
get "data-readonly"() {
|
|
81
|
+
return props.readOnly ? "" : undefined;
|
|
82
|
+
},
|
|
83
|
+
get "data-required"() {
|
|
84
|
+
return props.required ? "" : undefined;
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
edited: false,
|
|
88
|
+
sync,
|
|
89
|
+
enabled: () => !props.disabled && !props.readOnly,
|
|
90
|
+
canStep: (direction) => {
|
|
91
|
+
const number = value();
|
|
92
|
+
const limit = direction > 0 ? props.max : props.min;
|
|
93
|
+
return (context.enabled() &&
|
|
94
|
+
(number === null ||
|
|
95
|
+
limit === undefined ||
|
|
96
|
+
(direction > 0 ? number < limit : number > limit)));
|
|
97
|
+
},
|
|
98
|
+
step: (direction, large) => {
|
|
99
|
+
const size = props.step ?? 1;
|
|
100
|
+
const amount = large ? (props.largeStep ?? size * 10) : size;
|
|
101
|
+
const origin = props.min ?? 0;
|
|
102
|
+
// From a number off the grid, the grid's next one that way, as
|
|
103
|
+
// `stepUp()` does; from none, 0, as on an empty number input.
|
|
104
|
+
const steps = ((current() ?? 0) - origin) / size;
|
|
105
|
+
const onGrid = direction > 0 ? Math.floor(steps + 1e-9) : Math.ceil(steps - 1e-9);
|
|
106
|
+
const next = origin + onGrid * size + direction * amount;
|
|
107
|
+
change(roundAndClamp(next, Math.max(places(size), places(origin), places(amount))));
|
|
108
|
+
},
|
|
109
|
+
setLimit: (limit) => {
|
|
110
|
+
const number = props[limit];
|
|
111
|
+
if (number !== undefined)
|
|
112
|
+
change(number);
|
|
113
|
+
},
|
|
114
|
+
commit: () => {
|
|
115
|
+
if (!context.edited || !context.input)
|
|
116
|
+
return;
|
|
117
|
+
const text = context.input.value.trim();
|
|
118
|
+
const parsed = text ? parseNumber(formatter(), text) : null;
|
|
119
|
+
// Text that is not a number goes back to the value's.
|
|
120
|
+
if (Number.isNaN(parsed))
|
|
121
|
+
return sync();
|
|
122
|
+
if (parsed === null)
|
|
123
|
+
return change(null);
|
|
124
|
+
// Rounded to the digits the format shows, so the value is what the
|
|
125
|
+
// user sees: a percentage shows two digits fewer than it has.
|
|
126
|
+
const options = formatter().resolvedOptions();
|
|
127
|
+
change(roundAndClamp(parsed, options.maximumFractionDigits +
|
|
128
|
+
(options.style === "percent" ? 2 : 0)));
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
// A form reset puts a control back to its default, and so it does this
|
|
132
|
+
// one: through the setter, so that a controlled field asks.
|
|
133
|
+
onSettled(() => {
|
|
134
|
+
const form = hidden?.form;
|
|
135
|
+
if (!form)
|
|
136
|
+
return;
|
|
137
|
+
// A field the reset removes, such as a row of a list going back to its
|
|
138
|
+
// default length, asks for nothing.
|
|
139
|
+
let disposed = false;
|
|
140
|
+
const onReset = (event) => {
|
|
141
|
+
// In a task of its own, not `onFlush`: when the user presses a reset
|
|
142
|
+
// button, the browser runs microtasks as each listener returns,
|
|
143
|
+
// before it resets the controls and before later listeners could
|
|
144
|
+
// cancel the reset (src/input/README.md).
|
|
145
|
+
setTimeout(() => {
|
|
146
|
+
if (disposed || event.defaultPrevented)
|
|
147
|
+
return;
|
|
148
|
+
setValue(props.defaultValue ?? null);
|
|
149
|
+
onFlush(sync);
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
form.addEventListener("reset", onReset);
|
|
153
|
+
return () => {
|
|
154
|
+
disposed = true;
|
|
155
|
+
form.removeEventListener("reset", onReset);
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
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"));
|
|
159
|
+
return (<NumberFieldRootContext value={context}>
|
|
160
|
+
{renderElement(props, elementProps, (p) => (<div {...p}/>))}
|
|
161
|
+
<input type="number" aria-hidden="true" tabindex={-1} style={visuallyHidden} name={props.name} form={props.form} min={props.min} max={props.max}
|
|
162
|
+
// Without a `step`, any number is valid, as a currency's cents
|
|
163
|
+
// are: the field formats decimals by default. The keys still step
|
|
164
|
+
// by 1.
|
|
165
|
+
step={props.step ?? "any"} required={props.required} disabled={props.disabled} readonly={props.readOnly} value={formValue(value())}
|
|
166
|
+
// The browser focuses an invalid input when it blocks a submission.
|
|
167
|
+
// The user knows the Input, not the hidden input.
|
|
168
|
+
onFocus={() => context.input?.focus()} ref={(element) => {
|
|
169
|
+
hidden = element;
|
|
170
|
+
}}/>
|
|
171
|
+
</NumberFieldRootContext>);
|
|
172
|
+
}
|
|
173
|
+
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
174
|
+
// browser can still anchor a validation message to it.
|
|
175
|
+
const visuallyHidden = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ElementProps } from "../../core/renderElement.tsx";
|
|
2
|
+
import type { NumberFieldRootProps } from "./NumberFieldRoot.tsx";
|
|
3
|
+
/** What the Root shares with its parts. */
|
|
4
|
+
export interface NumberFieldRootContextValue {
|
|
5
|
+
readonly props: NumberFieldRootProps;
|
|
6
|
+
/** The value, `null` while the field is empty. */
|
|
7
|
+
value(): number | null;
|
|
8
|
+
/** The value as the Input shows it, `""` while `null`. */
|
|
9
|
+
text(): string;
|
|
10
|
+
/** The Input's `id`: the Root's, else a generated one. */
|
|
11
|
+
inputId(): string;
|
|
12
|
+
/** `data-disabled`, `data-readonly` and `data-required`, for every part. */
|
|
13
|
+
readonly attributes: ElementProps;
|
|
14
|
+
/** The Input, once rendered. */
|
|
15
|
+
input?: HTMLInputElement;
|
|
16
|
+
/** Whether the user has edited the text since it last showed the value. */
|
|
17
|
+
edited: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Puts the Input's text and the hidden input back to the value, and marks
|
|
20
|
+
* the text unedited.
|
|
21
|
+
*/
|
|
22
|
+
sync(): void;
|
|
23
|
+
/** Whether the user can change the value: neither disabled nor read-only. */
|
|
24
|
+
enabled(): boolean;
|
|
25
|
+
/** Whether a step that way could change the value. */
|
|
26
|
+
canStep(direction: 1 | -1): boolean;
|
|
27
|
+
/** Steps the value by `step` that way, or by `largeStep` when `large`. */
|
|
28
|
+
step(direction: 1 | -1, large?: boolean): void;
|
|
29
|
+
/** Sets the value to `min` or `max`, for Home and End. */
|
|
30
|
+
setLimit(limit: "min" | "max"): void;
|
|
31
|
+
/** Parses the text the user typed, if edited, into the value. */
|
|
32
|
+
commit(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare const NumberFieldRootContext: import("solid-js").Context<NumberFieldRootContextValue | null>;
|
|
35
|
+
export declare function useNumberFieldRootContext(): NumberFieldRootContextValue;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
import { isDev } from "@solidjs/web";
|
|
3
|
+
// `null` outside a Root, so that the part can name what is missing. Solid's
|
|
4
|
+
// own error for a context with no provider and no default does not.
|
|
5
|
+
export const NumberFieldRootContext = createContext(null);
|
|
6
|
+
export function useNumberFieldRootContext() {
|
|
7
|
+
const context = useContext(NumberFieldRootContext);
|
|
8
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
9
|
+
if (isDev && !context) {
|
|
10
|
+
throw new Error("[menhir] NumberField parts must be placed within <NumberField.Root>.");
|
|
11
|
+
}
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../core/renderElement.tsx";
|
|
3
|
+
export interface StepButtonProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** Renders the button with your own component, which must render a `<button>`. Read once. */
|
|
5
|
+
render?: RenderProp;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Increment or Decrement: a `<button type="button" tabindex="-1">` that
|
|
9
|
+
* steps the value that way on a click, and again and again while a pointer
|
|
10
|
+
* holds it. It never takes focus, and is disabled at its limit.
|
|
11
|
+
*/
|
|
12
|
+
export declare function renderStepButton(props: StepButtonProps, direction: 1 | -1, part: string): JSX.Element;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { merge, omit, onSettled } from "solid-js";
|
|
3
|
+
import { warnUnlessButton } from "../core/button.js";
|
|
4
|
+
import { mergeElementProps } from "../core/mergeElementProps.js";
|
|
5
|
+
import { renderElement } from "../core/renderElement.jsx";
|
|
6
|
+
import { useNumberFieldRootContext } from "./root/NumberFieldRootContext.js";
|
|
7
|
+
// How long a press is held before it repeats, and how often it repeats.
|
|
8
|
+
const holdDelay = 400;
|
|
9
|
+
const repeatInterval = 60;
|
|
10
|
+
/**
|
|
11
|
+
* Increment or Decrement: a `<button type="button" tabindex="-1">` that
|
|
12
|
+
* steps the value that way on a click, and again and again while a pointer
|
|
13
|
+
* holds it. It never takes focus, and is disabled at its limit.
|
|
14
|
+
*/
|
|
15
|
+
export function renderStepButton(props, direction, part) {
|
|
16
|
+
const root = useNumberFieldRootContext();
|
|
17
|
+
let timer;
|
|
18
|
+
const stop = () => clearTimeout(timer);
|
|
19
|
+
// Steps, then again after the delay, and then at each interval, until
|
|
20
|
+
// the press ends or the button is disabled at its limit.
|
|
21
|
+
const repeat = (delay) => {
|
|
22
|
+
root.step(direction);
|
|
23
|
+
timer = setTimeout(() => {
|
|
24
|
+
if (root.canStep(direction))
|
|
25
|
+
repeat(repeatInterval);
|
|
26
|
+
}, delay);
|
|
27
|
+
};
|
|
28
|
+
onSettled(() => stop);
|
|
29
|
+
const ownProps = merge(root.attributes, {
|
|
30
|
+
type: "button",
|
|
31
|
+
tabindex: -1,
|
|
32
|
+
get disabled() {
|
|
33
|
+
return !root.canStep(direction);
|
|
34
|
+
},
|
|
35
|
+
get "aria-controls"() {
|
|
36
|
+
return root.inputId();
|
|
37
|
+
},
|
|
38
|
+
onPointerDown: (event) => {
|
|
39
|
+
if (event.button !== 0)
|
|
40
|
+
return;
|
|
41
|
+
// The button never takes focus: the keys go on in the Input. A mouse
|
|
42
|
+
// press focuses the Input; a touch does not, which would open the
|
|
43
|
+
// on-screen keyboard.
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
if (event.pointerType === "mouse")
|
|
46
|
+
root.input?.focus();
|
|
47
|
+
const button = event.currentTarget;
|
|
48
|
+
// The release may land outside the button.
|
|
49
|
+
button.setPointerCapture?.(event.pointerId);
|
|
50
|
+
repeat(holdDelay);
|
|
51
|
+
},
|
|
52
|
+
onPointerUp: stop,
|
|
53
|
+
onPointerCancel: stop,
|
|
54
|
+
onLostPointerCapture: stop,
|
|
55
|
+
onClick: (event) => {
|
|
56
|
+
// A click with no pointer press before it, from a screen reader or
|
|
57
|
+
// voice control, steps once. A pointer's click stepped on its press.
|
|
58
|
+
if (event.detail === 0)
|
|
59
|
+
root.step(direction);
|
|
60
|
+
},
|
|
61
|
+
ref: (element) => {
|
|
62
|
+
// Production builds drop the check with `isDev`.
|
|
63
|
+
if (isDev)
|
|
64
|
+
warnUnlessButton(element, part);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
68
|
+
return renderElement(props, elementProps, (p) => <button {...p}/>);
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Radio from "./index.parts.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Radio from "./index.parts.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface RadioIndicatorProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
|
|
8
|
+
* so that CSS shows it by state. Place it inside `Radio.Item`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function RadioIndicator(props: RadioIndicatorProps): JSX.Element;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useRadioItemContext } from "../item/RadioItemContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
|
|
7
|
+
* so that CSS shows it by state. Place it inside `Radio.Item`.
|
|
8
|
+
*/
|
|
9
|
+
export function RadioIndicator(props) {
|
|
10
|
+
const elementProps = mergeElementProps(useRadioItemContext(), omit(props, "render"));
|
|
11
|
+
return renderElement(props, elementProps, (p) => <span {...p}/>);
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface RadioItemProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** Identifies the radio in the group's value, and is submitted while it is checked. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** Whether the radio ignores the user. It cannot be focused either. */
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
render?: RenderProp;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* One radio of the group: a `<button role="radio">`, checked while the
|
|
12
|
+
* group's value is its `value`. Place it inside `Radio.Root`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function RadioItem(props: RadioItemProps): JSX.Element;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createMemo, merge, omit, onSettled, untrack } from "solid-js";
|
|
3
|
+
import { warnUnlessButton } from "../../core/button.js";
|
|
4
|
+
import { createHiddenInput } from "../../core/hiddenInput.jsx";
|
|
5
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
6
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
7
|
+
import { useRadioRootContext } from "../root/RadioRootContext.js";
|
|
8
|
+
import { RadioItemContext } from "./RadioItemContext.js";
|
|
9
|
+
/**
|
|
10
|
+
* One radio of the group: a `<button role="radio">`, checked while the
|
|
11
|
+
* group's value is its `value`. Place it inside `Radio.Root`.
|
|
12
|
+
*/
|
|
13
|
+
export function RadioItem(props) {
|
|
14
|
+
const group = useRadioRootContext();
|
|
15
|
+
// A memo, so that a change of the value that leaves this radio as it was
|
|
16
|
+
// does not run its bindings again.
|
|
17
|
+
const checked = createMemo(() => group.value() === props.value);
|
|
18
|
+
const disabled = () => !!(group.disabled() || props.disabled);
|
|
19
|
+
let element;
|
|
20
|
+
const stateAttributes = {
|
|
21
|
+
get "data-checked"() {
|
|
22
|
+
return checked() ? "" : undefined;
|
|
23
|
+
},
|
|
24
|
+
get "data-unchecked"() {
|
|
25
|
+
return checked() ? undefined : "";
|
|
26
|
+
},
|
|
27
|
+
get "data-disabled"() {
|
|
28
|
+
return disabled() ? "" : undefined;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const inputProps = {
|
|
32
|
+
get name() {
|
|
33
|
+
return group.name();
|
|
34
|
+
},
|
|
35
|
+
get value() {
|
|
36
|
+
return props.value;
|
|
37
|
+
},
|
|
38
|
+
get disabled() {
|
|
39
|
+
return disabled();
|
|
40
|
+
},
|
|
41
|
+
get required() {
|
|
42
|
+
return group.required();
|
|
43
|
+
},
|
|
44
|
+
get form() {
|
|
45
|
+
return group.form();
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
// The input's change only ever checks it: the user cannot uncheck a radio.
|
|
49
|
+
const input = createHiddenInput("radio", inputProps, checked, () => group.select(props.value), () => element, group.reset);
|
|
50
|
+
// Read once: the tab stop of the server HTML, and of the first render.
|
|
51
|
+
const first = group.claimTabStop(untrack(checked), untrack(disabled));
|
|
52
|
+
const buttonProps = {
|
|
53
|
+
role: "radio",
|
|
54
|
+
type: "button",
|
|
55
|
+
get disabled() {
|
|
56
|
+
return disabled();
|
|
57
|
+
},
|
|
58
|
+
// Solid 2.0 removes an attribute set to false, so spell the value out.
|
|
59
|
+
get "aria-checked"() {
|
|
60
|
+
return checked() ? "true" : "false";
|
|
61
|
+
},
|
|
62
|
+
get tabindex() {
|
|
63
|
+
return group.isTabStop(element, first) ? 0 : -1;
|
|
64
|
+
},
|
|
65
|
+
// The change goes through the hidden input, so that the form sees the
|
|
66
|
+
// native `input` and `change` events a radio fires.
|
|
67
|
+
onClick: () => input.click(),
|
|
68
|
+
onKeyDown: (event) => {
|
|
69
|
+
// A button clicks on Enter, a radio does not: the pattern names only
|
|
70
|
+
// Space. Canceling the key down keeps the browser from clicking.
|
|
71
|
+
if (event.key === "Enter")
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
else
|
|
74
|
+
group.navigate(event, element);
|
|
75
|
+
},
|
|
76
|
+
// An arrow key checks the radio it moves to; Tab and a click focus it
|
|
77
|
+
// without checking it, though a click checks it then.
|
|
78
|
+
onFocus: () => {
|
|
79
|
+
if (group.moving())
|
|
80
|
+
input.click();
|
|
81
|
+
},
|
|
82
|
+
ref: (el) => {
|
|
83
|
+
element = el;
|
|
84
|
+
// Production builds drop the check with `isDev`.
|
|
85
|
+
if (isDev)
|
|
86
|
+
warnUnlessButton(el, "Radio.Item");
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
// Mounted, so its place in the document is known.
|
|
90
|
+
onSettled(() => group.register({ element: element, input, disabled, checked }));
|
|
91
|
+
const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "value", "disabled"));
|
|
92
|
+
return (<RadioItemContext value={stateAttributes}>
|
|
93
|
+
{renderElement(props, elementProps, (p) => (<button {...p}/>))}
|
|
94
|
+
{input}
|
|
95
|
+
</RadioItemContext>);
|
|
96
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The radio's state as `data-*` attributes, each `""` when it applies and
|
|
3
|
+
* absent otherwise. Item and Indicator both spread the same object, so its
|
|
4
|
+
* getters are read where each element uses them.
|
|
5
|
+
*/
|
|
6
|
+
export type RadioStateAttributes = {
|
|
7
|
+
readonly "data-checked": "" | undefined;
|
|
8
|
+
readonly "data-unchecked": "" | undefined;
|
|
9
|
+
readonly "data-disabled": "" | undefined;
|
|
10
|
+
};
|
|
11
|
+
export declare const RadioItemContext: import("solid-js").Context<RadioStateAttributes | null>;
|
|
12
|
+
export declare function useRadioItemContext(): RadioStateAttributes;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
import { isDev } from "@solidjs/web";
|
|
3
|
+
// `null` outside an Item, so that the part can name what is missing.
|
|
4
|
+
export const RadioItemContext = createContext(null);
|
|
5
|
+
export function useRadioItemContext() {
|
|
6
|
+
const context = useContext(RadioItemContext);
|
|
7
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
8
|
+
if (isDev && !context) {
|
|
9
|
+
throw new Error("[menhir] Radio.Indicator must be placed within <Radio.Item>.");
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface RadioRootProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** The checked radio's value, when controlled. `null` for none. */
|
|
5
|
+
value?: string | null;
|
|
6
|
+
/** The radio checked at first, when uncontrolled. */
|
|
7
|
+
defaultValue?: string | null;
|
|
8
|
+
/**
|
|
9
|
+
* Called with the new value when the user checks a radio, or with
|
|
10
|
+
* `defaultValue` when the form is reset. When controlled, the group
|
|
11
|
+
* changes only if `value` does.
|
|
12
|
+
*/
|
|
13
|
+
onValueChange?: (value: string | null) => void;
|
|
14
|
+
/** The name the checked radio's value is submitted under. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** Whether every radio ignores the user. The fieldset is disabled too. */
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
/** Whether the form can be submitted only while a radio is checked. */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
/** The `id` of the form the radios belong to, when they are not inside it. */
|
|
21
|
+
form?: string;
|
|
22
|
+
render?: RenderProp;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The group: a `<fieldset role="radiogroup">` around `Radio.Item`s, named
|
|
26
|
+
* by a `<legend>`, that holds the checked radio's value.
|
|
27
|
+
*/
|
|
28
|
+
export declare function RadioRoot(props: RadioRootProps): JSX.Element;
|