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
|
@@ -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
|
-
/**
|
|
7
|
-
|
|
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
|
-
|
|
68
|
-
|
|
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
|
-
|
|
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
|
|
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,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;
|
|
@@ -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;
|