@jesusgm/react-components 0.0.0-stage → 0.1.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/Readme.md +158 -0
- package/dist/components/atoms/avatar/index.d.ts +19 -0
- package/dist/components/atoms/avatar/index.js +44 -0
- package/dist/components/atoms/avatar/index.js.map +1 -0
- package/dist/components/atoms/avatar/styles.css +50 -0
- package/dist/components/atoms/badge/index.d.ts +17 -0
- package/dist/components/atoms/badge/index.js +37 -0
- package/dist/components/atoms/badge/index.js.map +1 -0
- package/dist/components/atoms/badge/styles.css +83 -0
- package/dist/components/atoms/button/index.d.ts +16 -0
- package/dist/components/atoms/button/index.js +22 -0
- package/dist/components/atoms/button/index.js.map +1 -0
- package/dist/components/atoms/button/styles.css +77 -0
- package/dist/components/atoms/checkbox/index.d.ts +13 -0
- package/dist/components/atoms/checkbox/index.js +63 -0
- package/dist/components/atoms/checkbox/index.js.map +1 -0
- package/dist/components/atoms/checkbox/styles.css +87 -0
- package/dist/components/atoms/code-highlighter/index.d.ts +8 -0
- package/dist/components/atoms/code-highlighter/index.js +24 -0
- package/dist/components/atoms/code-highlighter/index.js.map +1 -0
- package/dist/components/atoms/code-highlighter/railscasts.css +1 -0
- package/dist/components/atoms/code-highlighter/styles.css +17 -0
- package/dist/components/atoms/divider/index.d.ts +10 -0
- package/dist/components/atoms/divider/index.js +25 -0
- package/dist/components/atoms/divider/index.js.map +1 -0
- package/dist/components/atoms/divider/styles.css +56 -0
- package/dist/components/atoms/icon/index.d.ts +23 -0
- package/dist/components/atoms/icon/index.js +20 -0
- package/dist/components/atoms/icon/index.js.map +1 -0
- package/dist/components/atoms/icon/styles.css +13 -0
- package/dist/components/atoms/kbd/index.d.ts +7 -0
- package/dist/components/atoms/kbd/index.js +26 -0
- package/dist/components/atoms/kbd/index.js.map +1 -0
- package/dist/components/atoms/kbd/styles.css +29 -0
- package/dist/components/atoms/label/index.d.ts +9 -0
- package/dist/components/atoms/label/index.js +18 -0
- package/dist/components/atoms/label/index.js.map +1 -0
- package/dist/components/atoms/label/styles.css +19 -0
- package/dist/components/atoms/link/index.d.ts +12 -0
- package/dist/components/atoms/link/index.js +32 -0
- package/dist/components/atoms/link/index.js.map +1 -0
- package/dist/components/atoms/link/styles.css +54 -0
- package/dist/components/atoms/progress-bar/index.d.ts +23 -0
- package/dist/components/atoms/progress-bar/index.js +45 -0
- package/dist/components/atoms/progress-bar/index.js.map +1 -0
- package/dist/components/atoms/progress-bar/styles.css +75 -0
- package/dist/components/atoms/radio/context.d.ts +9 -0
- package/dist/components/atoms/radio/context.js +9 -0
- package/dist/components/atoms/radio/context.js.map +1 -0
- package/dist/components/atoms/radio/index.d.ts +15 -0
- package/dist/components/atoms/radio/index.js +44 -0
- package/dist/components/atoms/radio/index.js.map +1 -0
- package/dist/components/atoms/radio/radio-group.d.ts +25 -0
- package/dist/components/atoms/radio/radio-group.js +49 -0
- package/dist/components/atoms/radio/radio-group.js.map +1 -0
- package/dist/components/atoms/radio/styles.css +110 -0
- package/dist/components/atoms/skeleton/index.d.ts +15 -0
- package/dist/components/atoms/skeleton/index.js +38 -0
- package/dist/components/atoms/skeleton/index.js.map +1 -0
- package/dist/components/atoms/skeleton/styles.css +57 -0
- package/dist/components/atoms/spinner/index.d.ts +11 -0
- package/dist/components/atoms/spinner/index.js +22 -0
- package/dist/components/atoms/spinner/index.js.map +1 -0
- package/dist/components/atoms/spinner/styles.css +51 -0
- package/dist/components/atoms/switch/index.d.ts +14 -0
- package/dist/components/atoms/switch/index.js +33 -0
- package/dist/components/atoms/switch/index.js.map +1 -0
- package/dist/components/atoms/switch/styles.css +99 -0
- package/dist/components/atoms/text-input/index.d.ts +20 -0
- package/dist/components/atoms/text-input/index.js +33 -0
- package/dist/components/atoms/text-input/index.js.map +1 -0
- package/dist/components/atoms/text-input/styles.css +80 -0
- package/dist/components/atoms/textarea/index.d.ts +14 -0
- package/dist/components/atoms/textarea/index.js +43 -0
- package/dist/components/atoms/textarea/index.js.map +1 -0
- package/dist/components/atoms/textarea/styles.css +49 -0
- package/dist/components/atoms/typography/index.d.ts +30 -0
- package/dist/components/atoms/typography/index.js +30 -0
- package/dist/components/atoms/typography/index.js.map +1 -0
- package/dist/components/atoms/typography/styles.css +50 -0
- package/dist/components/atoms/video-player/index.d.ts +9 -0
- package/dist/components/atoms/video-player/index.js +17 -0
- package/dist/components/atoms/video-player/index.js.map +1 -0
- package/dist/components/atoms/video-player/styles.css +18 -0
- package/dist/components/atoms/visually-hidden/index.d.ts +7 -0
- package/dist/components/atoms/visually-hidden/index.js +13 -0
- package/dist/components/atoms/visually-hidden/index.js.map +1 -0
- package/dist/components/atoms/visually-hidden/styles.css +12 -0
- package/dist/components/hooks/index.d.ts +3 -0
- package/dist/components/hooks/index.js +4 -0
- package/dist/components/hooks/use-click-outside.d.ts +6 -0
- package/dist/components/hooks/use-click-outside.js +30 -0
- package/dist/components/hooks/use-click-outside.js.map +1 -0
- package/dist/components/hooks/use-controllable-state.d.ts +5 -0
- package/dist/components/hooks/use-controllable-state.js +22 -0
- package/dist/components/hooks/use-controllable-state.js.map +1 -0
- package/dist/components/hooks/use-focus-trap.d.ts +8 -0
- package/dist/components/hooks/use-focus-trap.js +57 -0
- package/dist/components/hooks/use-focus-trap.js.map +1 -0
- package/dist/components/index.d.ts +122 -0
- package/dist/components/index.js +74 -0
- package/dist/components/molecules/accordion/index.d.ts +27 -0
- package/dist/components/molecules/accordion/index.js +98 -0
- package/dist/components/molecules/accordion/index.js.map +1 -0
- package/dist/components/molecules/accordion/styles.css +75 -0
- package/dist/components/molecules/alert/index.d.ts +18 -0
- package/dist/components/molecules/alert/index.js +91 -0
- package/dist/components/molecules/alert/index.js.map +1 -0
- package/dist/components/molecules/alert/styles.css +97 -0
- package/dist/components/molecules/binary-clock/index.d.ts +16 -0
- package/dist/components/molecules/binary-clock/index.js +66 -0
- package/dist/components/molecules/binary-clock/index.js.map +1 -0
- package/dist/components/molecules/binary-clock/styles.css +46 -0
- package/dist/components/molecules/breadcrumb/index.d.ts +23 -0
- package/dist/components/molecules/breadcrumb/index.js +79 -0
- package/dist/components/molecules/breadcrumb/index.js.map +1 -0
- package/dist/components/molecules/breadcrumb/styles.css +61 -0
- package/dist/components/molecules/card/index.d.ts +39 -0
- package/dist/components/molecules/card/index.js +60 -0
- package/dist/components/molecules/card/index.js.map +1 -0
- package/dist/components/molecules/card/styles.css +105 -0
- package/dist/components/molecules/chart/index.d.ts +14 -0
- package/dist/components/molecules/chart/index.js +104 -0
- package/dist/components/molecules/chart/index.js.map +1 -0
- package/dist/components/molecules/conflict-resolver/components/conflictItem.d.ts +8 -0
- package/dist/components/molecules/conflict-resolver/components/conflictItem.js +58 -0
- package/dist/components/molecules/conflict-resolver/components/conflictItem.js.map +1 -0
- package/dist/components/molecules/conflict-resolver/components/styles.css +64 -0
- package/dist/components/molecules/conflict-resolver/index.d.ts +10 -0
- package/dist/components/molecules/conflict-resolver/index.js +35 -0
- package/dist/components/molecules/conflict-resolver/index.js.map +1 -0
- package/dist/components/molecules/conflict-resolver/types.d.ts +10 -0
- package/dist/components/molecules/dropable-list/draggable.d.ts +9 -0
- package/dist/components/molecules/dropable-list/draggable.js +18 -0
- package/dist/components/molecules/dropable-list/draggable.js.map +1 -0
- package/dist/components/molecules/dropable-list/index.d.ts +20 -0
- package/dist/components/molecules/dropable-list/index.js +52 -0
- package/dist/components/molecules/dropable-list/index.js.map +1 -0
- package/dist/components/molecules/dropable-list/styles.css +63 -0
- package/dist/components/molecules/dropdown-menu/index.d.ts +43 -0
- package/dist/components/molecules/dropdown-menu/index.js +226 -0
- package/dist/components/molecules/dropdown-menu/index.js.map +1 -0
- package/dist/components/molecules/dropdown-menu/styles.css +94 -0
- package/dist/components/molecules/empty-state/index.d.ts +16 -0
- package/dist/components/molecules/empty-state/index.js +32 -0
- package/dist/components/molecules/empty-state/index.js.map +1 -0
- package/dist/components/molecules/empty-state/styles.css +64 -0
- package/dist/components/molecules/file-input/index.d.ts +39 -0
- package/dist/components/molecules/file-input/index.js +121 -0
- package/dist/components/molecules/file-input/index.js.map +1 -0
- package/dist/components/molecules/file-input/styles.css +86 -0
- package/dist/components/molecules/form-field/index.d.ts +38 -0
- package/dist/components/molecules/form-field/index.js +71 -0
- package/dist/components/molecules/form-field/index.js.map +1 -0
- package/dist/components/molecules/form-field/styles.css +43 -0
- package/dist/components/molecules/list/index.d.ts +25 -0
- package/dist/components/molecules/list/index.js +30 -0
- package/dist/components/molecules/list/index.js.map +1 -0
- package/dist/components/molecules/list/styles.css +28 -0
- package/dist/components/molecules/number-stepper/index.d.ts +31 -0
- package/dist/components/molecules/number-stepper/index.js +163 -0
- package/dist/components/molecules/number-stepper/index.js.map +1 -0
- package/dist/components/molecules/number-stepper/styles.css +69 -0
- package/dist/components/molecules/pagination/index.d.ts +39 -0
- package/dist/components/molecules/pagination/index.js +111 -0
- package/dist/components/molecules/pagination/index.js.map +1 -0
- package/dist/components/molecules/pagination/styles.css +41 -0
- package/dist/components/molecules/popover/index.d.ts +22 -0
- package/dist/components/molecules/popover/index.js +76 -0
- package/dist/components/molecules/popover/index.js.map +1 -0
- package/dist/components/molecules/popover/positioning.d.ts +40 -0
- package/dist/components/molecules/popover/positioning.js +115 -0
- package/dist/components/molecules/popover/positioning.js.map +1 -0
- package/dist/components/molecules/popover/styles.css +36 -0
- package/dist/components/molecules/rating/index.d.ts +33 -0
- package/dist/components/molecules/rating/index.js +117 -0
- package/dist/components/molecules/rating/index.js.map +1 -0
- package/dist/components/molecules/rating/styles.css +94 -0
- package/dist/components/molecules/search-input/index.d.ts +23 -0
- package/dist/components/molecules/search-input/index.js +118 -0
- package/dist/components/molecules/search-input/index.js.map +1 -0
- package/dist/components/molecules/search-input/styles.css +69 -0
- package/dist/components/molecules/segmented-control/button-group.d.ts +10 -0
- package/dist/components/molecules/segmented-control/button-group.js +15 -0
- package/dist/components/molecules/segmented-control/button-group.js.map +1 -0
- package/dist/components/molecules/segmented-control/index.d.ts +38 -0
- package/dist/components/molecules/segmented-control/index.js +80 -0
- package/dist/components/molecules/segmented-control/index.js.map +1 -0
- package/dist/components/molecules/segmented-control/styles.css +130 -0
- package/dist/components/molecules/select/index.d.ts +30 -0
- package/dist/components/molecules/select/index.js +48 -0
- package/dist/components/molecules/select/index.js.map +1 -0
- package/dist/components/molecules/select/styles.css +102 -0
- package/dist/components/molecules/tabs/index.d.ts +31 -0
- package/dist/components/molecules/tabs/index.js +87 -0
- package/dist/components/molecules/tabs/index.js.map +1 -0
- package/dist/components/molecules/tabs/styles.css +87 -0
- package/dist/components/molecules/tag-cloud/index.d.ts +16 -0
- package/dist/components/molecules/tag-cloud/index.js +133 -0
- package/dist/components/molecules/tag-cloud/index.js.map +1 -0
- package/dist/components/molecules/tag-cloud/styles.css +29 -0
- package/dist/components/molecules/tooltip/index.d.ts +22 -0
- package/dist/components/molecules/tooltip/index.js +83 -0
- package/dist/components/molecules/tooltip/index.js.map +1 -0
- package/dist/components/molecules/tooltip/styles.css +30 -0
- package/dist/components/molecules/virtualized-list/index.d.ts +5 -0
- package/dist/components/molecules/virtualized-list/index.js +3 -0
- package/dist/components/molecules/virtualized-list/lista/index.d.ts +11 -0
- package/dist/components/molecules/virtualized-list/lista/index.js +30 -0
- package/dist/components/molecules/virtualized-list/lista/index.js.map +1 -0
- package/dist/components/molecules/virtualized-list/lista/styles.css +27 -0
- package/dist/components/molecules/virtualized-list/lista-slow/index.d.ts +10 -0
- package/dist/components/molecules/virtualized-list/lista-slow/index.js +17 -0
- package/dist/components/molecules/virtualized-list/lista-slow/index.js.map +1 -0
- package/dist/components/molecules/virtualized-list/row/index.d.ts +9 -0
- package/dist/components/molecules/virtualized-list/row/index.js +30 -0
- package/dist/components/molecules/virtualized-list/row/index.js.map +1 -0
- package/dist/components/molecules/virtualized-list/types.d.ts +4 -0
- package/dist/components/organisms/combobox/index.d.ts +82 -0
- package/dist/components/organisms/combobox/index.js +386 -0
- package/dist/components/organisms/combobox/index.js.map +1 -0
- package/dist/components/organisms/combobox/styles.css +269 -0
- package/dist/components/organisms/command-palette/index.d.ts +49 -0
- package/dist/components/organisms/command-palette/index.js +239 -0
- package/dist/components/organisms/command-palette/index.js.map +1 -0
- package/dist/components/organisms/command-palette/score.d.ts +12 -0
- package/dist/components/organisms/command-palette/score.js +43 -0
- package/dist/components/organisms/command-palette/score.js.map +1 -0
- package/dist/components/organisms/command-palette/styles.css +212 -0
- package/dist/components/organisms/date-picker/calendar.d.ts +29 -0
- package/dist/components/organisms/date-picker/calendar.js +178 -0
- package/dist/components/organisms/date-picker/calendar.js.map +1 -0
- package/dist/components/organisms/date-picker/date-utils.d.ts +52 -0
- package/dist/components/organisms/date-picker/date-utils.js +139 -0
- package/dist/components/organisms/date-picker/date-utils.js.map +1 -0
- package/dist/components/organisms/date-picker/index.d.ts +54 -0
- package/dist/components/organisms/date-picker/index.js +173 -0
- package/dist/components/organisms/date-picker/index.js.map +1 -0
- package/dist/components/organisms/date-picker/styles.css +179 -0
- package/dist/components/organisms/drawer/index.d.ts +16 -0
- package/dist/components/organisms/drawer/index.js +15 -0
- package/dist/components/organisms/drawer/index.js.map +1 -0
- package/dist/components/organisms/drawer/styles.css +163 -0
- package/dist/components/organisms/editor/index.d.ts +13 -0
- package/dist/components/organisms/editor/index.js +56 -0
- package/dist/components/organisms/editor/index.js.map +1 -0
- package/dist/components/organisms/editor/styles.css +61 -0
- package/dist/components/organisms/form/context.d.ts +7 -0
- package/dist/components/organisms/form/context.js +11 -0
- package/dist/components/organisms/form/context.js.map +1 -0
- package/dist/components/organisms/form/field.d.ts +18 -0
- package/dist/components/organisms/form/field.js +98 -0
- package/dist/components/organisms/form/field.js.map +1 -0
- package/dist/components/organisms/form/form-actions.d.ts +8 -0
- package/dist/components/organisms/form/form-actions.js +13 -0
- package/dist/components/organisms/form/form-actions.js.map +1 -0
- package/dist/components/organisms/form/index.d.ts +24 -0
- package/dist/components/organisms/form/index.js +215 -0
- package/dist/components/organisms/form/index.js.map +1 -0
- package/dist/components/organisms/form/styles.css +29 -0
- package/dist/components/organisms/form/types.d.ts +95 -0
- package/dist/components/organisms/form/validation.d.ts +5 -0
- package/dist/components/organisms/form/validation.js +35 -0
- package/dist/components/organisms/form/validation.js.map +1 -0
- package/dist/components/organisms/header/index.d.ts +40 -0
- package/dist/components/organisms/header/index.js +186 -0
- package/dist/components/organisms/header/index.js.map +1 -0
- package/dist/components/organisms/header/styles.css +126 -0
- package/dist/components/organisms/modal/dialog-base.d.ts +31 -0
- package/dist/components/organisms/modal/dialog-base.js +123 -0
- package/dist/components/organisms/modal/dialog-base.js.map +1 -0
- package/dist/components/organisms/modal/index.d.ts +13 -0
- package/dist/components/organisms/modal/index.js +15 -0
- package/dist/components/organisms/modal/index.js.map +1 -0
- package/dist/components/organisms/modal/styles.css +143 -0
- package/dist/components/organisms/presenter/components/slides/simple/index.d.ts +13 -0
- package/dist/components/organisms/presenter/components/slides/simple/index.js +16 -0
- package/dist/components/organisms/presenter/components/slides/simple/index.js.map +1 -0
- package/dist/components/organisms/presenter/components/slides/simple/styles.css +6 -0
- package/dist/components/organisms/presenter/index.d.ts +17 -0
- package/dist/components/organisms/presenter/index.js +63 -0
- package/dist/components/organisms/presenter/index.js.map +1 -0
- package/dist/components/organisms/presenter/styles.css +75 -0
- package/dist/components/organisms/sidebar/index.d.ts +50 -0
- package/dist/components/organisms/sidebar/index.js +157 -0
- package/dist/components/organisms/sidebar/index.js.map +1 -0
- package/dist/components/organisms/sidebar/styles.css +197 -0
- package/dist/components/organisms/slider/index.d.ts +47 -0
- package/dist/components/organisms/slider/index.js +214 -0
- package/dist/components/organisms/slider/index.js.map +1 -0
- package/dist/components/organisms/slider/styles.css +110 -0
- package/dist/components/organisms/table/components/cell/index.d.ts +8 -0
- package/dist/components/organisms/table/components/cell/index.js +11 -0
- package/dist/components/organisms/table/components/cell/index.js.map +1 -0
- package/dist/components/organisms/table/index.d.ts +29 -0
- package/dist/components/organisms/table/index.js +105 -0
- package/dist/components/organisms/table/index.js.map +1 -0
- package/dist/components/organisms/table/styles.css +66 -0
- package/dist/components/organisms/terminal/index.d.ts +15 -0
- package/dist/components/organisms/terminal/index.js +111 -0
- package/dist/components/organisms/terminal/index.js.map +1 -0
- package/dist/components/organisms/terminal/styles.css +123 -0
- package/dist/components/organisms/toast/index.d.ts +66 -0
- package/dist/components/organisms/toast/index.js +142 -0
- package/dist/components/organisms/toast/index.js.map +1 -0
- package/dist/components/organisms/toast/styles.css +138 -0
- package/dist/components/organisms/wizard/index.d.ts +44 -0
- package/dist/components/organisms/wizard/index.js +171 -0
- package/dist/components/organisms/wizard/index.js.map +1 -0
- package/dist/components/organisms/wizard/styles.css +160 -0
- package/dist/components/utils/cx.d.ts +2 -0
- package/dist/components/utils/cx.js +8 -0
- package/dist/components/utils/cx.js.map +1 -0
- package/dist/components/utils/portal/index.d.ts +9 -0
- package/dist/components/utils/portal/index.js +13 -0
- package/dist/components/utils/portal/index.js.map +1 -0
- package/dist/components/utils/theme-provider/index.d.ts +22 -0
- package/dist/components/utils/theme-provider/index.js +36 -0
- package/dist/components/utils/theme-provider/index.js.map +1 -0
- package/dist/styles.css +743 -0
- package/dist/tokens.css +299 -0
- package/package.json +390 -4
- package/README.md +0 -3
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import { useControllableState } from "../../hooks/use-controllable-state.js";
|
|
3
|
+
import { cx } from "../../utils/cx.js";
|
|
4
|
+
import './styles.css';/* empty css */
|
|
5
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/components/molecules/rating/index.tsx
|
|
7
|
+
var defaultStarLabel = (n) => `${n} ${n === 1 ? "star" : "stars"}`;
|
|
8
|
+
var defaultReadOnlyLabel = (value, max) => `Rated ${value} out of ${max}`;
|
|
9
|
+
var Star = () => /* @__PURE__ */ jsx("svg", {
|
|
10
|
+
viewBox: "0 0 24 24",
|
|
11
|
+
"aria-hidden": "true",
|
|
12
|
+
focusable: "false",
|
|
13
|
+
children: /* @__PURE__ */ jsx("path", {
|
|
14
|
+
d: "M12 2.5l2.94 5.96 6.56.95-4.75 4.63 1.12 6.54L12 17.5l-5.87 3.08 1.12-6.54L2.5 9.41l6.56-.95L12 2.5z",
|
|
15
|
+
fill: "currentColor"
|
|
16
|
+
})
|
|
17
|
+
});
|
|
18
|
+
/** Star rating: an accessible radio group when interactive, an image when read-only. */
|
|
19
|
+
function Rating({ value, defaultValue = 0, onChange, max = 5, allowHalf = false, readOnly = false, disabled = false, clearable = true, invalid = false, size = "md", label, getStarLabel = defaultStarLabel, getReadOnlyLabel = defaultReadOnlyLabel, className, onKeyDown, ...rest }) {
|
|
20
|
+
const [current, setCurrent] = useControllableState(value, defaultValue, onChange);
|
|
21
|
+
const [hover, setHover] = useState(null);
|
|
22
|
+
const refs = useRef(/* @__PURE__ */ new Map());
|
|
23
|
+
const step = allowHalf ? .5 : 1;
|
|
24
|
+
const shown = hover ?? current;
|
|
25
|
+
const stars = Array.from({ length: max }, (_, i) => i + 1);
|
|
26
|
+
const fillOf = (star) => shown >= star ? "full" : shown >= star - .5 ? "half" : "empty";
|
|
27
|
+
const renderStar = (star) => /* @__PURE__ */ jsxs("span", {
|
|
28
|
+
className: "ds-rating__star",
|
|
29
|
+
"data-fill": fillOf(star),
|
|
30
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
31
|
+
className: "ds-rating__empty",
|
|
32
|
+
children: /* @__PURE__ */ jsx(Star, {})
|
|
33
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
34
|
+
className: "ds-rating__filled",
|
|
35
|
+
children: /* @__PURE__ */ jsx(Star, {})
|
|
36
|
+
})]
|
|
37
|
+
});
|
|
38
|
+
const classes = cx("ds-rating", `ds-rating--${size}`, readOnly && "ds-rating--readonly", invalid && "ds-rating--invalid", disabled && "ds-rating--disabled", className);
|
|
39
|
+
if (readOnly) return /* @__PURE__ */ jsx("div", {
|
|
40
|
+
role: "img",
|
|
41
|
+
"aria-label": getReadOnlyLabel(current, max),
|
|
42
|
+
className: classes,
|
|
43
|
+
...rest,
|
|
44
|
+
children: stars.map((star) => /* @__PURE__ */ jsx("span", {
|
|
45
|
+
className: "ds-rating__item",
|
|
46
|
+
children: renderStar(star)
|
|
47
|
+
}, star))
|
|
48
|
+
});
|
|
49
|
+
const choose = (next) => {
|
|
50
|
+
if (disabled) return;
|
|
51
|
+
setCurrent(clearable && next === current ? 0 : next);
|
|
52
|
+
};
|
|
53
|
+
const moveTo = (next) => {
|
|
54
|
+
const clamped = Math.min(max, Math.max(step, next));
|
|
55
|
+
refs.current.get(clamped)?.focus();
|
|
56
|
+
setCurrent(clamped);
|
|
57
|
+
};
|
|
58
|
+
const handleKeyDown = (event) => {
|
|
59
|
+
onKeyDown?.(event);
|
|
60
|
+
if (event.defaultPrevented || disabled) return;
|
|
61
|
+
switch (event.key) {
|
|
62
|
+
case "ArrowRight":
|
|
63
|
+
case "ArrowUp":
|
|
64
|
+
moveTo(current + step);
|
|
65
|
+
break;
|
|
66
|
+
case "ArrowLeft":
|
|
67
|
+
case "ArrowDown":
|
|
68
|
+
moveTo(current - step);
|
|
69
|
+
break;
|
|
70
|
+
case "Home":
|
|
71
|
+
moveTo(step);
|
|
72
|
+
break;
|
|
73
|
+
case "End":
|
|
74
|
+
moveTo(max);
|
|
75
|
+
break;
|
|
76
|
+
case " ":
|
|
77
|
+
case "Enter": {
|
|
78
|
+
const focused = [...refs.current.entries()].find(([, el]) => el === document.activeElement);
|
|
79
|
+
if (focused) choose(focused[0]);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
default: return;
|
|
83
|
+
}
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
};
|
|
86
|
+
const tabbable = current > 0 && current <= max && Number.isInteger(current / step) ? current : step;
|
|
87
|
+
const option = (optionValue, half) => /* @__PURE__ */ jsx("span", {
|
|
88
|
+
ref: (el) => {
|
|
89
|
+
refs.current.set(optionValue, el);
|
|
90
|
+
},
|
|
91
|
+
role: "radio",
|
|
92
|
+
"aria-checked": optionValue === current,
|
|
93
|
+
"aria-label": getStarLabel(optionValue),
|
|
94
|
+
"aria-disabled": disabled || void 0,
|
|
95
|
+
tabIndex: disabled ? void 0 : optionValue === tabbable ? 0 : -1,
|
|
96
|
+
className: cx("ds-rating__option", half && `ds-rating__option--${half}`),
|
|
97
|
+
onClick: () => choose(optionValue),
|
|
98
|
+
onMouseEnter: () => !disabled && setHover(optionValue)
|
|
99
|
+
}, optionValue);
|
|
100
|
+
return /* @__PURE__ */ jsx("div", {
|
|
101
|
+
role: "radiogroup",
|
|
102
|
+
"aria-label": label,
|
|
103
|
+
"aria-disabled": disabled || void 0,
|
|
104
|
+
"aria-invalid": invalid || void 0,
|
|
105
|
+
className: classes,
|
|
106
|
+
onKeyDown: handleKeyDown,
|
|
107
|
+
onMouseLeave: () => setHover(null),
|
|
108
|
+
...rest,
|
|
109
|
+
children: stars.map((star) => /* @__PURE__ */ jsxs("span", {
|
|
110
|
+
className: "ds-rating__item",
|
|
111
|
+
children: [renderStar(star), allowHalf ? /* @__PURE__ */ jsxs(Fragment$1, { children: [option(star - .5, "left"), option(star, "right")] }) : option(star)]
|
|
112
|
+
}, star))
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export { Rating as default };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_jsx","_jsxs","_Fragment"],"sources":["../../../../src/components/molecules/rating/index.tsx"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport type { HTMLAttributes, KeyboardEvent } from \"react\";\nimport { useControllableState } from \"../../hooks\";\nimport { cx } from \"../../utils/cx\";\nimport \"./styles.css\";\n\nexport type RatingSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface RatingProps extends Omit<HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n /** Current rating (controlled). 0 means no rating */\n value?: number;\n /** Initial rating (uncontrolled) */\n defaultValue?: number;\n /** Called with the new rating (0 when cleared) */\n onChange?: (value: number) => void;\n /** Number of stars */\n max?: number;\n /** Allow half-star values */\n allowHalf?: boolean;\n /** Display only: rendered as an image with an accessible description */\n readOnly?: boolean;\n /** Disables interaction and removes the stars from the tab order */\n disabled?: boolean;\n /** Clicking the selected star again clears the rating */\n clearable?: boolean;\n /** Star size, from the icon-size tokens */\n size?: RatingSize;\n /** Marks the rating as invalid (sets `aria-invalid` on the group) */\n invalid?: boolean;\n /** Accessible name of the group (e.g. \"Hotel rating\") */\n label?: string;\n /** Accessible label of each option. Default `(n) => n + \" stars\"` (\"1 star\" for one) */\n getStarLabel?: (value: number) => string;\n /** Accessible label in read-only mode. Default `(v, max) => \"Rated \" + v + \" out of \" + max` */\n getReadOnlyLabel?: (value: number, max: number) => string;\n}\n\nconst defaultStarLabel = (n: number) => `${n} ${n === 1 ? \"star\" : \"stars\"}`;\nconst defaultReadOnlyLabel = (value: number, max: number) => `Rated ${value} out of ${max}`;\n\nconst Star = () => (\n <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\" focusable=\"false\">\n <path\n d=\"M12 2.5l2.94 5.96 6.56.95-4.75 4.63 1.12 6.54L12 17.5l-5.87 3.08 1.12-6.54L2.5 9.41l6.56-.95L12 2.5z\"\n fill=\"currentColor\"\n />\n </svg>\n);\n\n/** Star rating: an accessible radio group when interactive, an image when read-only. */\nfunction Rating({\n value,\n defaultValue = 0,\n onChange,\n max = 5,\n allowHalf = false,\n readOnly = false,\n disabled = false,\n clearable = true,\n invalid = false,\n size = \"md\",\n label,\n getStarLabel = defaultStarLabel,\n getReadOnlyLabel = defaultReadOnlyLabel,\n className,\n onKeyDown,\n ...rest\n}: RatingProps) {\n const [current, setCurrent] = useControllableState<number>(value, defaultValue, onChange);\n const [hover, setHover] = useState<number | null>(null);\n const refs = useRef<Map<number, HTMLSpanElement | null>>(new Map());\n const step = allowHalf ? 0.5 : 1;\n const shown = hover ?? current;\n const stars = Array.from({ length: max }, (_, i) => i + 1);\n\n const fillOf = (star: number) => (shown >= star ? \"full\" : shown >= star - 0.5 ? \"half\" : \"empty\");\n\n const renderStar = (star: number) => (\n <span className=\"ds-rating__star\" data-fill={fillOf(star)}>\n <span className=\"ds-rating__empty\">\n <Star />\n </span>\n <span className=\"ds-rating__filled\">\n <Star />\n </span>\n </span>\n );\n\n const classes = cx(\n \"ds-rating\",\n `ds-rating--${size}`,\n readOnly && \"ds-rating--readonly\",\n invalid && \"ds-rating--invalid\",\n disabled && \"ds-rating--disabled\",\n className\n );\n\n if (readOnly) {\n return (\n <div role=\"img\" aria-label={getReadOnlyLabel(current, max)} className={classes} {...rest}>\n {stars.map((star) => (\n <span key={star} className=\"ds-rating__item\">\n {renderStar(star)}\n </span>\n ))}\n </div>\n );\n }\n\n const choose = (next: number) => {\n if (disabled) return;\n setCurrent(clearable && next === current ? 0 : next);\n };\n\n const moveTo = (next: number) => {\n const clamped = Math.min(max, Math.max(step, next));\n refs.current.get(clamped)?.focus();\n setCurrent(clamped);\n };\n\n const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n onKeyDown?.(event);\n if (event.defaultPrevented || disabled) return;\n switch (event.key) {\n case \"ArrowRight\":\n case \"ArrowUp\":\n moveTo(current + step);\n break;\n case \"ArrowLeft\":\n case \"ArrowDown\":\n moveTo(current - step);\n break;\n case \"Home\":\n moveTo(step);\n break;\n case \"End\":\n moveTo(max);\n break;\n case \" \":\n case \"Enter\": {\n const focused = [...refs.current.entries()].find(([, el]) => el === document.activeElement);\n if (focused) choose(focused[0]);\n break;\n }\n default:\n return;\n }\n event.preventDefault();\n };\n\n // Roving tabindex: the checked option, or the first one when no option matches (no rating, off-grid value).\n const isOption = current > 0 && current <= max && Number.isInteger(current / step);\n const tabbable = isOption ? current : step;\n\n const option = (optionValue: number, half?: \"left\" | \"right\") => (\n <span\n key={optionValue}\n ref={(el) => {\n refs.current.set(optionValue, el);\n }}\n role=\"radio\"\n aria-checked={optionValue === current}\n aria-label={getStarLabel(optionValue)}\n aria-disabled={disabled || undefined}\n tabIndex={disabled ? undefined : optionValue === tabbable ? 0 : -1}\n className={cx(\"ds-rating__option\", half && `ds-rating__option--${half}`)}\n onClick={() => choose(optionValue)}\n onMouseEnter={() => !disabled && setHover(optionValue)}\n />\n );\n\n return (\n <div\n role=\"radiogroup\"\n aria-label={label}\n aria-disabled={disabled || undefined}\n aria-invalid={invalid || undefined}\n className={classes}\n onKeyDown={handleKeyDown}\n onMouseLeave={() => setHover(null)}\n {...rest}\n >\n {stars.map((star) => (\n <span key={star} className=\"ds-rating__item\">\n {renderStar(star)}\n {allowHalf ? (\n <>\n {option(star - 0.5, \"left\")}\n {option(star, \"right\")}\n </>\n ) : (\n option(star)\n )}\n </span>\n ))}\n </div>\n );\n}\n\nexport default Rating;\n"],"mappings":";;;;;;AAqCA,IAAM,oBAAoB,MAAc,GAAG,EAAE,GAAG,MAAM,IAAI,SAAS;AACnE,IAAM,wBAAwB,OAAe,QAAgB,SAAS,MAAM,UAAU;AAEtF,IAAM,aACJA,oBAAC,OAAD;CAAK,SAAQ;CAAY,eAAY;CAAO,WAAU;CACpD,UAAAA,oBAAC,QAAD;EACE,GAAE;EACF,MAAK;CACN,CAAA;AACE,CAAA;;AAIP,SAAS,OAAO,EACd,OACA,eAAe,GACf,UACA,MAAM,GACN,YAAY,OACZ,WAAW,OACX,WAAW,OACX,YAAY,MACZ,UAAU,OACV,OAAO,MACP,OACA,eAAe,kBACf,mBAAmB,sBACnB,WACA,WACA,GAAG,QACW;CACd,MAAM,CAAC,SAAS,cAAc,qBAA6B,OAAO,cAAc,QAAQ;CACxF,MAAM,CAAC,OAAO,YAAY,SAAwB,IAAI;CACtD,MAAM,OAAO,uBAA4C,IAAI,IAAI,CAAC;CAClE,MAAM,OAAO,YAAY,KAAM;CAC/B,MAAM,QAAQ,SAAS;CACvB,MAAM,QAAQ,MAAM,KAAK,EAAE,QAAQ,IAAI,IAAI,GAAG,MAAM,IAAI,CAAC;CA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,CAAC,CAAC,MAAM,GAAG,QAAQ,OAAO,SAAS,aAAa;IAC1F,IAAI,SAAS,OAAO,QAAQ,EAAE;IAC9B;GACF;GACA,SACE;EACJ;EACA,MAAM,eAAe;CACvB;CAIA,MAAM,WADW,UAAU,KAAK,WAAW,OAAO,OAAO,UAAU,UAAU,IAAI,IACrD,UAAU;CAEtC,MAAM,UAAU,aAAqB,SACnCA,oBAAC,QAAD;EAEE,MAAM,OAAO;GACX,KAAK,QAAQ,IAAI,aAAa,EAAE;EAClC;EACA,MAAK;EACL,gBAAc,gBAAgB;EAC9B,cAAY,aAAa,WAAW;EACpC,iBAAe,YAAY,KAAA;EAC3B,UAAU,WAAW,KAAA,IAAY,gBAAgB,WAAW,IAAI;EAChE,WAAW,GAAG,qBAAqB,QAAQ,sBAAsB,MAAM;EACvE,eAAe,OAAO,WAAW;EACjC,oBAAoB,CAAC,YAAY,SAAS,WAAW;CACtD,GAZM,WAYN;CAGH,OACEA,oBAAC,OAAD;EACE,MAAK;EACL,cAAY;EACZ,iBAAe,YAAY,KAAA;EAC3B,gBAAc,WAAW,KAAA;EACzB,WAAW;EACX,WAAW;EACX,oBAAoB,SAAS,IAAI;EACjC,GAAI;EAEH,UAAA,MAAM,KAAK,SACVC,qBAAC,QAAD;GAAiB,WAAU;GAA3B,UAAA,CACG,WAAW,IAAI,GACf,YACCA,qBAAAC,YAAA,EAAA,UAAA,CACG,OAAO,OAAO,IAAK,MAAM,GACzB,OAAO,MAAM,OAAO,CACrB,EAAA,CAAA,IAEF,OAAO,IAAI,CAET;EAVK,GAAA,IAUL,CACP;CACE,CAAA;AAET"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
.ds-rating {
|
|
2
|
+
display: inline-flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
gap: var(--space-1);
|
|
5
|
+
font-size: var(--icon-size-md);
|
|
6
|
+
line-height: 1;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.ds-rating--sm {
|
|
10
|
+
font-size: var(--icon-size-sm);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.ds-rating--lg {
|
|
14
|
+
font-size: var(--icon-size-lg);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.ds-rating__item {
|
|
18
|
+
position: relative;
|
|
19
|
+
display: inline-flex;
|
|
20
|
+
width: 1em;
|
|
21
|
+
height: 1em;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.ds-rating__star {
|
|
25
|
+
position: relative;
|
|
26
|
+
display: inline-flex;
|
|
27
|
+
width: 100%;
|
|
28
|
+
height: 100%;
|
|
29
|
+
pointer-events: none;
|
|
30
|
+
transition: transform var(--duration-fast) var(--easing-standard);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.ds-rating__star svg {
|
|
34
|
+
display: block;
|
|
35
|
+
width: 1em;
|
|
36
|
+
height: 1em;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.ds-rating__empty {
|
|
40
|
+
color: var(--color-border-strong);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.ds-rating__filled {
|
|
44
|
+
position: absolute;
|
|
45
|
+
inset: 0;
|
|
46
|
+
overflow: hidden;
|
|
47
|
+
width: 0;
|
|
48
|
+
color: var(--color-warning);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.ds-rating__star[data-fill="half"] .ds-rating__filled {
|
|
52
|
+
width: 50%;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.ds-rating__star[data-fill="full"] .ds-rating__filled {
|
|
56
|
+
width: 100%;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* Interactive hit areas (radios) laid over each star */
|
|
60
|
+
.ds-rating__option {
|
|
61
|
+
position: absolute;
|
|
62
|
+
inset: 0;
|
|
63
|
+
border-radius: var(--radius-sm);
|
|
64
|
+
cursor: pointer;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ds-rating__option--left {
|
|
68
|
+
right: 50%;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.ds-rating__option--right {
|
|
72
|
+
left: 50%;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.ds-rating__option:focus-visible {
|
|
76
|
+
outline: var(--border-width-2) solid var(--color-focus-ring);
|
|
77
|
+
outline-offset: 2px;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.ds-rating:not(.ds-rating--readonly):not(.ds-rating--disabled) .ds-rating__item:hover .ds-rating__star {
|
|
81
|
+
transform: scale(1.1);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.ds-rating--invalid .ds-rating__empty {
|
|
85
|
+
color: var(--color-danger);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.ds-rating--disabled {
|
|
89
|
+
opacity: 0.5;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.ds-rating--disabled .ds-rating__option {
|
|
93
|
+
cursor: not-allowed;
|
|
94
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TextInputProps } from "../../atoms/text-input";
|
|
2
|
+
export interface SearchInputProps extends Omit<TextInputProps, "value" | "defaultValue" | "onChange" | "type" | "startIcon" | "endIcon"> {
|
|
3
|
+
/** Current text (controlled) */
|
|
4
|
+
value?: string;
|
|
5
|
+
/** Initial text (uncontrolled) */
|
|
6
|
+
defaultValue?: string;
|
|
7
|
+
/** Called with the new text on every change, including clearing */
|
|
8
|
+
onChange?: (value: string) => void;
|
|
9
|
+
/** Called with the text when the user presses Enter, and after `debounce` ms of inactivity when set */
|
|
10
|
+
onSearch?: (value: string) => void;
|
|
11
|
+
/** Called after the clear button or Escape empties the field */
|
|
12
|
+
onClear?: () => void;
|
|
13
|
+
/** When set, calls `onSearch` this many milliseconds after the user stops typing */
|
|
14
|
+
debounce?: number;
|
|
15
|
+
/** Shows a spinner while results load */
|
|
16
|
+
loading?: boolean;
|
|
17
|
+
/** Accessible label of the clear button. Default "Clear search" */
|
|
18
|
+
clearLabel?: string;
|
|
19
|
+
/** Accessible text of the loading spinner. Default "Searching" */
|
|
20
|
+
loadingLabel?: string;
|
|
21
|
+
}
|
|
22
|
+
declare const SearchInput: import("react").ForwardRefExoticComponent<SearchInputProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
23
|
+
export default SearchInput;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useRef } from "react";
|
|
2
|
+
import TextInput from "../../atoms/text-input/index.js";
|
|
3
|
+
import Spinner from "../../atoms/spinner/index.js";
|
|
4
|
+
import { useControllableState } from "../../hooks/use-controllable-state.js";
|
|
5
|
+
import { cx } from "../../utils/cx.js";
|
|
6
|
+
import './styles.css';/* empty css */
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
// src/components/molecules/search-input/index.tsx
|
|
9
|
+
var SearchIcon = () => /* @__PURE__ */ jsxs("svg", {
|
|
10
|
+
viewBox: "0 0 20 20",
|
|
11
|
+
fill: "none",
|
|
12
|
+
width: "1em",
|
|
13
|
+
height: "1em",
|
|
14
|
+
"aria-hidden": "true",
|
|
15
|
+
focusable: "false",
|
|
16
|
+
children: [/* @__PURE__ */ jsx("circle", {
|
|
17
|
+
cx: "9",
|
|
18
|
+
cy: "9",
|
|
19
|
+
r: "5.5",
|
|
20
|
+
stroke: "currentColor",
|
|
21
|
+
strokeWidth: "1.6"
|
|
22
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
23
|
+
d: "M13 13l4 4",
|
|
24
|
+
stroke: "currentColor",
|
|
25
|
+
strokeWidth: "1.6",
|
|
26
|
+
strokeLinecap: "round"
|
|
27
|
+
})]
|
|
28
|
+
});
|
|
29
|
+
var SearchInput = forwardRef(function SearchInput({ value, defaultValue = "", onChange, onSearch, onClear, debounce, loading = false, clearLabel = "Clear search", loadingLabel = "Searching", size = "md", className, inputClassName, disabled, readOnly, onKeyDown, ...rest }, forwardedRef) {
|
|
30
|
+
const [current, setCurrent] = useControllableState(value, defaultValue, onChange);
|
|
31
|
+
const inputRef = useRef(null);
|
|
32
|
+
const onSearchRef = useRef(onSearch);
|
|
33
|
+
onSearchRef.current = onSearch;
|
|
34
|
+
const userChangedRef = useRef(false);
|
|
35
|
+
const timerRef = useRef(void 0);
|
|
36
|
+
const setRef = useCallback((node) => {
|
|
37
|
+
inputRef.current = node;
|
|
38
|
+
if (typeof forwardedRef === "function") forwardedRef(node);
|
|
39
|
+
else if (forwardedRef) forwardedRef.current = node;
|
|
40
|
+
}, [forwardedRef]);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (debounce == null || !userChangedRef.current) return;
|
|
43
|
+
userChangedRef.current = false;
|
|
44
|
+
timerRef.current = setTimeout(() => onSearchRef.current?.(current), debounce);
|
|
45
|
+
return () => clearTimeout(timerRef.current);
|
|
46
|
+
}, [current, debounce]);
|
|
47
|
+
const handleChange = (event) => {
|
|
48
|
+
userChangedRef.current = true;
|
|
49
|
+
setCurrent(event.target.value);
|
|
50
|
+
};
|
|
51
|
+
const clear = () => {
|
|
52
|
+
userChangedRef.current = true;
|
|
53
|
+
setCurrent("");
|
|
54
|
+
onClear?.();
|
|
55
|
+
inputRef.current?.focus();
|
|
56
|
+
};
|
|
57
|
+
const handleKeyDown = (event) => {
|
|
58
|
+
onKeyDown?.(event);
|
|
59
|
+
if (event.defaultPrevented) return;
|
|
60
|
+
if (event.key === "Enter") {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
clearTimeout(timerRef.current);
|
|
63
|
+
onSearchRef.current?.(current);
|
|
64
|
+
} else if (event.key === "Escape" && current !== "" && !readOnly) {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
event.stopPropagation();
|
|
67
|
+
clear();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const showClear = current !== "" && !disabled && !readOnly;
|
|
71
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
72
|
+
className: cx("ds-search-input", `ds-search-input--${size}`, rest.fullWidth && "ds-search-input--full", className),
|
|
73
|
+
children: [/* @__PURE__ */ jsx(TextInput, {
|
|
74
|
+
ref: setRef,
|
|
75
|
+
type: "search",
|
|
76
|
+
role: "searchbox",
|
|
77
|
+
size,
|
|
78
|
+
startIcon: /* @__PURE__ */ jsx(SearchIcon, {}),
|
|
79
|
+
className: "ds-search-input__control",
|
|
80
|
+
inputClassName: cx("ds-search-input__field", inputClassName),
|
|
81
|
+
value: current,
|
|
82
|
+
onChange: handleChange,
|
|
83
|
+
onKeyDown: handleKeyDown,
|
|
84
|
+
disabled,
|
|
85
|
+
readOnly,
|
|
86
|
+
"aria-busy": loading || void 0,
|
|
87
|
+
...rest
|
|
88
|
+
}), (loading || showClear) && /* @__PURE__ */ jsxs("span", {
|
|
89
|
+
className: "ds-search-input__actions",
|
|
90
|
+
children: [loading && /* @__PURE__ */ jsx(Spinner, {
|
|
91
|
+
size: "sm",
|
|
92
|
+
label: loadingLabel
|
|
93
|
+
}), showClear && /* @__PURE__ */ jsx("button", {
|
|
94
|
+
type: "button",
|
|
95
|
+
className: "ds-search-input__clear",
|
|
96
|
+
"aria-label": clearLabel,
|
|
97
|
+
onClick: clear,
|
|
98
|
+
children: /* @__PURE__ */ jsx("svg", {
|
|
99
|
+
viewBox: "0 0 16 16",
|
|
100
|
+
fill: "none",
|
|
101
|
+
width: "1em",
|
|
102
|
+
height: "1em",
|
|
103
|
+
"aria-hidden": "true",
|
|
104
|
+
focusable: "false",
|
|
105
|
+
children: /* @__PURE__ */ jsx("path", {
|
|
106
|
+
d: "M4 4l8 8M12 4l-8 8",
|
|
107
|
+
stroke: "currentColor",
|
|
108
|
+
strokeWidth: "1.6",
|
|
109
|
+
strokeLinecap: "round"
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
})]
|
|
113
|
+
})]
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
export { SearchInput as default };
|
|
117
|
+
|
|
118
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_jsxs","_jsx"],"sources":["../../../../src/components/molecules/search-input/index.tsx"],"sourcesContent":["import { forwardRef, useCallback, useEffect, useRef } from \"react\";\nimport type { ChangeEvent, KeyboardEvent } from \"react\";\nimport TextInput from \"../../atoms/text-input\";\nimport type { TextInputProps } from \"../../atoms/text-input\";\nimport Spinner from \"../../atoms/spinner\";\nimport { useControllableState } from \"../../hooks\";\nimport { cx } from \"../../utils/cx\";\nimport \"./styles.css\";\n\nexport interface SearchInputProps\n extends Omit<TextInputProps, \"value\" | \"defaultValue\" | \"onChange\" | \"type\" | \"startIcon\" | \"endIcon\"> {\n /** Current text (controlled) */\n value?: string;\n /** Initial text (uncontrolled) */\n defaultValue?: string;\n /** Called with the new text on every change, including clearing */\n onChange?: (value: string) => void;\n /** Called with the text when the user presses Enter, and after `debounce` ms of inactivity when set */\n onSearch?: (value: string) => void;\n /** Called after the clear button or Escape empties the field */\n onClear?: () => void;\n /** When set, calls `onSearch` this many milliseconds after the user stops typing */\n debounce?: number;\n /** Shows a spinner while results load */\n loading?: boolean;\n /** Accessible label of the clear button. Default \"Clear search\" */\n clearLabel?: string;\n /** Accessible text of the loading spinner. Default \"Searching\" */\n loadingLabel?: string;\n}\n\nconst SearchIcon = () => (\n <svg viewBox=\"0 0 20 20\" fill=\"none\" width=\"1em\" height=\"1em\" aria-hidden=\"true\" focusable=\"false\">\n <circle cx=\"9\" cy=\"9\" r=\"5.5\" stroke=\"currentColor\" strokeWidth=\"1.6\" />\n <path d=\"M13 13l4 4\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n </svg>\n);\n\nconst SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(function SearchInput(\n {\n value,\n defaultValue = \"\",\n onChange,\n onSearch,\n onClear,\n debounce,\n loading = false,\n clearLabel = \"Clear search\",\n loadingLabel = \"Searching\",\n size = \"md\",\n className,\n inputClassName,\n disabled,\n readOnly,\n onKeyDown,\n ...rest\n },\n forwardedRef\n) {\n const [current, setCurrent] = useControllableState<string>(value, defaultValue, onChange);\n const inputRef = useRef<HTMLInputElement | null>(null);\n const onSearchRef = useRef(onSearch);\n onSearchRef.current = onSearch;\n // Only user-driven changes (typing, clearing) start the debounce timer, not mount or prop updates.\n const userChangedRef = useRef(false);\n const timerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n\n const setRef = useCallback(\n (node: HTMLInputElement | null) => {\n inputRef.current = node;\n if (typeof forwardedRef === \"function\") forwardedRef(node);\n else if (forwardedRef) forwardedRef.current = node;\n },\n [forwardedRef]\n );\n\n useEffect(() => {\n if (debounce == null || !userChangedRef.current) return;\n userChangedRef.current = false;\n timerRef.current = setTimeout(() => onSearchRef.current?.(current), debounce);\n return () => clearTimeout(timerRef.current);\n }, [current, debounce]);\n\n const handleChange = (event: ChangeEvent<HTMLInputElement>) => {\n userChangedRef.current = true;\n setCurrent(event.target.value);\n };\n\n const clear = () => {\n userChangedRef.current = true;\n setCurrent(\"\");\n onClear?.();\n inputRef.current?.focus();\n };\n\n const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {\n onKeyDown?.(event);\n if (event.defaultPrevented) return;\n if (event.key === \"Enter\") {\n event.preventDefault();\n clearTimeout(timerRef.current);\n onSearchRef.current?.(current);\n } else if (event.key === \"Escape\" && current !== \"\" && !readOnly) {\n // Stop here so an enclosing dialog or popover does not close too.\n event.preventDefault();\n event.stopPropagation();\n clear();\n }\n };\n\n const showClear = current !== \"\" && !disabled && !readOnly;\n\n return (\n <span className={cx(\"ds-search-input\", `ds-search-input--${size}`, rest.fullWidth && \"ds-search-input--full\", className)}>\n <TextInput\n ref={setRef}\n type=\"search\"\n role=\"searchbox\"\n size={size}\n startIcon={<SearchIcon />}\n className=\"ds-search-input__control\"\n inputClassName={cx(\"ds-search-input__field\", inputClassName)}\n value={current}\n onChange={handleChange}\n onKeyDown={handleKeyDown}\n disabled={disabled}\n readOnly={readOnly}\n aria-busy={loading || undefined}\n {...rest}\n />\n {(loading || showClear) && (\n <span className=\"ds-search-input__actions\">\n {loading && <Spinner size=\"sm\" label={loadingLabel} />}\n {showClear && (\n <button type=\"button\" className=\"ds-search-input__clear\" aria-label={clearLabel} onClick={clear}>\n <svg viewBox=\"0 0 16 16\" fill=\"none\" width=\"1em\" height=\"1em\" aria-hidden=\"true\" focusable=\"false\">\n <path d=\"M4 4l8 8M12 4l-8 8\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n </svg>\n </button>\n )}\n </span>\n )}\n </span>\n );\n});\n\nexport default SearchInput;\n"],"mappings":";;;;;;;;AA+BA,IAAM,mBACJA,qBAAC,OAAD;CAAK,SAAQ;CAAY,MAAK;CAAO,OAAM;CAAM,QAAO;CAAM,eAAY;CAAO,WAAU;CAA3F,UAAA,CACEC,oBAAC,UAAD;EAAQ,IAAG;EAAI,IAAG;EAAI,GAAE;EAAM,QAAO;EAAe,aAAY;CAAO,CAAA,GACvEA,oBAAC,QAAD;EAAM,GAAE;EAAa,QAAO;EAAe,aAAY;EAAM,eAAc;CAAS,CAAA,CACjF;;AAGP,IAAM,cAAc,WAA+C,SAAS,YAC1E,EACE,OACA,eAAe,IACf,UACA,UACA,SACA,UACA,UAAU,OACV,aAAa,gBACb,eAAe,aACf,OAAO,MACP,WACA,gBACA,UACA,UACA,WACA,GAAG,QAEL,cACA;CACA,MAAM,CAAC,SAAS,cAAc,qBAA6B,OAAO,cAAc,QAAQ;CACxF,MAAM,WAAW,OAAgC,IAAI;CACrD,MAAM,cAAc,OAAO,QAAQ;CACnC,YAAY,UAAU;CAEtB,MAAM,iBAAiB,OAAO,KAAK;CACnC,MAAM,WAAW,OAAkD,KAAA,CAAS;CAE5E,MAAM,SAAS,aACZ,SAAkC;EACjC,SAAS,UAAU;EACnB,IAAI,OAAO,iBAAiB,YAAY,aAAa,IAAI;OACpD,IAAI,cAAc,aAAa,UAAU;CAChD,GACA,CAAC,YAAY,CACf;CAEA,gBAAgB;EACd,IAAI,YAAY,QA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A,oBAAC,OAAD;KAAK,SAAQ;KAAY,MAAK;KAAO,OAAM;KAAM,QAAO;KAAM,eAAY;KAAO,WAAU;KACzF,UAAAA,oBAAC,QAAD;MAAM,GAAE;MAAqB,QAAO;MAAe,aAAY;MAAM,eAAc;KAAS,CAAA;IACzF,CAAA;GACC,CAAA,CAEN;EAEJ,CAAA,CAAA;;AAEV,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
.ds-search-input {
|
|
2
|
+
position: relative;
|
|
3
|
+
display: inline-flex;
|
|
4
|
+
align-items: center;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.ds-search-input--full {
|
|
8
|
+
display: flex;
|
|
9
|
+
width: 100%;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.ds-search-input .ds-search-input__control {
|
|
13
|
+
/* Room for the spinner and the clear button */
|
|
14
|
+
padding-right: calc(var(--control-padding-x-md) + var(--icon-size-md) * 2);
|
|
15
|
+
}
|
|
16
|
+
.ds-search-input--full .ds-search-input__control {
|
|
17
|
+
flex: 1;
|
|
18
|
+
}
|
|
19
|
+
.ds-search-input--sm .ds-search-input__control.ds-text-input {
|
|
20
|
+
padding-right: calc(var(--control-padding-x-sm) + var(--icon-size-sm) * 2 + var(--space-1));
|
|
21
|
+
}
|
|
22
|
+
.ds-search-input--lg .ds-search-input__control.ds-text-input {
|
|
23
|
+
padding-right: calc(var(--control-padding-x-lg) + var(--icon-size-lg) * 2);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* Hide the browser's own clear control; ours is accessible and consistent */
|
|
27
|
+
.ds-search-input__field::-webkit-search-cancel-button,
|
|
28
|
+
.ds-search-input__field::-webkit-search-decoration {
|
|
29
|
+
appearance: none;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.ds-search-input__actions {
|
|
33
|
+
position: absolute;
|
|
34
|
+
top: 50%;
|
|
35
|
+
right: var(--space-2);
|
|
36
|
+
display: inline-flex;
|
|
37
|
+
align-items: center;
|
|
38
|
+
gap: var(--space-1);
|
|
39
|
+
color: var(--color-text-muted);
|
|
40
|
+
transform: translateY(-50%);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.ds-search-input__clear {
|
|
44
|
+
display: inline-flex;
|
|
45
|
+
align-items: center;
|
|
46
|
+
justify-content: center;
|
|
47
|
+
width: var(--icon-size-md);
|
|
48
|
+
height: var(--icon-size-md);
|
|
49
|
+
padding: 0;
|
|
50
|
+
border: 0;
|
|
51
|
+
border-radius: var(--radius-full);
|
|
52
|
+
background: transparent;
|
|
53
|
+
color: inherit;
|
|
54
|
+
font-size: var(--font-size-sm);
|
|
55
|
+
cursor: pointer;
|
|
56
|
+
transition:
|
|
57
|
+
background-color var(--duration-fast) var(--easing-standard),
|
|
58
|
+
color var(--duration-fast) var(--easing-standard);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.ds-search-input__clear:hover {
|
|
62
|
+
background: var(--color-neutral-hover);
|
|
63
|
+
color: var(--color-text);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.ds-search-input__clear:focus-visible {
|
|
67
|
+
outline: var(--border-width-2) solid var(--color-focus-ring);
|
|
68
|
+
outline-offset: 2px;
|
|
69
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HTMLAttributes } from "react";
|
|
2
|
+
export type ButtonGroupOrientation = "horizontal" | "vertical";
|
|
3
|
+
export interface ButtonGroupProps extends HTMLAttributes<HTMLDivElement> {
|
|
4
|
+
/** Layout direction of the buttons */
|
|
5
|
+
orientation?: ButtonGroupOrientation;
|
|
6
|
+
/** Join the buttons visually (shared borders, outer corners only). When false they are just spaced */
|
|
7
|
+
attached?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Groups related `Button`s (role="group"). Give it an `aria-label` describing the group. */
|
|
10
|
+
export declare function ButtonGroup({ orientation, attached, className, ...rest }: ButtonGroupProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { cx } from "../../utils/cx.js";
|
|
2
|
+
import './styles.css';/* empty css */
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/components/molecules/segmented-control/button-group.tsx
|
|
5
|
+
/** Groups related `Button`s (role="group"). Give it an `aria-label` describing the group. */
|
|
6
|
+
function ButtonGroup({ orientation = "horizontal", attached = true, className, ...rest }) {
|
|
7
|
+
return /* @__PURE__ */ jsx("div", {
|
|
8
|
+
role: "group",
|
|
9
|
+
className: cx("ds-button-group", `ds-button-group--${orientation}`, attached && "ds-button-group--attached", className),
|
|
10
|
+
...rest
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export { ButtonGroup };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=button-group.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"button-group.js","names":["_jsx"],"sources":["../../../../src/components/molecules/segmented-control/button-group.tsx"],"sourcesContent":["import type { HTMLAttributes } from \"react\";\nimport { cx } from \"../../utils/cx\";\nimport \"./styles.css\";\n\nexport type ButtonGroupOrientation = \"horizontal\" | \"vertical\";\n\nexport interface ButtonGroupProps extends HTMLAttributes<HTMLDivElement> {\n /** Layout direction of the buttons */\n orientation?: ButtonGroupOrientation;\n /** Join the buttons visually (shared borders, outer corners only). When false they are just spaced */\n attached?: boolean;\n}\n\n/** Groups related `Button`s (role=\"group\"). Give it an `aria-label` describing the group. */\nexport function ButtonGroup({ orientation = \"horizontal\", attached = true, className, ...rest }: ButtonGroupProps) {\n return (\n <div\n role=\"group\"\n className={cx(\n \"ds-button-group\",\n `ds-button-group--${orientation}`,\n attached && \"ds-button-group--attached\",\n className\n )}\n {...rest}\n />\n );\n}\n"],"mappings":";;;;;AAcA,SAAgB,YAAY,EAAE,cAAc,cAAc,WAAW,MAAM,WAAW,GAAG,QAA0B;CACjH,OACEA,oBAAC,OAAD;EACE,MAAK;EACL,WAAW,GACT,mBACA,oBAAoB,eACpB,YAAY,6BACZ,SACF;EACA,GAAI;CACL,CAAA;AAEL"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { HTMLAttributes, ReactNode } from "react";
|
|
2
|
+
export { ButtonGroup } from "./button-group";
|
|
3
|
+
export type { ButtonGroupProps, ButtonGroupOrientation } from "./button-group";
|
|
4
|
+
export type SegmentedControlSize = "sm" | "md" | "lg";
|
|
5
|
+
/** One segment of the control */
|
|
6
|
+
export interface SegmentedControlOption {
|
|
7
|
+
/** Value reported by `onChange` */
|
|
8
|
+
value: string;
|
|
9
|
+
/** Visible content. For icon-only segments pass a visually hidden text or use `aria-label` on the group */
|
|
10
|
+
label: ReactNode;
|
|
11
|
+
/** Icon shown before the label */
|
|
12
|
+
icon?: ReactNode;
|
|
13
|
+
/** Makes the segment unselectable */
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface SegmentedControlProps extends Omit<HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue"> {
|
|
17
|
+
/** The segments */
|
|
18
|
+
options: SegmentedControlOption[];
|
|
19
|
+
/** Selected value (controlled) */
|
|
20
|
+
value?: string;
|
|
21
|
+
/** Initially selected value (uncontrolled) */
|
|
22
|
+
defaultValue?: string;
|
|
23
|
+
/** Called with the newly selected value */
|
|
24
|
+
onChange?: (value: string) => void;
|
|
25
|
+
/** Height, from the control-size tokens */
|
|
26
|
+
size?: SegmentedControlSize;
|
|
27
|
+
/** Stretch to the container width, sharing it equally between segments */
|
|
28
|
+
fullWidth?: boolean;
|
|
29
|
+
/** Disables every segment */
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
/** Marks the selection as invalid (sets `aria-invalid` on the group) */
|
|
32
|
+
invalid?: boolean;
|
|
33
|
+
/** Accessible name of the group (or use `aria-labelledby`) */
|
|
34
|
+
"aria-label"?: string;
|
|
35
|
+
}
|
|
36
|
+
/** Single choice between a few options, as a radio group of buttons. */
|
|
37
|
+
declare function SegmentedControl({ options, value, defaultValue, onChange, size, fullWidth, disabled, invalid, className, onKeyDown, ...rest }: SegmentedControlProps): import("react").JSX.Element;
|
|
38
|
+
export default SegmentedControl;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
import { useControllableState } from "../../hooks/use-controllable-state.js";
|
|
3
|
+
import { cx } from "../../utils/cx.js";
|
|
4
|
+
import './styles.css';/* empty css */
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { ButtonGroup } from "./button-group.js";
|
|
7
|
+
// src/components/molecules/segmented-control/index.tsx
|
|
8
|
+
/** Single choice between a few options, as a radio group of buttons. */
|
|
9
|
+
function SegmentedControl({ options, value, defaultValue, onChange, size = "md", fullWidth = false, disabled = false, invalid = false, className, onKeyDown, ...rest }) {
|
|
10
|
+
const [current, setCurrent] = useControllableState(value, defaultValue ?? "", onChange);
|
|
11
|
+
const refs = useRef([]);
|
|
12
|
+
const enabled = options.map((option, index) => !disabled && !option.disabled ? index : -1).filter((i) => i >= 0);
|
|
13
|
+
const selectedIndex = options.findIndex((option) => option.value === current);
|
|
14
|
+
const tabbable = selectedIndex >= 0 && enabled.includes(selectedIndex) ? selectedIndex : enabled[0];
|
|
15
|
+
const select = (index) => {
|
|
16
|
+
refs.current[index]?.focus();
|
|
17
|
+
setCurrent(options[index].value);
|
|
18
|
+
};
|
|
19
|
+
const handleKeyDown = (event) => {
|
|
20
|
+
onKeyDown?.(event);
|
|
21
|
+
if (event.defaultPrevented || enabled.length === 0) return;
|
|
22
|
+
const focused = refs.current.findIndex((el) => el === document.activeElement);
|
|
23
|
+
const position = enabled.indexOf(focused >= 0 ? focused : tabbable);
|
|
24
|
+
let next;
|
|
25
|
+
switch (event.key) {
|
|
26
|
+
case "ArrowRight":
|
|
27
|
+
case "ArrowDown":
|
|
28
|
+
next = enabled[(position + 1) % enabled.length];
|
|
29
|
+
break;
|
|
30
|
+
case "ArrowLeft":
|
|
31
|
+
case "ArrowUp":
|
|
32
|
+
next = enabled[(position - 1 + enabled.length) % enabled.length];
|
|
33
|
+
break;
|
|
34
|
+
case "Home":
|
|
35
|
+
next = enabled[0];
|
|
36
|
+
break;
|
|
37
|
+
case "End":
|
|
38
|
+
next = enabled[enabled.length - 1];
|
|
39
|
+
break;
|
|
40
|
+
default: return;
|
|
41
|
+
}
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
select(next);
|
|
44
|
+
};
|
|
45
|
+
return /* @__PURE__ */ jsx("div", {
|
|
46
|
+
role: "radiogroup",
|
|
47
|
+
"aria-disabled": disabled || void 0,
|
|
48
|
+
"aria-invalid": invalid || void 0,
|
|
49
|
+
className: cx("ds-segmented-control", `ds-segmented-control--${size}`, fullWidth && "ds-segmented-control--full", disabled && "ds-segmented-control--disabled", invalid && "ds-segmented-control--invalid", className),
|
|
50
|
+
onKeyDown: handleKeyDown,
|
|
51
|
+
...rest,
|
|
52
|
+
children: options.map((option, index) => {
|
|
53
|
+
const checked = option.value === current;
|
|
54
|
+
const isDisabled = disabled || option.disabled;
|
|
55
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
56
|
+
ref: (el) => {
|
|
57
|
+
refs.current[index] = el;
|
|
58
|
+
},
|
|
59
|
+
type: "button",
|
|
60
|
+
role: "radio",
|
|
61
|
+
"aria-checked": checked,
|
|
62
|
+
disabled: isDisabled,
|
|
63
|
+
tabIndex: index === tabbable ? 0 : -1,
|
|
64
|
+
className: cx("ds-segmented-control__item", checked && "ds-segmented-control__item--checked"),
|
|
65
|
+
onClick: () => select(index),
|
|
66
|
+
children: [option.icon != null && /* @__PURE__ */ jsx("span", {
|
|
67
|
+
className: "ds-segmented-control__icon",
|
|
68
|
+
"aria-hidden": "true",
|
|
69
|
+
children: option.icon
|
|
70
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
71
|
+
className: "ds-segmented-control__label",
|
|
72
|
+
children: option.label
|
|
73
|
+
})]
|
|
74
|
+
}, option.value);
|
|
75
|
+
})
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export { ButtonGroup, SegmentedControl as default };
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_jsx","_jsxs"],"sources":["../../../../src/components/molecules/segmented-control/index.tsx"],"sourcesContent":["import { useRef } from \"react\";\nimport type { HTMLAttributes, KeyboardEvent, ReactNode } from \"react\";\nimport { useControllableState } from \"../../hooks\";\nimport { cx } from \"../../utils/cx\";\nimport \"./styles.css\";\n\nexport { ButtonGroup } from \"./button-group\";\nexport type { ButtonGroupProps, ButtonGroupOrientation } from \"./button-group\";\n\nexport type SegmentedControlSize = \"sm\" | \"md\" | \"lg\";\n\n/** One segment of the control */\nexport interface SegmentedControlOption {\n /** Value reported by `onChange` */\n value: string;\n /** Visible content. For icon-only segments pass a visually hidden text or use `aria-label` on the group */\n label: ReactNode;\n /** Icon shown before the label */\n icon?: ReactNode;\n /** Makes the segment unselectable */\n disabled?: boolean;\n}\n\nexport interface SegmentedControlProps extends Omit<HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n /** The segments */\n options: SegmentedControlOption[];\n /** Selected value (controlled) */\n value?: string;\n /** Initially selected value (uncontrolled) */\n defaultValue?: string;\n /** Called with the newly selected value */\n onChange?: (value: string) => void;\n /** Height, from the control-size tokens */\n size?: SegmentedControlSize;\n /** Stretch to the container width, sharing it equally between segments */\n fullWidth?: boolean;\n /** Disables every segment */\n disabled?: boolean;\n /** Marks the selection as invalid (sets `aria-invalid` on the group) */\n invalid?: boolean;\n /** Accessible name of the group (or use `aria-labelledby`) */\n \"aria-label\"?: string;\n}\n\n/** Single choice between a few options, as a radio group of buttons. */\nfunction SegmentedControl({\n options,\n value,\n defaultValue,\n onChange,\n size = \"md\",\n fullWidth = false,\n disabled = false,\n invalid = false,\n className,\n onKeyDown,\n ...rest\n}: SegmentedControlProps) {\n const [current, setCurrent] = useControllableState<string>(value, defaultValue ?? \"\", onChange);\n const refs = useRef<Array<HTMLButtonElement | null>>([]);\n\n const enabled = options.map((option, index) => (!disabled && !option.disabled ? index : -1)).filter((i) => i >= 0);\n const selectedIndex = options.findIndex((option) => option.value === current);\n const tabbable = selectedIndex >= 0 && enabled.includes(selectedIndex) ? selectedIndex : enabled[0];\n\n const select = (index: number) => {\n refs.current[index]?.focus();\n setCurrent(options[index].value);\n };\n\n const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n onKeyDown?.(event);\n if (event.defaultPrevented || enabled.length === 0) return;\n const focused = refs.current.findIndex((el) => el === document.activeElement);\n const position = enabled.indexOf(focused >= 0 ? focused : tabbable);\n let next: number;\n switch (event.key) {\n case \"ArrowRight\":\n case \"ArrowDown\":\n next = enabled[(position + 1) % enabled.length];\n break;\n case \"ArrowLeft\":\n case \"ArrowUp\":\n next = enabled[(position - 1 + enabled.length) % enabled.length];\n break;\n case \"Home\":\n next = enabled[0];\n break;\n case \"End\":\n next = enabled[enabled.length - 1];\n break;\n default:\n return;\n }\n event.preventDefault();\n select(next);\n };\n\n return (\n <div\n role=\"radiogroup\"\n aria-disabled={disabled || undefined}\n aria-invalid={invalid || undefined}\n className={cx(\n \"ds-segmented-control\",\n `ds-segmented-control--${size}`,\n fullWidth && \"ds-segmented-control--full\",\n disabled && \"ds-segmented-control--disabled\",\n invalid && \"ds-segmented-control--invalid\",\n className\n )}\n onKeyDown={handleKeyDown}\n {...rest}\n >\n {options.map((option, index) => {\n const checked = option.value === current;\n const isDisabled = disabled || option.disabled;\n return (\n <button\n key={option.value}\n ref={(el) => {\n refs.current[index] = el;\n }}\n type=\"button\"\n role=\"radio\"\n aria-checked={checked}\n disabled={isDisabled}\n tabIndex={index === tabbable ? 0 : -1}\n className={cx(\"ds-segmented-control__item\", checked && \"ds-segmented-control__item--checked\")}\n onClick={() => select(index)}\n >\n {option.icon != null && (\n <span className=\"ds-segmented-control__icon\" aria-hidden=\"true\">\n {option.icon}\n </span>\n )}\n <span className=\"ds-segmented-control__label\">{option.label}</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