menhir 0.1.6 → 0.1.7
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/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/package.json +6 -1
package/README.md
CHANGED
|
@@ -60,9 +60,9 @@ For everything else, go to the docs:
|
|
|
60
60
|
|
|
61
61
|
## Status
|
|
62
62
|
|
|
63
|
-
Early 0.x versions: the API may change between minor versions.
|
|
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, Checkbox, Collapsible, Combobox, Context Menu, Dialog, 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, 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,11 @@
|
|
|
1
|
+
import { AutocompleteRoot } from "./root/AutocompleteRoot.js";
|
|
2
|
+
import { AutocompleteInput } from "./input/AutocompleteInput.js";
|
|
3
|
+
import { ComboboxTrigger } from "../combobox/trigger/ComboboxTrigger.js";
|
|
4
|
+
import { ComboboxClear } from "../combobox/clear/ComboboxClear.js";
|
|
5
|
+
import { ComboboxPopup } from "../combobox/popup/ComboboxPopup.js";
|
|
6
|
+
import { ComboboxList } from "../combobox/list/ComboboxList.js";
|
|
7
|
+
import { AutocompleteItem } from "./item/AutocompleteItem.js";
|
|
8
|
+
import { ComboboxGroup } from "../combobox/group/ComboboxGroup.js";
|
|
9
|
+
import { ComboboxGroupLabel } from "../combobox/group-label/ComboboxGroupLabel.js";
|
|
10
|
+
import { ComboboxStatus } from "../combobox/status/ComboboxStatus.js";
|
|
11
|
+
export { ComboboxClear as Clear, ComboboxGroup as Group, ComboboxGroupLabel as GroupLabel, AutocompleteInput as Input, AutocompleteItem as Item, ComboboxList as List, ComboboxPopup as Popup, AutocompleteRoot as Root, ComboboxStatus as Status, ComboboxTrigger as Trigger };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { createComboboxInput, onFormReset } from "../../combobox/input/createComboboxInput.js";
|
|
4
|
+
import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
|
|
5
|
+
import { spread, template } from "@solidjs/web";
|
|
6
|
+
import { merge, omit, onFlush } from "solid-js";
|
|
7
|
+
// src/autocomplete/input/AutocompleteInput.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input>`);
|
|
9
|
+
/**
|
|
10
|
+
* The autocomplete itself, and the form's control: an `<input type="text"
|
|
11
|
+
* role="combobox" aria-autocomplete="list">` that takes `name`, `form`,
|
|
12
|
+
* `required` and `disabled` from the Root, so the form receives its text.
|
|
13
|
+
* It keeps focus while the list is open: typing opens it, and the arrow
|
|
14
|
+
* keys, Enter and Escape act on it. Name it with a `<label>`. Place it
|
|
15
|
+
* inside `Autocomplete.Root`.
|
|
16
|
+
*/
|
|
17
|
+
function AutocompleteInput(props) {
|
|
18
|
+
const autocomplete = useComboboxContext("Autocomplete");
|
|
19
|
+
const input = createComboboxInput(autocomplete);
|
|
20
|
+
onFormReset(() => autocomplete.input, (event) => {
|
|
21
|
+
autocomplete.setInputValue(autocomplete.props.defaultValue ?? "", {
|
|
22
|
+
reason: "reset",
|
|
23
|
+
event
|
|
24
|
+
});
|
|
25
|
+
onFlush(input.syncText);
|
|
26
|
+
});
|
|
27
|
+
const ownProps = merge(input.props, {
|
|
28
|
+
get name() {
|
|
29
|
+
return autocomplete.props.name;
|
|
30
|
+
},
|
|
31
|
+
get form() {
|
|
32
|
+
return autocomplete.props.form;
|
|
33
|
+
},
|
|
34
|
+
get required() {
|
|
35
|
+
return autocomplete.props.required;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
39
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
40
|
+
var _el$ = _tmpl$();
|
|
41
|
+
spread(_el$, p, false);
|
|
42
|
+
return _el$;
|
|
43
|
+
})());
|
|
44
|
+
}
|
|
45
|
+
export { AutocompleteInput };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
|
|
4
|
+
import { createComboboxItem } from "../../combobox/item/createComboboxItem.js";
|
|
5
|
+
import { spread, template } from "@solidjs/web";
|
|
6
|
+
import { merge, omit } from "solid-js";
|
|
7
|
+
// src/autocomplete/item/AutocompleteItem.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
9
|
+
/**
|
|
10
|
+
* A suggestion: a `<div role="option">`, with `aria-selected="true"` while
|
|
11
|
+
* it is the active item. A click accepts it, sets the input's text to its
|
|
12
|
+
* `value` and closes the list, and so does Enter while it is active; a
|
|
13
|
+
* disabled item is not accepted. Render the suggestions that match the
|
|
14
|
+
* text, and place them inside `Autocomplete.List`.
|
|
15
|
+
*/
|
|
16
|
+
function AutocompleteItem(props) {
|
|
17
|
+
const autocomplete = useComboboxContext("Autocomplete");
|
|
18
|
+
const { item, props: itemProps } = createComboboxItem(autocomplete, props, () => props.value);
|
|
19
|
+
const ownProps = merge(itemProps, { get "aria-selected"() {
|
|
20
|
+
return autocomplete.isActive(item) ? "true" : void 0;
|
|
21
|
+
} });
|
|
22
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "id"));
|
|
23
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
24
|
+
var _el$ = _tmpl$();
|
|
25
|
+
spread(_el$, p, false);
|
|
26
|
+
return _el$;
|
|
27
|
+
})());
|
|
28
|
+
}
|
|
29
|
+
export { AutocompleteItem };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { createComboboxBase } from "../../combobox/root/ComboboxContext.js";
|
|
3
|
+
// src/autocomplete/root/AutocompleteContext.ts
|
|
4
|
+
/** Creates an autocomplete's state, on Combobox's shared state. */
|
|
5
|
+
function createAutocomplete(props) {
|
|
6
|
+
const [text, setText, requestedText] = createControllableSignal({
|
|
7
|
+
get value() {
|
|
8
|
+
return props.value;
|
|
9
|
+
},
|
|
10
|
+
get defaultValue() {
|
|
11
|
+
return props.defaultValue ?? "";
|
|
12
|
+
},
|
|
13
|
+
onChange: (text, details) => props.onValueChange?.(text, details)
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
...createComboboxBase(props),
|
|
17
|
+
component: "Autocomplete",
|
|
18
|
+
inputValue: text,
|
|
19
|
+
setInputValue: setText,
|
|
20
|
+
select: (item, event) => {
|
|
21
|
+
if (!item || item.disabled) return false;
|
|
22
|
+
setText(item.value, {
|
|
23
|
+
reason: "item-press",
|
|
24
|
+
event
|
|
25
|
+
});
|
|
26
|
+
return true;
|
|
27
|
+
},
|
|
28
|
+
clear: (event, reason) => {
|
|
29
|
+
if (!requestedText()) return false;
|
|
30
|
+
setText("", {
|
|
31
|
+
reason,
|
|
32
|
+
event
|
|
33
|
+
});
|
|
34
|
+
return true;
|
|
35
|
+
},
|
|
36
|
+
props
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export { createAutocomplete };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ComboboxContext } from "../../combobox/root/ComboboxContext.js";
|
|
2
|
+
import { createAutocomplete } from "./AutocompleteContext.js";
|
|
3
|
+
import { createComponent } from "@solidjs/web";
|
|
4
|
+
// src/autocomplete/root/AutocompleteRoot.tsx
|
|
5
|
+
/**
|
|
6
|
+
* A text input with suggestions: holds the text, which is its value, and
|
|
7
|
+
* whether the list is open, and shares them with `Autocomplete.Input`,
|
|
8
|
+
* `Autocomplete.Trigger`, `Autocomplete.Clear`, `Autocomplete.Popup` and
|
|
9
|
+
* the parts inside it. Its form props go on the Input. Renders no element
|
|
10
|
+
* of its own.
|
|
11
|
+
*/
|
|
12
|
+
function AutocompleteRoot(props) {
|
|
13
|
+
return createComponent(ComboboxContext, {
|
|
14
|
+
get value() {
|
|
15
|
+
return createAutocomplete(props);
|
|
16
|
+
},
|
|
17
|
+
get children() {
|
|
18
|
+
return props.children;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export { AutocompleteRoot };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
2
|
import { renderElement } from "../../core/renderElement.js";
|
|
3
3
|
import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
4
|
+
import { createComboboxInput, onFormReset } from "./createComboboxInput.js";
|
|
4
5
|
import { effect, heldRead, memo, ref, setAttribute, spread, template } from "@solidjs/web";
|
|
5
|
-
import {
|
|
6
|
+
import { omit, onFlush, untrack } from "solid-js";
|
|
6
7
|
// src/combobox/input/ComboboxInput.tsx
|
|
7
8
|
var _tmpl$ = /* @__PURE__ */ template(`<input aria-hidden=true tabindex=-1 style=position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none>`);
|
|
8
9
|
var _tmpl$2 = /* @__PURE__ */ template(`<input>`);
|
|
@@ -16,89 +17,9 @@ var _tmpl$2 = /* @__PURE__ */ template(`<input>`);
|
|
|
16
17
|
* `Combobox.Root`.
|
|
17
18
|
*/
|
|
18
19
|
function ComboboxInput(props) {
|
|
19
|
-
const combobox = useComboboxContext();
|
|
20
|
-
const
|
|
21
|
-
let input;
|
|
20
|
+
const combobox = useComboboxContext("Combobox");
|
|
21
|
+
const input = createComboboxInput(combobox, combobox.revert);
|
|
22
22
|
let hiddenInput;
|
|
23
|
-
const details = (reason, event) => ({
|
|
24
|
-
reason,
|
|
25
|
-
event
|
|
26
|
-
});
|
|
27
|
-
const setOpen = (open, reason, event) => combobox.setOpen(open, details(reason, event));
|
|
28
|
-
const [draft, setDraft] = createSignal(() => combobox.inputValue());
|
|
29
|
-
const syncText = () => {
|
|
30
|
-
const text = untrack(combobox.inputValue);
|
|
31
|
-
setDraft(text);
|
|
32
|
-
if (input && input.value !== text) input.value = text;
|
|
33
|
-
};
|
|
34
|
-
const ownProps = merge(combobox.attributes, {
|
|
35
|
-
type: "text",
|
|
36
|
-
role: "combobox",
|
|
37
|
-
"aria-autocomplete": "list",
|
|
38
|
-
autocomplete: "off",
|
|
39
|
-
get value() {
|
|
40
|
-
return draft();
|
|
41
|
-
},
|
|
42
|
-
get disabled() {
|
|
43
|
-
return combobox.props.disabled;
|
|
44
|
-
},
|
|
45
|
-
get "aria-expanded"() {
|
|
46
|
-
return combobox.open() ? "true" : "false";
|
|
47
|
-
},
|
|
48
|
-
"aria-controls": combobox.listId,
|
|
49
|
-
get "aria-activedescendant"() {
|
|
50
|
-
return combobox.open() ? list.activeId() : void 0;
|
|
51
|
-
},
|
|
52
|
-
get "aria-required"() {
|
|
53
|
-
return combobox.props.required ? "true" : void 0;
|
|
54
|
-
},
|
|
55
|
-
onInput: (event) => {
|
|
56
|
-
setDraft(input.value);
|
|
57
|
-
combobox.setInputValue(input.value, details("input-change", event));
|
|
58
|
-
list.move(void 0);
|
|
59
|
-
setOpen(true, "input-change", event);
|
|
60
|
-
onFlush(syncText);
|
|
61
|
-
},
|
|
62
|
-
onKeyDown: (event) => {
|
|
63
|
-
if (event.isComposing || event.keyCode === 229) return;
|
|
64
|
-
if (event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
65
|
-
const isOpen = combobox.open();
|
|
66
|
-
const { key, altKey } = event;
|
|
67
|
-
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
68
|
-
const down = key === "ArrowDown";
|
|
69
|
-
if (altKey) {
|
|
70
|
-
if (down === isOpen) return;
|
|
71
|
-
setOpen(down, "arrow-key", event);
|
|
72
|
-
} else if (!isOpen) {
|
|
73
|
-
setOpen(true, "arrow-key", event);
|
|
74
|
-
const items = list.items();
|
|
75
|
-
list.move(down ? items[0] : items[items.length - 1]);
|
|
76
|
-
} else if (!list.onKeyDown(event)) return;
|
|
77
|
-
} else if (key === "Enter" && !altKey) {
|
|
78
|
-
if (!isOpen) return;
|
|
79
|
-
const item = list.active();
|
|
80
|
-
if (!item) {
|
|
81
|
-
setOpen(false, "enter-key", event);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
if (combobox.select(item, event)) setOpen(false, "item-press", event);
|
|
85
|
-
} else if (key === "Escape" && !altKey) {
|
|
86
|
-
if (isOpen) setOpen(false, "escape-key", event);
|
|
87
|
-
else if (combobox.inputValue() || combobox.value() !== null) combobox.clear(event, "escape-key");
|
|
88
|
-
else return;
|
|
89
|
-
} else return;
|
|
90
|
-
event.preventDefault();
|
|
91
|
-
},
|
|
92
|
-
onBlur: (event) => {
|
|
93
|
-
if (!document.hasFocus()) return;
|
|
94
|
-
setOpen(false, "focus-out", event);
|
|
95
|
-
combobox.revert(event);
|
|
96
|
-
},
|
|
97
|
-
ref: (element) => {
|
|
98
|
-
input = element;
|
|
99
|
-
combobox.input = input;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
23
|
const onHiddenChange = () => {
|
|
103
24
|
combobox.setValue(hiddenInput.value || null);
|
|
104
25
|
onFlush(syncHidden);
|
|
@@ -106,22 +27,19 @@ function ComboboxInput(props) {
|
|
|
106
27
|
const syncHidden = () => {
|
|
107
28
|
if (hiddenInput) hiddenInput.value = untrack(combobox.value) ?? "";
|
|
108
29
|
};
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
form.addEventListener("reset", onReset);
|
|
122
|
-
return () => form.removeEventListener("reset", onReset);
|
|
30
|
+
onFormReset(() => hiddenInput, (event) => {
|
|
31
|
+
const value = combobox.props.defaultValue ?? null;
|
|
32
|
+
combobox.setValue(value);
|
|
33
|
+
combobox.setInputValue(combobox.props.defaultInputValue ?? combobox.labelOfValue(value) ?? "", {
|
|
34
|
+
reason: "reset",
|
|
35
|
+
event
|
|
36
|
+
});
|
|
37
|
+
onFlush(() => {
|
|
38
|
+
syncHidden();
|
|
39
|
+
input.syncText();
|
|
40
|
+
});
|
|
123
41
|
});
|
|
124
|
-
const elementProps = mergeElementProps(
|
|
42
|
+
const elementProps = mergeElementProps(input.props, omit(props, "render"));
|
|
125
43
|
return [memo(() => {
|
|
126
44
|
return renderElement(props, elementProps, (p) => (() => {
|
|
127
45
|
var _el$2 = _tmpl$2();
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createSignal, merge, onFlush, onSettled, untrack } from "solid-js";
|
|
2
|
+
// src/combobox/input/createComboboxInput.ts
|
|
3
|
+
/**
|
|
4
|
+
* The input of a combobox, the code `Combobox.Input` and
|
|
5
|
+
* `Autocomplete.Input` share: its attributes, typing, the keys, focus
|
|
6
|
+
* leaving, and the draft its `value` binds to. Focus leaving closes the
|
|
7
|
+
* list and then calls `onFocusOut`, which a Combobox puts its text back
|
|
8
|
+
* in; an Autocomplete keeps the text, and passes none. Returns its props
|
|
9
|
+
* and `syncText`, which puts the element back to the combobox's text once
|
|
10
|
+
* that has settled, as after a form reset.
|
|
11
|
+
*/
|
|
12
|
+
function createComboboxInput(combobox, onFocusOut) {
|
|
13
|
+
const { list } = combobox;
|
|
14
|
+
let input;
|
|
15
|
+
const details = (reason, event) => ({
|
|
16
|
+
reason,
|
|
17
|
+
event
|
|
18
|
+
});
|
|
19
|
+
const setOpen = (open, reason, event) => combobox.setOpen(open, details(reason, event));
|
|
20
|
+
const [draft, setDraft] = createSignal(() => combobox.inputValue());
|
|
21
|
+
const syncText = () => {
|
|
22
|
+
const text = untrack(combobox.inputValue);
|
|
23
|
+
setDraft(text);
|
|
24
|
+
if (input && input.value !== text) input.value = text;
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
props: merge(combobox.attributes, {
|
|
28
|
+
type: "text",
|
|
29
|
+
role: "combobox",
|
|
30
|
+
"aria-autocomplete": "list",
|
|
31
|
+
autocomplete: "off",
|
|
32
|
+
get value() {
|
|
33
|
+
return draft();
|
|
34
|
+
},
|
|
35
|
+
get disabled() {
|
|
36
|
+
return combobox.props.disabled;
|
|
37
|
+
},
|
|
38
|
+
get "aria-expanded"() {
|
|
39
|
+
return combobox.open() ? "true" : "false";
|
|
40
|
+
},
|
|
41
|
+
"aria-controls": combobox.listId,
|
|
42
|
+
get "aria-activedescendant"() {
|
|
43
|
+
return combobox.open() ? list.activeId() : void 0;
|
|
44
|
+
},
|
|
45
|
+
get "aria-required"() {
|
|
46
|
+
return combobox.props.required ? "true" : void 0;
|
|
47
|
+
},
|
|
48
|
+
onInput: (event) => {
|
|
49
|
+
setDraft(input.value);
|
|
50
|
+
combobox.setInputValue(input.value, details("input-change", event));
|
|
51
|
+
list.move(void 0);
|
|
52
|
+
setOpen(true, "input-change", event);
|
|
53
|
+
onFlush(syncText);
|
|
54
|
+
},
|
|
55
|
+
onKeyDown: (event) => {
|
|
56
|
+
if (event.isComposing || event.keyCode === 229) return;
|
|
57
|
+
if (event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
58
|
+
const isOpen = combobox.open();
|
|
59
|
+
const { key, altKey } = event;
|
|
60
|
+
if (key === "ArrowDown" || key === "ArrowUp") {
|
|
61
|
+
const down = key === "ArrowDown";
|
|
62
|
+
if (altKey) {
|
|
63
|
+
if (down === isOpen) return;
|
|
64
|
+
setOpen(down, "arrow-key", event);
|
|
65
|
+
} else if (!isOpen) {
|
|
66
|
+
setOpen(true, "arrow-key", event);
|
|
67
|
+
const items = list.items();
|
|
68
|
+
list.move(down ? items[0] : items[items.length - 1]);
|
|
69
|
+
} else if (!list.onKeyDown(event)) return;
|
|
70
|
+
} else if (key === "Enter" && !altKey) {
|
|
71
|
+
if (!isOpen) return;
|
|
72
|
+
const item = list.active();
|
|
73
|
+
if (!item) {
|
|
74
|
+
setOpen(false, "enter-key", event);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (combobox.select(item, event)) setOpen(false, "item-press", event);
|
|
78
|
+
} else if (key === "Escape" && !altKey) {
|
|
79
|
+
if (isOpen) setOpen(false, "escape-key", event);
|
|
80
|
+
else if (!combobox.clear(event, "escape-key")) return;
|
|
81
|
+
} else return;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
},
|
|
84
|
+
onBlur: (event) => {
|
|
85
|
+
if (!document.hasFocus()) return;
|
|
86
|
+
setOpen(false, "focus-out", event);
|
|
87
|
+
onFocusOut?.(event);
|
|
88
|
+
},
|
|
89
|
+
ref: (element) => {
|
|
90
|
+
input = element;
|
|
91
|
+
combobox.input = input;
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
syncText
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Calls `reset` when the form of `input()` is reset, from the time the
|
|
99
|
+
* part has settled: a form reset puts a control back to its default, and
|
|
100
|
+
* so it does a combobox, through its setters, so that a controlled one
|
|
101
|
+
* asks. The browser resets the form's inputs after the event, to their
|
|
102
|
+
* `value` attributes; an `onFlush` in `reset` runs after that.
|
|
103
|
+
*/
|
|
104
|
+
function onFormReset(input, reset) {
|
|
105
|
+
onSettled(() => {
|
|
106
|
+
const form = input()?.form;
|
|
107
|
+
if (!form) return;
|
|
108
|
+
form.addEventListener("reset", reset);
|
|
109
|
+
return () => form.removeEventListener("reset", reset);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export { createComboboxInput, onFormReset };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
2
|
import { renderElement } from "../../core/renderElement.js";
|
|
3
3
|
import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
4
|
+
import { createComboboxItem } from "./createComboboxItem.js";
|
|
4
5
|
import { ComboboxItemContext } from "./ComboboxItemContext.js";
|
|
5
6
|
import { createComponent, spread, template } from "@solidjs/web";
|
|
6
|
-
import {
|
|
7
|
+
import { merge, omit } from "solid-js";
|
|
7
8
|
// src/combobox/item/ComboboxItem.tsx
|
|
8
9
|
var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
9
10
|
/**
|
|
@@ -14,28 +15,8 @@ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
|
14
15
|
* `Combobox.List`.
|
|
15
16
|
*/
|
|
16
17
|
function ComboboxItem(props) {
|
|
17
|
-
const combobox = useComboboxContext();
|
|
18
|
-
|
|
19
|
-
const generated = createUniqueId();
|
|
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));
|
|
18
|
+
const combobox = useComboboxContext("Combobox");
|
|
19
|
+
const { props: itemProps } = createComboboxItem(combobox, props, (element) => props.label ?? labelOf(element));
|
|
39
20
|
const selected = () => combobox.value() === props.value;
|
|
40
21
|
const attributes = {
|
|
41
22
|
get "data-selected"() {
|
|
@@ -45,34 +26,9 @@ function ComboboxItem(props) {
|
|
|
45
26
|
return selected() ? void 0 : "";
|
|
46
27
|
}
|
|
47
28
|
};
|
|
48
|
-
const ownProps = merge(attributes, {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return id();
|
|
52
|
-
},
|
|
53
|
-
get "aria-selected"() {
|
|
54
|
-
return selected() ? "true" : "false";
|
|
55
|
-
},
|
|
56
|
-
get "data-active"() {
|
|
57
|
-
return combobox.isActive(item) ? "" : void 0;
|
|
58
|
-
},
|
|
59
|
-
get "aria-disabled"() {
|
|
60
|
-
return props.disabled ? "true" : void 0;
|
|
61
|
-
},
|
|
62
|
-
get "data-disabled"() {
|
|
63
|
-
return props.disabled ? "" : void 0;
|
|
64
|
-
},
|
|
65
|
-
onPointerMove: (event) => combobox.list.onPointerMove(item, event),
|
|
66
|
-
onClick: (event) => {
|
|
67
|
-
if (combobox.select(item, event)) combobox.setOpen(false, {
|
|
68
|
-
reason: "item-press",
|
|
69
|
-
event
|
|
70
|
-
});
|
|
71
|
-
},
|
|
72
|
-
ref: (el) => {
|
|
73
|
-
element = el;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
29
|
+
const ownProps = merge(attributes, itemProps, { get "aria-selected"() {
|
|
30
|
+
return selected() ? "true" : "false";
|
|
31
|
+
} });
|
|
76
32
|
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
|
|
77
33
|
return createComponent(ComboboxItemContext, {
|
|
78
34
|
value: attributes,
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createUniqueId, onSettled } from "solid-js";
|
|
2
|
+
// src/combobox/item/createComboboxItem.ts
|
|
3
|
+
/**
|
|
4
|
+
* An item of a combobox, the code `Combobox.Item` and `Autocomplete.Item`
|
|
5
|
+
* share: its entry in the list navigation, registered once mounted, and
|
|
6
|
+
* the props of its `option` element, its `id`, `data-active`, the disabled
|
|
7
|
+
* state, the pointer and the click that selects or accepts it. `text` is
|
|
8
|
+
* what the input shows once it is: a Combobox's label, an Autocomplete's
|
|
9
|
+
* value. Each Item adds its own `aria-selected`.
|
|
10
|
+
*/
|
|
11
|
+
function createComboboxItem(combobox, props, text) {
|
|
12
|
+
let element;
|
|
13
|
+
const generated = createUniqueId();
|
|
14
|
+
const id = () => props.id || generated;
|
|
15
|
+
const item = {
|
|
16
|
+
get element() {
|
|
17
|
+
return element;
|
|
18
|
+
},
|
|
19
|
+
get id() {
|
|
20
|
+
return id();
|
|
21
|
+
},
|
|
22
|
+
get text() {
|
|
23
|
+
return text(element);
|
|
24
|
+
},
|
|
25
|
+
get value() {
|
|
26
|
+
return props.value;
|
|
27
|
+
},
|
|
28
|
+
get disabled() {
|
|
29
|
+
return props.disabled;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
onSettled(() => combobox.list.register(item));
|
|
33
|
+
return {
|
|
34
|
+
item,
|
|
35
|
+
props: {
|
|
36
|
+
role: "option",
|
|
37
|
+
get id() {
|
|
38
|
+
return id();
|
|
39
|
+
},
|
|
40
|
+
get "data-active"() {
|
|
41
|
+
return combobox.isActive(item) ? "" : void 0;
|
|
42
|
+
},
|
|
43
|
+
get "aria-disabled"() {
|
|
44
|
+
return props.disabled ? "true" : void 0;
|
|
45
|
+
},
|
|
46
|
+
get "data-disabled"() {
|
|
47
|
+
return props.disabled ? "" : void 0;
|
|
48
|
+
},
|
|
49
|
+
onPointerMove: (event) => combobox.list.onPointerMove(item, event),
|
|
50
|
+
onClick: (event) => {
|
|
51
|
+
if (combobox.select(item, event)) combobox.setOpen(false, {
|
|
52
|
+
reason: "item-press",
|
|
53
|
+
event
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
ref: (el) => {
|
|
57
|
+
element = el;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export { createComboboxItem };
|