menhir 0.1.5 → 0.1.6
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/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 +160 -0
- package/dist/combobox/item/ComboboxItem.js +98 -0
- package/dist/combobox/item/ComboboxItemContext.js +10 -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 +137 -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/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 +174 -0
- package/dist/source/combobox/item/ComboboxItem.d.ts +25 -0
- package/dist/source/combobox/item/ComboboxItem.jsx +93 -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-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 +132 -0
- package/dist/source/combobox/root/ComboboxContext.js +156 -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 +11 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, createMemo, createUniqueId, useContext, } from "solid-js";
|
|
3
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
4
|
+
import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
|
|
5
|
+
import { createTypeahead } from "../../floating/typeahead/typeahead.js";
|
|
6
|
+
/** Creates a select's state, its list navigation and its typeahead. */
|
|
7
|
+
export function createSelect(props) {
|
|
8
|
+
const [open, setOpen] = createControllableSignal({
|
|
9
|
+
get value() {
|
|
10
|
+
return props.open;
|
|
11
|
+
},
|
|
12
|
+
get defaultValue() {
|
|
13
|
+
return props.defaultOpen ?? false;
|
|
14
|
+
},
|
|
15
|
+
onChange: (value, details) => props.onOpenChange?.(value, details),
|
|
16
|
+
});
|
|
17
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
18
|
+
get value() {
|
|
19
|
+
return props.value;
|
|
20
|
+
},
|
|
21
|
+
get defaultValue() {
|
|
22
|
+
return props.defaultValue ?? null;
|
|
23
|
+
},
|
|
24
|
+
onChange: (value) => props.onValueChange?.(value),
|
|
25
|
+
});
|
|
26
|
+
// Disabled items stay reachable, and the keys do not wrap (README,
|
|
27
|
+
// "Decisions"): list navigation's defaults.
|
|
28
|
+
const list = createActiveDescendantList({
|
|
29
|
+
get open() {
|
|
30
|
+
return open();
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
const typeahead = createTypeahead({
|
|
34
|
+
get open() {
|
|
35
|
+
return open();
|
|
36
|
+
},
|
|
37
|
+
get items() {
|
|
38
|
+
return list.items();
|
|
39
|
+
},
|
|
40
|
+
get active() {
|
|
41
|
+
return active();
|
|
42
|
+
},
|
|
43
|
+
move: (item) => list.move(item),
|
|
44
|
+
});
|
|
45
|
+
// Runs again when an item mounts or leaves, and when the value changes:
|
|
46
|
+
// not for each item on each move.
|
|
47
|
+
const selectedItem = createMemo(() => {
|
|
48
|
+
const selected = value();
|
|
49
|
+
return list.items().find((item) => item.value === selected);
|
|
50
|
+
});
|
|
51
|
+
// Whenever the list opens, the selected item is active, else the first
|
|
52
|
+
// (README, "Decisions"). Derived, not written to the list by an effect,
|
|
53
|
+
// so the Trigger names it in the flush that opens the list. It changes
|
|
54
|
+
// as the list opens and closes and on the first move, not on each move.
|
|
55
|
+
const initial = createMemo(() => open() && !list.active() ? (selectedItem() ?? list.items()[0]) : undefined);
|
|
56
|
+
const active = () => list.active() ?? initial();
|
|
57
|
+
const select = {
|
|
58
|
+
open,
|
|
59
|
+
setOpen,
|
|
60
|
+
value,
|
|
61
|
+
setValue,
|
|
62
|
+
select: (item) => {
|
|
63
|
+
if (!item || item.disabled)
|
|
64
|
+
return false;
|
|
65
|
+
const input = select.input;
|
|
66
|
+
if (item.value !== requested()) {
|
|
67
|
+
if (input) {
|
|
68
|
+
// The form sees the events a native control fires, and the
|
|
69
|
+
// Select takes the value from the `change`, as it does one the
|
|
70
|
+
// browser filled in.
|
|
71
|
+
input.value = item.value;
|
|
72
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
73
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
setValue(item.value);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
},
|
|
80
|
+
selectedItem,
|
|
81
|
+
active,
|
|
82
|
+
// Only while open: a move made while closed, as typing makes, waits
|
|
83
|
+
// for the list to open, which a controlled Select may refuse.
|
|
84
|
+
isActive: (item) => open() && (list.isActive(item) || initial() === item),
|
|
85
|
+
initial,
|
|
86
|
+
list,
|
|
87
|
+
typeahead,
|
|
88
|
+
popupId: createUniqueId(),
|
|
89
|
+
triggerId: () => undefined,
|
|
90
|
+
trigger: undefined,
|
|
91
|
+
input: undefined,
|
|
92
|
+
attributes: {
|
|
93
|
+
get "data-open"() {
|
|
94
|
+
return open() ? "" : undefined;
|
|
95
|
+
},
|
|
96
|
+
get "data-closed"() {
|
|
97
|
+
return open() ? undefined : "";
|
|
98
|
+
},
|
|
99
|
+
get "data-disabled"() {
|
|
100
|
+
return props.disabled ? "" : undefined;
|
|
101
|
+
},
|
|
102
|
+
get "data-required"() {
|
|
103
|
+
return props.required ? "" : undefined;
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
props,
|
|
107
|
+
};
|
|
108
|
+
return select;
|
|
109
|
+
}
|
|
110
|
+
// `null` outside a Root, so that the part can name what is missing.
|
|
111
|
+
export const SelectContext = createContext(null);
|
|
112
|
+
export function useSelectContext() {
|
|
113
|
+
const context = useContext(SelectContext);
|
|
114
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
115
|
+
if (isDev && !context) {
|
|
116
|
+
throw new Error("[menhir] Select parts must be placed within <Select.Root>.");
|
|
117
|
+
}
|
|
118
|
+
return context;
|
|
119
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { SelectStateProps } from "./SelectContext.ts";
|
|
3
|
+
export interface SelectRootProps extends SelectStateProps {
|
|
4
|
+
children?: JSX.Element;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A value selected from a list that a button opens: holds the value and
|
|
8
|
+
* whether the list is open, and shares them with `Select.Trigger`,
|
|
9
|
+
* `Select.Value`, `Select.Popup` and the items inside it. Its form props
|
|
10
|
+
* go on the hidden input the Trigger renders. Renders no element of its
|
|
11
|
+
* own.
|
|
12
|
+
*/
|
|
13
|
+
export declare function SelectRoot(props: SelectRootProps): JSX.Element;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createSelect, SelectContext } from "./SelectContext.js";
|
|
2
|
+
/**
|
|
3
|
+
* A value selected from a list that a button opens: holds the value and
|
|
4
|
+
* whether the list is open, and shares them with `Select.Trigger`,
|
|
5
|
+
* `Select.Value`, `Select.Popup` and the items inside it. Its form props
|
|
6
|
+
* go on the hidden input the Trigger renders. Renders no element of its
|
|
7
|
+
* own.
|
|
8
|
+
*/
|
|
9
|
+
export function SelectRoot(props) {
|
|
10
|
+
return (<SelectContext value={createSelect(props)}>{props.children}</SelectContext>);
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SelectTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
render?: RenderProp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The button that shows the value and opens the list: a `<button
|
|
8
|
+
* type="button" role="combobox" aria-haspopup="listbox">` with
|
|
9
|
+
* `aria-expanded`, naming the Popup in `popovertarget` and `aria-controls`
|
|
10
|
+
* and the active item in `aria-activedescendant`. It keeps focus while the
|
|
11
|
+
* list is open and takes every key. A visually hidden `<input>` follows
|
|
12
|
+
* it, the form's view of the value. Name it with a `<label>`. Place it
|
|
13
|
+
* inside `Select.Root`, right before `Select.Popup`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function SelectTrigger(props: SelectTriggerProps): JSX.Element;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createUniqueId, merge, omit, onFlush, onSettled, untrack, } from "solid-js";
|
|
3
|
+
import { warnUnlessButton } from "../../core/button.js";
|
|
4
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
5
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
6
|
+
import { useSelectContext } from "../root/SelectContext.js";
|
|
7
|
+
/**
|
|
8
|
+
* The button that shows the value and opens the list: a `<button
|
|
9
|
+
* type="button" role="combobox" aria-haspopup="listbox">` with
|
|
10
|
+
* `aria-expanded`, naming the Popup in `popovertarget` and `aria-controls`
|
|
11
|
+
* and the active item in `aria-activedescendant`. It keeps focus while the
|
|
12
|
+
* list is open and takes every key. A visually hidden `<input>` follows
|
|
13
|
+
* it, the form's view of the value. Name it with a `<label>`. Place it
|
|
14
|
+
* inside `Select.Root`, right before `Select.Popup`.
|
|
15
|
+
*/
|
|
16
|
+
export function SelectTrigger(props) {
|
|
17
|
+
const select = useSelectContext();
|
|
18
|
+
const { list } = select;
|
|
19
|
+
const generated = createUniqueId();
|
|
20
|
+
// The consumer's `id`, else the generated one, which labels the Popup
|
|
21
|
+
// rendered after it. Kept out of the merge, where a wrapper's
|
|
22
|
+
// `id={undefined}` would remove it ("`undefined` is a value", PLAN.md
|
|
23
|
+
// section 8).
|
|
24
|
+
const id = () => props.id ?? generated;
|
|
25
|
+
select.triggerId = id;
|
|
26
|
+
let input;
|
|
27
|
+
const press = (event) => ({ reason: "trigger-press", event });
|
|
28
|
+
// Opens the list on its first or last item, or, with no `edge`, on the
|
|
29
|
+
// selected item, else the first. While closed, the move waits for the
|
|
30
|
+
// list to open (list navigation).
|
|
31
|
+
const openList = (event, edge) => {
|
|
32
|
+
select.setOpen(true, press(event));
|
|
33
|
+
const items = list.items();
|
|
34
|
+
if (edge)
|
|
35
|
+
list.move(edge === "last" ? items[items.length - 1] : items[0]);
|
|
36
|
+
};
|
|
37
|
+
// Selects the active item, unless it is disabled, and closes the list.
|
|
38
|
+
const selectActive = (event, reason) => {
|
|
39
|
+
if (select.select(select.active()) || reason === "focus-out")
|
|
40
|
+
select.setOpen(false, { reason, event });
|
|
41
|
+
};
|
|
42
|
+
const ownProps = merge(select.attributes, {
|
|
43
|
+
type: "button",
|
|
44
|
+
role: "combobox",
|
|
45
|
+
get id() {
|
|
46
|
+
return id();
|
|
47
|
+
},
|
|
48
|
+
get disabled() {
|
|
49
|
+
return select.props.disabled;
|
|
50
|
+
},
|
|
51
|
+
// So that the browser counts a press on the Trigger as one on the
|
|
52
|
+
// popover, which light dismiss leaves open (`src/popover/README.md`).
|
|
53
|
+
popovertarget: select.popupId,
|
|
54
|
+
"aria-haspopup": "listbox",
|
|
55
|
+
get "aria-expanded"() {
|
|
56
|
+
return select.open() ? "true" : "false";
|
|
57
|
+
},
|
|
58
|
+
"aria-controls": select.popupId,
|
|
59
|
+
// Only while open, as an item's `data-active`.
|
|
60
|
+
get "aria-activedescendant"() {
|
|
61
|
+
return select.open() ? select.active()?.id : undefined;
|
|
62
|
+
},
|
|
63
|
+
get "aria-required"() {
|
|
64
|
+
return select.props.required ? "true" : undefined;
|
|
65
|
+
},
|
|
66
|
+
onClick: (event) => {
|
|
67
|
+
// The state shows and hides the popover, in the flush that places
|
|
68
|
+
// it: the browser's own toggle would show it a task before.
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
select.setOpen(!select.open(), press(event));
|
|
71
|
+
},
|
|
72
|
+
onKeyDown: (event) => {
|
|
73
|
+
const isOpen = select.open();
|
|
74
|
+
// A typed character opens a closed Select on its match, and the
|
|
75
|
+
// string goes on (PLAN.md section 8, "Typeahead").
|
|
76
|
+
if (select.typeahead.onKeyDown(event)) {
|
|
77
|
+
if (!isOpen)
|
|
78
|
+
select.setOpen(true, press(event));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const { key, altKey } = event;
|
|
82
|
+
if (event.ctrlKey || event.metaKey)
|
|
83
|
+
return;
|
|
84
|
+
if (key === "Tab") {
|
|
85
|
+
// Focus moves on by itself.
|
|
86
|
+
if (isOpen)
|
|
87
|
+
selectActive(event, "focus-out");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (event.shiftKey)
|
|
91
|
+
return;
|
|
92
|
+
const enterOrSpace = key === "Enter" || key === " ";
|
|
93
|
+
if (!isOpen) {
|
|
94
|
+
if (enterOrSpace || key === "ArrowDown")
|
|
95
|
+
openList(event);
|
|
96
|
+
else if (altKey)
|
|
97
|
+
return;
|
|
98
|
+
else if (key === "ArrowUp" || key === "Home")
|
|
99
|
+
openList(event, "first");
|
|
100
|
+
else if (key === "End")
|
|
101
|
+
openList(event, "last");
|
|
102
|
+
else
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
else if (enterOrSpace || (altKey && key === "ArrowUp")) {
|
|
106
|
+
selectActive(event, "item-press");
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// The keys move on from the item the list opened on.
|
|
110
|
+
const active = select.active();
|
|
111
|
+
if (!list.active())
|
|
112
|
+
list.move(active);
|
|
113
|
+
if (key === "PageDown" || key === "PageUp") {
|
|
114
|
+
const items = list.items();
|
|
115
|
+
const index = items.indexOf(active) + (key === "PageUp" ? -10 : 10);
|
|
116
|
+
list.move(items[Math.min(Math.max(index, 0), items.length - 1)]);
|
|
117
|
+
}
|
|
118
|
+
else if (!list.onKeyDown(event))
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Enter and Space would click the button, and toggle the list again.
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
},
|
|
124
|
+
ref: (element) => {
|
|
125
|
+
select.trigger = element;
|
|
126
|
+
// Production builds drop the check with `isDev`.
|
|
127
|
+
if (isDev)
|
|
128
|
+
warnUnlessButton(element, "Select.Trigger");
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
// Puts the input back to the Select's value, once that has settled: a
|
|
132
|
+
// controlled Select's consumer may have declined the change.
|
|
133
|
+
const syncInput = () => {
|
|
134
|
+
if (input)
|
|
135
|
+
input.value = untrack(select.value) ?? "";
|
|
136
|
+
};
|
|
137
|
+
// A selection, which the Select makes through the input, and a value the
|
|
138
|
+
// browser fills in. The browser's is an item's `value`, or its label.
|
|
139
|
+
const onInputChange = () => {
|
|
140
|
+
const filled = input.value;
|
|
141
|
+
const items = list.items();
|
|
142
|
+
const label = filled.toLowerCase();
|
|
143
|
+
const item = items.find((item) => item.value === filled) ??
|
|
144
|
+
items.find((item) => (item.text ?? item.element.textContent ?? "").trim().toLowerCase() ===
|
|
145
|
+
label);
|
|
146
|
+
if (item)
|
|
147
|
+
select.setValue(item.value);
|
|
148
|
+
else if (!filled)
|
|
149
|
+
select.setValue(null);
|
|
150
|
+
onFlush(syncInput);
|
|
151
|
+
};
|
|
152
|
+
// A form reset puts a control back to its default, and so it does the
|
|
153
|
+
// Select: through the setter, so that a controlled Select asks.
|
|
154
|
+
onSettled(() => {
|
|
155
|
+
const form = input?.form;
|
|
156
|
+
if (!form)
|
|
157
|
+
return;
|
|
158
|
+
const onReset = () => {
|
|
159
|
+
select.setValue(select.props.defaultValue ?? null);
|
|
160
|
+
// The browser resets the input after this event, to its `value`
|
|
161
|
+
// attribute; `onFlush` runs after that.
|
|
162
|
+
onFlush(syncInput);
|
|
163
|
+
};
|
|
164
|
+
form.addEventListener("reset", onReset);
|
|
165
|
+
return () => form.removeEventListener("reset", onReset);
|
|
166
|
+
});
|
|
167
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
|
|
168
|
+
return (<>
|
|
169
|
+
{renderElement(props, elementProps, (p) => (<button {...p}/>))}
|
|
170
|
+
<input aria-hidden="true" tabindex={-1} style={visuallyHidden} name={select.props.name} form={select.props.form} required={select.props.required} disabled={select.props.disabled} autocomplete={select.props.autocomplete} value={select.value() ?? ""} onChange={onInputChange}
|
|
171
|
+
// The browser focuses an invalid input when it blocks a submission.
|
|
172
|
+
// The user knows the Trigger, not the hidden input.
|
|
173
|
+
onFocus={() => select.trigger?.focus()} ref={(element) => {
|
|
174
|
+
input = element;
|
|
175
|
+
select.input = element;
|
|
176
|
+
}}/>
|
|
177
|
+
</>);
|
|
178
|
+
}
|
|
179
|
+
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
180
|
+
// browser can still anchor a validation message to it.
|
|
181
|
+
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,20 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SelectValueProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "children"> {
|
|
4
|
+
/** What it shows while no item is selected. */
|
|
5
|
+
placeholder?: JSX.Element;
|
|
6
|
+
/**
|
|
7
|
+
* The label of a value, for a Select whose labels differ from its
|
|
8
|
+
* values: the server HTML cannot read a label from the items, which are
|
|
9
|
+
* rendered after it. Without it, the selected item's `label` or text
|
|
10
|
+
* content once the items have mounted, and the value itself before.
|
|
11
|
+
*/
|
|
12
|
+
children?: (value: string) => JSX.Element;
|
|
13
|
+
render?: RenderProp;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The selected item's label: a `<span>`, with the `placeholder` and
|
|
17
|
+
* `data-placeholder` while no item is selected. Place it inside
|
|
18
|
+
* `Select.Trigger`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function SelectValue(props: SelectValueProps): JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useSelectContext } from "../root/SelectContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The selected item's label: a `<span>`, with the `placeholder` and
|
|
7
|
+
* `data-placeholder` while no item is selected. Place it inside
|
|
8
|
+
* `Select.Trigger`.
|
|
9
|
+
*/
|
|
10
|
+
export function SelectValue(props) {
|
|
11
|
+
const select = useSelectContext();
|
|
12
|
+
const elementProps = mergeElementProps({
|
|
13
|
+
get "data-placeholder"() {
|
|
14
|
+
return select.value() === null ? "" : undefined;
|
|
15
|
+
},
|
|
16
|
+
get children() {
|
|
17
|
+
const value = select.value();
|
|
18
|
+
if (value === null)
|
|
19
|
+
return props.placeholder;
|
|
20
|
+
if (props.children)
|
|
21
|
+
return props.children(value);
|
|
22
|
+
// An item's text is always given: its `label`, else its content.
|
|
23
|
+
return select.selectedItem()?.text.trim() ?? value;
|
|
24
|
+
},
|
|
25
|
+
}, omit(props, "render", "placeholder", "children"));
|
|
26
|
+
return renderElement(props, elementProps, (p) => <span {...p}/>);
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "menhir",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solid",
|
|
@@ -54,6 +54,11 @@
|
|
|
54
54
|
"solid": "./dist/source/color-scheme/local-storage/index.js",
|
|
55
55
|
"import": "./dist/color-scheme/local-storage/index.js"
|
|
56
56
|
},
|
|
57
|
+
"./combobox": {
|
|
58
|
+
"types": "./dist/source/combobox/index.d.ts",
|
|
59
|
+
"solid": "./dist/source/combobox/index.js",
|
|
60
|
+
"import": "./dist/combobox/index.js"
|
|
61
|
+
},
|
|
57
62
|
"./context-menu": {
|
|
58
63
|
"types": "./dist/source/context-menu/index.d.ts",
|
|
59
64
|
"solid": "./dist/source/context-menu/index.js",
|
|
@@ -94,6 +99,11 @@
|
|
|
94
99
|
"solid": "./dist/source/progress/index.js",
|
|
95
100
|
"import": "./dist/progress/index.js"
|
|
96
101
|
},
|
|
102
|
+
"./select": {
|
|
103
|
+
"types": "./dist/source/select/index.d.ts",
|
|
104
|
+
"solid": "./dist/source/select/index.js",
|
|
105
|
+
"import": "./dist/select/index.js"
|
|
106
|
+
},
|
|
97
107
|
"./separator": {
|
|
98
108
|
"types": "./dist/source/separator/index.d.ts",
|
|
99
109
|
"solid": "./dist/source/separator/index.js",
|