@vokality/design-system 1.3.0 → 3.0.0
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/CHANGELOG.md +54 -0
- package/README.md +51 -5
- package/dist/browser.d.ts +13 -0
- package/dist/browser.js +7 -0
- package/dist/charts/chart.d.ts +6 -5
- package/dist/charts/chart.js +44 -25
- package/dist/chat.d.ts +54 -0
- package/dist/chat.js +1 -0
- package/dist/components/Accordion/Accordion.js +1 -1
- package/dist/components/Alert/Alert.js +5 -2
- package/dist/components/AppShell/AppShell.js +10 -14
- package/dist/components/Avatar/Avatar.js +2 -2
- package/dist/components/AvatarGroup/AvatarGroup.js +2 -2
- package/dist/components/BarChart/BarChart.js +17 -15
- package/dist/components/Breadcrumbs/Breadcrumbs.js +6 -3
- package/dist/components/Calendar/Calendar.js +6 -5
- package/dist/components/Card/Card.js +2 -3
- package/dist/components/ChatInbox/ChatInbox.d.ts +11 -0
- package/dist/components/ChatInbox/ChatInbox.js +49 -0
- package/dist/components/ChatPanel/ChatPanel.d.ts +3 -0
- package/dist/components/ChatPanel/ChatPanel.js +13 -0
- package/dist/components/ChatPanel/composer.d.ts +5 -0
- package/dist/components/ChatPanel/composer.js +32 -0
- package/dist/components/ChatPanel/transcript.d.ts +13 -0
- package/dist/components/ChatPanel/transcript.js +39 -0
- package/dist/components/ChatWidget/ChatWidget.d.ts +12 -0
- package/dist/components/ChatWidget/ChatWidget.js +42 -0
- package/dist/components/Checkbox/Checkbox.d.ts +5 -10
- package/dist/components/Checkbox/Checkbox.js +5 -19
- package/dist/components/CodeBlock/CodeBlock.js +19 -5
- package/dist/components/Combobox/Combobox.js +22 -15
- package/dist/components/Combobox/ListPanel.js +20 -9
- package/dist/components/CommandPalette/CommandPalette.js +33 -14
- package/dist/components/ContextMenu/ContextMenu.d.ts +2 -2
- package/dist/components/ContextMenu/ContextMenu.js +5 -50
- package/dist/components/DatePicker/DatePicker.js +35 -29
- package/dist/components/DescriptionList/DescriptionList.js +1 -1
- package/dist/components/Dialog/Dialog.d.ts +24 -7
- package/dist/components/Dialog/Dialog.js +30 -41
- package/dist/components/Drawer/Drawer.d.ts +4 -2
- package/dist/components/EmptyState/EmptyState.js +2 -2
- package/dist/components/ErrorSummary/ErrorSummary.js +5 -3
- package/dist/components/FaqList/FaqList.js +1 -1
- package/dist/components/Field/Field.js +4 -4
- package/dist/components/Fieldset/Fieldset.js +3 -3
- package/dist/components/FileUpload/FileUpload.js +17 -9
- package/dist/components/Grid/Grid.d.ts +867 -5
- package/dist/components/Grid/Grid.js +3 -4
- package/dist/components/Heading/Heading.js +2 -1
- package/dist/components/Icon/Icon.js +4 -2
- package/dist/components/Inline/Inline.d.ts +867 -5
- package/dist/components/Inline/Inline.js +2 -3
- package/dist/components/LineChart/LineChart.js +17 -15
- package/dist/components/LinkProvider/LinkProvider.d.ts +1 -1
- package/dist/components/LinkProvider/LinkProvider.js +6 -2
- package/dist/components/List/List.js +1 -1
- package/dist/components/MediaCard/MediaCard.js +2 -2
- package/dist/components/Menu/Menu.js +6 -43
- package/dist/components/Menu/entries.d.ts +1 -30
- package/dist/components/Menu/entries.js +16 -100
- package/dist/components/MultiSelect/MultiSelect.js +43 -34
- package/dist/components/NumberInput/NumberInput.js +17 -18
- package/dist/components/OtpInput/OtpInput.d.ts +1 -1
- package/dist/components/OtpInput/OtpInput.js +10 -67
- package/dist/components/PageHeader/PageHeader.js +2 -2
- package/dist/components/Pagination/Pagination.js +3 -1
- package/dist/components/PasswordInput/PasswordInput.d.ts +1 -1
- package/dist/components/PasswordInput/PasswordInput.js +9 -4
- package/dist/components/Popover/Popover.d.ts +6 -7
- package/dist/components/Popover/Popover.js +2 -19
- package/dist/components/Progress/Progress.d.ts +1 -1
- package/dist/components/Progress/Progress.js +7 -11
- package/dist/components/Radio/Radio.d.ts +4 -2
- package/dist/components/RadioGroup/RadioGroup.d.ts +5 -12
- package/dist/components/RadioGroup/RadioGroup.js +6 -33
- package/dist/components/RichTextEditor/RichTextEditor.d.ts +63 -0
- package/dist/components/RichTextEditor/RichTextEditor.js +175 -0
- package/dist/components/RichTextEditor/toolbar.d.ts +15 -0
- package/dist/components/RichTextEditor/toolbar.js +142 -0
- package/dist/components/RichTextEditor/uploads.d.ts +41 -0
- package/dist/components/RichTextEditor/uploads.js +124 -0
- package/dist/components/SearchInput/SearchInput.js +4 -2
- package/dist/components/SectionHeader/SectionHeader.js +1 -3
- package/dist/components/SegmentedControl/SegmentedControl.js +2 -12
- package/dist/components/Select/Select.d.ts +15 -16
- package/dist/components/Select/Select.js +11 -252
- package/dist/components/Slider/Slider.js +11 -21
- package/dist/components/Sparkline/Sparkline.js +8 -3
- package/dist/components/Spinner/Spinner.js +5 -2
- package/dist/components/Stack/Stack.d.ts +867 -5
- package/dist/components/Stack/Stack.js +2 -3
- package/dist/components/Stat/Stat.js +6 -3
- package/dist/components/Steps/Steps.js +6 -3
- package/dist/components/Switch/Switch.d.ts +7 -7
- package/dist/components/Switch/Switch.js +4 -10
- package/dist/components/Table/Table.js +38 -19
- package/dist/components/Tabs/Tabs.js +2 -22
- package/dist/components/Tag/Tag.js +3 -1
- package/dist/components/Text/Text.d.ts +9 -5
- package/dist/components/Text/Text.js +3 -4
- package/dist/components/TileGrid/TileGrid.js +1 -1
- package/dist/components/TimePicker/TimePicker.js +35 -33
- package/dist/components/Timeline/Timeline.js +5 -4
- package/dist/components/Toaster/Toaster.js +21 -17
- package/dist/components/Tooltip/Tooltip.d.ts +6 -6
- package/dist/components/Tooltip/Tooltip.js +3 -17
- package/dist/components/TreeView/TreeView.js +15 -12
- package/dist/components.css +281 -60
- package/dist/hooks/useTheme.js +6 -6
- package/dist/icons/message-circle.svg +1 -0
- package/dist/internal/CharCount.js +3 -1
- package/dist/internal/choice.d.ts +10 -48
- package/dist/internal/choice.js +8 -96
- package/dist/internal/describedBy.d.ts +3 -0
- package/dist/internal/describedBy.js +4 -0
- package/dist/internal/options.d.ts +10 -5
- package/dist/internal/options.js +25 -11
- package/dist/internal/status.d.ts +3 -2
- package/dist/internal/status.js +10 -2
- package/dist/internal/useScrollActive.js +2 -2
- package/dist/internal/useToday.d.ts +3 -0
- package/dist/internal/useToday.js +42 -0
- package/dist/lib/dates.js +25 -15
- package/dist/lib/format.js +11 -8
- package/dist/lib/icons.d.ts +5 -0
- package/dist/lib/icons.js +2 -0
- package/dist/lib/math.js +17 -5
- package/dist/lib/rich-text.d.ts +13 -0
- package/dist/lib/rich-text.js +62 -0
- package/dist/lib/space.d.ts +1 -2
- package/dist/lib/space.js +4 -1
- package/dist/lib/text.js +1 -1
- package/dist/lib/theme.js +1 -1
- package/dist/localization.d.ts +166 -0
- package/dist/localization.js +142 -0
- package/dist/tokens.css +2 -0
- package/dist/tokens.dtcg.json +10 -0
- package/dist/tokens.json +10 -0
- package/dist/types.d.ts +6 -1
- package/dist/vokality.css +283 -60
- package/dist/vokality.iife.js +35981 -9698
- package/docs/brand.md +8 -5
- package/docs/components/AppShell/README.md +1 -1
- package/docs/components/Badge/README.md +1 -1
- package/docs/components/BarChart/README.md +1 -0
- package/docs/components/Button/README.md +4 -2
- package/docs/components/ChatInbox/README.md +41 -0
- package/docs/components/ChatWidget/README.md +47 -0
- package/docs/components/Checkbox/README.md +7 -3
- package/docs/components/ContextMenu/README.md +7 -3
- package/docs/components/Dialog/README.md +9 -4
- package/docs/components/Drawer/README.md +1 -1
- package/docs/components/Field/README.md +1 -1
- package/docs/components/Fieldset/README.md +2 -2
- package/docs/components/Inline/README.md +1 -1
- package/docs/components/LineChart/README.md +1 -0
- package/docs/components/LocaleProvider/README.md +48 -0
- package/docs/components/Menu/README.md +13 -8
- package/docs/components/OtpInput/README.md +9 -5
- package/docs/components/PasswordInput/README.md +11 -2
- package/docs/components/Popover/README.md +6 -3
- package/docs/components/Progress/README.md +3 -1
- package/docs/components/RadioGroup/README.md +5 -2
- package/docs/components/RichTextEditor/README.md +84 -0
- package/docs/components/SegmentedControl/README.md +2 -0
- package/docs/components/Select/README.md +13 -17
- package/docs/components/Skeleton/README.md +1 -1
- package/docs/components/Slider/README.md +4 -2
- package/docs/components/Steps/README.md +1 -1
- package/docs/components/Switch/README.md +5 -2
- package/docs/components/Tabs/README.md +4 -2
- package/docs/components/Text/README.md +1 -1
- package/docs/components/Toaster/README.md +3 -1
- package/docs/components/Tooltip/README.md +5 -3
- package/package.json +49 -5
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createElement } from "react";
|
|
2
2
|
import { cx } from "../../lib/cx.js";
|
|
3
3
|
import { space } from "../../lib/space.js";
|
|
4
4
|
/** Lays children out in a row that wraps, with one gap. */
|
|
5
5
|
export function Inline({ as, gap, align, justify, wrap, children, className, style, ...rest }) {
|
|
6
|
-
|
|
7
|
-
return (_jsx(Tag, { className: cx("vk-layout-inline", className), style: { gap: space(gap, "sm"), alignItems: align || "center", justifyContent: justify, flexWrap: wrap === false ? "nowrap" : "wrap", ...style }, ...rest, children: children }));
|
|
6
|
+
return createElement(as || "div", { className: cx("vk-layout-inline", className), style: { gap: space(gap, "sm"), alignItems: align || "center", justifyContent: justify, flexWrap: wrap === false ? "nowrap" : "wrap", ...style }, ...rest }, children);
|
|
8
7
|
}
|
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useLocale } from "../../localization.js";
|
|
3
4
|
import { ChartFrame, cellLabel, cellNumber, chartKeys, chartTooltip, seriesColor, useChart } from "../../charts/chart.js";
|
|
4
5
|
import { niceTicks } from "../../lib/math.js";
|
|
5
6
|
/** Lines for change over time: one to four series, each labeled directly at its end. */
|
|
6
7
|
export function LineChart(props) {
|
|
7
|
-
const
|
|
8
|
-
const
|
|
8
|
+
const { messages } = useLocale(props.locale);
|
|
9
|
+
const { setPlotElement, width, active, setActive, fmt, series, xKey } = useChart(props);
|
|
10
|
+
const data = props.data;
|
|
9
11
|
const height = props.height || 240;
|
|
10
|
-
const label =
|
|
12
|
+
const label = series.length <= 4 && props.directLabels !== false;
|
|
11
13
|
const pad = { top: 12, right: label ? 96 : 12, bottom: 28, left: 48 };
|
|
12
14
|
let max = 0;
|
|
13
15
|
for (const d of data)
|
|
14
|
-
for (const s of
|
|
16
|
+
for (const s of series)
|
|
15
17
|
max = Math.max(max, cellNumber(d, s.key));
|
|
16
|
-
const ticks = niceTicks(max, 4), top = ticks
|
|
17
|
-
const plotW = Math.max(10,
|
|
18
|
+
const ticks = niceTicks(max, 4), top = ticks.at(-1) ?? 1;
|
|
19
|
+
const plotW = Math.max(10, width - pad.left - pad.right), plotH = height - pad.top - pad.bottom;
|
|
18
20
|
const x = (i) => pad.left + (data.length < 2 ? plotW / 2 : (plotW * i) / (data.length - 1));
|
|
19
21
|
const y = (v) => pad.top + plotH - (v / top) * plotH;
|
|
20
|
-
const fmtTick = props.tickFormat ||
|
|
22
|
+
const fmtTick = props.tickFormat || fmt;
|
|
21
23
|
// Label every nth point so x labels stay about 72px apart.
|
|
22
24
|
const every = Math.max(1, Math.ceil(data.length / Math.max(2, Math.floor(plotW / 72))));
|
|
23
|
-
const view = { ...props, data, series
|
|
25
|
+
const view = { ...props, data, series, format: fmt, xKey };
|
|
24
26
|
const lastY = [];
|
|
25
27
|
const onMouseMove = (e) => {
|
|
26
28
|
const r = e.currentTarget.getBoundingClientRect(), px = e.clientX - r.left;
|
|
27
29
|
const i = Math.round(((px - pad.left) / plotW) * (data.length - 1));
|
|
28
|
-
|
|
30
|
+
setActive(Math.max(0, Math.min(data.length - 1, i)));
|
|
29
31
|
};
|
|
30
|
-
return (_jsx(ChartFrame, { ...view, children: _jsxs("div", { ref:
|
|
31
|
-
const pts = data.map((d, i) => x(i)
|
|
32
|
+
return (_jsx(ChartFrame, { ...view, children: _jsxs("div", { ref: setPlotElement, className: "vk-chart-plot", tabIndex: 0, role: "img", "aria-label": messages.chartInstructions(props.title || messages.lineChart), onKeyDown: chartKeys({ active, setActive }, data.length), onMouseLeave: () => { setActive(null); }, onBlur: () => { setActive(null); }, onMouseMove: onMouseMove, children: [_jsxs("svg", { width: width, height: height, viewBox: `0 0 ${String(width)} ${String(height)}`, "aria-hidden": "true", className: "vk-chart-svg", children: [ticks.map((t) => (_jsxs("g", { children: [_jsx("line", { x1: pad.left, x2: pad.left + plotW, y1: y(t), y2: y(t), className: t === 0 ? "vk-chart-baseline" : "vk-chart-grid" }), _jsx("text", { x: pad.left - 8, y: y(t), dy: "0.32em", textAnchor: "end", className: "vk-chart-tick", children: fmtTick(t) })] }, t))), data.map((d, i) => i % every === 0 || i === data.length - 1 ? _jsx("text", { x: x(i), y: height - 8, textAnchor: i === 0 ? "start" : i === data.length - 1 ? "end" : "middle", className: "vk-chart-tick", children: cellLabel(d, xKey) }, `x${String(i)}`) : null), active != null ? _jsx("line", { x1: x(active), x2: x(active), y1: pad.top, y2: pad.top + plotH, className: "vk-chart-crosshair" }) : null, series.map((s, si) => {
|
|
33
|
+
const pts = data.map((d, i) => `${String(x(i))},${String(y(cellNumber(d, s.key)))}`).join(" ");
|
|
32
34
|
return _jsx("polyline", { points: pts, fill: "none", stroke: seriesColor(si), strokeWidth: 2, strokeLinejoin: "round", strokeLinecap: "round" }, s.key);
|
|
33
|
-
}),
|
|
35
|
+
}), series.map((s, si) => {
|
|
34
36
|
// A point on each line at the active row, or at the last one.
|
|
35
|
-
const i =
|
|
37
|
+
const i = active == null ? data.length - 1 : active;
|
|
36
38
|
const d = data[i];
|
|
37
39
|
if (!d)
|
|
38
40
|
return null;
|
|
39
41
|
return _jsx("circle", { cx: x(i), cy: y(cellNumber(d, s.key)), r: 4, fill: seriesColor(si), className: "vk-chart-point" }, "p" + s.key);
|
|
40
42
|
}), label
|
|
41
|
-
?
|
|
43
|
+
? series.map((s) => {
|
|
42
44
|
const d = data[data.length - 1];
|
|
43
45
|
if (!d)
|
|
44
46
|
return null;
|
|
@@ -49,5 +51,5 @@ export function LineChart(props) {
|
|
|
49
51
|
lastY.push(ly);
|
|
50
52
|
return _jsx("text", { x: x(data.length - 1) + 10, y: ly, dy: "0.32em", className: "vk-chart-direct", children: s.label }, "l" + s.key);
|
|
51
53
|
})
|
|
52
|
-
: null] }), chartTooltip(view,
|
|
54
|
+
: null] }), chartTooltip(view, active, active == null ? 0 : x(active), width)] }) }));
|
|
53
55
|
}
|
|
@@ -8,14 +8,18 @@ export const LinkContext = createContext(null);
|
|
|
8
8
|
* Hash, external, mailto: and tel: links, and links with target or download, stay plain <a>.
|
|
9
9
|
*/
|
|
10
10
|
export function LinkProvider({ component, children }) {
|
|
11
|
-
return _jsx(LinkContext.Provider, { value: component
|
|
11
|
+
return _jsx(LinkContext.Provider, { value: component, children: children });
|
|
12
12
|
}
|
|
13
13
|
/** Hash links, links with a scheme (https:, mailto:, tel:) and protocol-relative links. */
|
|
14
14
|
export const PLAIN_HREF = /^(#|[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
15
|
+
/** The router adapter is an existing component type supplied by the provider. */
|
|
16
|
+
const RouterAnchor = forwardRef(function RouterAnchor({ component: Component, href, ...props }, ref) {
|
|
17
|
+
return _jsx(Component, { ...props, href: href, ref: ref });
|
|
18
|
+
});
|
|
15
19
|
/** The <a> behind every link in the system: the LinkProvider's component for internal links, else a plain <a>. */
|
|
16
20
|
export const Anchor = forwardRef(function Anchor(props, ref) {
|
|
17
21
|
const Comp = useContext(LinkContext);
|
|
18
22
|
if (!Comp || typeof props.href !== "string" || PLAIN_HREF.test(props.href) || props.target || props.download)
|
|
19
23
|
return _jsx("a", { ref: ref, ...props });
|
|
20
|
-
return _jsx(
|
|
24
|
+
return _jsx(RouterAnchor, { component: Comp, ...props, href: props.href, ref: ref });
|
|
21
25
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { Anchor } from "../LinkProvider/LinkProvider.js";
|
|
3
3
|
import { cx } from "../../lib/cx.js";
|
|
4
4
|
/** Rows of records: people, files, notifications, recent items. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createElement } from "react";
|
|
2
3
|
import { Badge } from "../Badge/Badge.js";
|
|
3
4
|
import { Icon } from "../Icon/Icon.js";
|
|
4
5
|
import { Anchor } from "../LinkProvider/LinkProvider.js";
|
|
@@ -6,8 +7,7 @@ import { cx } from "../../lib/cx.js";
|
|
|
6
7
|
import { para } from "../../lib/text.js";
|
|
7
8
|
// The text under the image.
|
|
8
9
|
function MediaCardBody({ title, eyebrow, meta, description, badge, href, linkLabel, outlined }) {
|
|
9
|
-
|
|
10
|
-
return (_jsxs("div", { className: "vk-media-card-body", children: [eyebrow ? _jsx("span", { className: "vk-card-eyebrow", children: eyebrow }) : null, _jsxs("span", { className: cx(outlined ? "vk-card-title" : "vk-media-card-title", "vk-media-title-row"), children: [title, badge ? _jsx(Badge, { children: badge }) : null] }), meta ? _jsx("span", { className: "vk-media-card-meta", children: meta }) : null, description ? (_jsx(Description, { className: outlined ? "vk-card-body" : "vk-media-card-services", children: description })) : null, href && linkLabel ? (_jsxs("span", { className: "vk-media-card-link", children: [_jsx("span", { children: linkLabel }), _jsx(Icon, { name: "arrow-up-right", className: "vk-media-card-arrow" })] })) : null] }));
|
|
10
|
+
return (_jsxs("div", { className: "vk-media-card-body", children: [eyebrow ? _jsx("span", { className: "vk-card-eyebrow", children: eyebrow }) : null, _jsxs("span", { className: cx(outlined ? "vk-card-title" : "vk-media-card-title", "vk-media-title-row"), children: [title, badge ? _jsx(Badge, { children: badge }) : null] }), meta ? _jsx("span", { className: "vk-media-card-meta", children: meta }) : null, description ? (createElement(para(description), { className: outlined ? "vk-card-body" : "vk-media-card-services" }, description)) : null, href && linkLabel ? (_jsxs("span", { className: "vk-media-card-link", children: [_jsx("span", { children: linkLabel }), _jsx(Icon, { name: "arrow-up-right", className: "vk-media-card-arrow" })] })) : null] }));
|
|
11
11
|
}
|
|
12
12
|
// With an href the whole card is one link; external links open in a new tab.
|
|
13
13
|
function MediaCardWrap({ href, children }) {
|
|
@@ -1,53 +1,16 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { useLocale } from "../../localization.js";
|
|
4
4
|
import { DropdownMenu as Primitive } from "radix-ui";
|
|
5
5
|
import { Icon } from "../Icon/Icon.js";
|
|
6
|
-
import { focusables } from "../../internal/focusables.js";
|
|
7
6
|
import { cx } from "../../lib/cx.js";
|
|
8
|
-
import { MenuEntries
|
|
7
|
+
import { MenuEntries } from "./entries.js";
|
|
9
8
|
/** Menu button (WAI-ARIA), built on Radix DropdownMenu. */
|
|
10
9
|
export function Menu({ items, label, icon, triggerLabel, variant, size, align, disabled, className }) {
|
|
11
|
-
const
|
|
12
|
-
// Which item takes focus as the menu opens: the last one after ↑ on the trigger, else the first.
|
|
13
|
-
const openWith = useRef("first");
|
|
14
|
-
// Set when Tab closed the menu: focus moves on from the trigger in that direction.
|
|
15
|
-
const tabOut = useRef(0);
|
|
16
|
-
const triggerRef = useRef(null);
|
|
17
|
-
const { defer, flush } = useDeferredSelect();
|
|
18
|
-
const { arm, onFocus } = useFocusOnOpen();
|
|
19
|
-
const onOpenChange = (next) => {
|
|
20
|
-
arm(next ? openWith.current : null);
|
|
21
|
-
setOpen(next);
|
|
22
|
-
};
|
|
10
|
+
const { messages } = useLocale();
|
|
23
11
|
// An icon with no label makes an icon button, named by triggerLabel.
|
|
24
12
|
const iconOnly = label ? undefined : icon;
|
|
25
|
-
return (_jsx("div", { className: cx("vk-menu-root", className), children: _jsxs(Primitive.Root, {
|
|
26
|
-
openWith.current = "first";
|
|
27
|
-
if (e.key === "ArrowUp" && !disabled) {
|
|
28
|
-
e.preventDefault();
|
|
29
|
-
openWith.current = "last";
|
|
30
|
-
onOpenChange(true);
|
|
31
|
-
}
|
|
32
|
-
}, children: iconOnly
|
|
13
|
+
return (_jsx("div", { className: cx("vk-menu-root", className), children: _jsxs(Primitive.Root, { children: [_jsx(Primitive.Trigger, { disabled: disabled, className: iconOnly ? "vk-icon-btn vk-icon-btn-" + (variant || "ghost") : cx("vk-btn", "vk-btn-" + (variant || "outline"), size === "sm" ? "vk-btn-sm" : null), "aria-label": iconOnly ? triggerLabel || messages.moreActions : undefined, title: iconOnly ? triggerLabel || messages.moreActions : undefined, children: iconOnly
|
|
33
14
|
? _jsx(Icon, { name: iconOnly })
|
|
34
|
-
: _jsxs(_Fragment, { children: [_jsxs("span", { className: "vk-btn-label", children: [icon ? _jsx(Icon, { name: icon, size: 16, className: "vk-menu-trigger-icon" }) : null, label] }), _jsx("span", { className: "vk-btn-glyph", "aria-hidden": "true", children: _jsx(Icon, { name: "chevron-down", size: 14 }) })] }) }), _jsx(Primitive.Portal, { children: _jsx(Primitive.Content, { className: "vk-menu", side: "bottom", align: align === "end" ? "end" : "start", sideOffset: 4, collisionPadding: 8,
|
|
35
|
-
const dir = tabOut.current;
|
|
36
|
-
tabOut.current = 0;
|
|
37
|
-
if (dir) {
|
|
38
|
-
// Tab closes the menu and moves on from the trigger, as if focus had stayed there.
|
|
39
|
-
e.preventDefault();
|
|
40
|
-
const trigger = triggerRef.current;
|
|
41
|
-
const list = focusables(document.body);
|
|
42
|
-
const at = trigger ? list.indexOf(trigger) : -1;
|
|
43
|
-
(list[at + dir] || trigger)?.focus();
|
|
44
|
-
}
|
|
45
|
-
flush();
|
|
46
|
-
}, onKeyDown: (e) => {
|
|
47
|
-
if (e.key === "Tab") {
|
|
48
|
-
e.preventDefault();
|
|
49
|
-
tabOut.current = e.shiftKey ? -1 : 1;
|
|
50
|
-
onOpenChange(false);
|
|
51
|
-
}
|
|
52
|
-
}, children: _jsx(MenuEntries, { parts: Primitive, items: items || [], defer: defer }) }) })] }) }));
|
|
15
|
+
: _jsxs(_Fragment, { children: [_jsxs("span", { className: "vk-btn-label", children: [icon ? _jsx(Icon, { name: icon, size: 16, className: "vk-menu-trigger-icon" }) : null, label] }), _jsx("span", { className: "vk-btn-glyph", "aria-hidden": "true", children: _jsx(Icon, { name: "chevron-down", size: 14 }) })] }) }), _jsx(Primitive.Portal, { children: _jsx(Primitive.Content, { className: "vk-menu", side: "bottom", align: align === "end" ? "end" : "start", sideOffset: 4, collisionPadding: 8, children: _jsx(MenuEntries, { parts: Primitive, items: items }) }) })] }) }));
|
|
53
16
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type FocusEvent, type KeyboardEvent } from "react";
|
|
2
1
|
import type { DropdownMenu } from "radix-ui";
|
|
3
2
|
import type { MenuEntry } from "../../types.js";
|
|
4
3
|
/** The Radix menu parts the entries are built from: DropdownMenu's or ContextMenu's (same shapes). */
|
|
@@ -6,35 +5,7 @@ export type MenuParts = Pick<typeof DropdownMenu, "Item" | "CheckboxItem" | "Rad
|
|
|
6
5
|
interface MenuEntriesProps {
|
|
7
6
|
parts: MenuParts;
|
|
8
7
|
items: MenuEntry[];
|
|
9
|
-
/** Runs a plain item's onSelect once the menu has closed and focus has gone back (see useDeferredSelect). */
|
|
10
|
-
defer: (run: () => void) => void;
|
|
11
8
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Plain items' onSelect runs after the menu has closed and focus is back on the trigger,
|
|
14
|
-
* so an action that opens a dialog captures the trigger as its return focus.
|
|
15
|
-
* `defer` queues the action; call `flush` from the content's onCloseAutoFocus.
|
|
16
|
-
*/
|
|
17
|
-
export declare function useDeferredSelect(): {
|
|
18
|
-
defer: (run: () => void) => void;
|
|
19
|
-
flush: () => void;
|
|
20
|
-
};
|
|
21
|
-
/** Focuses the first or last enabled item of a menu, or the menu itself when none can take focus. */
|
|
22
|
-
export declare function focusEdgeItem(content: HTMLElement | null, edge: "first" | "last"): void;
|
|
23
|
-
/**
|
|
24
|
-
* Radix focuses a menu's content as it opens, and only moves on to the first item when it was
|
|
25
|
-
* opened with the keyboard. `arm(edge)` before opening; the content's `onFocus` then moves
|
|
26
|
-
* straight to the first (or last) item, however the menu was opened.
|
|
27
|
-
*/
|
|
28
|
-
export declare function useFocusOnOpen(): {
|
|
29
|
-
arm: (which: "first" | "last" | null) => void;
|
|
30
|
-
onFocus: (e: FocusEvent<HTMLDivElement>) => void;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* onKeyDownCapture for a menu's content (root or submenu): ↓ ↑ Home End move focus in the same
|
|
34
|
-
* keystroke, wrapping. Radix's roving focus moves it a task later (setTimeout), which lets a
|
|
35
|
-
* key that follows at once act on the old item.
|
|
36
|
-
*/
|
|
37
|
-
export declare function moveFocusNow(e: KeyboardEvent<HTMLDivElement>): void;
|
|
38
9
|
/** Renders MenuEntry[] as Radix menu parts: items, checkbox and radio items, submenus, separators and labels. */
|
|
39
|
-
export declare function MenuEntries({ parts: P, items
|
|
10
|
+
export declare function MenuEntries({ parts: P, items }: MenuEntriesProps): import("react/jsx-runtime").JSX.Element;
|
|
40
11
|
export {};
|
|
@@ -1,92 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import { Fragment,
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Fragment, useId } from "react";
|
|
4
4
|
import { Icon } from "../Icon/Icon.js";
|
|
5
5
|
import { cx } from "../../lib/cx.js";
|
|
6
|
-
/**
|
|
7
|
-
* Plain items' onSelect runs after the menu has closed and focus is back on the trigger,
|
|
8
|
-
* so an action that opens a dialog captures the trigger as its return focus.
|
|
9
|
-
* `defer` queues the action; call `flush` from the content's onCloseAutoFocus.
|
|
10
|
-
*/
|
|
11
|
-
export function useDeferredSelect() {
|
|
12
|
-
const pending = useRef(null);
|
|
13
|
-
const defer = useCallback((run) => { pending.current = run; }, []);
|
|
14
|
-
const flush = useCallback(() => {
|
|
15
|
-
const run = pending.current;
|
|
16
|
-
pending.current = null;
|
|
17
|
-
// After Radix has moved focus back (it does so right after the onCloseAutoFocus handlers).
|
|
18
|
-
if (run)
|
|
19
|
-
queueMicrotask(run);
|
|
20
|
-
}, []);
|
|
21
|
-
return { defer, flush };
|
|
22
|
-
}
|
|
23
|
-
/** Focuses the first or last enabled item of a menu, or the menu itself when none can take focus. */
|
|
24
|
-
export function focusEdgeItem(content, edge) {
|
|
25
|
-
if (!content)
|
|
26
|
-
return;
|
|
27
|
-
const items = Array.from(content.querySelectorAll('[role^="menuitem"]:not([data-disabled])'));
|
|
28
|
-
const target = edge === "last" ? items[items.length - 1] : items[0];
|
|
29
|
-
(target || content).focus({ preventScroll: true });
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Radix focuses a menu's content as it opens, and only moves on to the first item when it was
|
|
33
|
-
* opened with the keyboard. `arm(edge)` before opening; the content's `onFocus` then moves
|
|
34
|
-
* straight to the first (or last) item, however the menu was opened.
|
|
35
|
-
*/
|
|
36
|
-
export function useFocusOnOpen() {
|
|
37
|
-
const edge = useRef(null);
|
|
38
|
-
const arm = useCallback((which) => { edge.current = which; }, []);
|
|
39
|
-
const onFocus = useCallback((e) => {
|
|
40
|
-
const which = edge.current;
|
|
41
|
-
if (!which || e.target !== e.currentTarget)
|
|
42
|
-
return;
|
|
43
|
-
edge.current = null;
|
|
44
|
-
// Skips Radix's own entry focus (composed after this handler), which would pick the first item.
|
|
45
|
-
e.preventDefault();
|
|
46
|
-
focusEdgeItem(e.currentTarget, which);
|
|
47
|
-
}, []);
|
|
48
|
-
return { arm, onFocus };
|
|
49
|
-
}
|
|
50
|
-
const STEP = { ArrowDown: 1, ArrowUp: -1, Home: "first", End: "last", PageUp: "first", PageDown: "last" };
|
|
51
|
-
/**
|
|
52
|
-
* onKeyDownCapture for a menu's content (root or submenu): ↓ ↑ Home End move focus in the same
|
|
53
|
-
* keystroke, wrapping. Radix's roving focus moves it a task later (setTimeout), which lets a
|
|
54
|
-
* key that follows at once act on the old item.
|
|
55
|
-
*/
|
|
56
|
-
export function moveFocusNow(e) {
|
|
57
|
-
const step = STEP[e.key];
|
|
58
|
-
if (step === undefined || e.metaKey || e.ctrlKey || e.altKey || e.shiftKey)
|
|
59
|
-
return;
|
|
60
|
-
const content = e.currentTarget;
|
|
61
|
-
const target = e.target;
|
|
62
|
-
// Keys from a submenu bubble through the React tree (portals included); each level handles its own.
|
|
63
|
-
if (target.closest('[role="menu"]') !== content)
|
|
64
|
-
return;
|
|
65
|
-
const items = Array.from(content.querySelectorAll('[role^="menuitem"]:not([data-disabled])'));
|
|
66
|
-
if (!items.length)
|
|
67
|
-
return;
|
|
68
|
-
const at = items.indexOf(target);
|
|
69
|
-
let next;
|
|
70
|
-
if (step === "first")
|
|
71
|
-
next = items[0];
|
|
72
|
-
else if (step === "last")
|
|
73
|
-
next = items[items.length - 1];
|
|
74
|
-
else if (at < 0)
|
|
75
|
-
next = step === 1 ? items[0] : items[items.length - 1];
|
|
76
|
-
else
|
|
77
|
-
next = items[(at + step + items.length) % items.length];
|
|
78
|
-
// Prevented, so Radix's own (deferred) handlers for the same key stand down.
|
|
79
|
-
e.preventDefault();
|
|
80
|
-
next.focus();
|
|
81
|
-
}
|
|
82
6
|
function Body({ icon, label, shortcut, sub }) {
|
|
83
7
|
return (_jsxs(_Fragment, { children: [icon ? _jsx(Icon, { name: icon, size: 16, className: "vk-menu-item-icon" }) : _jsx("span", { className: "vk-menu-item-icon", "aria-hidden": "true" }), _jsx("span", { className: "vk-menu-item-label", children: label }), shortcut ? _jsx("kbd", { className: "vk-menu-shortcut", children: shortcut }) : null, sub ? _jsx(Icon, { name: "chevron-right", size: 14, className: "vk-menu-sub-icon" }) : null] }));
|
|
84
8
|
}
|
|
85
|
-
/**
|
|
86
|
-
function SubEntry({ parts: P, entry, items
|
|
87
|
-
|
|
88
|
-
const triggerRef = useRef(null);
|
|
89
|
-
return (_jsxs(P.Sub, { open: open, onOpenChange: setOpen, children: [_jsx(P.SubTrigger, { ref: triggerRef, className: cx("vk-menu-item", entry.danger ? "vk-menu-item-danger" : null), disabled: entry.disabled, textValue: entry.label, children: _jsx(Body, { icon: entry.icon, label: entry.label, shortcut: entry.shortcut, sub: true }) }), _jsx(P.Portal, { children: _jsx(P.SubContent, { className: "vk-menu vk-menu-sub", sideOffset: -4, alignOffset: -5, collisionPadding: 8, loop: true, onKeyDownCapture: moveFocusNow, onEscapeKeyDown: (e) => { e.preventDefault(); setOpen(false); triggerRef.current?.focus(); }, children: _jsx(MenuEntries, { parts: P, items: items, defer: defer }) }) })] }));
|
|
9
|
+
/** The primitive owns submenu opening, keyboard navigation and dismissal. */
|
|
10
|
+
function SubEntry({ parts: P, entry, items }) {
|
|
11
|
+
return (_jsxs(P.Sub, { children: [_jsx(P.SubTrigger, { className: cx("vk-menu-item", entry.danger ? "vk-menu-item-danger" : null), disabled: entry.disabled, textValue: entry.label, children: _jsx(Body, { icon: entry.icon, label: entry.label, shortcut: entry.shortcut, sub: true }) }), _jsx(P.Portal, { children: _jsx(P.SubContent, { className: "vk-menu vk-menu-sub", sideOffset: -4, alignOffset: -5, collisionPadding: 8, children: _jsx(MenuEntries, { parts: P, items: items }) }) })] }));
|
|
90
12
|
}
|
|
91
13
|
/** Splits entries into single entries and runs of consecutive radio entries. */
|
|
92
14
|
function segments(items) {
|
|
@@ -107,40 +29,34 @@ function segments(items) {
|
|
|
107
29
|
/** Consecutive radio entries as one group; `start` is the first one's index in the menu. */
|
|
108
30
|
function RadioRun({ parts: P, run, start, labelledBy }) {
|
|
109
31
|
const checked = run.findIndex((r) => r.checked);
|
|
110
|
-
return (_jsx(P.RadioGroup, { value: checked >= 0 ? String(start + checked) : "", "aria-labelledby": labelledBy, children: run.map((r, k) => (_jsxs(P.RadioItem, { value: String(start + k), className: "vk-menu-item", disabled: r.disabled, textValue: r.label,
|
|
111
|
-
// Choosing a radio item keeps the menu open.
|
|
112
|
-
onSelect: (e) => { e.preventDefault(); r.onSelect?.(); }, children: [_jsx("span", { className: "vk-menu-item-icon vk-menu-check", "aria-hidden": "true", children: _jsx(P.ItemIndicator, { asChild: true, children: _jsx("span", { className: "vk-choice-dot vk-menu-dot" }) }) }), _jsx("span", { className: "vk-menu-item-label", children: r.label })] }, k + ":" + r.label))) }));
|
|
32
|
+
return (_jsx(P.RadioGroup, { value: checked >= 0 ? String(start + checked) : "", "aria-labelledby": labelledBy, children: run.map((r, k) => (_jsxs(P.RadioItem, { value: String(start + k), className: "vk-menu-item", disabled: r.disabled, textValue: r.label, onSelect: r.onSelect, children: [_jsx("span", { className: "vk-menu-item-icon vk-menu-check", "aria-hidden": "true", children: _jsx(P.ItemIndicator, { asChild: true, children: _jsx("span", { className: "vk-choice-dot vk-menu-dot" }) }) }), _jsx("span", { className: "vk-menu-item-label", children: r.label })] }, String(k) + ":" + r.label))) }));
|
|
113
33
|
}
|
|
114
34
|
function CheckboxItem({ parts: P, entry: it }) {
|
|
115
|
-
return (_jsxs(P.CheckboxItem, { className: "vk-menu-item", checked: it.checked, disabled: it.disabled, textValue: it.label,
|
|
116
|
-
// Keeps the menu open; onCheckedChange first, then onSelect (Radix's own onCheckedChange is unused).
|
|
117
|
-
onSelect: (e) => { e.preventDefault(); it.onCheckedChange?.(!it.checked); it.onSelect?.(); }, children: [_jsx("span", { className: "vk-menu-item-icon vk-menu-check", "aria-hidden": "true", children: _jsx(P.ItemIndicator, { className: "vk-menu-indicator", children: _jsx(Icon, { name: "check", size: 16 }) }) }), _jsx("span", { className: "vk-menu-item-label", children: it.label })] }));
|
|
35
|
+
return (_jsxs(P.CheckboxItem, { className: "vk-menu-item", checked: it.checked, disabled: it.disabled, textValue: it.label, onCheckedChange: it.onCheckedChange, onSelect: it.onSelect, children: [_jsx("span", { className: "vk-menu-item-icon vk-menu-check", "aria-hidden": "true", children: _jsx(P.ItemIndicator, { className: "vk-menu-indicator", children: _jsx(Icon, { name: "check", size: 16 }) }) }), _jsx("span", { className: "vk-menu-item-label", children: it.label })] }));
|
|
118
36
|
}
|
|
119
|
-
function PlainItem({ parts: P, entry: it
|
|
120
|
-
|
|
121
|
-
return (_jsx(P.Item, { className: cx("vk-menu-item", it.danger ? "vk-menu-item-danger" : null), disabled: it.disabled, textValue: it.label, onSelect: () => { if (onSelect)
|
|
122
|
-
defer(() => onSelect()); }, children: _jsx(Body, { icon: it.icon, label: it.label, shortcut: it.shortcut }) }));
|
|
37
|
+
function PlainItem({ parts: P, entry: it }) {
|
|
38
|
+
return (_jsx(P.Item, { className: cx("vk-menu-item", it.danger ? "vk-menu-item-danger" : null), disabled: it.disabled, textValue: it.label, onSelect: it.onSelect, children: _jsx(Body, { icon: it.icon, label: it.label, shortcut: it.shortcut }) }));
|
|
123
39
|
}
|
|
124
40
|
/** Renders MenuEntry[] as Radix menu parts: items, checkbox and radio items, submenus, separators and labels. */
|
|
125
|
-
export function MenuEntries({ parts: P, items
|
|
41
|
+
export function MenuEntries({ parts: P, items }) {
|
|
126
42
|
const uid = useId();
|
|
127
43
|
return (_jsx(Fragment, { children: segments(items).map((s) => {
|
|
128
|
-
const first =
|
|
129
|
-
const key = s.at + ":" + ("label" in first ? first.label : first.type);
|
|
44
|
+
const first = "run" in s ? s.run[0] : s.entry;
|
|
45
|
+
const key = String(s.at) + ":" + ("label" in first ? first.label : first.type);
|
|
130
46
|
if ("run" in s) {
|
|
131
47
|
// A label right before a radio group names it.
|
|
132
48
|
const prev = items[s.at - 1];
|
|
133
|
-
return _jsx(RadioRun, { parts: P, run: s.run, start: s.at, labelledBy: prev
|
|
49
|
+
return _jsx(RadioRun, { parts: P, run: s.run, start: s.at, labelledBy: prev?.type === "label" ? uid + "-l" + String(s.at - 1) : undefined }, key);
|
|
134
50
|
}
|
|
135
51
|
const it = s.entry;
|
|
136
52
|
if (it.type === "separator")
|
|
137
53
|
return _jsx(P.Separator, { className: "vk-menu-separator" }, key);
|
|
138
54
|
if (it.type === "label")
|
|
139
|
-
return _jsx(P.Label, { id: uid + "-l" + s.at, className: "vk-menu-label", children: it.label }, key);
|
|
55
|
+
return _jsx(P.Label, { id: uid + "-l" + String(s.at), className: "vk-menu-label", children: it.label }, key);
|
|
140
56
|
if (it.type === "checkbox")
|
|
141
57
|
return _jsx(CheckboxItem, { parts: P, entry: it }, key);
|
|
142
58
|
if (it.items)
|
|
143
|
-
return _jsx(SubEntry, { parts: P, entry: it, items: it.items
|
|
144
|
-
return _jsx(PlainItem, { parts: P, entry: it
|
|
59
|
+
return _jsx(SubEntry, { parts: P, entry: it, items: it.items }, key);
|
|
60
|
+
return _jsx(PlainItem, { parts: P, entry: it }, key);
|
|
145
61
|
}) }));
|
|
146
62
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useLocale } from "../../localization.js";
|
|
3
4
|
import { useRef, useState } from "react";
|
|
4
5
|
import { Popover } from "radix-ui";
|
|
5
6
|
import { ListPanel } from "../Combobox/ListPanel.js";
|
|
@@ -13,14 +14,14 @@ import { omit } from "../../lib/omit.js";
|
|
|
13
14
|
/** The popup, which ListPanel makes the Radix content (so a plain element, not a component): a <ul> listbox of checkable options, or a <div> status when nothing matches. */
|
|
14
15
|
function optionList({ listId, options, values, max, active, setActive, query, onToggle, emptyText, ...labels }) {
|
|
15
16
|
if (!options.length) {
|
|
16
|
-
return (_jsx("div", { id: listId, className: "vk-select-menu vk-select-empty", role: "status", children: emptyText
|
|
17
|
+
return (_jsx("div", { id: listId, className: "vk-select-menu vk-select-empty", role: "status", children: emptyText }));
|
|
17
18
|
}
|
|
18
19
|
const chosen = new Set(values);
|
|
19
20
|
const atMax = Boolean(max && values.length >= max);
|
|
20
21
|
return (_jsx("ul", { id: listId, role: "listbox", "aria-multiselectable": "true", className: "vk-select-menu", "aria-labelledby": labels["aria-labelledby"], "aria-label": labels["aria-label"], children: options.map((o, i) => {
|
|
21
22
|
const isSel = chosen.has(o.value);
|
|
22
23
|
const blocked = o.disabled || (!isSel && atMax);
|
|
23
|
-
return (_jsxs("li", { id: listId + "-" + i, "data-index": i, role: "option", "aria-selected": isSel ? "true" : "false", "aria-disabled": blocked ? "true" : undefined, className: cx("vk-select-option", "vk-multi-option", i === active && "vk-select-option-active"), onMouseDown: (e) => e.preventDefault(), onMouseMove: () => {
|
|
24
|
+
return (_jsxs("li", { id: listId + "-" + String(i), "data-index": i, role: "option", "aria-selected": isSel ? "true" : "false", "aria-disabled": blocked ? "true" : undefined, className: cx("vk-select-option", "vk-multi-option", i === active && "vk-select-option-active"), onMouseDown: (e) => { e.preventDefault(); }, onMouseMove: () => {
|
|
24
25
|
if (!o.disabled && i !== active)
|
|
25
26
|
setActive(i);
|
|
26
27
|
}, onClick: () => {
|
|
@@ -29,15 +30,36 @@ function optionList({ listId, options, values, max, active, setActive, query, on
|
|
|
29
30
|
}, children: [_jsx("span", { className: "vk-choice-box vk-multi-box", "aria-hidden": "true", children: _jsx("svg", { className: "vk-choice-mark", viewBox: "0 0 16 16", width: 12, height: 12, fill: "none", children: _jsx("path", { d: CHECK_PATH, stroke: "currentColor", strokeWidth: 2, strokeLinecap: "square" }) }) }), _jsxs("span", { className: "vk-select-option-text", children: [_jsx("span", { children: highlight(o.label, query) }), o.description ? _jsx("span", { className: "vk-select-option-description", children: o.description }) : null] })] }, o.value));
|
|
30
31
|
}) }));
|
|
31
32
|
}
|
|
33
|
+
/** Selection owns controlled/default values and enforces the maximum independently of popup navigation. */
|
|
34
|
+
function useMultiSelection(props, options) {
|
|
35
|
+
const controlled = props.value !== undefined;
|
|
36
|
+
const [uncontrolled, setUncontrolled] = useState(props.defaultValue || []);
|
|
37
|
+
const values = controlled ? props.value || [] : uncontrolled;
|
|
38
|
+
const set = (next) => {
|
|
39
|
+
if (!controlled)
|
|
40
|
+
setUncontrolled(next);
|
|
41
|
+
props.onChange?.(next);
|
|
42
|
+
};
|
|
43
|
+
const toggle = (option) => {
|
|
44
|
+
if (!option || option.disabled)
|
|
45
|
+
return;
|
|
46
|
+
const chosen = values.includes(option.value);
|
|
47
|
+
if (!chosen && props.max && values.length >= props.max)
|
|
48
|
+
return;
|
|
49
|
+
set(chosen ? values.filter((value) => value !== option.value) : values.concat(option.value));
|
|
50
|
+
};
|
|
51
|
+
const remove = (value) => { set(values.filter((item) => item !== value)); };
|
|
52
|
+
const optionForValue = (value) => options.find((option) => option.value === value) || { value, label: value };
|
|
53
|
+
return { values, toggle, remove, optionForValue };
|
|
54
|
+
}
|
|
32
55
|
/** Pick several options; chosen values show as removable tags, typing filters. The list is a Radix Popover anchored to the field. */
|
|
33
56
|
export function MultiSelect(props) {
|
|
57
|
+
const { messages } = useLocale();
|
|
34
58
|
const options = normalizeOptions(props.options);
|
|
35
59
|
const base = useUid("vk-multi");
|
|
36
60
|
const inputId = props.id || base + "-input";
|
|
37
61
|
const listId = base + "-list";
|
|
38
|
-
const
|
|
39
|
-
const [uncontrolledValues, setUncontrolledValues] = useState(props.defaultValue || []);
|
|
40
|
-
const values = controlled ? props.value || [] : uncontrolledValues;
|
|
62
|
+
const { values, toggle, remove: removeValue, optionForValue } = useMultiSelection(props, options);
|
|
41
63
|
const [query, setQuery] = useState("");
|
|
42
64
|
const [open, setOpen] = useState(Boolean(props.defaultOpen));
|
|
43
65
|
const [active, setActive] = useState(props.defaultOpen ? 0 : -1);
|
|
@@ -49,27 +71,8 @@ export function MultiSelect(props) {
|
|
|
49
71
|
setActive(-1);
|
|
50
72
|
setQuery("");
|
|
51
73
|
};
|
|
52
|
-
const byValue = (v) => {
|
|
53
|
-
for (let i = 0; i < options.length; i++)
|
|
54
|
-
if (options[i].value === v)
|
|
55
|
-
return options[i];
|
|
56
|
-
return { value: v, label: v };
|
|
57
|
-
};
|
|
58
|
-
const setValues = (next) => {
|
|
59
|
-
if (!controlled)
|
|
60
|
-
setUncontrolledValues(next);
|
|
61
|
-
props.onChange?.(next);
|
|
62
|
-
};
|
|
63
|
-
const toggle = (o) => {
|
|
64
|
-
if (!o || o.disabled)
|
|
65
|
-
return;
|
|
66
|
-
const has = values.indexOf(o.value) >= 0;
|
|
67
|
-
if (!has && props.max && values.length >= props.max)
|
|
68
|
-
return;
|
|
69
|
-
setValues(has ? values.filter((v) => v !== o.value) : values.concat([o.value]));
|
|
70
|
-
};
|
|
71
74
|
const remove = (v) => {
|
|
72
|
-
|
|
75
|
+
removeValue(v);
|
|
73
76
|
inputRef.current?.focus();
|
|
74
77
|
};
|
|
75
78
|
const openAt = (i) => {
|
|
@@ -82,16 +85,20 @@ export function MultiSelect(props) {
|
|
|
82
85
|
const key = e.key;
|
|
83
86
|
if (key === "ArrowDown") {
|
|
84
87
|
e.preventDefault();
|
|
85
|
-
if (!open)
|
|
86
|
-
|
|
88
|
+
if (!open) {
|
|
89
|
+
openAt(nextEnabled(filtered, 0, 1));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
87
92
|
const n = nextEnabled(filtered, active + 1, 1);
|
|
88
93
|
if (n >= 0)
|
|
89
94
|
setActive(n);
|
|
90
95
|
}
|
|
91
96
|
else if (key === "ArrowUp") {
|
|
92
97
|
e.preventDefault();
|
|
93
|
-
if (!open)
|
|
94
|
-
|
|
98
|
+
if (!open) {
|
|
99
|
+
openAt(nextEnabled(filtered, filtered.length - 1, -1));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
95
102
|
const p = nextEnabled(filtered, active - 1, -1);
|
|
96
103
|
if (p >= 0)
|
|
97
104
|
setActive(p);
|
|
@@ -117,7 +124,9 @@ export function MultiSelect(props) {
|
|
|
117
124
|
}
|
|
118
125
|
}
|
|
119
126
|
else if (key === "Backspace" && !query && values.length) {
|
|
120
|
-
|
|
127
|
+
const last = values[values.length - 1];
|
|
128
|
+
if (last !== undefined)
|
|
129
|
+
remove(last);
|
|
121
130
|
}
|
|
122
131
|
else if (key === "Tab") {
|
|
123
132
|
close();
|
|
@@ -140,9 +149,9 @@ export function MultiSelect(props) {
|
|
|
140
149
|
if (!open)
|
|
141
150
|
openAt(nextEnabled(filtered, 0, 1));
|
|
142
151
|
}, children: [values.map((v) => {
|
|
143
|
-
const o =
|
|
144
|
-
return (_jsx(Tag, { removeTabIndex: -1, removeLabel:
|
|
145
|
-
}), _jsx("input", { ref: inputRef, id: inputId, type: "text", role: "combobox", autoComplete: "off", spellCheck: false, className: cx("vk-multiselect-input", values.length > 0 && "vk-multiselect-input-compact"), placeholder: values.length ? "" : props.placeholder, disabled: props.disabled, "aria-autocomplete": "list", "aria-expanded": open ? "true" : "false", "aria-controls": listId, "aria-activedescendant": open && active >= 0 ? listId + "-" + active : undefined, "aria-required": props.required ? "true" : undefined, value: query, onChange: (e) => {
|
|
152
|
+
const o = optionForValue(v);
|
|
153
|
+
return (_jsx(Tag, { removeTabIndex: -1, removeLabel: messages.remove(o.label), onRemove: props.disabled ? undefined : () => { remove(v); }, children: o.label }, v));
|
|
154
|
+
}), _jsx("input", { ref: inputRef, id: inputId, type: "text", role: "combobox", autoComplete: "off", spellCheck: false, className: cx("vk-multiselect-input", values.length > 0 && "vk-multiselect-input-compact"), placeholder: values.length ? "" : props.placeholder, disabled: props.disabled, "aria-autocomplete": "list", "aria-expanded": open ? "true" : "false", "aria-controls": listId, "aria-activedescendant": open && active >= 0 ? listId + "-" + String(active) : undefined, "aria-required": props.required ? "true" : undefined, value: query, onChange: (e) => {
|
|
146
155
|
const v = e.target.value;
|
|
147
156
|
setQuery(v);
|
|
148
157
|
const list = options.filter((o) => matches(o, v));
|
|
@@ -159,7 +168,7 @@ export function MultiSelect(props) {
|
|
|
159
168
|
setActive,
|
|
160
169
|
query,
|
|
161
170
|
onToggle: toggle,
|
|
162
|
-
emptyText: props.emptyText,
|
|
171
|
+
emptyText: props.emptyText || messages.noMatches(query),
|
|
163
172
|
"aria-label": props["aria-label"],
|
|
164
173
|
"aria-labelledby": props["aria-labelledby"],
|
|
165
174
|
}) }), props.name ? values.map((v) => _jsx("input", { type: "hidden", name: props.name, value: v }, v)) : null] }) }));
|