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.
- package/README.md +2 -2
- package/dist/autocomplete/index.js +3 -0
- package/dist/autocomplete/index.parts.js +11 -0
- package/dist/autocomplete/input/AutocompleteInput.js +45 -0
- package/dist/autocomplete/item/AutocompleteItem.js +29 -0
- package/dist/autocomplete/root/AutocompleteContext.js +39 -0
- package/dist/autocomplete/root/AutocompleteRoot.js +22 -0
- package/dist/combobox/input/ComboboxInput.js +16 -98
- package/dist/combobox/input/createComboboxInput.js +112 -0
- package/dist/combobox/item/ComboboxItem.js +7 -51
- package/dist/combobox/item/createComboboxItem.js +62 -0
- package/dist/combobox/root/ComboboxContext.js +56 -32
- package/dist/field/control/FieldControl.js +39 -0
- package/dist/field/description/FieldDescription.js +25 -0
- package/dist/field/error/FieldError.js +45 -0
- package/dist/field/index.js +2 -0
- package/dist/field/index.parts.js +6 -0
- package/dist/field/label/FieldLabel.js +23 -0
- package/dist/field/root/FieldRoot.js +113 -0
- package/dist/field/root/FieldRootContext.js +10 -0
- package/dist/source/autocomplete/index.d.ts +2 -0
- package/dist/source/autocomplete/index.js +4 -0
- package/dist/source/autocomplete/index.parts.d.ts +10 -0
- package/dist/source/autocomplete/index.parts.js +12 -0
- package/dist/source/autocomplete/input/AutocompleteInput.d.ts +18 -0
- package/dist/source/autocomplete/input/AutocompleteInput.jsx +37 -0
- package/dist/source/autocomplete/item/AutocompleteItem.d.ts +20 -0
- package/dist/source/autocomplete/item/AutocompleteItem.jsx +25 -0
- package/dist/source/autocomplete/root/AutocompleteContext.d.ts +59 -0
- package/dist/source/autocomplete/root/AutocompleteContext.js +33 -0
- package/dist/source/autocomplete/root/AutocompleteRoot.d.ts +13 -0
- package/dist/source/autocomplete/root/AutocompleteRoot.jsx +15 -0
- package/dist/source/combobox/input/ComboboxInput.jsx +13 -135
- package/dist/source/combobox/input/createComboboxInput.d.ts +30 -0
- package/dist/source/combobox/input/createComboboxInput.js +140 -0
- package/dist/source/combobox/item/ComboboxItem.jsx +5 -48
- package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
- package/dist/source/combobox/item/createComboboxItem.js +61 -0
- package/dist/source/combobox/root/ComboboxContext.d.ts +69 -22
- package/dist/source/combobox/root/ComboboxContext.js +66 -36
- package/dist/source/field/control/FieldControl.d.ts +20 -0
- package/dist/source/field/control/FieldControl.jsx +36 -0
- package/dist/source/field/description/FieldDescription.d.ts +12 -0
- package/dist/source/field/description/FieldDescription.jsx +19 -0
- package/dist/source/field/error/FieldError.d.ts +19 -0
- package/dist/source/field/error/FieldError.jsx +39 -0
- package/dist/source/field/index.d.ts +1 -0
- package/dist/source/field/index.js +1 -0
- package/dist/source/field/index.parts.d.ts +5 -0
- package/dist/source/field/index.parts.js +5 -0
- package/dist/source/field/label/FieldLabel.d.ts +11 -0
- package/dist/source/field/label/FieldLabel.jsx +19 -0
- package/dist/source/field/root/FieldRoot.d.ts +25 -0
- package/dist/source/field/root/FieldRoot.jsx +133 -0
- package/dist/source/field/root/FieldRootContext.d.ts +50 -0
- package/dist/source/field/root/FieldRootContext.js +13 -0
- package/package.json +11 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ComboboxContext } from "../../combobox/root/ComboboxContext.js";
|
|
2
|
+
import { createAutocomplete } from "./AutocompleteContext.js";
|
|
3
|
+
/**
|
|
4
|
+
* A text input with suggestions: holds the text, which is its value, and
|
|
5
|
+
* whether the list is open, and shares them with `Autocomplete.Input`,
|
|
6
|
+
* `Autocomplete.Trigger`, `Autocomplete.Clear`, `Autocomplete.Popup` and
|
|
7
|
+
* the parts inside it. Its form props go on the Input. Renders no element
|
|
8
|
+
* of its own.
|
|
9
|
+
*/
|
|
10
|
+
export function AutocompleteRoot(props) {
|
|
11
|
+
// Through Combobox's context, so that Combobox's parts work inside it.
|
|
12
|
+
return (<ComboboxContext value={createAutocomplete(props)}>
|
|
13
|
+
{props.children}
|
|
14
|
+
</ComboboxContext>);
|
|
15
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { omit, onFlush, untrack } from "solid-js";
|
|
2
2
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
3
|
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
4
|
import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
5
|
+
import { createComboboxInput, onFormReset } from "./createComboboxInput.js";
|
|
5
6
|
/**
|
|
6
7
|
* The combobox itself: an `<input type="text" role="combobox"
|
|
7
8
|
* aria-autocomplete="list">` with `aria-expanded`, naming the List in
|
|
@@ -12,121 +13,9 @@ import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
|
12
13
|
* `Combobox.Root`.
|
|
13
14
|
*/
|
|
14
15
|
export function ComboboxInput(props) {
|
|
15
|
-
const combobox = useComboboxContext();
|
|
16
|
-
const
|
|
17
|
-
let input;
|
|
16
|
+
const combobox = useComboboxContext("Combobox");
|
|
17
|
+
const input = createComboboxInput(combobox, combobox.revert);
|
|
18
18
|
let hiddenInput;
|
|
19
|
-
const details = (reason, event) => ({ reason, event });
|
|
20
|
-
const setOpen = (open, reason, event) => combobox.setOpen(open, details(reason, event));
|
|
21
|
-
// What the input's `value` binds to: the Combobox's text, and each edit
|
|
22
|
-
// as it is made. The Combobox writes its own text after the event's flush
|
|
23
|
-
// (`createControllableSignal`), and a binding that re-ran in that flush,
|
|
24
|
-
// as `aria-activedescendant` does when typing empties the active item,
|
|
25
|
-
// would write the old text over the edit.
|
|
26
|
-
const [draft, setDraft] = createSignal(() => combobox.inputValue());
|
|
27
|
-
// Puts the draft and the element back to the Combobox's text, once that
|
|
28
|
-
// has settled: a controlled Combobox's consumer may have declined the
|
|
29
|
-
// edit, and a form reset sets the element without an `input` event.
|
|
30
|
-
const syncText = () => {
|
|
31
|
-
const text = untrack(combobox.inputValue);
|
|
32
|
-
setDraft(text);
|
|
33
|
-
// Only when it differs, since a write moves the caret to the end.
|
|
34
|
-
if (input && input.value !== text)
|
|
35
|
-
input.value = text;
|
|
36
|
-
};
|
|
37
|
-
const ownProps = merge(combobox.attributes, {
|
|
38
|
-
type: "text",
|
|
39
|
-
role: "combobox",
|
|
40
|
-
"aria-autocomplete": "list",
|
|
41
|
-
autocomplete: "off",
|
|
42
|
-
get value() {
|
|
43
|
-
return draft();
|
|
44
|
-
},
|
|
45
|
-
get disabled() {
|
|
46
|
-
return combobox.props.disabled;
|
|
47
|
-
},
|
|
48
|
-
get "aria-expanded"() {
|
|
49
|
-
return combobox.open() ? "true" : "false";
|
|
50
|
-
},
|
|
51
|
-
"aria-controls": combobox.listId,
|
|
52
|
-
// Only while open, as an item's `data-active`.
|
|
53
|
-
get "aria-activedescendant"() {
|
|
54
|
-
return combobox.open() ? list.activeId() : undefined;
|
|
55
|
-
},
|
|
56
|
-
get "aria-required"() {
|
|
57
|
-
return combobox.props.required ? "true" : undefined;
|
|
58
|
-
},
|
|
59
|
-
onInput: (event) => {
|
|
60
|
-
setDraft(input.value);
|
|
61
|
-
combobox.setInputValue(input.value, details("input-change", event));
|
|
62
|
-
// The keys start again from the text (README, "Keys while open").
|
|
63
|
-
list.move(undefined);
|
|
64
|
-
setOpen(true, "input-change", event);
|
|
65
|
-
onFlush(syncText);
|
|
66
|
-
},
|
|
67
|
-
onKeyDown: (event) => {
|
|
68
|
-
// An input method's keys, such as the Enter that commits what it
|
|
69
|
-
// composed (README, "An input method's composition").
|
|
70
|
-
if (event.isComposing || event.keyCode === 229)
|
|
71
|
-
return;
|
|
72
|
-
if (event.ctrlKey || event.metaKey || event.shiftKey)
|
|
73
|
-
return;
|
|
74
|
-
const isOpen = combobox.open();
|
|
75
|
-
const { key, altKey } = event;
|
|
76
|
-
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
77
|
-
const down = key === "ArrowDown";
|
|
78
|
-
if (altKey) {
|
|
79
|
-
// Alt+Down opens without moving, Alt+Up closes.
|
|
80
|
-
if (down === isOpen)
|
|
81
|
-
return;
|
|
82
|
-
setOpen(down, "arrow-key", event);
|
|
83
|
-
}
|
|
84
|
-
else if (!isOpen) {
|
|
85
|
-
setOpen(true, "arrow-key", event);
|
|
86
|
-
// While closed, the move waits for the list to open.
|
|
87
|
-
const items = list.items();
|
|
88
|
-
list.move(down ? items[0] : items[items.length - 1]);
|
|
89
|
-
}
|
|
90
|
-
else if (!list.onKeyDown(event))
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
else if (key === "Enter" && !altKey) {
|
|
94
|
-
if (!isOpen)
|
|
95
|
-
return;
|
|
96
|
-
const item = list.active();
|
|
97
|
-
if (!item) {
|
|
98
|
-
// A form submits, as it would from a text input.
|
|
99
|
-
setOpen(false, "enter-key", event);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (combobox.select(item, event))
|
|
103
|
-
setOpen(false, "item-press", event);
|
|
104
|
-
}
|
|
105
|
-
else if (key === "Escape" && !altKey) {
|
|
106
|
-
if (isOpen)
|
|
107
|
-
setOpen(false, "escape-key", event);
|
|
108
|
-
else if (combobox.inputValue() || combobox.value() !== null)
|
|
109
|
-
combobox.clear(event, "escape-key");
|
|
110
|
-
// Nothing to close or clear: a dialog around it may close.
|
|
111
|
-
else
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
else
|
|
115
|
-
return;
|
|
116
|
-
event.preventDefault();
|
|
117
|
-
},
|
|
118
|
-
onBlur: (event) => {
|
|
119
|
-
// Focus lost to another window comes back to the input.
|
|
120
|
-
if (!document.hasFocus())
|
|
121
|
-
return;
|
|
122
|
-
setOpen(false, "focus-out", event);
|
|
123
|
-
combobox.revert(event);
|
|
124
|
-
},
|
|
125
|
-
ref: (element) => {
|
|
126
|
-
input = element;
|
|
127
|
-
combobox.input = input;
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
19
|
// A selection, which the Combobox makes through the hidden input.
|
|
131
20
|
const onHiddenChange = () => {
|
|
132
21
|
combobox.setValue(hiddenInput.value || null);
|
|
@@ -137,27 +26,16 @@ export function ComboboxInput(props) {
|
|
|
137
26
|
if (hiddenInput)
|
|
138
27
|
hiddenInput.value = untrack(combobox.value) ?? "";
|
|
139
28
|
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
combobox.setValue(value);
|
|
149
|
-
combobox.setInputValue(combobox.props.defaultInputValue ?? combobox.labelOfValue(value) ?? "", details("reset", event));
|
|
150
|
-
// The browser resets the inputs after this event, to their `value`
|
|
151
|
-
// attributes; `onFlush` runs after that.
|
|
152
|
-
onFlush(() => {
|
|
153
|
-
syncHidden();
|
|
154
|
-
syncText();
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
form.addEventListener("reset", onReset);
|
|
158
|
-
return () => form.removeEventListener("reset", onReset);
|
|
29
|
+
onFormReset(() => hiddenInput, (event) => {
|
|
30
|
+
const value = combobox.props.defaultValue ?? null;
|
|
31
|
+
combobox.setValue(value);
|
|
32
|
+
combobox.setInputValue(combobox.props.defaultInputValue ?? combobox.labelOfValue(value) ?? "", { reason: "reset", event });
|
|
33
|
+
onFlush(() => {
|
|
34
|
+
syncHidden();
|
|
35
|
+
input.syncText();
|
|
36
|
+
});
|
|
159
37
|
});
|
|
160
|
-
const elementProps = mergeElementProps(
|
|
38
|
+
const elementProps = mergeElementProps(input.props, omit(props, "render"));
|
|
161
39
|
return (<>
|
|
162
40
|
{renderElement(props, elementProps, (p) => (<input {...p}/>))}
|
|
163
41
|
<input aria-hidden="true" tabindex={-1} style={visuallyHidden} name={combobox.props.name} form={combobox.props.form} required={combobox.props.required} disabled={combobox.props.disabled} value={combobox.value() ?? ""} onChange={onHiddenChange}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ElementProps } from "../../core/renderElement.tsx";
|
|
2
|
+
import type { ComboboxBase } from "../root/ComboboxContext.ts";
|
|
3
|
+
/** The props `createComboboxInput` gives the `<input>`. */
|
|
4
|
+
export type ComboboxInputElementProps = ElementProps & {
|
|
5
|
+
type: string;
|
|
6
|
+
autocomplete: string;
|
|
7
|
+
value: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The input of a combobox, the code `Combobox.Input` and
|
|
12
|
+
* `Autocomplete.Input` share: its attributes, typing, the keys, focus
|
|
13
|
+
* leaving, and the draft its `value` binds to. Focus leaving closes the
|
|
14
|
+
* list and then calls `onFocusOut`, which a Combobox puts its text back
|
|
15
|
+
* in; an Autocomplete keeps the text, and passes none. Returns its props
|
|
16
|
+
* and `syncText`, which puts the element back to the combobox's text once
|
|
17
|
+
* that has settled, as after a form reset.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createComboboxInput(combobox: ComboboxBase, onFocusOut?: (event: FocusEvent) => void): {
|
|
20
|
+
props: ComboboxInputElementProps;
|
|
21
|
+
syncText(): void;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Calls `reset` when the form of `input()` is reset, from the time the
|
|
25
|
+
* part has settled: a form reset puts a control back to its default, and
|
|
26
|
+
* so it does a combobox, through its setters, so that a controlled one
|
|
27
|
+
* asks. The browser resets the form's inputs after the event, to their
|
|
28
|
+
* `value` attributes; an `onFlush` in `reset` runs after that.
|
|
29
|
+
*/
|
|
30
|
+
export declare function onFormReset(input: () => HTMLInputElement | undefined, reset: (event: Event) => void): void;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createSignal, merge, onFlush, onSettled, untrack } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* The input of a combobox, the code `Combobox.Input` and
|
|
4
|
+
* `Autocomplete.Input` share: its attributes, typing, the keys, focus
|
|
5
|
+
* leaving, and the draft its `value` binds to. Focus leaving closes the
|
|
6
|
+
* list and then calls `onFocusOut`, which a Combobox puts its text back
|
|
7
|
+
* in; an Autocomplete keeps the text, and passes none. Returns its props
|
|
8
|
+
* and `syncText`, which puts the element back to the combobox's text once
|
|
9
|
+
* that has settled, as after a form reset.
|
|
10
|
+
*/
|
|
11
|
+
export function createComboboxInput(combobox, onFocusOut) {
|
|
12
|
+
const { list } = combobox;
|
|
13
|
+
let input;
|
|
14
|
+
const details = (reason, event) => ({ reason, event });
|
|
15
|
+
const setOpen = (open, reason, event) => combobox.setOpen(open, details(reason, event));
|
|
16
|
+
// What the input's `value` binds to: the combobox's text, and each edit
|
|
17
|
+
// as it is made. The combobox writes its own text after the event's flush
|
|
18
|
+
// (`createControllableSignal`), and a binding that re-ran in that flush,
|
|
19
|
+
// as `aria-activedescendant` does when typing empties the active item,
|
|
20
|
+
// would write the old text over the edit.
|
|
21
|
+
const [draft, setDraft] = createSignal(() => combobox.inputValue());
|
|
22
|
+
// Puts the draft and the element back to the combobox's text, once that
|
|
23
|
+
// has settled: a controlled consumer may have declined the edit, and a
|
|
24
|
+
// form reset sets the element without an `input` event.
|
|
25
|
+
const syncText = () => {
|
|
26
|
+
const text = untrack(combobox.inputValue);
|
|
27
|
+
setDraft(text);
|
|
28
|
+
// Only when it differs, since a write moves the caret to the end.
|
|
29
|
+
if (input && input.value !== text)
|
|
30
|
+
input.value = text;
|
|
31
|
+
};
|
|
32
|
+
const props = merge(combobox.attributes, {
|
|
33
|
+
type: "text",
|
|
34
|
+
role: "combobox",
|
|
35
|
+
"aria-autocomplete": "list",
|
|
36
|
+
autocomplete: "off",
|
|
37
|
+
get value() {
|
|
38
|
+
return draft();
|
|
39
|
+
},
|
|
40
|
+
get disabled() {
|
|
41
|
+
return combobox.props.disabled;
|
|
42
|
+
},
|
|
43
|
+
get "aria-expanded"() {
|
|
44
|
+
return combobox.open() ? "true" : "false";
|
|
45
|
+
},
|
|
46
|
+
"aria-controls": combobox.listId,
|
|
47
|
+
// Only while open, as an item's `data-active`.
|
|
48
|
+
get "aria-activedescendant"() {
|
|
49
|
+
return combobox.open() ? list.activeId() : undefined;
|
|
50
|
+
},
|
|
51
|
+
get "aria-required"() {
|
|
52
|
+
return combobox.props.required ? "true" : undefined;
|
|
53
|
+
},
|
|
54
|
+
onInput: (event) => {
|
|
55
|
+
setDraft(input.value);
|
|
56
|
+
combobox.setInputValue(input.value, details("input-change", event));
|
|
57
|
+
// The keys start again from the text (README, "Keys while open").
|
|
58
|
+
list.move(undefined);
|
|
59
|
+
setOpen(true, "input-change", event);
|
|
60
|
+
onFlush(syncText);
|
|
61
|
+
},
|
|
62
|
+
onKeyDown: (event) => {
|
|
63
|
+
// An input method's keys, such as the Enter that commits what it
|
|
64
|
+
// composed (README, "An input method's composition").
|
|
65
|
+
if (event.isComposing || event.keyCode === 229)
|
|
66
|
+
return;
|
|
67
|
+
if (event.ctrlKey || event.metaKey || event.shiftKey)
|
|
68
|
+
return;
|
|
69
|
+
const isOpen = combobox.open();
|
|
70
|
+
const { key, altKey } = event;
|
|
71
|
+
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
72
|
+
const down = key === "ArrowDown";
|
|
73
|
+
if (altKey) {
|
|
74
|
+
// Alt+Down opens without moving, Alt+Up closes.
|
|
75
|
+
if (down === isOpen)
|
|
76
|
+
return;
|
|
77
|
+
setOpen(down, "arrow-key", event);
|
|
78
|
+
}
|
|
79
|
+
else if (!isOpen) {
|
|
80
|
+
setOpen(true, "arrow-key", event);
|
|
81
|
+
// While closed, the move waits for the list to open.
|
|
82
|
+
const items = list.items();
|
|
83
|
+
list.move(down ? items[0] : items[items.length - 1]);
|
|
84
|
+
}
|
|
85
|
+
else if (!list.onKeyDown(event))
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
else if (key === "Enter" && !altKey) {
|
|
89
|
+
if (!isOpen)
|
|
90
|
+
return;
|
|
91
|
+
const item = list.active();
|
|
92
|
+
if (!item) {
|
|
93
|
+
// A form submits, as it would from a text input.
|
|
94
|
+
setOpen(false, "enter-key", event);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (combobox.select(item, event))
|
|
98
|
+
setOpen(false, "item-press", event);
|
|
99
|
+
}
|
|
100
|
+
else if (key === "Escape" && !altKey) {
|
|
101
|
+
if (isOpen)
|
|
102
|
+
setOpen(false, "escape-key", event);
|
|
103
|
+
// Nothing to close or clear: a dialog around it may close.
|
|
104
|
+
else if (!combobox.clear(event, "escape-key"))
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
return;
|
|
109
|
+
event.preventDefault();
|
|
110
|
+
},
|
|
111
|
+
onBlur: (event) => {
|
|
112
|
+
// Focus lost to another window comes back to the input.
|
|
113
|
+
if (!document.hasFocus())
|
|
114
|
+
return;
|
|
115
|
+
setOpen(false, "focus-out", event);
|
|
116
|
+
onFocusOut?.(event);
|
|
117
|
+
},
|
|
118
|
+
ref: (element) => {
|
|
119
|
+
input = element;
|
|
120
|
+
combobox.input = input;
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
return { props, syncText };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Calls `reset` when the form of `input()` is reset, from the time the
|
|
127
|
+
* part has settled: a form reset puts a control back to its default, and
|
|
128
|
+
* so it does a combobox, through its setters, so that a controlled one
|
|
129
|
+
* asks. The browser resets the form's inputs after the event, to their
|
|
130
|
+
* `value` attributes; an `onFlush` in `reset` runs after that.
|
|
131
|
+
*/
|
|
132
|
+
export function onFormReset(input, reset) {
|
|
133
|
+
onSettled(() => {
|
|
134
|
+
const form = input()?.form;
|
|
135
|
+
if (!form)
|
|
136
|
+
return;
|
|
137
|
+
form.addEventListener("reset", reset);
|
|
138
|
+
return () => form.removeEventListener("reset", reset);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { merge, omit } from "solid-js";
|
|
2
2
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
3
|
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
4
|
import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
5
|
+
import { createComboboxItem } from "./createComboboxItem.js";
|
|
5
6
|
import { ComboboxItemContext } from "./ComboboxItemContext.js";
|
|
6
7
|
/**
|
|
7
8
|
* A value to select: a `<div role="option">` with `aria-selected`. A click
|
|
@@ -11,31 +12,8 @@ import { ComboboxItemContext } from "./ComboboxItemContext.js";
|
|
|
11
12
|
* `Combobox.List`.
|
|
12
13
|
*/
|
|
13
14
|
export function ComboboxItem(props) {
|
|
14
|
-
const combobox = useComboboxContext();
|
|
15
|
-
|
|
16
|
-
const generated = createUniqueId();
|
|
17
|
-
// The consumer's `id`, else the generated one, which the Input names
|
|
18
|
-
// while the item is active, so `false` does not remove it. Kept out of
|
|
19
|
-
// the merge ("`undefined` is a value", PLAN.md section 8).
|
|
20
|
-
const id = () => props.id || generated;
|
|
21
|
-
const item = {
|
|
22
|
-
get element() {
|
|
23
|
-
return element;
|
|
24
|
-
},
|
|
25
|
-
get id() {
|
|
26
|
-
return id();
|
|
27
|
-
},
|
|
28
|
-
get text() {
|
|
29
|
-
return props.label ?? labelOf(element);
|
|
30
|
-
},
|
|
31
|
-
get value() {
|
|
32
|
-
return props.value;
|
|
33
|
-
},
|
|
34
|
-
get disabled() {
|
|
35
|
-
return props.disabled;
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
onSettled(() => combobox.list.register(item));
|
|
15
|
+
const combobox = useComboboxContext("Combobox");
|
|
16
|
+
const { props: itemProps } = createComboboxItem(combobox, props, (element) => props.label ?? labelOf(element));
|
|
39
17
|
const selected = () => combobox.value() === props.value;
|
|
40
18
|
const attributes = {
|
|
41
19
|
get "data-selected"() {
|
|
@@ -45,32 +23,11 @@ export function ComboboxItem(props) {
|
|
|
45
23
|
return selected() ? undefined : "";
|
|
46
24
|
},
|
|
47
25
|
};
|
|
48
|
-
const ownProps = merge(attributes, {
|
|
49
|
-
role: "option",
|
|
50
|
-
get id() {
|
|
51
|
-
return id();
|
|
52
|
-
},
|
|
26
|
+
const ownProps = merge(attributes, itemProps, {
|
|
53
27
|
// Solid 2.0 removes an attribute set to false, so spell the value out.
|
|
54
28
|
get "aria-selected"() {
|
|
55
29
|
return selected() ? "true" : "false";
|
|
56
30
|
},
|
|
57
|
-
get "data-active"() {
|
|
58
|
-
return combobox.isActive(item) ? "" : undefined;
|
|
59
|
-
},
|
|
60
|
-
get "aria-disabled"() {
|
|
61
|
-
return props.disabled ? "true" : undefined;
|
|
62
|
-
},
|
|
63
|
-
get "data-disabled"() {
|
|
64
|
-
return props.disabled ? "" : undefined;
|
|
65
|
-
},
|
|
66
|
-
onPointerMove: (event) => combobox.list.onPointerMove(item, event),
|
|
67
|
-
onClick: (event) => {
|
|
68
|
-
if (combobox.select(item, event))
|
|
69
|
-
combobox.setOpen(false, { reason: "item-press", event });
|
|
70
|
-
},
|
|
71
|
-
ref: (el) => {
|
|
72
|
-
element = el;
|
|
73
|
-
},
|
|
74
31
|
});
|
|
75
32
|
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
|
|
76
33
|
return (<ComboboxItemContext value={attributes}>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { ElementProps } from "../../core/renderElement.tsx";
|
|
3
|
+
import type { ComboboxBase, ComboboxItemEntry } from "../root/ComboboxContext.ts";
|
|
4
|
+
/** The props of an item that `createComboboxItem` reads. */
|
|
5
|
+
export interface ComboboxItemOptions {
|
|
6
|
+
readonly id?: JSX.HTMLAttributes<HTMLElement>["id"];
|
|
7
|
+
readonly value: string;
|
|
8
|
+
readonly disabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* An item of a combobox, the code `Combobox.Item` and `Autocomplete.Item`
|
|
12
|
+
* share: its entry in the list navigation, registered once mounted, and
|
|
13
|
+
* the props of its `option` element, its `id`, `data-active`, the disabled
|
|
14
|
+
* state, the pointer and the click that selects or accepts it. `text` is
|
|
15
|
+
* what the input shows once it is: a Combobox's label, an Autocomplete's
|
|
16
|
+
* value. Each Item adds its own `aria-selected`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createComboboxItem(combobox: ComboboxBase, props: ComboboxItemOptions, text: (element: HTMLElement) => string): {
|
|
19
|
+
item: ComboboxItemEntry;
|
|
20
|
+
props: ElementProps;
|
|
21
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createUniqueId, onSettled } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* An item of a combobox, the code `Combobox.Item` and `Autocomplete.Item`
|
|
4
|
+
* share: its entry in the list navigation, registered once mounted, and
|
|
5
|
+
* the props of its `option` element, its `id`, `data-active`, the disabled
|
|
6
|
+
* state, the pointer and the click that selects or accepts it. `text` is
|
|
7
|
+
* what the input shows once it is: a Combobox's label, an Autocomplete's
|
|
8
|
+
* value. Each Item adds its own `aria-selected`.
|
|
9
|
+
*/
|
|
10
|
+
export function createComboboxItem(combobox, props, text) {
|
|
11
|
+
let element;
|
|
12
|
+
const generated = createUniqueId();
|
|
13
|
+
// The consumer's `id`, else the generated one, which the Input names
|
|
14
|
+
// while the item is active, so `false` does not remove it. Kept out of
|
|
15
|
+
// the merge ("`undefined` is a value", PLAN.md section 8).
|
|
16
|
+
const id = () => props.id || generated;
|
|
17
|
+
const item = {
|
|
18
|
+
get element() {
|
|
19
|
+
return element;
|
|
20
|
+
},
|
|
21
|
+
get id() {
|
|
22
|
+
return id();
|
|
23
|
+
},
|
|
24
|
+
get text() {
|
|
25
|
+
return text(element);
|
|
26
|
+
},
|
|
27
|
+
get value() {
|
|
28
|
+
return props.value;
|
|
29
|
+
},
|
|
30
|
+
get disabled() {
|
|
31
|
+
return props.disabled;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
onSettled(() => combobox.list.register(item));
|
|
35
|
+
return {
|
|
36
|
+
item,
|
|
37
|
+
props: {
|
|
38
|
+
role: "option",
|
|
39
|
+
get id() {
|
|
40
|
+
return id();
|
|
41
|
+
},
|
|
42
|
+
get "data-active"() {
|
|
43
|
+
return combobox.isActive(item) ? "" : undefined;
|
|
44
|
+
},
|
|
45
|
+
get "aria-disabled"() {
|
|
46
|
+
return props.disabled ? "true" : undefined;
|
|
47
|
+
},
|
|
48
|
+
get "data-disabled"() {
|
|
49
|
+
return props.disabled ? "" : undefined;
|
|
50
|
+
},
|
|
51
|
+
onPointerMove: (event) => combobox.list.onPointerMove(item, event),
|
|
52
|
+
onClick: (event) => {
|
|
53
|
+
if (combobox.select(item, event))
|
|
54
|
+
combobox.setOpen(false, { reason: "item-press", event });
|
|
55
|
+
},
|
|
56
|
+
ref: (el) => {
|
|
57
|
+
element = el;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -81,52 +81,99 @@ export interface ComboboxStateProps {
|
|
|
81
81
|
/** Whether the Combobox ignores the user. It cannot be focused either. */
|
|
82
82
|
disabled?: boolean;
|
|
83
83
|
}
|
|
84
|
-
/**
|
|
85
|
-
|
|
84
|
+
/**
|
|
85
|
+
* What a combobox's shared parts read through context: the Trigger, Clear,
|
|
86
|
+
* the Popup, the List, the Status, Group and the Input's shared code.
|
|
87
|
+
* `Combobox.Root` shares a `Combobox`, and `Autocomplete.Root`, which builds
|
|
88
|
+
* on Combobox, shares its own (`src/autocomplete/README.md`).
|
|
89
|
+
*/
|
|
90
|
+
export interface ComboboxBase {
|
|
91
|
+
/**
|
|
92
|
+
* Which Root made it, so that a part only one of them takes can say it
|
|
93
|
+
* is inside the other.
|
|
94
|
+
*/
|
|
95
|
+
readonly component: "Combobox" | "Autocomplete";
|
|
86
96
|
/** Whether the list is open. */
|
|
87
97
|
open(): boolean;
|
|
88
98
|
/** Asks to open or close the list, saying why. */
|
|
89
99
|
setOpen(open: boolean, details: OpenChangeDetails): void;
|
|
100
|
+
/** The text the input shows. */
|
|
101
|
+
inputValue(): string;
|
|
102
|
+
/**
|
|
103
|
+
* Asks for the text the user typed: the one reason the shared parts
|
|
104
|
+
* report. Each Root's own parts set it for their own reasons too.
|
|
105
|
+
*/
|
|
106
|
+
setInputValue(text: string, details: {
|
|
107
|
+
readonly reason: "input-change";
|
|
108
|
+
readonly event: Event;
|
|
109
|
+
}): void;
|
|
110
|
+
/**
|
|
111
|
+
* Selects `item`, or accepts it, as Enter or a click does. A disabled
|
|
112
|
+
* item changes nothing. Returns whether it could be selected.
|
|
113
|
+
*/
|
|
114
|
+
select(item: ComboboxItemEntry | undefined, event: Event): boolean;
|
|
115
|
+
/** Empties the text, and the value. Returns whether there was any. */
|
|
116
|
+
clear(event: Event, reason: "escape-key" | "clear-press"): boolean;
|
|
117
|
+
/** Whether `item` is the active one of an open list, for its `data-active`. */
|
|
118
|
+
isActive(item: ComboboxItemEntry): boolean;
|
|
119
|
+
/** The number of items in the list, for `Combobox.Status`. */
|
|
120
|
+
count(): number;
|
|
121
|
+
readonly list: ActiveDescendantList<ComboboxItemEntry>;
|
|
122
|
+
/** The List's `id`, which the Input names before the List is rendered. */
|
|
123
|
+
readonly listId: string;
|
|
124
|
+
/** The input, once rendered: focused by the Trigger and Clear. */
|
|
125
|
+
input: HTMLInputElement | undefined;
|
|
126
|
+
/** The `data-*` attributes of the Input, the Trigger and the Popup. */
|
|
127
|
+
readonly attributes: ComboboxStateAttributes;
|
|
128
|
+
/** The Root's props that the shared parts read. */
|
|
129
|
+
readonly props: {
|
|
130
|
+
readonly disabled?: boolean;
|
|
131
|
+
readonly required?: boolean;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** What `Combobox.Root` shares with its parts through context. */
|
|
135
|
+
export interface Combobox extends ComboboxBase {
|
|
136
|
+
readonly component: "Combobox";
|
|
90
137
|
/** The selected item's `value`, or `null`. */
|
|
91
138
|
value(): string | null;
|
|
92
139
|
/** Asks for a value, as the hidden input's `change` and a reset do. */
|
|
93
140
|
setValue(value: string | null): void;
|
|
94
|
-
/** The text the input shows. */
|
|
95
|
-
inputValue(): string;
|
|
96
141
|
/** Asks for a text, saying why. */
|
|
97
142
|
setInputValue(text: string, details: InputValueChangeDetails): void;
|
|
98
143
|
/**
|
|
99
144
|
* Selects `item`: its value through the hidden input, which fires
|
|
100
|
-
* `input` and `change` for the form, and its label as the text.
|
|
101
|
-
* disabled item changes nothing. Returns whether it could be selected.
|
|
145
|
+
* `input` and `change` for the form, and its label as the text.
|
|
102
146
|
*/
|
|
103
147
|
select(item: ComboboxItemEntry | undefined, event: Event): boolean;
|
|
104
|
-
/** Empties the text and the value. */
|
|
105
|
-
clear(event: Event, reason: "escape-key" | "clear-press"): void;
|
|
106
148
|
/**
|
|
107
149
|
* Puts the text back to the selected item's label as focus leaves, or
|
|
108
|
-
* clears the value if the user emptied the text
|
|
150
|
+
* clears the value if the user emptied the text: the Input's `onFocusOut`.
|
|
109
151
|
*/
|
|
110
152
|
revert(event: Event): void;
|
|
111
153
|
/** The label of the item with `value`, if one has mounted. */
|
|
112
154
|
labelOfValue(value: string | null): string | undefined;
|
|
113
|
-
/** Whether `item` is the active one of an open list, for its `data-active`. */
|
|
114
|
-
isActive(item: ComboboxItemEntry): boolean;
|
|
115
|
-
/** The number of items in the list, for `Combobox.Status`. */
|
|
116
|
-
count(): number;
|
|
117
|
-
readonly list: ActiveDescendantList<ComboboxItemEntry>;
|
|
118
|
-
/** The List's `id`, which the Input names before the List is rendered. */
|
|
119
|
-
readonly listId: string;
|
|
120
|
-
/** The input, once rendered: focused by the Trigger and Clear. */
|
|
121
|
-
input: HTMLInputElement | undefined;
|
|
122
155
|
/** The hidden input, once rendered: the form's view of the value. */
|
|
123
156
|
hiddenInput: HTMLInputElement | undefined;
|
|
124
|
-
/** The `data-*` attributes of the Input, the Trigger and the Popup. */
|
|
125
|
-
readonly attributes: ComboboxStateAttributes;
|
|
126
157
|
/** The Root's props, for the parts' elements. */
|
|
127
158
|
readonly props: ComboboxStateProps;
|
|
128
159
|
}
|
|
160
|
+
/** The Root's props that every combobox's shared state reads. */
|
|
161
|
+
export type ComboboxBaseProps = Pick<ComboboxStateProps, "open" | "defaultOpen" | "onOpenChange" | "disabled" | "required">;
|
|
162
|
+
/** What `createComboboxBase` makes: the state no Root does otherwise. */
|
|
163
|
+
export type ComboboxBaseState = Pick<ComboboxBase, "open" | "setOpen" | "isActive" | "count" | "list" | "listId" | "input" | "attributes">;
|
|
164
|
+
/**
|
|
165
|
+
* The state `Combobox.Root` and `Autocomplete.Root` share: whether the list
|
|
166
|
+
* is open, its list navigation, the List's `id` and the `data-*`
|
|
167
|
+
* attributes. Each Root spreads it into its own combobox, beside its text
|
|
168
|
+
* and what accepting an item does.
|
|
169
|
+
*/
|
|
170
|
+
export declare function createComboboxBase(props: ComboboxBaseProps): ComboboxBaseState;
|
|
129
171
|
/** Creates a combobox's state and its list navigation. */
|
|
130
172
|
export declare function createCombobox(props: ComboboxStateProps): Combobox;
|
|
131
|
-
export declare const ComboboxContext: import("solid-js").Context<
|
|
132
|
-
|
|
173
|
+
export declare const ComboboxContext: import("solid-js").Context<ComboboxBase | null>;
|
|
174
|
+
/**
|
|
175
|
+
* The combobox of the Root around the part. A shared part passes no
|
|
176
|
+
* `component` and takes either Root's; a part only one Root takes passes
|
|
177
|
+
* that Root's `component`, and its type, `Combobox` or an Autocomplete's.
|
|
178
|
+
*/
|
|
179
|
+
export declare function useComboboxContext<T extends ComboboxBase = ComboboxBase>(component?: T["component"]): T;
|