menhir 0.1.9 → 0.1.10
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/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/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 +14 -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, 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
|
|
@@ -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,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.10",
|
|
4
4
|
"description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solid",
|
|
@@ -79,6 +79,16 @@
|
|
|
79
79
|
"solid": "./dist/source/field/index.js",
|
|
80
80
|
"import": "./dist/field/index.js"
|
|
81
81
|
},
|
|
82
|
+
"./form": {
|
|
83
|
+
"types": "./dist/source/form/index.d.ts",
|
|
84
|
+
"solid": "./dist/source/form/index.js",
|
|
85
|
+
"import": "./dist/form/index.js"
|
|
86
|
+
},
|
|
87
|
+
"./form/standard-schema": {
|
|
88
|
+
"types": "./dist/source/form/standard-schema/index.d.ts",
|
|
89
|
+
"solid": "./dist/source/form/standard-schema/index.js",
|
|
90
|
+
"import": "./dist/form/standard-schema/index.js"
|
|
91
|
+
},
|
|
82
92
|
"./input": {
|
|
83
93
|
"types": "./dist/source/input/index.d.ts",
|
|
84
94
|
"solid": "./dist/source/input/index.js",
|
|
@@ -191,8 +201,10 @@
|
|
|
191
201
|
"publint": "^0.3.25",
|
|
192
202
|
"solid-js": "^2.0.1",
|
|
193
203
|
"typescript": "^7.0.2",
|
|
204
|
+
"valibot": "^1.5.0",
|
|
194
205
|
"vite": "^8.3.8",
|
|
195
|
-
"vitest": "5.1.0"
|
|
206
|
+
"vitest": "5.1.0",
|
|
207
|
+
"zod": "^4.6.7"
|
|
196
208
|
},
|
|
197
209
|
"scripts": {
|
|
198
210
|
"build": "vite build && tsc -p tsconfig.build.json",
|