menhir 0.1.6 → 0.1.8

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.
Files changed (57) hide show
  1. package/README.md +2 -2
  2. package/dist/autocomplete/index.js +3 -0
  3. package/dist/autocomplete/index.parts.js +11 -0
  4. package/dist/autocomplete/input/AutocompleteInput.js +45 -0
  5. package/dist/autocomplete/item/AutocompleteItem.js +29 -0
  6. package/dist/autocomplete/root/AutocompleteContext.js +39 -0
  7. package/dist/autocomplete/root/AutocompleteRoot.js +22 -0
  8. package/dist/combobox/input/ComboboxInput.js +16 -98
  9. package/dist/combobox/input/createComboboxInput.js +112 -0
  10. package/dist/combobox/item/ComboboxItem.js +7 -51
  11. package/dist/combobox/item/createComboboxItem.js +62 -0
  12. package/dist/combobox/root/ComboboxContext.js +56 -32
  13. package/dist/field/control/FieldControl.js +39 -0
  14. package/dist/field/description/FieldDescription.js +25 -0
  15. package/dist/field/error/FieldError.js +45 -0
  16. package/dist/field/index.js +2 -0
  17. package/dist/field/index.parts.js +6 -0
  18. package/dist/field/label/FieldLabel.js +23 -0
  19. package/dist/field/root/FieldRoot.js +113 -0
  20. package/dist/field/root/FieldRootContext.js +10 -0
  21. package/dist/source/autocomplete/index.d.ts +2 -0
  22. package/dist/source/autocomplete/index.js +4 -0
  23. package/dist/source/autocomplete/index.parts.d.ts +10 -0
  24. package/dist/source/autocomplete/index.parts.js +12 -0
  25. package/dist/source/autocomplete/input/AutocompleteInput.d.ts +18 -0
  26. package/dist/source/autocomplete/input/AutocompleteInput.jsx +37 -0
  27. package/dist/source/autocomplete/item/AutocompleteItem.d.ts +20 -0
  28. package/dist/source/autocomplete/item/AutocompleteItem.jsx +25 -0
  29. package/dist/source/autocomplete/root/AutocompleteContext.d.ts +59 -0
  30. package/dist/source/autocomplete/root/AutocompleteContext.js +33 -0
  31. package/dist/source/autocomplete/root/AutocompleteRoot.d.ts +13 -0
  32. package/dist/source/autocomplete/root/AutocompleteRoot.jsx +15 -0
  33. package/dist/source/combobox/input/ComboboxInput.jsx +13 -135
  34. package/dist/source/combobox/input/createComboboxInput.d.ts +30 -0
  35. package/dist/source/combobox/input/createComboboxInput.js +140 -0
  36. package/dist/source/combobox/item/ComboboxItem.jsx +5 -48
  37. package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
  38. package/dist/source/combobox/item/createComboboxItem.js +61 -0
  39. package/dist/source/combobox/root/ComboboxContext.d.ts +69 -22
  40. package/dist/source/combobox/root/ComboboxContext.js +66 -36
  41. package/dist/source/field/control/FieldControl.d.ts +20 -0
  42. package/dist/source/field/control/FieldControl.jsx +36 -0
  43. package/dist/source/field/description/FieldDescription.d.ts +12 -0
  44. package/dist/source/field/description/FieldDescription.jsx +19 -0
  45. package/dist/source/field/error/FieldError.d.ts +19 -0
  46. package/dist/source/field/error/FieldError.jsx +39 -0
  47. package/dist/source/field/index.d.ts +1 -0
  48. package/dist/source/field/index.js +1 -0
  49. package/dist/source/field/index.parts.d.ts +5 -0
  50. package/dist/source/field/index.parts.js +5 -0
  51. package/dist/source/field/label/FieldLabel.d.ts +11 -0
  52. package/dist/source/field/label/FieldLabel.jsx +19 -0
  53. package/dist/source/field/root/FieldRoot.d.ts +25 -0
  54. package/dist/source/field/root/FieldRoot.jsx +133 -0
  55. package/dist/source/field/root/FieldRootContext.d.ts +50 -0
  56. package/dist/source/field/root/FieldRootContext.js +13 -0
  57. package/package.json +11 -1
