menhir 0.1.5 → 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/clear/ComboboxClear.js +39 -0
- package/dist/combobox/filter/includesText.js +24 -0
- package/dist/combobox/group/ComboboxGroup.js +32 -0
- package/dist/combobox/group/ComboboxGroupContext.js +10 -0
- package/dist/combobox/group-label/ComboboxGroupLabel.js +26 -0
- package/dist/combobox/index.js +3 -0
- package/dist/combobox/index.parts.js +12 -0
- package/dist/combobox/input/ComboboxInput.js +78 -0
- package/dist/combobox/input/createComboboxInput.js +112 -0
- package/dist/combobox/item/ComboboxItem.js +54 -0
- package/dist/combobox/item/ComboboxItemContext.js +10 -0
- package/dist/combobox/item/createComboboxItem.js +62 -0
- package/dist/combobox/item-indicator/ComboboxItemIndicator.js +22 -0
- package/dist/combobox/list/ComboboxList.js +26 -0
- package/dist/combobox/popup/ComboboxPopup.js +78 -0
- package/dist/combobox/root/ComboboxContext.js +161 -0
- package/dist/combobox/root/ComboboxRoot.js +21 -0
- package/dist/combobox/status/ComboboxStatus.js +31 -0
- package/dist/combobox/trigger/ComboboxTrigger.js +47 -0
- package/dist/select/group/SelectGroup.js +32 -0
- package/dist/select/group/SelectGroupContext.js +10 -0
- package/dist/select/group-label/SelectGroupLabel.js +26 -0
- package/dist/select/index.js +2 -0
- package/dist/select/index.parts.js +9 -0
- package/dist/select/item/SelectItem.js +97 -0
- package/dist/select/item/SelectItemContext.js +10 -0
- package/dist/select/item-indicator/SelectItemIndicator.js +22 -0
- package/dist/select/popup/SelectPopup.js +92 -0
- package/dist/select/root/SelectContext.js +99 -0
- package/dist/select/root/SelectRoot.js +21 -0
- package/dist/select/trigger/SelectTrigger.js +163 -0
- package/dist/select/value/SelectValue.js +32 -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/clear/ComboboxClear.d.ts +12 -0
- package/dist/source/combobox/clear/ComboboxClear.jsx +36 -0
- package/dist/source/combobox/filter/includesText.d.ts +12 -0
- package/dist/source/combobox/filter/includesText.js +26 -0
- package/dist/source/combobox/group/ComboboxGroup.d.ts +10 -0
- package/dist/source/combobox/group/ComboboxGroup.jsx +23 -0
- package/dist/source/combobox/group/ComboboxGroupContext.d.ts +10 -0
- package/dist/source/combobox/group/ComboboxGroupContext.js +12 -0
- package/dist/source/combobox/group-label/ComboboxGroupLabel.d.ts +10 -0
- package/dist/source/combobox/group-label/ComboboxGroupLabel.jsx +24 -0
- package/dist/source/combobox/index.d.ts +2 -0
- package/dist/source/combobox/index.js +4 -0
- package/dist/source/combobox/index.parts.d.ts +11 -0
- package/dist/source/combobox/index.parts.js +11 -0
- package/dist/source/combobox/input/ComboboxInput.d.ts +19 -0
- package/dist/source/combobox/input/ComboboxInput.jsx +52 -0
- 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.d.ts +25 -0
- package/dist/source/combobox/item/ComboboxItem.jsx +50 -0
- package/dist/source/combobox/item/ComboboxItemContext.d.ts +11 -0
- package/dist/source/combobox/item/ComboboxItemContext.js +12 -0
- package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
- package/dist/source/combobox/item/createComboboxItem.js +61 -0
- package/dist/source/combobox/item-indicator/ComboboxItemIndicator.d.ts +11 -0
- package/dist/source/combobox/item-indicator/ComboboxItemIndicator.jsx +16 -0
- package/dist/source/combobox/list/ComboboxList.d.ts +12 -0
- package/dist/source/combobox/list/ComboboxList.jsx +17 -0
- package/dist/source/combobox/popup/ComboboxPopup.d.ts +34 -0
- package/dist/source/combobox/popup/ComboboxPopup.jsx +76 -0
- package/dist/source/combobox/root/ComboboxContext.d.ts +179 -0
- package/dist/source/combobox/root/ComboboxContext.js +186 -0
- package/dist/source/combobox/root/ComboboxRoot.d.ts +13 -0
- package/dist/source/combobox/root/ComboboxRoot.jsx +13 -0
- package/dist/source/combobox/status/ComboboxStatus.d.ts +19 -0
- package/dist/source/combobox/status/ComboboxStatus.jsx +27 -0
- package/dist/source/combobox/trigger/ComboboxTrigger.d.ts +13 -0
- package/dist/source/combobox/trigger/ComboboxTrigger.jsx +40 -0
- package/dist/source/floating/dismissal/reason.d.ts +7 -2
- package/dist/source/select/group/SelectGroup.d.ts +10 -0
- package/dist/source/select/group/SelectGroup.jsx +23 -0
- package/dist/source/select/group/SelectGroupContext.d.ts +10 -0
- package/dist/source/select/group/SelectGroupContext.js +12 -0
- package/dist/source/select/group-label/SelectGroupLabel.d.ts +10 -0
- package/dist/source/select/group-label/SelectGroupLabel.jsx +24 -0
- package/dist/source/select/index.d.ts +1 -0
- package/dist/source/select/index.js +1 -0
- package/dist/source/select/index.parts.d.ts +8 -0
- package/dist/source/select/index.parts.js +8 -0
- package/dist/source/select/item/SelectItem.d.ts +24 -0
- package/dist/source/select/item/SelectItem.jsx +92 -0
- package/dist/source/select/item/SelectItemContext.d.ts +11 -0
- package/dist/source/select/item/SelectItemContext.js +12 -0
- package/dist/source/select/item-indicator/SelectItemIndicator.d.ts +11 -0
- package/dist/source/select/item-indicator/SelectItemIndicator.jsx +16 -0
- package/dist/source/select/popup/SelectPopup.d.ts +32 -0
- package/dist/source/select/popup/SelectPopup.jsx +97 -0
- package/dist/source/select/root/SelectContext.d.ts +106 -0
- package/dist/source/select/root/SelectContext.js +119 -0
- package/dist/source/select/root/SelectRoot.d.ts +13 -0
- package/dist/source/select/root/SelectRoot.jsx +11 -0
- package/dist/source/select/trigger/SelectTrigger.d.ts +15 -0
- package/dist/source/select/trigger/SelectTrigger.jsx +181 -0
- package/dist/source/select/value/SelectValue.d.ts +20 -0
- package/dist/source/select/value/SelectValue.jsx +27 -0
- package/package.json +16 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Made on the first call, never at import time (PLAN.md section 6, rule 2),
|
|
2
|
+
// and kept for the page: it holds no state.
|
|
3
|
+
let collator;
|
|
4
|
+
/**
|
|
5
|
+
* Whether `text` contains `query`, case and accents ignored, with the
|
|
6
|
+
* built-in `Intl.Collator`; an empty query matches everything. The filter
|
|
7
|
+
* for a Combobox whose items match what the user types:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const shown = createMemo(() =>
|
|
11
|
+
* countries.filter((country) => includesText(country.name, query())),
|
|
12
|
+
* );
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function includesText(text, query) {
|
|
16
|
+
// Composed, so that a letter and its accent are one character in both.
|
|
17
|
+
const haystack = text.normalize();
|
|
18
|
+
const needle = query.normalize();
|
|
19
|
+
collator ??= new Intl.Collator(undefined, { sensitivity: "base" });
|
|
20
|
+
for (let start = 0; start + needle.length <= haystack.length; start++) {
|
|
21
|
+
const slice = haystack.slice(start, start + needle.length);
|
|
22
|
+
if (!collator.compare(slice, needle))
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxGroupProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Items that belong together: a `<div role="group">` named by its
|
|
8
|
+
* `Combobox.GroupLabel`. Place it inside `Combobox.List`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function ComboboxGroup(props: ComboboxGroupProps): JSX.Element;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { createIdSlot } from "../../core/slot.js";
|
|
5
|
+
import { ComboboxGroupContext } from "./ComboboxGroupContext.js";
|
|
6
|
+
/**
|
|
7
|
+
* Items that belong together: a `<div role="group">` named by its
|
|
8
|
+
* `Combobox.GroupLabel`. Place it inside `Combobox.List`.
|
|
9
|
+
*/
|
|
10
|
+
export function ComboboxGroup(props) {
|
|
11
|
+
const [labelId, registerLabelId] = createIdSlot();
|
|
12
|
+
const elementProps = mergeElementProps({
|
|
13
|
+
role: "group",
|
|
14
|
+
// The consumer's, else the GroupLabel's id, while one is rendered.
|
|
15
|
+
// Kept out of the merge ("`undefined` is a value", PLAN.md section 8).
|
|
16
|
+
get "aria-labelledby"() {
|
|
17
|
+
return props["aria-labelledby"] ?? labelId();
|
|
18
|
+
},
|
|
19
|
+
}, omit(props, "render", "aria-labelledby"));
|
|
20
|
+
return (<ComboboxGroupContext value={{ registerLabelId }}>
|
|
21
|
+
{renderElement(props, elementProps, (p) => (<div {...p}/>))}
|
|
22
|
+
</ComboboxGroupContext>);
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** What a group shares with its label. */
|
|
2
|
+
export interface ComboboxGroupContextValue {
|
|
3
|
+
/**
|
|
4
|
+
* Registers the `id` of a mounted GroupLabel, as a getter. Returns what
|
|
5
|
+
* unregisters it, for the label's disposal.
|
|
6
|
+
*/
|
|
7
|
+
registerLabelId(id: () => string | false): () => void;
|
|
8
|
+
}
|
|
9
|
+
export declare const ComboboxGroupContext: import("solid-js").Context<ComboboxGroupContextValue | null>;
|
|
10
|
+
export declare function useComboboxGroupContext(): ComboboxGroupContextValue;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
import { isDev } from "@solidjs/web";
|
|
3
|
+
// `null` outside a group, so that the part can name what is missing.
|
|
4
|
+
export const ComboboxGroupContext = createContext(null);
|
|
5
|
+
export function useComboboxGroupContext() {
|
|
6
|
+
const context = useContext(ComboboxGroupContext);
|
|
7
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
8
|
+
if (isDev && !context) {
|
|
9
|
+
throw new Error("[menhir] Combobox.GroupLabel must be placed within <Combobox.Group>.");
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxGroupLabelProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The label of a group, which names it in `aria-labelledby`: a `<div>`,
|
|
8
|
+
* not reached by the keys. Place it inside `Combobox.Group`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function ComboboxGroupLabel(props: ComboboxGroupLabelProps): JSX.Element;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createUniqueId, omit, onSettled } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useComboboxGroupContext } from "../group/ComboboxGroupContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The label of a group, which names it in `aria-labelledby`: a `<div>`,
|
|
7
|
+
* not reached by the keys. Place it inside `Combobox.Group`.
|
|
8
|
+
*/
|
|
9
|
+
export function ComboboxGroupLabel(props) {
|
|
10
|
+
const group = useComboboxGroupContext();
|
|
11
|
+
const generated = createUniqueId();
|
|
12
|
+
// The consumer's `id`, else the generated one. Kept out of the merge,
|
|
13
|
+
// where a wrapper's `id={undefined}` would remove it.
|
|
14
|
+
const id = () => props.id ?? generated;
|
|
15
|
+
// Named once mounted, so the group names only a label that is rendered,
|
|
16
|
+
// and none in the server HTML, as a popup's Title.
|
|
17
|
+
onSettled(() => group.registerLabelId(id));
|
|
18
|
+
const elementProps = mergeElementProps({
|
|
19
|
+
get id() {
|
|
20
|
+
return id();
|
|
21
|
+
},
|
|
22
|
+
}, omit(props, "render", "id"));
|
|
23
|
+
return renderElement(props, elementProps, (p) => <div {...p}/>);
|
|
24
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { ComboboxRoot as Root } from "./root/ComboboxRoot.tsx";
|
|
2
|
+
export { ComboboxInput as Input } from "./input/ComboboxInput.tsx";
|
|
3
|
+
export { ComboboxTrigger as Trigger } from "./trigger/ComboboxTrigger.tsx";
|
|
4
|
+
export { ComboboxClear as Clear } from "./clear/ComboboxClear.tsx";
|
|
5
|
+
export { ComboboxPopup as Popup } from "./popup/ComboboxPopup.tsx";
|
|
6
|
+
export { ComboboxList as List } from "./list/ComboboxList.tsx";
|
|
7
|
+
export { ComboboxItem as Item } from "./item/ComboboxItem.tsx";
|
|
8
|
+
export { ComboboxItemIndicator as ItemIndicator } from "./item-indicator/ComboboxItemIndicator.tsx";
|
|
9
|
+
export { ComboboxGroup as Group } from "./group/ComboboxGroup.tsx";
|
|
10
|
+
export { ComboboxGroupLabel as GroupLabel } from "./group-label/ComboboxGroupLabel.tsx";
|
|
11
|
+
export { ComboboxStatus as Status } from "./status/ComboboxStatus.tsx";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { ComboboxRoot as Root } from "./root/ComboboxRoot.jsx";
|
|
2
|
+
export { ComboboxInput as Input } from "./input/ComboboxInput.jsx";
|
|
3
|
+
export { ComboboxTrigger as Trigger } from "./trigger/ComboboxTrigger.jsx";
|
|
4
|
+
export { ComboboxClear as Clear } from "./clear/ComboboxClear.jsx";
|
|
5
|
+
export { ComboboxPopup as Popup } from "./popup/ComboboxPopup.jsx";
|
|
6
|
+
export { ComboboxList as List } from "./list/ComboboxList.jsx";
|
|
7
|
+
export { ComboboxItem as Item } from "./item/ComboboxItem.jsx";
|
|
8
|
+
export { ComboboxItemIndicator as ItemIndicator } from "./item-indicator/ComboboxItemIndicator.jsx";
|
|
9
|
+
export { ComboboxGroup as Group } from "./group/ComboboxGroup.jsx";
|
|
10
|
+
export { ComboboxGroupLabel as GroupLabel } from "./group-label/ComboboxGroupLabel.jsx";
|
|
11
|
+
export { ComboboxStatus as Status } from "./status/ComboboxStatus.jsx";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxInputProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** What the browser may suggest. Default `"off"`, so its list does not cover the Combobox's. */
|
|
5
|
+
autocomplete?: string;
|
|
6
|
+
/** Shown while the input is empty. */
|
|
7
|
+
placeholder?: string;
|
|
8
|
+
render?: RenderProp;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The combobox itself: an `<input type="text" role="combobox"
|
|
12
|
+
* aria-autocomplete="list">` with `aria-expanded`, naming the List in
|
|
13
|
+
* `aria-controls` and the active item in `aria-activedescendant`. It keeps
|
|
14
|
+
* focus while the list is open: typing opens and filters it, and the arrow
|
|
15
|
+
* keys, Enter and Escape act on it. A visually hidden `<input>` follows it,
|
|
16
|
+
* the form's view of the value. Name it with a `<label>`. Place it inside
|
|
17
|
+
* `Combobox.Root`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function ComboboxInput(props: ComboboxInputProps): JSX.Element;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { omit, onFlush, untrack } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useComboboxContext } from "../root/ComboboxContext.js";
|
|
5
|
+
import { createComboboxInput, onFormReset } from "./createComboboxInput.js";
|
|
6
|
+
/**
|
|
7
|
+
* The combobox itself: an `<input type="text" role="combobox"
|
|
8
|
+
* aria-autocomplete="list">` with `aria-expanded`, naming the List in
|
|
9
|
+
* `aria-controls` and the active item in `aria-activedescendant`. It keeps
|
|
10
|
+
* focus while the list is open: typing opens and filters it, and the arrow
|
|
11
|
+
* keys, Enter and Escape act on it. A visually hidden `<input>` follows it,
|
|
12
|
+
* the form's view of the value. Name it with a `<label>`. Place it inside
|
|
13
|
+
* `Combobox.Root`.
|
|
14
|
+
*/
|
|
15
|
+
export function ComboboxInput(props) {
|
|
16
|
+
const combobox = useComboboxContext("Combobox");
|
|
17
|
+
const input = createComboboxInput(combobox, combobox.revert);
|
|
18
|
+
let hiddenInput;
|
|
19
|
+
// A selection, which the Combobox makes through the hidden input.
|
|
20
|
+
const onHiddenChange = () => {
|
|
21
|
+
combobox.setValue(hiddenInput.value || null);
|
|
22
|
+
onFlush(syncHidden);
|
|
23
|
+
};
|
|
24
|
+
// Puts the hidden input back to the value, once that has settled.
|
|
25
|
+
const syncHidden = () => {
|
|
26
|
+
if (hiddenInput)
|
|
27
|
+
hiddenInput.value = untrack(combobox.value) ?? "";
|
|
28
|
+
};
|
|
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
|
+
});
|
|
37
|
+
});
|
|
38
|
+
const elementProps = mergeElementProps(input.props, omit(props, "render"));
|
|
39
|
+
return (<>
|
|
40
|
+
{renderElement(props, elementProps, (p) => (<input {...p}/>))}
|
|
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}
|
|
42
|
+
// The browser focuses an invalid input when it blocks a submission.
|
|
43
|
+
// The user knows the visible input, not the hidden one.
|
|
44
|
+
onFocus={() => combobox.input?.focus()} ref={(element) => {
|
|
45
|
+
hiddenInput = element;
|
|
46
|
+
combobox.hiddenInput = element;
|
|
47
|
+
}}/>
|
|
48
|
+
</>);
|
|
49
|
+
}
|
|
50
|
+
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
51
|
+
// browser can still anchor a validation message to it.
|
|
52
|
+
const visuallyHidden = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none";
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxItemProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** What the Combobox's value is while this item is selected. Not `""`. */
|
|
5
|
+
value: string;
|
|
6
|
+
/**
|
|
7
|
+
* Whether the item is disabled: still reached by the keys and the
|
|
8
|
+
* pointer, but not selected.
|
|
9
|
+
*/
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The item's text, when its content is not its label, such as an icon
|
|
13
|
+
* beside it: what the input shows once the item is selected.
|
|
14
|
+
*/
|
|
15
|
+
label?: string;
|
|
16
|
+
render?: RenderProp;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A value to select: a `<div role="option">` with `aria-selected`. A click
|
|
20
|
+
* selects it, sets the input's text to its label and closes the list, and
|
|
21
|
+
* so does Enter while it is the active item; a disabled item is not
|
|
22
|
+
* selected. Render the items that match the text, and place them inside
|
|
23
|
+
* `Combobox.List`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function ComboboxItem(props: ComboboxItemProps): JSX.Element;
|
|
@@ -0,0 +1,50 @@
|
|
|
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 "../root/ComboboxContext.js";
|
|
5
|
+
import { createComboboxItem } from "./createComboboxItem.js";
|
|
6
|
+
import { ComboboxItemContext } from "./ComboboxItemContext.js";
|
|
7
|
+
/**
|
|
8
|
+
* A value to select: a `<div role="option">` with `aria-selected`. A click
|
|
9
|
+
* selects it, sets the input's text to its label and closes the list, and
|
|
10
|
+
* so does Enter while it is the active item; a disabled item is not
|
|
11
|
+
* selected. Render the items that match the text, and place them inside
|
|
12
|
+
* `Combobox.List`.
|
|
13
|
+
*/
|
|
14
|
+
export function ComboboxItem(props) {
|
|
15
|
+
const combobox = useComboboxContext("Combobox");
|
|
16
|
+
const { props: itemProps } = createComboboxItem(combobox, props, (element) => props.label ?? labelOf(element));
|
|
17
|
+
const selected = () => combobox.value() === props.value;
|
|
18
|
+
const attributes = {
|
|
19
|
+
get "data-selected"() {
|
|
20
|
+
return selected() ? "" : undefined;
|
|
21
|
+
},
|
|
22
|
+
get "data-unselected"() {
|
|
23
|
+
return selected() ? undefined : "";
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const ownProps = merge(attributes, itemProps, {
|
|
27
|
+
// Solid 2.0 removes an attribute set to false, so spell the value out.
|
|
28
|
+
get "aria-selected"() {
|
|
29
|
+
return selected() ? "true" : "false";
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
|
|
33
|
+
return (<ComboboxItemContext value={attributes}>
|
|
34
|
+
{renderElement(props, elementProps, (p) => (<div {...p}/>))}
|
|
35
|
+
</ComboboxItemContext>);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An item's own text: its text content without what is `aria-hidden`, such
|
|
39
|
+
* as its ItemIndicator's check mark, which is no part of its name either.
|
|
40
|
+
*/
|
|
41
|
+
function labelOf(node) {
|
|
42
|
+
let text = "";
|
|
43
|
+
for (const child of node.childNodes) {
|
|
44
|
+
if (child.nodeType === 3)
|
|
45
|
+
text += child.nodeValue;
|
|
46
|
+
else if (child.getAttribute?.("aria-hidden") !== "true")
|
|
47
|
+
text += labelOf(child);
|
|
48
|
+
}
|
|
49
|
+
return text;
|
|
50
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an item is selected, as `data-*` attributes, each `""` when it
|
|
3
|
+
* applies and absent otherwise: what the item and its
|
|
4
|
+
* `Combobox.ItemIndicator` carry.
|
|
5
|
+
*/
|
|
6
|
+
export type ComboboxItemAttributes = {
|
|
7
|
+
readonly "data-selected": "" | undefined;
|
|
8
|
+
readonly "data-unselected": "" | undefined;
|
|
9
|
+
};
|
|
10
|
+
export declare const ComboboxItemContext: import("solid-js").Context<ComboboxItemAttributes | null>;
|
|
11
|
+
export declare function useComboboxItemContext(): ComboboxItemAttributes;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
import { isDev } from "@solidjs/web";
|
|
3
|
+
// `null` outside an Item, so that the part can say so.
|
|
4
|
+
export const ComboboxItemContext = createContext(null);
|
|
5
|
+
export function useComboboxItemContext() {
|
|
6
|
+
const context = useContext(ComboboxItemContext);
|
|
7
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
8
|
+
if (isDev && !context) {
|
|
9
|
+
throw new Error("[menhir] Combobox.ItemIndicator must be placed within <Combobox.Item>.");
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxItemIndicatorProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The check mark of an item: an `aria-hidden` `<span>` with its item's
|
|
8
|
+
* `data-selected` or `data-unselected`, always rendered, so that CSS shows
|
|
9
|
+
* it by state. Place it inside `Combobox.Item`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function ComboboxItemIndicator(props: ComboboxItemIndicatorProps): JSX.Element;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { merge, omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useComboboxItemContext } from "../item/ComboboxItemContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The check mark of an item: an `aria-hidden` `<span>` with its item's
|
|
7
|
+
* `data-selected` or `data-unselected`, always rendered, so that CSS shows
|
|
8
|
+
* it by state. Place it inside `Combobox.Item`.
|
|
9
|
+
*/
|
|
10
|
+
export function ComboboxItemIndicator(props) {
|
|
11
|
+
const ownProps = merge(useComboboxItemContext(), {
|
|
12
|
+
"aria-hidden": "true",
|
|
13
|
+
});
|
|
14
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
15
|
+
return renderElement(props, elementProps, (p) => <span {...p}/>);
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface ComboboxListProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The list of items: a `<div role="listbox">` with the `id` the Input
|
|
8
|
+
* names in `aria-controls`. Name it with `aria-labelledby`, such as the
|
|
9
|
+
* `id` of the input's `<label>`, or `aria-label`. Place it inside
|
|
10
|
+
* `Combobox.Popup`, around the items.
|
|
11
|
+
*/
|
|
12
|
+
export declare function ComboboxList(props: ComboboxListProps): JSX.Element;
|