menhir 0.1.8 → 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 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, 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
@@ -0,0 +1,4 @@
1
+ import { createContext } from "solid-js";
2
+ // src/core/formContext.ts
3
+ var FormContext = createContext(null);
4
+ export { FormContext };
@@ -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: false });
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)) if (control.willValidate && !control.validity.valid) return control;
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: () => invalidControl()?.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 };
@@ -0,0 +1,2 @@
1
+ import * as Input from "./index.parts.js";
2
+ export { Input };
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.js";
@@ -0,0 +1,59 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { spread, template } from "@solidjs/web";
4
+ import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
5
+ // src/input/root/InputRoot.tsx
6
+ var _tmpl$ = /* @__PURE__ */ template(`<input>`);
7
+ /**
8
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
9
+ * `<label>`, or place it in `Field.Control`'s `render`.
10
+ */
11
+ function InputRoot(props) {
12
+ let element;
13
+ const restoreValue = () => {
14
+ const value = untrack(() => props.value);
15
+ if (element && value !== void 0 && element.value !== value) element.value = value;
16
+ };
17
+ onSettled(() => {
18
+ const form = element?.form;
19
+ if (!form) return;
20
+ let disposed = false;
21
+ const onReset = (event) => {
22
+ const text = element.value;
23
+ const defaultText = untrack(() => props.defaultValue) ?? "";
24
+ if (untrack(() => props.value) !== void 0) element.defaultValue = text;
25
+ setTimeout(() => {
26
+ if (disposed || event.defaultPrevented || text === defaultText) return;
27
+ props.onValueChange?.(defaultText, {
28
+ reason: "reset",
29
+ event
30
+ });
31
+ onFlush(restoreValue);
32
+ });
33
+ };
34
+ form.addEventListener("reset", onReset);
35
+ return () => {
36
+ disposed = true;
37
+ form.removeEventListener("reset", onReset);
38
+ };
39
+ });
40
+ const elementProps = merge(mergeElementProps({
41
+ onInput: (event) => {
42
+ props.onValueChange?.(element.value, {
43
+ reason: "input-change",
44
+ event
45
+ });
46
+ if (!event.isComposing) onFlush(restoreValue);
47
+ },
48
+ onCompositionEnd: () => onFlush(restoreValue),
49
+ ref: (el) => {
50
+ element = el;
51
+ }
52
+ }, omit(props, "render", "value", "onValueChange")), () => props.value === void 0 ? {} : { value: props.value });
53
+ return renderElement(props, elementProps, (p) => (() => {
54
+ var _el$ = _tmpl$();
55
+ spread(_el$, p, false);
56
+ return _el$;
57
+ })());
58
+ }
59
+ export { InputRoot };
@@ -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>;
@@ -0,0 +1,5 @@
1
+ import { createContext } from "solid-js";
2
+ // `null` outside a Form, where a Field reads no errors. In `core`, so that
3
+ // Form imports no module of Field and Field none of Form (PLAN.md section
4
+ // 6, rule 4).
5
+ export const FormContext = createContext(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. Set
16
- // again on each event, even to the same element, whose message may have
17
- // changed.
18
- const [invalidControl, setInvalidControl] = createSignal(undefined, { equals: false });
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
- if (control.willValidate && !control.validity.valid)
30
- return control;
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
- message: () => props.error || invalidControl()?.validationMessage,
63
- validity: () => invalidControl()?.validity,
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,3 @@
1
+ export { Form } from "./Form.tsx";
2
+ export type { FormProps } from "./Form.tsx";
3
+ export type { FormErrors } from "../core/formContext.ts";
@@ -0,0 +1 @@
1
+ export { Form } from "./Form.jsx";
@@ -0,0 +1,2 @@
1
+ export { issuesToErrors } from "./issuesToErrors.ts";
2
+ export type { Issue, PathSegment } from "./issuesToErrors.ts";
@@ -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
+ }
@@ -0,0 +1 @@
1
+ export * as Input from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Input from "./index.parts.js";
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.tsx";
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.jsx";
@@ -0,0 +1,29 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ /** Why the text changed. */
4
+ export type InputValueChangeReason = "input-change" | "reset";
5
+ /** The second argument of `onValueChange`. */
6
+ export interface InputValueChangeDetails {
7
+ readonly reason: InputValueChangeReason;
8
+ /** The `input` event of an edit, or the form's `reset` event. */
9
+ readonly event: Event;
10
+ }
11
+ export interface InputRootProps extends Omit<JSX.InputHTMLAttributes<HTMLInputElement>, "ref" | "value" | "defaultValue"> {
12
+ /** Typed for any element, since `render` may render a `<textarea>`. */
13
+ ref?: JSX.Ref<HTMLElement>;
14
+ /** The text, when controlled. */
15
+ value?: string;
16
+ /** The text to start with when uncontrolled, and the text a form reset puts back. */
17
+ defaultValue?: string;
18
+ /**
19
+ * Called with the new text, and why, on each edit and on a form reset.
20
+ */
21
+ onValueChange?: (value: string, details: InputValueChangeDetails) => void;
22
+ /** Renders the input as another element or component, such as a `<textarea>`. Read once. */
23
+ render?: RenderProp;
24
+ }
25
+ /**
26
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
27
+ * `<label>`, or place it in `Field.Control`'s `render`.
28
+ */
29
+ export declare function InputRoot(props: InputRootProps): JSX.Element;
@@ -0,0 +1,73 @@
1
+ import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ /**
5
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
6
+ * `<label>`, or place it in `Field.Control`'s `render`.
7
+ */
8
+ export function InputRoot(props) {
9
+ // An `<input>`, or a `<textarea>` rendered through `render`.
10
+ let element;
11
+ // Puts the element back to a controlled `value` once the consumer has
12
+ // had its say: Solid writes `value` only when it changes, so a declined
13
+ // edit would stay. Only when the two differ, since a write moves the
14
+ // caret to the end.
15
+ const restoreValue = () => {
16
+ const value = untrack(() => props.value);
17
+ if (element && value !== undefined && element.value !== value) {
18
+ element.value = value;
19
+ }
20
+ };
21
+ // A form reset puts a control back to its default, and so it does an
22
+ // Input: through `onValueChange`, so that a controlled one asks.
23
+ onSettled(() => {
24
+ const form = element?.form;
25
+ if (!form)
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;
30
+ const onReset = (event) => {
31
+ const text = element.value;
32
+ const defaultText = untrack(() => props.defaultValue) ?? "";
33
+ // The browser resets the element to its `value` attribute after this
34
+ // event. A controlled Input keeps its text through it, by making that
35
+ // its attribute, so that the element never shows a text its consumer
36
+ // does not hold; its consumer then decides.
37
+ if (untrack(() => props.value) !== undefined) {
38
+ element.defaultValue = text;
39
+ }
40
+ // In a task of its own, not `onFlush`: when the user presses a reset
41
+ // button, the browser runs microtasks as each listener returns,
42
+ // before later listeners could cancel the reset.
43
+ setTimeout(() => {
44
+ if (disposed || event.defaultPrevented || text === defaultText)
45
+ return;
46
+ props.onValueChange?.(defaultText, { reason: "reset", event });
47
+ onFlush(restoreValue);
48
+ });
49
+ };
50
+ form.addEventListener("reset", onReset);
51
+ return () => {
52
+ disposed = true;
53
+ form.removeEventListener("reset", onReset);
54
+ };
55
+ });
56
+ const ownProps = {
57
+ onInput: (event) => {
58
+ props.onValueChange?.(element.value, { reason: "input-change", event });
59
+ // A write while an input method composes would end the composition.
60
+ if (!event.isComposing)
61
+ onFlush(restoreValue);
62
+ },
63
+ onCompositionEnd: () => onFlush(restoreValue),
64
+ ref: (el) => {
65
+ element = el;
66
+ },
67
+ };
68
+ const elementProps = merge(mergeElementProps(ownProps, omit(props, "render", "value", "onValueChange")),
69
+ // Solid writes `""` for a `value` of `undefined`, which would hide
70
+ // `defaultValue`: the key is there only while controlled.
71
+ () => (props.value === undefined ? {} : { value: props.value }));
72
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.8",
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,21 @@
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
+ },
92
+ "./input": {
93
+ "types": "./dist/source/input/index.d.ts",
94
+ "solid": "./dist/source/input/index.js",
95
+ "import": "./dist/input/index.js"
96
+ },
82
97
  "./menu": {
83
98
  "types": "./dist/source/menu/index.d.ts",
84
99
  "solid": "./dist/source/menu/index.js",
@@ -186,8 +201,10 @@
186
201
  "publint": "^0.3.25",
187
202
  "solid-js": "^2.0.1",
188
203
  "typescript": "^7.0.2",
204
+ "valibot": "^1.5.0",
189
205
  "vite": "^8.3.8",
190
- "vitest": "5.1.0"
206
+ "vitest": "5.1.0",
207
+ "zod": "^4.6.7"
191
208
  },
192
209
  "scripts": {
193
210
  "build": "vite build && tsc -p tsconfig.build.json",