@@ -3,8 +3,13 @@ import { createActiveDescendantList } from "../../floating/list-navigation/activ
3
3
  import { isDev } from "@solidjs/web";
4
4
  import { createContext, createMemo, createUniqueId, untrack, useContext } from "solid-js";
5
5
  // src/combobox/root/ComboboxContext.ts
6
- /** Creates a combobox's state and its list navigation. */
7
- function createCombobox(props) {
6
+ /**
7
+ * The state `Combobox.Root` and `Autocomplete.Root` share: whether the list
8
+ * is open, its list navigation, the List's `id` and the `data-*`
9
+ * attributes. Each Root spreads it into its own combobox, beside its text
10
+ * and what accepting an item does.
11
+ */
12
+ function createComboboxBase(props) {
8
13
  const [open, setOpen] = createControllableSignal({
9
14
  get value() {
10
15
  return props.open;
@@ -14,6 +19,40 @@ function createCombobox(props) {
14
19
  },
15
20
  onChange: (value, details) => props.onOpenChange?.(value, details)
16
21
  });
22
+ const list = createActiveDescendantList({
23
+ get open() {
24
+ return open();
25
+ },
26
+ wrap: true
27
+ });
28
+ return {
29
+ open,
30
+ setOpen,
31
+ isActive: (item) => open() && list.isActive(item),
32
+ count: () => list.items().length,
33
+ list,
34
+ listId: createUniqueId(),
35
+ input: void 0,
36
+ attributes: {
37
+ get "data-open"() {
38
+ return open() ? "" : void 0;
39
+ },
40
+ get "data-closed"() {
41
+ return open() ? void 0 : "";
42
+ },
43
+ get "data-disabled"() {
44
+ return props.disabled ? "" : void 0;
45
+ },
46
+ get "data-required"() {
47
+ return props.required ? "" : void 0;
48
+ }
49
+ }
50
+ };
51
+ }
52
+ /** Creates a combobox's state and its list navigation. */
53
+ function createCombobox(props) {
54
+ const base = createComboboxBase(props);
55
+ const { list } = base;
17
56
  const [value, setValue, requestedValue] = createControllableSignal({
18
57
  get value() {
19
58
  return props.value;
@@ -32,12 +71,6 @@ function createCombobox(props) {
32
71
  },
33
72
  onChange: (text, details) => props.onInputValueChange?.(text, details)
34
73
  });
35
- const list = createActiveDescendantList({
36
- get open() {
37
- return open();
38
- },
39
- wrap: true
40
- });
41
74
  const labelOfValue = (selected) => {
42
75
  return list.items().find((item) => item.value === selected)?.text.trim();
43
76
  };
@@ -64,8 +97,8 @@ function createCombobox(props) {
64
97
  input.dispatchEvent(new Event("change", { bubbles: true }));
65
98
  };
66
99
  const combobox = {
67
- open,
68
- setOpen,
100
+ ...base,
101
+ component: "Combobox",
69
102
  value,
70
103
  setValue,
71
104
  inputValue,
@@ -80,11 +113,13 @@ function createCombobox(props) {
80
113
  return true;
81
114
  },
82
115
  clear: (event, reason) => {
116
+ if (!requestedInputValue() && requestedValue() === null) return false;
83
117
  commit(null);
84
118
  setInputValue("", {
85
119
  reason,
86
120
  event
87
121
  });
122
+ return true;
88
123
  },
89
124
  revert: (event) => {
90
125
  if (!edited) return;
@@ -104,34 +139,23 @@ function createCombobox(props) {
104
139
  });
105
140
  },
106
141
  labelOfValue,
107
- isActive: (item) => open() && list.isActive(item),
108
- count: () => list.items().length,
109
- list,
110
- listId: createUniqueId(),
111
- input: void 0,
112
142
  hiddenInput: void 0,
113
- attributes: {
114
- get "data-open"() {
115
- return open() ? "" : void 0;
116
- },
117
- get "data-closed"() {
118
- return open() ? void 0 : "";
119
- },
120
- get "data-disabled"() {
121
- return props.disabled ? "" : void 0;
122
- },
123
- get "data-required"() {
124
- return props.required ? "" : void 0;
125
- }
126
- },
127
143
  props
128
144
  };
129
145
  return combobox;
130
146
  }
131
147
  var ComboboxContext = createContext(null);
132
- function useComboboxContext() {
148
+ /**
149
+ * The combobox of the Root around the part. A shared part passes no
150
+ * `component` and takes either Root's; a part only one Root takes passes
151
+ * that Root's `component`, and its type, `Combobox` or an Autocomplete's.
152
+ */
153
+ function useComboboxContext(component) {
133
154
  const context = useContext(ComboboxContext);
134
- if (isDev && !context) throw new Error("[menhir] Combobox parts must be placed within <Combobox.Root>.");
155
+ if (isDev) {
156
+ if (!context) throw new Error(component ? `[menhir] ${component} parts must be placed within <${component}.Root>.` : "[menhir] Combobox parts must be placed within <Combobox.Root>, and Autocomplete parts within <Autocomplete.Root>.");
157
+ if (component && context.component !== component) throw new Error(`[menhir] ${component} parts must be placed within <${component}.Root>, not <${context.component}.Root>.`);
158
+ }
135
159
  return context;
136
160
  }
137
- export { ComboboxContext, createCombobox, useComboboxContext };
161
+ export { ComboboxContext, createCombobox, createComboboxBase, useComboboxContext };
@@ -0,0 +1,39 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { merge, omit } from "solid-js";
6
+ // src/field/control/FieldControl.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<input>`);
8
+ /**
9
+ * The field's control: an `<input>`, or what `render` renders, with the
10
+ * field's `id`, which the Label names, its `name` and `disabled`, and
11
+ * `aria-describedby` naming the Description and the rendered Error. Takes
12
+ * no `id`: the Root generates it. Place it inside `Field.Root`.
13
+ */
14
+ function FieldControl(props) {
15
+ const root = useFieldRootContext();
16
+ const ownProps = merge(root.attributes, {
17
+ id: root.controlId,
18
+ get name() {
19
+ return root.name();
20
+ },
21
+ get disabled() {
22
+ return root.disabled();
23
+ },
24
+ get "aria-invalid"() {
25
+ return root.showsError() ? "true" : void 0;
26
+ },
27
+ get "aria-describedby"() {
28
+ return props["aria-describedby"] ?? root.describedBy();
29
+ }
30
+ });
31
+ const consumerProps = omit(props, "render", "id", "aria-describedby");
32
+ const elementProps = mergeElementProps(ownProps, consumerProps);
33
+ return renderElement(props, elementProps, (p) => (() => {
34
+ var _el$ = _tmpl$();
35
+ spread(_el$, p, false);
36
+ return _el$;
37
+ })());
38
+ }
39
+ export { FieldControl };
@@ -0,0 +1,25 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { merge, omit, onSettled } from "solid-js";
6
+ // src/field/description/FieldDescription.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<p>`);
8
+ /**
9
+ * What the control is for, or what it takes, which describes the control
10
+ * in `aria-describedby`: a `<p>`. Its `id` is the Root's, so the server
11
+ * HTML links it before hydration; it takes none from the consumer. Place
12
+ * it inside `Field.Root`.
13
+ */
14
+ function FieldDescription(props) {
15
+ const root = useFieldRootContext();
16
+ onSettled(() => root.registerDescription());
17
+ const ownProps = merge(root.attributes, { id: root.descriptionId });
18
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
19
+ return renderElement(props, elementProps, (p) => (() => {
20
+ var _el$ = _tmpl$();
21
+ spread(_el$, p, false);
22
+ return _el$;
23
+ })());
24
+ }
25
+ export { FieldDescription };
@@ -0,0 +1,45 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
4
+ import { createComponent, spread, template } from "@solidjs/web";
5
+ import { Show, createUniqueId, merge, omit, onSettled } from "solid-js";
6
+ // src/field/error/FieldError.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<p>`);
8
+ /**
9
+ * The field's error message: a `<p>`, rendered only while the field shows
10
+ * an error, which the control names in `aria-describedby` while it is
11
+ * rendered. Without children it shows the error from outside, else the
12
+ * browser's message. Place it inside `Field.Root`.
13
+ */
14
+ function FieldError(props) {
15
+ const root = useFieldRootContext();
16
+ const generated = createUniqueId();
17
+ const id = () => props.id ?? generated;
18
+ const shown = () => root.showsError() && (props.match === void 0 || !!root.validity()?.[props.match]) && ("children" in props || !!root.message());
19
+ onSettled(() => root.registerError({
20
+ id,
21
+ shown
22
+ }));
23
+ const ownProps = merge(root.attributes, {
24
+ get id() {
25
+ return id();
26
+ },
27
+ get children() {
28
+ return props.children ?? root.message();
29
+ }
30
+ });
31
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id", "match", "children"));
32
+ return createComponent(Show, {
33
+ get when() {
34
+ return shown();
35
+ },
36
+ get children() {
37
+ return renderElement(props, elementProps, (p) => (() => {
38
+ var _el$ = _tmpl$();
39
+ spread(_el$, p, false);
40
+ return _el$;
41
+ })());
42
+ }
43
+ });
44
+ }
45
+ export { FieldError };
@@ -0,0 +1,2 @@
1
+ import * as Field from "./index.parts.js";
2
+ export { Field };
@@ -0,0 +1,6 @@
1
+ import { FieldRoot } from "./root/FieldRoot.js";
2
+ import { FieldLabel } from "./label/FieldLabel.js";
3
+ import { FieldControl } from "./control/FieldControl.js";
4
+ import { FieldDescription } from "./description/FieldDescription.js";
5
+ import { FieldError } from "./error/FieldError.js";
6
+ export { FieldControl as Control, FieldDescription as Description, FieldError as Error, FieldLabel as Label, FieldRoot as Root };
@@ -0,0 +1,23 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { merge, omit } from "solid-js";
6
+ // src/field/label/FieldLabel.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<label>`);
8
+ /**
9
+ * The control's label: a `<label>` whose `for` is the control's `id`, which
10
+ * the Root generates, so the server HTML is labelled before hydration.
11
+ * Place it inside `Field.Root`.
12
+ */
13
+ function FieldLabel(props) {
14
+ const root = useFieldRootContext();
15
+ const ownProps = merge(root.attributes, { for: root.controlId });
16
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
17
+ return renderElement(props, elementProps, (p) => (() => {
18
+ var _el$ = _tmpl$();
19
+ spread(_el$, p, false);
20
+ return _el$;
21
+ })());
22
+ }
23
+ export { FieldLabel };
@@ -0,0 +1,113 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { FieldRootContext } from "./FieldRootContext.js";
4
+ import { createComponent, spread, template } from "@solidjs/web";
5
+ import { createSignal, createUniqueId, merge, omit, onFlush, onSettled } from "solid-js";
6
+ // src/field/root/FieldRoot.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
8
+ var controlSelector = "input,select,textarea";
9
+ /**
10
+ * The field: a `<div>` that holds a control's label, description and error,
11
+ * and its validity. It generates the control's `id` and the Description's,
12
+ * and tracks whether the field was touched.
13
+ */
14
+ function FieldRoot(props) {
15
+ const controlId = createUniqueId();
16
+ const descriptionId = createUniqueId();
17
+ const [touched, setTouched] = createSignal(false);
18
+ const [invalidControl, setInvalidControl] = createSignal(void 0, { equals: false });
19
+ const [settled, setSettled] = createSignal(false);
20
+ const [descriptions, setDescriptions] = createSignal(0);
21
+ const [errors, setErrors] = createSignal([]);
22
+ let root;
23
+ const readValidity = () => {
24
+ if (!root) return void 0;
25
+ for (const control of root.querySelectorAll(controlSelector)) if (control.willValidate && !control.validity.valid) return control;
26
+ };
27
+ const update = (touch) => onFlush(() => {
28
+ setInvalidControl(readValidity());
29
+ if (touch !== void 0) setTouched(touch);
30
+ });
31
+ const invalid = () => !!props.invalid || !!props.error || invalidControl() !== void 0;
32
+ const showsError = () => touched() && invalid();
33
+ const attributes = {
34
+ get "data-invalid"() {
35
+ return showsError() ? "" : void 0;
36
+ },
37
+ get "data-touched"() {
38
+ return touched() ? "" : void 0;
39
+ },
40
+ get "data-disabled"() {
41
+ return props.disabled ? "" : void 0;
42
+ }
43
+ };
44
+ const context = {
45
+ controlId,
46
+ descriptionId,
47
+ name: () => props.name,
48
+ disabled: () => props.disabled,
49
+ showsError,
50
+ message: () => props.error || invalidControl()?.validationMessage,
51
+ validity: () => invalidControl()?.validity,
52
+ describedBy() {
53
+ const ids = [];
54
+ if (!settled() || descriptions() > 0) ids.push(descriptionId);
55
+ for (const error of errors()) {
56
+ const id = error.shown() && error.id();
57
+ if (id) ids.push(id);
58
+ }
59
+ return ids.join(" ") || void 0;
60
+ },
61
+ registerDescription() {
62
+ setDescriptions((count) => count + 1);
63
+ return () => setDescriptions((count) => count - 1);
64
+ },
65
+ registerError(entry) {
66
+ setErrors((list) => [...list, entry]);
67
+ return () => setErrors((list) => list.filter((e) => e !== entry));
68
+ },
69
+ attributes
70
+ };
71
+ const touch = () => update(true);
72
+ onSettled(() => {
73
+ setSettled(true);
74
+ const form = root?.querySelector(controlSelector)?.form ?? root?.closest("form");
75
+ if (!form) return;
76
+ const onReset = (event) => onFlush(() => {
77
+ if (!event.defaultPrevented) update(false);
78
+ });
79
+ form.addEventListener("submit", touch);
80
+ form.addEventListener("invalid", touch, true);
81
+ form.addEventListener("reset", onReset);
82
+ return () => {
83
+ form.removeEventListener("submit", touch);
84
+ form.removeEventListener("invalid", touch, true);
85
+ form.removeEventListener("reset", onReset);
86
+ };
87
+ });
88
+ const ownProps = merge(attributes, {
89
+ onFocusOut: (event) => {
90
+ const to = event.relatedTarget;
91
+ const left = to ? !root?.contains(to) : document.activeElement !== event.target;
92
+ update(left || void 0);
93
+ },
94
+ onChange: touch,
95
+ onInput: () => update(),
96
+ ref: (element) => {
97
+ root = element;
98
+ element.addEventListener("invalid", touch, true);
99
+ }
100
+ });
101
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "name", "disabled", "invalid", "error"));
102
+ return createComponent(FieldRootContext, {
103
+ value: context,
104
+ get children() {
105
+ return renderElement(props, elementProps, (p) => (() => {
106
+ var _el$ = _tmpl$();
107
+ spread(_el$, p, false);
108
+ return _el$;
109
+ })());
110
+ }
111
+ });
112
+ }
113
+ export { FieldRoot };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/field/root/FieldRootContext.ts
4
+ var FieldRootContext = createContext(null);
5
+ function useFieldRootContext() {
6
+ const context = useContext(FieldRootContext);
7
+ if (isDev && !context) throw new Error("[menhir] Field parts must be placed within <Field.Root>.");
8
+ return context;
9
+ }
10
+ export { FieldRootContext, useFieldRootContext };
@@ -0,0 +1,2 @@
1
+ export * as Autocomplete from "./index.parts.ts";
2
+ export { includesText } from "../combobox/filter/includesText.ts";
@@ -0,0 +1,4 @@
1
+ export * as Autocomplete from "./index.parts.js";
2
+ // Combobox's, its own import, so that an app that filters otherwise does
3
+ // not bundle it (../combobox/README.md, "Filtering is the consumer's").
4
+ export { includesText } from "../combobox/filter/includesText.js";
@@ -0,0 +1,10 @@
1
+ export { AutocompleteRoot as Root } from "./root/AutocompleteRoot.tsx";
2
+ export { AutocompleteInput as Input } from "./input/AutocompleteInput.tsx";
3
+ export { ComboboxTrigger as Trigger } from "../combobox/trigger/ComboboxTrigger.tsx";
4
+ export { ComboboxClear as Clear } from "../combobox/clear/ComboboxClear.tsx";
5
+ export { ComboboxPopup as Popup } from "../combobox/popup/ComboboxPopup.tsx";
6
+ export { ComboboxList as List } from "../combobox/list/ComboboxList.tsx";
7
+ export { AutocompleteItem as Item } from "./item/AutocompleteItem.tsx";
8
+ export { ComboboxGroup as Group } from "../combobox/group/ComboboxGroup.tsx";
9
+ export { ComboboxGroupLabel as GroupLabel } from "../combobox/group-label/ComboboxGroupLabel.tsx";
10
+ export { ComboboxStatus as Status } from "../combobox/status/ComboboxStatus.tsx";
@@ -0,0 +1,12 @@
1
+ // Its own Root, Input and Item, and Combobox's other parts (README,
2
+ // "Decisions"; #104).
3
+ export { AutocompleteRoot as Root } from "./root/AutocompleteRoot.jsx";
4
+ export { AutocompleteInput as Input } from "./input/AutocompleteInput.jsx";
5
+ export { ComboboxTrigger as Trigger } from "../combobox/trigger/ComboboxTrigger.jsx";
6
+ export { ComboboxClear as Clear } from "../combobox/clear/ComboboxClear.jsx";
7
+ export { ComboboxPopup as Popup } from "../combobox/popup/ComboboxPopup.jsx";
8
+ export { ComboboxList as List } from "../combobox/list/ComboboxList.jsx";
9
+ export { AutocompleteItem as Item } from "./item/AutocompleteItem.jsx";
10
+ export { ComboboxGroup as Group } from "../combobox/group/ComboboxGroup.jsx";
11
+ export { ComboboxGroupLabel as GroupLabel } from "../combobox/group-label/ComboboxGroupLabel.jsx";
12
+ export { ComboboxStatus as Status } from "../combobox/status/ComboboxStatus.jsx";
@@ -0,0 +1,18 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface AutocompleteInputProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** What the browser may suggest. Default `"off"`, so its list does not cover the Autocomplete's. */
5
+ autocomplete?: string;
6
+ /** Shown while the input is empty. */
7
+ placeholder?: string;
8
+ render?: RenderProp;
9
+ }
10
+ /**
11
+ * The autocomplete itself, and the form's control: an `<input type="text"
12
+ * role="combobox" aria-autocomplete="list">` that takes `name`, `form`,
13
+ * `required` and `disabled` from the Root, so the form receives its text.
14
+ * It keeps focus while the list is open: typing opens it, and the arrow
15
+ * keys, Enter and Escape act on it. Name it with a `<label>`. Place it
16
+ * inside `Autocomplete.Root`.
17
+ */
18
+ export declare function AutocompleteInput(props: AutocompleteInputProps): JSX.Element;
@@ -0,0 +1,37 @@
1
+ import { merge, omit, onFlush } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createComboboxInput, onFormReset, } from "../../combobox/input/createComboboxInput.js";
5
+ import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
6
+ /**
7
+ * The autocomplete itself, and the form's control: an `<input type="text"
8
+ * role="combobox" aria-autocomplete="list">` that takes `name`, `form`,
9
+ * `required` and `disabled` from the Root, so the form receives its text.
10
+ * It keeps focus while the list is open: typing opens it, and the arrow
11
+ * keys, Enter and Escape act on it. Name it with a `<label>`. Place it
12
+ * inside `Autocomplete.Root`.
13
+ */
14
+ export function AutocompleteInput(props) {
15
+ const autocomplete = useComboboxContext("Autocomplete");
16
+ const input = createComboboxInput(autocomplete);
17
+ onFormReset(() => autocomplete.input, (event) => {
18
+ autocomplete.setInputValue(autocomplete.props.defaultValue ?? "", {
19
+ reason: "reset",
20
+ event,
21
+ });
22
+ onFlush(input.syncText);
23
+ });
24
+ const ownProps = merge(input.props, {
25
+ get name() {
26
+ return autocomplete.props.name;
27
+ },
28
+ get form() {
29
+ return autocomplete.props.form;
30
+ },
31
+ get required() {
32
+ return autocomplete.props.required;
33
+ },
34
+ });
35
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
36
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
37
+ }
@@ -0,0 +1,20 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface AutocompleteItemProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** The text it fills the input with when accepted. */
5
+ value: string;
6
+ /**
7
+ * Whether the item is disabled: still reached by the keys and the
8
+ * pointer, but not accepted.
9
+ */
10
+ disabled?: boolean;
11
+ render?: RenderProp;
12
+ }
13
+ /**
14
+ * A suggestion: a `<div role="option">`, with `aria-selected="true"` while
15
+ * it is the active item. A click accepts it, sets the input's text to its
16
+ * `value` and closes the list, and so does Enter while it is active; a
17
+ * disabled item is not accepted. Render the suggestions that match the
18
+ * text, and place them inside `Autocomplete.List`.
19
+ */
20
+ export declare function AutocompleteItem(props: AutocompleteItemProps): JSX.Element;
@@ -0,0 +1,25 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
5
+ import { createComboboxItem } from "../../combobox/item/createComboboxItem.js";
6
+ /**
7
+ * A suggestion: a `<div role="option">`, with `aria-selected="true"` while
8
+ * it is the active item. A click accepts it, sets the input's text to its
9
+ * `value` and closes the list, and so does Enter while it is active; a
10
+ * disabled item is not accepted. Render the suggestions that match the
11
+ * text, and place them inside `Autocomplete.List`.
12
+ */
13
+ export function AutocompleteItem(props) {
14
+ const autocomplete = useComboboxContext("Autocomplete");
15
+ const { item, props: itemProps } = createComboboxItem(autocomplete, props, () => props.value);
16
+ const ownProps = merge(itemProps, {
17
+ // Only on the active item, as the list autocomplete example: there is
18
+ // no selected item, only the text (README, "Decisions").
19
+ get "aria-selected"() {
20
+ return autocomplete.isActive(item) ? "true" : undefined;
21
+ },
22
+ });
23
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "id"));
24
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
25
+ }
@@ -0,0 +1,59 @@
1
+ import type { ComboboxBase, InputValueChangeReason } from "../../combobox/root/ComboboxContext.ts";
2
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
3
+ /**
4
+ * Why the text changed: Combobox's reasons for its input value, but
5
+ * `focus-out`, since focus leaving keeps the text.
6
+ *
7
+ * - `input-change`: the user typed or edited it.
8
+ * - `item-press`: an item was accepted, and the text is its `value`.
9
+ * - `escape-key`: Escape on a closed list emptied it.
10
+ * - `clear-press`: `Autocomplete.Clear` emptied it.
11
+ * - `reset`: its form was reset.
12
+ */
13
+ export type ValueChangeReason = Exclude<InputValueChangeReason, "focus-out">;
14
+ /** The second argument of `onValueChange`. */
15
+ export interface ValueChangeDetails {
16
+ readonly reason: ValueChangeReason;
17
+ readonly event: Event;
18
+ }
19
+ /** The props of `Autocomplete.Root`, less its children. */
20
+ export interface AutocompleteStateProps {
21
+ /** The text in the input, when controlled. */
22
+ value?: string;
23
+ /** The text at first, when uncontrolled. Default `""`. */
24
+ defaultValue?: string;
25
+ /**
26
+ * Called with the new text, and why, when the user types, accepts an
27
+ * item or empties it, or its form is reset. Filter by it for
28
+ * `input-change` only. When controlled, the text changes only if `value`
29
+ * does.
30
+ */
31
+ onValueChange?: (text: string, details: ValueChangeDetails) => void;
32
+ /** Whether the list is open, when controlled. */
33
+ open?: boolean;
34
+ /** Whether the list is open at first, when uncontrolled. */
35
+ defaultOpen?: boolean;
36
+ /**
37
+ * Called with the new value, and why, when the user opens or closes the
38
+ * list. When controlled, the list changes only if `open` does.
39
+ */
40
+ onOpenChange?: (open: boolean, details: OpenChangeDetails) => void;
41
+ /** Submitted with the form, as the input's `name`. */
42
+ name?: string;
43
+ /** The `id` of the form it belongs to, when it is not inside it. */
44
+ form?: string;
45
+ /** Whether the form can be submitted only with some text. */
46
+ required?: boolean;
47
+ /** Whether it ignores the user. It cannot be focused either. */
48
+ disabled?: boolean;
49
+ }
50
+ /** What `Autocomplete.Root` shares with its parts, through Combobox's context. */
51
+ export interface Autocomplete extends ComboboxBase {
52
+ readonly component: "Autocomplete";
53
+ /** Asks for a text, saying why. */
54
+ setInputValue(text: string, details: ValueChangeDetails): void;
55
+ /** The Root's props, for the Input. */
56
+ readonly props: AutocompleteStateProps;
57
+ }
58
+ /** Creates an autocomplete's state, on Combobox's shared state. */
59
+ export declare function createAutocomplete(props: AutocompleteStateProps): Autocomplete;
@@ -0,0 +1,33 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createComboboxBase } from "../../combobox/root/ComboboxContext.js";
3
+ /** Creates an autocomplete's state, on Combobox's shared state. */
4
+ export function createAutocomplete(props) {
5
+ const [text, setText, requestedText] = createControllableSignal({
6
+ get value() {
7
+ return props.value;
8
+ },
9
+ get defaultValue() {
10
+ return props.defaultValue ?? "";
11
+ },
12
+ onChange: (text, details) => props.onValueChange?.(text, details),
13
+ });
14
+ return {
15
+ ...createComboboxBase(props),
16
+ component: "Autocomplete",
17
+ inputValue: text,
18
+ setInputValue: setText,
19
+ select: (item, event) => {
20
+ if (!item || item.disabled)
21
+ return false;
22
+ setText(item.value, { reason: "item-press", event });
23
+ return true;
24
+ },
25
+ clear: (event, reason) => {
26
+ if (!requestedText())
27
+ return false;
28
+ setText("", { reason, event });
29
+ return true;
30
+ },
31
+ props,
32
+ };
33
+ }
@@ -0,0 +1,13 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { AutocompleteStateProps } from "./AutocompleteContext.ts";
3
+ export interface AutocompleteRootProps extends AutocompleteStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * A text input with suggestions: holds the text, which is its value, and
8
+ * whether the list is open, and shares them with `Autocomplete.Input`,
9
+ * `Autocomplete.Trigger`, `Autocomplete.Clear`, `Autocomplete.Popup` and
10
+ * the parts inside it. Its form props go on the Input. Renders no element
11
+ * of its own.
12
+ */
13
+ export declare function AutocompleteRoot(props: AutocompleteRootProps): JSX.Element;