menhir 0.1.9 → 0.1.11
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-group/index.js +2 -0
- package/dist/checkbox-group/index.parts.js +5 -0
- package/dist/checkbox-group/item/CheckboxGroupItem.js +40 -0
- package/dist/checkbox-group/parent/CheckboxGroupParent.js +55 -0
- package/dist/checkbox-group/root/CheckboxGroupRoot.js +89 -0
- package/dist/checkbox-group/root/CheckboxGroupRootContext.js +10 -0
- package/dist/core/formContext.js +4 -0
- package/dist/field/root/FieldRoot.js +16 -6
- package/dist/form/Form.js +68 -0
- package/dist/form/index.js +1 -0
- package/dist/form/standard-schema/index.js +1 -0
- package/dist/form/standard-schema/issuesToErrors.js +16 -0
- package/dist/input/root/InputRoot.js +6 -2
- package/dist/source/checkbox-group/index.d.ts +1 -0
- package/dist/source/checkbox-group/index.js +1 -0
- package/dist/source/checkbox-group/index.parts.d.ts +4 -0
- package/dist/source/checkbox-group/index.parts.js +4 -0
- package/dist/source/checkbox-group/item/CheckboxGroupItem.d.ts +10 -0
- package/dist/source/checkbox-group/item/CheckboxGroupItem.jsx +47 -0
- package/dist/source/checkbox-group/parent/CheckboxGroupParent.d.ts +17 -0
- package/dist/source/checkbox-group/parent/CheckboxGroupParent.jsx +72 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRoot.d.ts +28 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRoot.jsx +75 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRootContext.d.ts +20 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRootContext.js +13 -0
- package/dist/source/core/formContext.d.ts +12 -0
- package/dist/source/core/formContext.js +5 -0
- package/dist/source/field/root/FieldRoot.jsx +29 -10
- package/dist/source/field/root/FieldRootContext.d.ts +2 -2
- package/dist/source/form/Form.d.ts +20 -0
- package/dist/source/form/Form.jsx +89 -0
- package/dist/source/form/index.d.ts +3 -0
- package/dist/source/form/index.js +1 -0
- package/dist/source/form/standard-schema/index.d.ts +2 -0
- package/dist/source/form/standard-schema/index.js +1 -0
- package/dist/source/form/standard-schema/issuesToErrors.d.ts +17 -0
- package/dist/source/form/standard-schema/issuesToErrors.js +18 -0
- package/dist/source/input/root/InputRoot.jsx +8 -2
- package/package.json +19 -2
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, Collapsible, Combobox, Context Menu, Dialog, Field, 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, Popover, Progress, 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
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CheckboxGroupRoot } from "./root/CheckboxGroupRoot.js";
|
|
2
|
+
import { CheckboxGroupItem } from "./item/CheckboxGroupItem.js";
|
|
3
|
+
import { CheckboxGroupParent } from "./parent/CheckboxGroupParent.js";
|
|
4
|
+
import { CheckboxIndicator } from "../checkbox/indicator/CheckboxIndicator.js";
|
|
5
|
+
export { CheckboxIndicator as Indicator, CheckboxGroupItem as Item, CheckboxGroupParent as Parent, CheckboxGroupRoot as Root };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.js";
|
|
2
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
3
|
+
import { createComponent } from "@solidjs/web";
|
|
4
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
5
|
+
// src/checkbox-group/item/CheckboxGroupItem.tsx
|
|
6
|
+
/**
|
|
7
|
+
* One box of the group: a `Checkbox.Root`, checked while the group's value
|
|
8
|
+
* has its `value`. Place it inside `CheckboxGroup.Root`.
|
|
9
|
+
*/
|
|
10
|
+
function CheckboxGroupItem(props) {
|
|
11
|
+
const group = useCheckboxGroupRootContext();
|
|
12
|
+
const checked = createMemo(() => group.value().includes(props.value));
|
|
13
|
+
const checkboxProps = merge(omit(props, "value", "disabled"), {
|
|
14
|
+
get checked() {
|
|
15
|
+
return checked();
|
|
16
|
+
},
|
|
17
|
+
get defaultChecked() {
|
|
18
|
+
return checked();
|
|
19
|
+
},
|
|
20
|
+
onCheckedChange: (included) => {
|
|
21
|
+
const next = group.requested().filter((item) => item !== props.value);
|
|
22
|
+
if (included) next.push(props.value);
|
|
23
|
+
group.setValue(next);
|
|
24
|
+
},
|
|
25
|
+
get name() {
|
|
26
|
+
return group.name();
|
|
27
|
+
},
|
|
28
|
+
get value() {
|
|
29
|
+
return props.value;
|
|
30
|
+
},
|
|
31
|
+
get form() {
|
|
32
|
+
return group.form();
|
|
33
|
+
},
|
|
34
|
+
get disabled() {
|
|
35
|
+
return group.disabled() || props.disabled;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return createComponent(CheckboxRoot, checkboxProps);
|
|
39
|
+
}
|
|
40
|
+
export { CheckboxGroupItem };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.js";
|
|
2
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
3
|
+
import { createComponent } from "@solidjs/web";
|
|
4
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
5
|
+
// src/checkbox-group/parent/CheckboxGroupParent.tsx
|
|
6
|
+
/**
|
|
7
|
+
* A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
|
|
8
|
+
* box of `values` is, mixed while some are. A click on a mixed parent checks
|
|
9
|
+
* all, on a checked one unchecks all, and on an unchecked one brings back
|
|
10
|
+
* the boxes checked when it was last mixed, or checks all. It submits
|
|
11
|
+
* nothing. Place it inside `CheckboxGroup.Root`.
|
|
12
|
+
*/
|
|
13
|
+
function CheckboxGroupParent(props) {
|
|
14
|
+
const group = useCheckboxGroupRootContext();
|
|
15
|
+
let set;
|
|
16
|
+
const boxes = createMemo((previous) => {
|
|
17
|
+
const list = group.value();
|
|
18
|
+
const checked = props.values.filter((item) => list.includes(item));
|
|
19
|
+
return {
|
|
20
|
+
checked,
|
|
21
|
+
remembered: previous && list === set ? previous.remembered : checked
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
const allChecked = createMemo(() => props.values.length > 0 && boxes().checked.length === props.values.length);
|
|
25
|
+
const someChecked = createMemo(() => boxes().checked.length > 0);
|
|
26
|
+
const checkboxProps = merge(omit(props, "values", "disabled"), {
|
|
27
|
+
get checked() {
|
|
28
|
+
return allChecked();
|
|
29
|
+
},
|
|
30
|
+
get defaultChecked() {
|
|
31
|
+
return allChecked();
|
|
32
|
+
},
|
|
33
|
+
get indeterminate() {
|
|
34
|
+
return someChecked() && !allChecked();
|
|
35
|
+
},
|
|
36
|
+
onCheckedChange: (checked) => {
|
|
37
|
+
const values = props.values;
|
|
38
|
+
const current = group.requested();
|
|
39
|
+
const others = current.filter((item) => !values.includes(item));
|
|
40
|
+
const { remembered } = boxes();
|
|
41
|
+
let next = others;
|
|
42
|
+
if (checked) next = values.some((item) => current.includes(item)) || remembered.length === 0 ? [...current, ...values.filter((item) => !current.includes(item))] : [...others, ...remembered];
|
|
43
|
+
set = next;
|
|
44
|
+
group.setValue(next);
|
|
45
|
+
},
|
|
46
|
+
get form() {
|
|
47
|
+
return group.form();
|
|
48
|
+
},
|
|
49
|
+
get disabled() {
|
|
50
|
+
return group.disabled() || props.disabled;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return createComponent(CheckboxRoot, checkboxProps);
|
|
54
|
+
}
|
|
55
|
+
export { CheckboxGroupParent };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
4
|
+
import { CheckboxGroupRootContext } from "./CheckboxGroupRootContext.js";
|
|
5
|
+
import { createComponent, effect, memo, ref, setAttribute, spread, template } from "@solidjs/web";
|
|
6
|
+
import { createUniqueId, omit, onSettled } from "solid-js";
|
|
7
|
+
// src/checkbox-group/root/CheckboxGroupRoot.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input type=radio 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>`);
|
|
9
|
+
var _tmpl$2 = /* @__PURE__ */ template(`<fieldset>`);
|
|
10
|
+
/**
|
|
11
|
+
* The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
|
|
12
|
+
* `<legend>`, that holds the values of the checked boxes.
|
|
13
|
+
*/
|
|
14
|
+
function CheckboxGroupRoot(props) {
|
|
15
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
16
|
+
get value() {
|
|
17
|
+
return props.value;
|
|
18
|
+
},
|
|
19
|
+
get defaultValue() {
|
|
20
|
+
return props.defaultValue ?? [];
|
|
21
|
+
},
|
|
22
|
+
onChange: (value) => props.onValueChange?.(value)
|
|
23
|
+
});
|
|
24
|
+
const context = {
|
|
25
|
+
value,
|
|
26
|
+
requested,
|
|
27
|
+
setValue: (next) => setValue(next),
|
|
28
|
+
name: () => props.name,
|
|
29
|
+
form: () => props.form,
|
|
30
|
+
disabled: () => props.disabled
|
|
31
|
+
};
|
|
32
|
+
const validityName = createUniqueId();
|
|
33
|
+
let root;
|
|
34
|
+
let input;
|
|
35
|
+
onSettled(() => {
|
|
36
|
+
const form = input?.form;
|
|
37
|
+
if (!form) return;
|
|
38
|
+
const onReset = () => setValue(props.defaultValue ?? []);
|
|
39
|
+
form.addEventListener("reset", onReset);
|
|
40
|
+
return () => form.removeEventListener("reset", onReset);
|
|
41
|
+
});
|
|
42
|
+
const elementProps = mergeElementProps({
|
|
43
|
+
get disabled() {
|
|
44
|
+
return props.disabled;
|
|
45
|
+
},
|
|
46
|
+
get "data-disabled"() {
|
|
47
|
+
return props.disabled ? "" : void 0;
|
|
48
|
+
},
|
|
49
|
+
get "data-required"() {
|
|
50
|
+
return props.required ? "" : void 0;
|
|
51
|
+
},
|
|
52
|
+
ref: (element) => {
|
|
53
|
+
root = element;
|
|
54
|
+
}
|
|
55
|
+
}, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
|
|
56
|
+
return createComponent(CheckboxGroupRootContext, {
|
|
57
|
+
value: context,
|
|
58
|
+
get children() {
|
|
59
|
+
return [memo(() => {
|
|
60
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
61
|
+
var _el$2 = _tmpl$2();
|
|
62
|
+
spread(_el$2, p, false);
|
|
63
|
+
return _el$2;
|
|
64
|
+
})());
|
|
65
|
+
}), (() => {
|
|
66
|
+
var _el$ = _tmpl$();
|
|
67
|
+
_el$.addEventListener("focus", () => root?.querySelector("[role=checkbox]:enabled")?.focus());
|
|
68
|
+
setAttribute(_el$, "name", validityName);
|
|
69
|
+
var _ref$ = input;
|
|
70
|
+
typeof _ref$ === "function" || Array.isArray(_ref$) ? ref(() => {
|
|
71
|
+
return _ref$;
|
|
72
|
+
}, _el$) : input = _el$;
|
|
73
|
+
effect(() => {
|
|
74
|
+
return {
|
|
75
|
+
e: props.disabled,
|
|
76
|
+
t: props.required && value().length === 0,
|
|
77
|
+
a: props.form
|
|
78
|
+
};
|
|
79
|
+
}, ({ e, t, a }, _p$) => {
|
|
80
|
+
e !== _p$?.e && setAttribute(_el$, "disabled", e);
|
|
81
|
+
t !== _p$?.t && setAttribute(_el$, "required", t);
|
|
82
|
+
a !== _p$?.a && setAttribute(_el$, "form", a);
|
|
83
|
+
});
|
|
84
|
+
return _el$;
|
|
85
|
+
})()];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
export { CheckboxGroupRoot };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/checkbox-group/root/CheckboxGroupRootContext.ts
|
|
4
|
+
var CheckboxGroupRootContext = createContext(null);
|
|
5
|
+
function useCheckboxGroupRootContext() {
|
|
6
|
+
const context = useContext(CheckboxGroupRootContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] CheckboxGroup parts must be placed within <CheckboxGroup.Root>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { CheckboxGroupRootContext, useCheckboxGroupRootContext };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { FormContext } from "../../core/formContext.js";
|
|
1
2
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
3
|
import { renderElement } from "../../core/renderElement.js";
|
|
3
4
|
import { FieldRootContext } from "./FieldRootContext.js";
|
|
4
5
|
import { createComponent, spread, template } from "@solidjs/web";
|
|
5
|
-
import { createSignal, createUniqueId, merge, omit, onFlush, onSettled } from "solid-js";
|
|
6
|
+
import { createSignal, createUniqueId, merge, omit, onFlush, onSettled, useContext } from "solid-js";
|
|
6
7
|
// src/field/root/FieldRoot.tsx
|
|
7
8
|
var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
8
9
|
var controlSelector = "input,select,textarea";
|
|
@@ -15,21 +16,30 @@ function FieldRoot(props) {
|
|
|
15
16
|
const controlId = createUniqueId();
|
|
16
17
|
const descriptionId = createUniqueId();
|
|
17
18
|
const [touched, setTouched] = createSignal(false);
|
|
18
|
-
const [invalidControl, setInvalidControl] = createSignal(void 0, { equals:
|
|
19
|
+
const [invalidControl, setInvalidControl] = createSignal(void 0, { equals: (a, b) => JSON.stringify(a) === JSON.stringify(b) });
|
|
19
20
|
const [settled, setSettled] = createSignal(false);
|
|
20
21
|
const [descriptions, setDescriptions] = createSignal(0);
|
|
21
22
|
const [errors, setErrors] = createSignal([]);
|
|
22
23
|
let root;
|
|
23
24
|
const readValidity = () => {
|
|
24
25
|
if (!root) return void 0;
|
|
25
|
-
for (const control of root.querySelectorAll(controlSelector))
|
|
26
|
+
for (const control of root.querySelectorAll(controlSelector)) {
|
|
27
|
+
const { validity } = control;
|
|
28
|
+
if (control.willValidate && !validity.valid) {
|
|
29
|
+
const copy = { validationMessage: control.validationMessage };
|
|
30
|
+
for (const flag in validity) copy[flag] = validity[flag];
|
|
31
|
+
return copy;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
26
34
|
};
|
|
27
35
|
const update = (touch) => onFlush(() => {
|
|
28
36
|
setInvalidControl(readValidity());
|
|
29
37
|
if (touch !== void 0) setTouched(touch);
|
|
30
38
|
});
|
|
39
|
+
const form = useContext(FormContext);
|
|
40
|
+
const formError = () => props.name === void 0 ? void 0 : form?.error(props.name);
|
|
31
41
|
const invalid = () => !!props.invalid || !!props.error || invalidControl() !== void 0;
|
|
32
|
-
const showsError = () => touched() && invalid();
|
|
42
|
+
const showsError = () => touched() && invalid() || !!formError();
|
|
33
43
|
const attributes = {
|
|
34
44
|
get "data-invalid"() {
|
|
35
45
|
return showsError() ? "" : void 0;
|
|
@@ -47,8 +57,8 @@ function FieldRoot(props) {
|
|
|
47
57
|
name: () => props.name,
|
|
48
58
|
disabled: () => props.disabled,
|
|
49
59
|
showsError,
|
|
50
|
-
message: () => props.error || invalidControl()?.validationMessage,
|
|
51
|
-
validity:
|
|
60
|
+
message: () => touched() && props.error || formError() || invalidControl()?.validationMessage,
|
|
61
|
+
validity: invalidControl,
|
|
52
62
|
describedBy() {
|
|
53
63
|
const ids = [];
|
|
54
64
|
if (!settled() || descriptions() > 0) ids.push(descriptionId);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { FormContext } from "../core/formContext.js";
|
|
2
|
+
import { callHandler, mergeElementProps } from "../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../core/renderElement.js";
|
|
4
|
+
import { createComponent, spread, template } from "@solidjs/web";
|
|
5
|
+
import { createMemo, createSignal, omit, onFlush, onSettled, untrack } from "solid-js";
|
|
6
|
+
// src/form/Form.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<form>`);
|
|
8
|
+
/**
|
|
9
|
+
* A `<form>` that hands `errors` to the Fields inside it by `name`, and
|
|
10
|
+
* blocks a submission while a control is invalid, focusing the first one.
|
|
11
|
+
* It holds no values: they stay in the controls, or in the app's state.
|
|
12
|
+
* `onSubmit` is called only for a submission that is not blocked.
|
|
13
|
+
*/
|
|
14
|
+
function Form(props) {
|
|
15
|
+
const errors = createMemo(() => props.errors);
|
|
16
|
+
const [hidden, setHidden] = createSignal(() => {
|
|
17
|
+
errors();
|
|
18
|
+
return /* @__PURE__ */ new Set();
|
|
19
|
+
});
|
|
20
|
+
const context = { error(name) {
|
|
21
|
+
const current = errors();
|
|
22
|
+
const names = hidden();
|
|
23
|
+
return current && names !== true && !names.has(name) && Object.hasOwn(current, name) ? current[name] : void 0;
|
|
24
|
+
} };
|
|
25
|
+
const hide = (update) => {
|
|
26
|
+
const met = untrack(errors);
|
|
27
|
+
onFlush(() => {
|
|
28
|
+
if (untrack(errors) === met) setHidden((names) => names === true ? names : update(names));
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const hideError = (event) => {
|
|
32
|
+
const name = event.target.name;
|
|
33
|
+
if (name) hide((names) => new Set(names).add(name));
|
|
34
|
+
};
|
|
35
|
+
let form;
|
|
36
|
+
onSettled(() => {
|
|
37
|
+
if (form) form.noValidate = true;
|
|
38
|
+
});
|
|
39
|
+
const ownProps = {
|
|
40
|
+
onInput: hideError,
|
|
41
|
+
onChange: hideError,
|
|
42
|
+
onReset: (event) => hide((names) => event.defaultPrevented ? names : true),
|
|
43
|
+
onSubmit: (event) => {
|
|
44
|
+
if (!event.submitter?.formNoValidate && !form.checkValidity()) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
Array.from(form.elements).find((control) => control.willValidate && !control.validity.valid)?.focus();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
callHandler(props.onSubmit, event);
|
|
50
|
+
},
|
|
51
|
+
ref: (element) => {
|
|
52
|
+
form = element;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const consumerProps = omit(props, "render", "errors", "onSubmit");
|
|
56
|
+
const elementProps = mergeElementProps(ownProps, consumerProps);
|
|
57
|
+
return createComponent(FormContext, {
|
|
58
|
+
value: context,
|
|
59
|
+
get children() {
|
|
60
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
61
|
+
var _el$ = _tmpl$();
|
|
62
|
+
spread(_el$, p, false);
|
|
63
|
+
return _el$;
|
|
64
|
+
})());
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export { Form };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Form } from "./Form.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { issuesToErrors } from "./issuesToErrors.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/form/standard-schema/issuesToErrors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Turns a schema's issues into Form's `errors`: each issue's message under
|
|
4
|
+
* its path, the keys joined with dots (`content.year`, `sources.0`). The
|
|
5
|
+
* first issue on a path wins. An issue with no path, an error of the whole
|
|
6
|
+
* form, goes under `""`.
|
|
7
|
+
*/
|
|
8
|
+
function issuesToErrors(issues) {
|
|
9
|
+
const errors = Object.create(null);
|
|
10
|
+
for (const issue of issues) {
|
|
11
|
+
const key = (issue.path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
|
|
12
|
+
errors[key] ??= issue.message;
|
|
13
|
+
}
|
|
14
|
+
return errors;
|
|
15
|
+
}
|
|
16
|
+
export { issuesToErrors };
|
|
@@ -17,12 +17,13 @@ function InputRoot(props) {
|
|
|
17
17
|
onSettled(() => {
|
|
18
18
|
const form = element?.form;
|
|
19
19
|
if (!form) return;
|
|
20
|
+
let disposed = false;
|
|
20
21
|
const onReset = (event) => {
|
|
21
22
|
const text = element.value;
|
|
22
23
|
const defaultText = untrack(() => props.defaultValue) ?? "";
|
|
23
24
|
if (untrack(() => props.value) !== void 0) element.defaultValue = text;
|
|
24
25
|
setTimeout(() => {
|
|
25
|
-
if (event.defaultPrevented || text === defaultText) return;
|
|
26
|
+
if (disposed || event.defaultPrevented || text === defaultText) return;
|
|
26
27
|
props.onValueChange?.(defaultText, {
|
|
27
28
|
reason: "reset",
|
|
28
29
|
event
|
|
@@ -31,7 +32,10 @@ function InputRoot(props) {
|
|
|
31
32
|
});
|
|
32
33
|
};
|
|
33
34
|
form.addEventListener("reset", onReset);
|
|
34
|
-
return () =>
|
|
35
|
+
return () => {
|
|
36
|
+
disposed = true;
|
|
37
|
+
form.removeEventListener("reset", onReset);
|
|
38
|
+
};
|
|
35
39
|
});
|
|
36
40
|
const elementProps = merge(mergeElementProps({
|
|
37
41
|
onInput: (event) => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as CheckboxGroup from "./index.parts.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as CheckboxGroup from "./index.parts.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CheckboxGroupRoot as Root } from "./root/CheckboxGroupRoot.tsx";
|
|
2
|
+
export { CheckboxGroupItem as Item } from "./item/CheckboxGroupItem.tsx";
|
|
3
|
+
export { CheckboxGroupParent as Parent } from "./parent/CheckboxGroupParent.tsx";
|
|
4
|
+
export { CheckboxIndicator as Indicator } from "../checkbox/indicator/CheckboxIndicator.tsx";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CheckboxGroupRoot as Root } from "./root/CheckboxGroupRoot.jsx";
|
|
2
|
+
export { CheckboxGroupItem as Item } from "./item/CheckboxGroupItem.jsx";
|
|
3
|
+
export { CheckboxGroupParent as Parent } from "./parent/CheckboxGroupParent.jsx";
|
|
4
|
+
export { CheckboxIndicator as Indicator } from "../checkbox/indicator/CheckboxIndicator.jsx";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CheckboxRootProps } from "../../checkbox/root/CheckboxRoot.tsx";
|
|
2
|
+
export interface CheckboxGroupItemProps extends Omit<CheckboxRootProps, "checked" | "defaultChecked" | "onCheckedChange" | "indeterminate" | "name" | "value" | "required" | "form"> {
|
|
3
|
+
/** Identifies the box in the group's value, and is submitted while it is checked. */
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* One box of the group: a `Checkbox.Root`, checked while the group's value
|
|
8
|
+
* has its `value`. Place it inside `CheckboxGroup.Root`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function CheckboxGroupItem(props: CheckboxGroupItemProps): import("@solidjs/web").JSX.Element;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
2
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.jsx";
|
|
3
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* One box of the group: a `Checkbox.Root`, checked while the group's value
|
|
6
|
+
* has its `value`. Place it inside `CheckboxGroup.Root`.
|
|
7
|
+
*/
|
|
8
|
+
export function CheckboxGroupItem(props) {
|
|
9
|
+
const group = useCheckboxGroupRootContext();
|
|
10
|
+
// A memo, so that a change to the list that leaves this box as it was
|
|
11
|
+
// does not run the box's bindings again: a hidden input the user changed
|
|
12
|
+
// in the same turn, not yet in the list, would be written back over.
|
|
13
|
+
const checked = createMemo(() => group.value().includes(props.value));
|
|
14
|
+
const own = {
|
|
15
|
+
get checked() {
|
|
16
|
+
return checked();
|
|
17
|
+
},
|
|
18
|
+
// The group puts the list back on a form reset, once. Checkbox's own
|
|
19
|
+
// reset asks for `defaultChecked`, so passing the box's state makes it
|
|
20
|
+
// ask for nothing, and put its hidden input back to that state.
|
|
21
|
+
get defaultChecked() {
|
|
22
|
+
return checked();
|
|
23
|
+
},
|
|
24
|
+
onCheckedChange: (included) => {
|
|
25
|
+
// Reads lag writes, so a second change before the flush builds on
|
|
26
|
+
// the first, not on what `value` still reads.
|
|
27
|
+
const next = group.requested().filter((item) => item !== props.value);
|
|
28
|
+
if (included)
|
|
29
|
+
next.push(props.value);
|
|
30
|
+
group.setValue(next);
|
|
31
|
+
},
|
|
32
|
+
get name() {
|
|
33
|
+
return group.name();
|
|
34
|
+
},
|
|
35
|
+
get value() {
|
|
36
|
+
return props.value;
|
|
37
|
+
},
|
|
38
|
+
get form() {
|
|
39
|
+
return group.form();
|
|
40
|
+
},
|
|
41
|
+
get disabled() {
|
|
42
|
+
return group.disabled() || props.disabled;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const checkboxProps = merge(omit(props, "value", "disabled"), own);
|
|
46
|
+
return <CheckboxRoot {...checkboxProps}/>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CheckboxRootProps } from "../../checkbox/root/CheckboxRoot.tsx";
|
|
2
|
+
export interface CheckboxGroupParentProps extends Omit<CheckboxRootProps, "checked" | "defaultChecked" | "onCheckedChange" | "indeterminate" | "name" | "value" | "required" | "form"> {
|
|
3
|
+
/**
|
|
4
|
+
* The values of the boxes the parent checks and unchecks, usually every
|
|
5
|
+
* Item's. It is checked while the group's value has all of them, mixed
|
|
6
|
+
* while it has some.
|
|
7
|
+
*/
|
|
8
|
+
values: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
|
|
12
|
+
* box of `values` is, mixed while some are. A click on a mixed parent checks
|
|
13
|
+
* all, on a checked one unchecks all, and on an unchecked one brings back
|
|
14
|
+
* the boxes checked when it was last mixed, or checks all. It submits
|
|
15
|
+
* nothing. Place it inside `CheckboxGroup.Root`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function CheckboxGroupParent(props: CheckboxGroupParentProps): import("@solidjs/web").JSX.Element;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
2
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.jsx";
|
|
3
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
|
|
6
|
+
* box of `values` is, mixed while some are. A click on a mixed parent checks
|
|
7
|
+
* all, on a checked one unchecks all, and on an unchecked one brings back
|
|
8
|
+
* the boxes checked when it was last mixed, or checks all. It submits
|
|
9
|
+
* nothing. Place it inside `CheckboxGroup.Root`.
|
|
10
|
+
*/
|
|
11
|
+
export function CheckboxGroupParent(props) {
|
|
12
|
+
const group = useCheckboxGroupRootContext();
|
|
13
|
+
// The list the parent set last, by reference: a list it did not set is
|
|
14
|
+
// a change the user made, through a box or a reset.
|
|
15
|
+
let set;
|
|
16
|
+
// The boxes of `values` that are checked, and the combination a click on
|
|
17
|
+
// the unchecked parent brings back: the one the user made last, which a
|
|
18
|
+
// change by the parent leaves as it was, as the WAI-ARIA mixed-state
|
|
19
|
+
// example does. Read by the bindings below, so it sees every change.
|
|
20
|
+
const boxes = createMemo((previous) => {
|
|
21
|
+
const list = group.value();
|
|
22
|
+
const checked = props.values.filter((item) => list.includes(item));
|
|
23
|
+
return {
|
|
24
|
+
checked,
|
|
25
|
+
remembered: previous && list === set ? previous.remembered : checked,
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
// Memos, so that a change to the list that leaves the parent as it was
|
|
29
|
+
// does not run its bindings again (CheckboxGroupItem).
|
|
30
|
+
const allChecked = createMemo(() => props.values.length > 0 && boxes().checked.length === props.values.length);
|
|
31
|
+
const someChecked = createMemo(() => boxes().checked.length > 0);
|
|
32
|
+
const own = {
|
|
33
|
+
get checked() {
|
|
34
|
+
return allChecked();
|
|
35
|
+
},
|
|
36
|
+
// The group puts the list back on a form reset, so Checkbox's own reset
|
|
37
|
+
// must ask for nothing (CheckboxGroupItem).
|
|
38
|
+
get defaultChecked() {
|
|
39
|
+
return allChecked();
|
|
40
|
+
},
|
|
41
|
+
get indeterminate() {
|
|
42
|
+
return someChecked() && !allChecked();
|
|
43
|
+
},
|
|
44
|
+
// Checkbox asks for the opposite of `checked`: `false` from a checked
|
|
45
|
+
// parent, `true` from a mixed or an unchecked one.
|
|
46
|
+
onCheckedChange: (checked) => {
|
|
47
|
+
const values = props.values;
|
|
48
|
+
const current = group.requested();
|
|
49
|
+
const others = current.filter((item) => !values.includes(item));
|
|
50
|
+
const { remembered } = boxes();
|
|
51
|
+
let next = others;
|
|
52
|
+
if (checked) {
|
|
53
|
+
// Mixed, or unchecked with nothing to bring back: check all.
|
|
54
|
+
next =
|
|
55
|
+
values.some((item) => current.includes(item)) ||
|
|
56
|
+
remembered.length === 0
|
|
57
|
+
? [...current, ...values.filter((item) => !current.includes(item))]
|
|
58
|
+
: [...others, ...remembered];
|
|
59
|
+
}
|
|
60
|
+
set = next;
|
|
61
|
+
group.setValue(next);
|
|
62
|
+
},
|
|
63
|
+
get form() {
|
|
64
|
+
return group.form();
|
|
65
|
+
},
|
|
66
|
+
get disabled() {
|
|
67
|
+
return group.disabled() || props.disabled;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
const checkboxProps = merge(omit(props, "values", "disabled"), own);
|
|
71
|
+
return <CheckboxRoot {...checkboxProps}/>;
|
|
72
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface CheckboxGroupRootProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** The values of the checked boxes, when controlled. `[]` for none. */
|
|
5
|
+
value?: string[];
|
|
6
|
+
/** The values of the boxes checked at first, when uncontrolled. */
|
|
7
|
+
defaultValue?: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Called with the new list when the user checks or unchecks a box, or
|
|
10
|
+
* the form is reset. When controlled, the group changes only if `value`
|
|
11
|
+
* does.
|
|
12
|
+
*/
|
|
13
|
+
onValueChange?: (value: string[]) => void;
|
|
14
|
+
/** The name each checked box is submitted under, one entry per box. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** Whether every box ignores the user. The fieldset is disabled too. */
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
/** Whether the form can be submitted only while a box is checked. */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
/** The `id` of the form the boxes belong to, when they are not inside it. */
|
|
21
|
+
form?: string;
|
|
22
|
+
render?: RenderProp;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
|
|
26
|
+
* `<legend>`, that holds the values of the checked boxes.
|
|
27
|
+
*/
|
|
28
|
+
export declare function CheckboxGroupRoot(props: CheckboxGroupRootProps): JSX.Element;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createUniqueId, omit, onSettled } 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 { CheckboxGroupRootContext } from "./CheckboxGroupRootContext.js";
|
|
6
|
+
/**
|
|
7
|
+
* The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
|
|
8
|
+
* `<legend>`, that holds the values of the checked boxes.
|
|
9
|
+
*/
|
|
10
|
+
export function CheckboxGroupRoot(props) {
|
|
11
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
12
|
+
get value() {
|
|
13
|
+
return props.value;
|
|
14
|
+
},
|
|
15
|
+
get defaultValue() {
|
|
16
|
+
return props.defaultValue ?? [];
|
|
17
|
+
},
|
|
18
|
+
onChange: (value) => props.onValueChange?.(value),
|
|
19
|
+
});
|
|
20
|
+
const context = {
|
|
21
|
+
value,
|
|
22
|
+
requested,
|
|
23
|
+
setValue: (next) => setValue(next),
|
|
24
|
+
name: () => props.name,
|
|
25
|
+
form: () => props.form,
|
|
26
|
+
disabled: () => props.disabled,
|
|
27
|
+
};
|
|
28
|
+
// The hidden radio's name, which the server render and hydration agree on.
|
|
29
|
+
const validityName = createUniqueId();
|
|
30
|
+
let root;
|
|
31
|
+
let input;
|
|
32
|
+
// A form reset puts the list back to its default, once, through the
|
|
33
|
+
// setter, so that a controlled group asks once. Each Item's Checkbox asks
|
|
34
|
+
// for nothing itself (CheckboxGroupItem).
|
|
35
|
+
onSettled(() => {
|
|
36
|
+
const form = input?.form;
|
|
37
|
+
if (!form)
|
|
38
|
+
return;
|
|
39
|
+
const onReset = () => setValue(props.defaultValue ?? []);
|
|
40
|
+
form.addEventListener("reset", onReset);
|
|
41
|
+
return () => form.removeEventListener("reset", onReset);
|
|
42
|
+
});
|
|
43
|
+
const ownProps = {
|
|
44
|
+
get disabled() {
|
|
45
|
+
return props.disabled;
|
|
46
|
+
},
|
|
47
|
+
get "data-disabled"() {
|
|
48
|
+
return props.disabled ? "" : undefined;
|
|
49
|
+
},
|
|
50
|
+
get "data-required"() {
|
|
51
|
+
return props.required ? "" : undefined;
|
|
52
|
+
},
|
|
53
|
+
ref: (element) => {
|
|
54
|
+
root = element;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
|
|
58
|
+
return (<CheckboxGroupRootContext value={context}>
|
|
59
|
+
{renderElement(props, elementProps, (p) => (<fieldset {...p}/>))}
|
|
60
|
+
{/* "At least one" has no constraint of its own. An unchecked radio
|
|
61
|
+
that is required while the list is empty blocks a submission, and
|
|
62
|
+
the browser's message for it asks to select an option. Browsers
|
|
63
|
+
find a radio missing only with a name, unique so that no other
|
|
64
|
+
group's radio joins its radio group; unchecked, it submits nothing.
|
|
65
|
+
Its `required` follows the list, not its checkedness, which a form
|
|
66
|
+
reset would change behind the binding's back. */}
|
|
67
|
+
<input type="radio" name={validityName} aria-hidden="true" tabindex={-1} style={visuallyHidden} disabled={props.disabled} required={props.required && value().length === 0} form={props.form}
|
|
68
|
+
// The browser focuses an invalid input when it blocks a submission.
|
|
69
|
+
// The user knows the boxes, not the hidden input.
|
|
70
|
+
onFocus={() => root?.querySelector("[role=checkbox]:enabled")?.focus()} ref={input}/>
|
|
71
|
+
</CheckboxGroupRootContext>);
|
|
72
|
+
}
|
|
73
|
+
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
74
|
+
// browser can still anchor a validation message to it.
|
|
75
|
+
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,20 @@
|
|
|
1
|
+
/** What the group shares with its Items and its Parent. */
|
|
2
|
+
export interface CheckboxGroupRootContextValue {
|
|
3
|
+
/** The values of the checked boxes. */
|
|
4
|
+
value(): string[];
|
|
5
|
+
/**
|
|
6
|
+
* The list set last in this turn, or `value`, untracked: for a change
|
|
7
|
+
* that builds on one made earlier in the same turn.
|
|
8
|
+
*/
|
|
9
|
+
requested(): string[];
|
|
10
|
+
/** Asks to set the whole list. */
|
|
11
|
+
setValue(next: string[]): void;
|
|
12
|
+
/** The `name` each Item submits under. */
|
|
13
|
+
name(): string | undefined;
|
|
14
|
+
/** The `id` of the form the Items belong to, when they are not inside it. */
|
|
15
|
+
form(): string | undefined;
|
|
16
|
+
/** Whether the whole group is disabled. */
|
|
17
|
+
disabled(): boolean | undefined;
|
|
18
|
+
}
|
|
19
|
+
export declare const CheckboxGroupRootContext: import("solid-js").Context<CheckboxGroupRootContextValue | null>;
|
|
20
|
+
export declare function useCheckboxGroupRootContext(): CheckboxGroupRootContextValue;
|
|
@@ -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 CheckboxGroupRootContext = createContext(null);
|
|
6
|
+
export function useCheckboxGroupRootContext() {
|
|
7
|
+
const context = useContext(CheckboxGroupRootContext);
|
|
8
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
9
|
+
if (isDev && !context) {
|
|
10
|
+
throw new Error("[menhir] CheckboxGroup parts must be placed within <CheckboxGroup.Root>.");
|
|
11
|
+
}
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors keyed by a Field's `name`, each one message, such as a server's
|
|
3
|
+
* answer to a submission or a schema's issues. A nested or repeated value's
|
|
4
|
+
* key is its path joined with dots: `content.year`, `sources.0`.
|
|
5
|
+
*/
|
|
6
|
+
export type FormErrors = Readonly<Partial<Record<string, string>>>;
|
|
7
|
+
/** What Form shares with the Fields inside it. */
|
|
8
|
+
export interface FormContextValue {
|
|
9
|
+
/** The error Form shows in the Field with this `name`, if any. */
|
|
10
|
+
error(name: string): string | undefined;
|
|
11
|
+
}
|
|
12
|
+
export declare const FormContext: import("solid-js").Context<FormContextValue | null>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { createSignal, createUniqueId, merge, omit, onFlush, onSettled, } from "solid-js";
|
|
1
|
+
import { createSignal, createUniqueId, merge, omit, onFlush, onSettled, useContext, } from "solid-js";
|
|
2
|
+
import { FormContext } from "../../core/formContext.js";
|
|
2
3
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
4
|
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
5
|
import { FieldRootContext } from "./FieldRootContext.js";
|
|
@@ -12,10 +13,12 @@ export function FieldRoot(props) {
|
|
|
12
13
|
const controlId = createUniqueId();
|
|
13
14
|
const descriptionId = createUniqueId();
|
|
14
15
|
const [touched, setTouched] = createSignal(false);
|
|
15
|
-
// The first control inside the Root that the browser finds invalid
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
|
|
16
|
+
// The first control inside the Root that the browser finds invalid, as
|
|
17
|
+
// its message and its validity's flags, copied: the element's own
|
|
18
|
+
// `validity` is live, the same object as its flags change. Compared by
|
|
19
|
+
// value, so an event that changes neither, such as each `invalid` of a
|
|
20
|
+
// submission, wakes no reader.
|
|
21
|
+
const [invalidControl, setInvalidControl] = createSignal(undefined, { equals: (a, b) => JSON.stringify(a) === JSON.stringify(b) });
|
|
19
22
|
// False in the server HTML and the first render, when the control names
|
|
20
23
|
// the Description before it is rendered (README, "Decisions").
|
|
21
24
|
const [settled, setSettled] = createSignal(false);
|
|
@@ -26,8 +29,17 @@ export function FieldRoot(props) {
|
|
|
26
29
|
if (!root)
|
|
27
30
|
return undefined;
|
|
28
31
|
for (const control of root.querySelectorAll(controlSelector)) {
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
const { validity } = control;
|
|
33
|
+
if (control.willValidate && !validity.valid) {
|
|
34
|
+
const copy = {
|
|
35
|
+
validationMessage: control.validationMessage,
|
|
36
|
+
};
|
|
37
|
+
for (const flag in validity) {
|
|
38
|
+
copy[flag] =
|
|
39
|
+
validity[flag];
|
|
40
|
+
}
|
|
41
|
+
return copy;
|
|
42
|
+
}
|
|
31
43
|
}
|
|
32
44
|
return undefined;
|
|
33
45
|
};
|
|
@@ -40,8 +52,12 @@ export function FieldRoot(props) {
|
|
|
40
52
|
if (touch !== undefined)
|
|
41
53
|
setTouched(touch);
|
|
42
54
|
});
|
|
55
|
+
// The error the Form around the Root hands it by `name`, the answer to a
|
|
56
|
+
// submission, which shows touched or not (src/form/README.md).
|
|
57
|
+
const form = useContext(FormContext);
|
|
58
|
+
const formError = () => props.name === undefined ? undefined : form?.error(props.name);
|
|
43
59
|
const invalid = () => !!props.invalid || !!props.error || invalidControl() !== undefined;
|
|
44
|
-
const showsError = () => touched() && invalid();
|
|
60
|
+
const showsError = () => (touched() && invalid()) || !!formError();
|
|
45
61
|
const attributes = {
|
|
46
62
|
get "data-invalid"() {
|
|
47
63
|
return showsError() ? "" : undefined;
|
|
@@ -59,8 +75,11 @@ export function FieldRoot(props) {
|
|
|
59
75
|
name: () => props.name,
|
|
60
76
|
disabled: () => props.disabled,
|
|
61
77
|
showsError,
|
|
62
|
-
|
|
63
|
-
|
|
78
|
+
// The Field's own error shows once touched; before, only the Form's.
|
|
79
|
+
message: () => (touched() && props.error) ||
|
|
80
|
+
formError() ||
|
|
81
|
+
invalidControl()?.validationMessage,
|
|
82
|
+
validity: invalidControl,
|
|
64
83
|
describedBy() {
|
|
65
84
|
const ids = [];
|
|
66
85
|
if (!settled() || descriptions() > 0)
|
|
@@ -29,8 +29,8 @@ export interface FieldRootContextValue {
|
|
|
29
29
|
showsError(): boolean;
|
|
30
30
|
/** The message an Error without children shows. */
|
|
31
31
|
message(): string | undefined;
|
|
32
|
-
/** The browser's validity of the first invalid control, if any. */
|
|
33
|
-
validity(): ValidityState | undefined;
|
|
32
|
+
/** The browser's validity of the first invalid control, if any, copied. */
|
|
33
|
+
validity(): Partial<ValidityState> | undefined;
|
|
34
34
|
/** The `id`s the control names in `aria-describedby`, if any. */
|
|
35
35
|
describedBy(): string | undefined;
|
|
36
36
|
/**
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { FormErrors } from "../core/formContext.ts";
|
|
3
|
+
import type { RenderProp } from "../core/renderElement.tsx";
|
|
4
|
+
export interface FormProps extends JSX.FormHTMLAttributes<HTMLFormElement> {
|
|
5
|
+
/**
|
|
6
|
+
* Errors keyed by a Field's `name`, such as a server's answer to a
|
|
7
|
+
* submission or a schema's issues (`issuesToErrors`). Each shows in its
|
|
8
|
+
* Field at once, and hides once its control is edited, until the next
|
|
9
|
+
* `errors` object.
|
|
10
|
+
*/
|
|
11
|
+
errors?: FormErrors;
|
|
12
|
+
render?: RenderProp;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A `<form>` that hands `errors` to the Fields inside it by `name`, and
|
|
16
|
+
* blocks a submission while a control is invalid, focusing the first one.
|
|
17
|
+
* It holds no values: they stay in the controls, or in the app's state.
|
|
18
|
+
* `onSubmit` is called only for a submission that is not blocked.
|
|
19
|
+
*/
|
|
20
|
+
export declare function Form(props: FormProps): JSX.Element;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createMemo, createSignal, omit, onFlush, onSettled, untrack, } from "solid-js";
|
|
2
|
+
import { FormContext } from "../core/formContext.js";
|
|
3
|
+
import { callHandler, mergeElementProps } from "../core/mergeElementProps.js";
|
|
4
|
+
import { renderElement } from "../core/renderElement.jsx";
|
|
5
|
+
/**
|
|
6
|
+
* A `<form>` that hands `errors` to the Fields inside it by `name`, and
|
|
7
|
+
* blocks a submission while a control is invalid, focusing the first one.
|
|
8
|
+
* It holds no values: they stay in the controls, or in the app's state.
|
|
9
|
+
* `onSubmit` is called only for a submission that is not blocked.
|
|
10
|
+
*/
|
|
11
|
+
export function Form(props) {
|
|
12
|
+
// The names whose error is hidden, or `true` for all of them after a
|
|
13
|
+
// reset. Derived from `errors`, so each new object shows every error
|
|
14
|
+
// again.
|
|
15
|
+
// Held in a memo: `errors={{ email: message() }}` is a prop getter that
|
|
16
|
+
// makes a new object on each read, and is the same errors until
|
|
17
|
+
// `message()` changes.
|
|
18
|
+
const errors = createMemo(() => props.errors);
|
|
19
|
+
const [hidden, setHidden] = createSignal(() => {
|
|
20
|
+
void errors();
|
|
21
|
+
return new Set();
|
|
22
|
+
});
|
|
23
|
+
const context = {
|
|
24
|
+
error(name) {
|
|
25
|
+
const current = errors();
|
|
26
|
+
const names = hidden();
|
|
27
|
+
return current &&
|
|
28
|
+
names !== true &&
|
|
29
|
+
!names.has(name) &&
|
|
30
|
+
Object.hasOwn(current, name)
|
|
31
|
+
? current[name]
|
|
32
|
+
: undefined;
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
// Hides errors once the event's flush has run, as Field writes its own
|
|
36
|
+
// state, and only the `errors` the event met: a new object passed in the
|
|
37
|
+
// same update is the answer to a later submission.
|
|
38
|
+
const hide = (update) => {
|
|
39
|
+
const met = untrack(errors);
|
|
40
|
+
onFlush(() => {
|
|
41
|
+
if (untrack(errors) === met) {
|
|
42
|
+
setHidden((names) => (names === true ? names : update(names)));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
// An edit makes the error of that control's name stale.
|
|
47
|
+
const hideError = (event) => {
|
|
48
|
+
const name = event.target.name;
|
|
49
|
+
if (name)
|
|
50
|
+
hide((names) => new Set(names).add(name));
|
|
51
|
+
};
|
|
52
|
+
let form;
|
|
53
|
+
// Not in the server HTML, so the browser validates a submission made
|
|
54
|
+
// before hydration itself.
|
|
55
|
+
onSettled(() => {
|
|
56
|
+
if (form)
|
|
57
|
+
form.noValidate = true;
|
|
58
|
+
});
|
|
59
|
+
const ownProps = {
|
|
60
|
+
onInput: hideError,
|
|
61
|
+
onChange: hideError,
|
|
62
|
+
// A reset the app cancels, by its own handler, keeps the errors.
|
|
63
|
+
onReset: (event) => hide((names) => (event.defaultPrevented ? names : true)),
|
|
64
|
+
onSubmit: (event) => {
|
|
65
|
+
const submitter = event.submitter;
|
|
66
|
+
// `checkValidity()` fires `invalid` at each invalid control, which
|
|
67
|
+
// touches its Field, and shows nothing.
|
|
68
|
+
if (!submitter?.formNoValidate && !form.checkValidity()) {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
// The first invalid control in tree order. A Menhir control's
|
|
71
|
+
// hidden input hands focus to its button.
|
|
72
|
+
Array.from(form.elements)
|
|
73
|
+
.find((control) => control.willValidate && !control.validity.valid)
|
|
74
|
+
?.focus();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
callHandler(props.onSubmit, event);
|
|
78
|
+
},
|
|
79
|
+
ref: (element) => {
|
|
80
|
+
form = element;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
// `onSubmit` is called by Form's own, only when not blocked.
|
|
84
|
+
const consumerProps = omit(props, "render", "errors", "onSubmit");
|
|
85
|
+
const elementProps = mergeElementProps(ownProps, consumerProps);
|
|
86
|
+
return (<FormContext value={context}>
|
|
87
|
+
{renderElement(props, elementProps, (p) => (<form {...p}/>))}
|
|
88
|
+
</FormContext>);
|
|
89
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Form } from "./Form.jsx";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { issuesToErrors } from "./issuesToErrors.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FormErrors } from "../../core/formContext.ts";
|
|
2
|
+
/** A segment of an issue's path, in its object form. */
|
|
3
|
+
export interface PathSegment {
|
|
4
|
+
readonly key: PropertyKey;
|
|
5
|
+
}
|
|
6
|
+
/** A problem a schema found: its message, and where in the value. */
|
|
7
|
+
export interface Issue {
|
|
8
|
+
readonly message: string;
|
|
9
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Turns a schema's issues into Form's `errors`: each issue's message under
|
|
13
|
+
* its path, the keys joined with dots (`content.year`, `sources.0`). The
|
|
14
|
+
* first issue on a path wins. An issue with no path, an error of the whole
|
|
15
|
+
* form, goes under `""`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function issuesToErrors(issues: ReadonlyArray<Issue>): FormErrors;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a schema's issues into Form's `errors`: each issue's message under
|
|
3
|
+
* its path, the keys joined with dots (`content.year`, `sources.0`). The
|
|
4
|
+
* first issue on a path wins. An issue with no path, an error of the whole
|
|
5
|
+
* form, goes under `""`.
|
|
6
|
+
*/
|
|
7
|
+
export function issuesToErrors(issues) {
|
|
8
|
+
// No prototype, so a key such as `__proto__` is an own key, and
|
|
9
|
+
// `constructor` is not read from `Object.prototype`.
|
|
10
|
+
const errors = Object.create(null);
|
|
11
|
+
for (const issue of issues) {
|
|
12
|
+
const key = (issue.path ?? [])
|
|
13
|
+
.map((segment) => String(typeof segment === "object" ? segment.key : segment))
|
|
14
|
+
.join(".");
|
|
15
|
+
errors[key] ??= issue.message;
|
|
16
|
+
}
|
|
17
|
+
return errors;
|
|
18
|
+
}
|
|
@@ -24,6 +24,9 @@ export function InputRoot(props) {
|
|
|
24
24
|
const form = element?.form;
|
|
25
25
|
if (!form)
|
|
26
26
|
return;
|
|
27
|
+
// An Input the reset removes, such as a row of a list going back to
|
|
28
|
+
// its default length, asks for nothing.
|
|
29
|
+
let disposed = false;
|
|
27
30
|
const onReset = (event) => {
|
|
28
31
|
const text = element.value;
|
|
29
32
|
const defaultText = untrack(() => props.defaultValue) ?? "";
|
|
@@ -38,14 +41,17 @@ export function InputRoot(props) {
|
|
|
38
41
|
// button, the browser runs microtasks as each listener returns,
|
|
39
42
|
// before later listeners could cancel the reset.
|
|
40
43
|
setTimeout(() => {
|
|
41
|
-
if (event.defaultPrevented || text === defaultText)
|
|
44
|
+
if (disposed || event.defaultPrevented || text === defaultText)
|
|
42
45
|
return;
|
|
43
46
|
props.onValueChange?.(defaultText, { reason: "reset", event });
|
|
44
47
|
onFlush(restoreValue);
|
|
45
48
|
});
|
|
46
49
|
};
|
|
47
50
|
form.addEventListener("reset", onReset);
|
|
48
|
-
return () =>
|
|
51
|
+
return () => {
|
|
52
|
+
disposed = true;
|
|
53
|
+
form.removeEventListener("reset", onReset);
|
|
54
|
+
};
|
|
49
55
|
});
|
|
50
56
|
const ownProps = {
|
|
51
57
|
onInput: (event) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "menhir",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solid",
|
|
@@ -44,6 +44,11 @@
|
|
|
44
44
|
"solid": "./dist/source/checkbox/index.js",
|
|
45
45
|
"import": "./dist/checkbox/index.js"
|
|
46
46
|
},
|
|
47
|
+
"./checkbox-group": {
|
|
48
|
+
"types": "./dist/source/checkbox-group/index.d.ts",
|
|
49
|
+
"solid": "./dist/source/checkbox-group/index.js",
|
|
50
|
+
"import": "./dist/checkbox-group/index.js"
|
|
51
|
+
},
|
|
47
52
|
"./collapsible": {
|
|
48
53
|
"types": "./dist/source/collapsible/index.d.ts",
|
|
49
54
|
"solid": "./dist/source/collapsible/index.js",
|
|
@@ -79,6 +84,16 @@
|
|
|
79
84
|
"solid": "./dist/source/field/index.js",
|
|
80
85
|
"import": "./dist/field/index.js"
|
|
81
86
|
},
|
|
87
|
+
"./form": {
|
|
88
|
+
"types": "./dist/source/form/index.d.ts",
|
|
89
|
+
"solid": "./dist/source/form/index.js",
|
|
90
|
+
"import": "./dist/form/index.js"
|
|
91
|
+
},
|
|
92
|
+
"./form/standard-schema": {
|
|
93
|
+
"types": "./dist/source/form/standard-schema/index.d.ts",
|
|
94
|
+
"solid": "./dist/source/form/standard-schema/index.js",
|
|
95
|
+
"import": "./dist/form/standard-schema/index.js"
|
|
96
|
+
},
|
|
82
97
|
"./input": {
|
|
83
98
|
"types": "./dist/source/input/index.d.ts",
|
|
84
99
|
"solid": "./dist/source/input/index.js",
|
|
@@ -191,8 +206,10 @@
|
|
|
191
206
|
"publint": "^0.3.25",
|
|
192
207
|
"solid-js": "^2.0.1",
|
|
193
208
|
"typescript": "^7.0.2",
|
|
209
|
+
"valibot": "^1.5.0",
|
|
194
210
|
"vite": "^8.3.8",
|
|
195
|
-
"vitest": "5.1.0"
|
|
211
|
+
"vitest": "5.1.0",
|
|
212
|
+
"zod": "^4.6.7"
|
|
196
213
|
},
|
|
197
214
|
"scripts": {
|
|
198
215
|
"build": "vite build && tsc -p tsconfig.build.json",
|