@ada-cx/lovelace 1.21.2 → 1.21.3
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.
|
@@ -3,50 +3,58 @@ import { Chip as t } from "../Chip/Chip.js";
|
|
|
3
3
|
import { surveyRatingRoot as n, surveyRatingScale as r } from "./SurveyRating.css.js";
|
|
4
4
|
import { jsx as i, jsxs as a } from "react/jsx-runtime";
|
|
5
5
|
import { ToggleButtonGroup as o } from "react-aria-components";
|
|
6
|
+
import { useId as s } from "react";
|
|
6
7
|
//#region src/components/SurveyRating/SurveyRating.tsx
|
|
7
|
-
var
|
|
8
|
-
let t = Math.max(1, Math.ceil(e /
|
|
8
|
+
var c = (e, t) => e.key ?? t, l = 7, u = 48, d = 8, f = (e) => {
|
|
9
|
+
let t = Math.max(1, Math.ceil(e / l));
|
|
9
10
|
return Math.max(1, Math.ceil(e / t));
|
|
10
|
-
},
|
|
11
|
-
let t =
|
|
12
|
-
return t *
|
|
11
|
+
}, p = (e) => {
|
|
12
|
+
let t = f(e);
|
|
13
|
+
return t * u + (t - 1) * d;
|
|
13
14
|
};
|
|
14
|
-
function
|
|
15
|
-
let
|
|
16
|
-
let t = e.size ? [...e][0] : null, n = t == null ? null :
|
|
17
|
-
|
|
18
|
-
},
|
|
15
|
+
function m({ options: l, showLabels: u = !1, isDisabled: d, selectedKey: f, defaultSelectedKey: m, onChange: h, className: g, style: _, ref: v, "aria-label": y, "aria-labelledby": b }) {
|
|
16
|
+
let x = s(), S = l[0]?.label, C = l[l.length - 1]?.label, w = u && (S != null || C != null), T = w && S != null ? `${x}-low` : void 0, E = w && C != null ? `${x}-high` : void 0, D = (e) => e === 0 ? T : e === l.length - 1 ? E : void 0, O = (e) => {
|
|
17
|
+
let t = e.size ? [...e][0] : null, n = t == null ? null : l.find((e, n) => c(e, n) === t) ?? null;
|
|
18
|
+
h?.(t, n);
|
|
19
|
+
}, k = (e) => e == null ? [] : [e], A = f !== void 0;
|
|
19
20
|
return /* @__PURE__ */ a("div", {
|
|
20
|
-
ref:
|
|
21
|
-
className: e(n,
|
|
22
|
-
style:
|
|
21
|
+
ref: v,
|
|
22
|
+
className: e(n, g),
|
|
23
|
+
style: _,
|
|
23
24
|
children: [/* @__PURE__ */ i(o, {
|
|
24
25
|
selectionMode: "single",
|
|
25
|
-
isDisabled:
|
|
26
|
-
"aria-label":
|
|
27
|
-
"aria-labelledby":
|
|
26
|
+
isDisabled: d,
|
|
27
|
+
"aria-label": b ? void 0 : y,
|
|
28
|
+
"aria-labelledby": b,
|
|
28
29
|
className: r,
|
|
29
|
-
style: { maxWidth:
|
|
30
|
-
onSelectionChange:
|
|
31
|
-
selectedKeys:
|
|
32
|
-
defaultSelectedKeys:
|
|
33
|
-
children:
|
|
30
|
+
style: { maxWidth: p(l.length) },
|
|
31
|
+
onSelectionChange: O,
|
|
32
|
+
selectedKeys: A ? k(f) : void 0,
|
|
33
|
+
defaultSelectedKeys: A ? void 0 : k(m),
|
|
34
|
+
children: l.map((e, n) => {
|
|
34
35
|
let r = e.icon != null;
|
|
35
36
|
return /* @__PURE__ */ i(t, {
|
|
36
|
-
id:
|
|
37
|
+
id: c(e, n),
|
|
37
38
|
variant: r ? "icon" : "number",
|
|
38
39
|
size: "lg",
|
|
39
40
|
"aria-label": e["aria-label"] ?? (r ? e.label : void 0),
|
|
41
|
+
"aria-describedby": D(n),
|
|
40
42
|
children: e.icon ?? e.value
|
|
41
|
-
},
|
|
43
|
+
}, c(e, n));
|
|
42
44
|
})
|
|
43
|
-
}),
|
|
45
|
+
}), w && /* @__PURE__ */ a("div", {
|
|
44
46
|
className: "ewm1i82",
|
|
45
|
-
children: [/* @__PURE__ */ i("span", {
|
|
47
|
+
children: [/* @__PURE__ */ i("span", {
|
|
48
|
+
id: T,
|
|
49
|
+
children: S
|
|
50
|
+
}), /* @__PURE__ */ i("span", {
|
|
51
|
+
id: E,
|
|
52
|
+
children: C
|
|
53
|
+
})]
|
|
46
54
|
})]
|
|
47
55
|
});
|
|
48
56
|
}
|
|
49
57
|
//#endregion
|
|
50
|
-
export {
|
|
58
|
+
export { m as SurveyRating };
|
|
51
59
|
|
|
52
60
|
//# sourceMappingURL=SurveyRating.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SurveyRating.js","names":[],"sources":["../../../src/components/SurveyRating/SurveyRating.tsx"],"sourcesContent":["import type { CSSProperties, ReactNode, Ref } from \"react\";\nimport type { Key } from \"react-aria-components\";\nimport { ToggleButtonGroup } from \"react-aria-components\";\nimport { cx } from \"../../utils/cx.js\";\nimport { Chip } from \"../Chip/index.js\";\nimport type { SurveyLabelProps } from \"./SurveyLabelProps.js\";\nimport {\n\tsurveyRatingLabels,\n\tsurveyRatingRoot,\n\tsurveyRatingScale,\n} from \"./SurveyRating.css.js\";\n\nexport interface SurveyRatingOption {\n\t/** Numeric value shown on the chip (e.g. 1–5, 1–10). Rendered unless `icon` is set. */\n\tvalue: number;\n\t/** Stable identity used for selection. Defaults to the option's array index. */\n\tkey?: Key;\n\t/** Endpoint label to render as selected option. */\n\tlabel?: string;\n\t/** Renders an icon chip (emoji, thumbs) instead of the numeric value. */\n\ticon?: ReactNode;\n\t/** Accessible name for the option. Overrides the numeric value / icon; icon options fall back to `label`. */\n\t\"aria-label\"?: string;\n}\n\nexport type SurveyRatingProps = SurveyLabelProps & {\n\t/** Ordered rating options, lowest value first. */\n\toptions: SurveyRatingOption[];\n\t/** Show the first and last options' labels as scale endpoints. */\n\tshowLabels?: boolean;\n\tisDisabled?: boolean;\n\t/** Selected option key (controlled). `null` means nothing selected. */\n\tselectedKey?: Key | null;\n\t/** Initially-selected option key (uncontrolled). */\n\tdefaultSelectedKey?: Key | null;\n\t/** Fires with the newly-selected key and option, or `null` when deselected. */\n\tonChange?: (key: Key | null, option: SurveyRatingOption | null) => void;\n\tclassName?: string;\n\tstyle?: CSSProperties;\n\tref?: Ref<HTMLDivElement>;\n};\n\nconst keyFor = (option: SurveyRatingOption, index: number) =>\n\toption.key ?? index;\n\n/** Most chips allowed on one row before the scale wraps to a balanced grid. */\nconst MAX_PER_ROW = 7;\n\n/** Chip footprint at `size=\"lg\"` — mirrors Chip.css.ts's fixed 48px square + gap-8. */\nconst CHIP_SIZE_LG = 48;\nconst CHIP_GAP = 8;\n\n/** Column count that splits `count` options into evenly-sized rows (11 → 6, 10 → 5, 7 → 7). */\nconst balancedColumns = (count: number) => {\n\tconst rows = Math.max(1, Math.ceil(count / MAX_PER_ROW));\n\treturn Math.max(1, Math.ceil(count / rows));\n};\n\n/**\n * Width that wraps the scale at the balanced column count. A cap rather than a fixed\n * track count, so a container narrower than this wraps further instead of overflowing\n * (the 0-10 scale is 328px wide — wider than the 320px BubbleOverlay cap).\n */\nconst balancedMaxWidth = (count: number) => {\n\tconst cols = balancedColumns(count);\n\treturn cols * CHIP_SIZE_LG + (cols - 1) * CHIP_GAP;\n};\n\n/**\n * Single-select rating scale for post-conversation surveys. Renders each option as a\n * square {@link Chip}; a numeric `value` or an `icon` (emoji, thumbs) per option supports\n * arbitrary scales. Exactly one option is selectable at a time, and tapping the selected\n * option deselects it (React Aria toggle semantics).\n */\nexport function SurveyRating({\n\toptions,\n\tshowLabels = false,\n\tisDisabled,\n\tselectedKey,\n\tdefaultSelectedKey,\n\tonChange,\n\tclassName,\n\tstyle,\n\tref,\n\t\"aria-label\": ariaLabel,\n\t\"aria-labelledby\": ariaLabelledBy,\n}: SurveyRatingProps) {\n\tconst lowLabel = options[0]?.label;\n\tconst highLabel = options[options.length - 1]?.label;\n\n\tconst handleSelectionChange = (keys: Set<Key>) => {\n\t\tconst key = keys.size ? [...keys][0] : null;\n\t\tconst option =\n\t\t\tkey == null\n\t\t\t\t? null\n\t\t\t\t: (options.find((o, i) => keyFor(o, i) === key) ?? null);\n\t\tonChange?.(key, option);\n\t};\n\n\t// Translate the single-key facade into RAC's Set-based controlled/uncontrolled props:\n\t// a defined `selectedKey` (including `null`) is controlled; otherwise it's uncontrolled.\n\tconst toKeys = (key: Key | null | undefined) => (key == null ? [] : [key]);\n\tconst isControlled = selectedKey !== undefined;\n\n\treturn (\n\t\t<div ref={ref} className={cx(surveyRatingRoot, className)} style={style}>\n\t\t\t<ToggleButtonGroup\n\t\t\t\tselectionMode=\"single\"\n\t\t\t\tisDisabled={isDisabled}\n\t\t\t\taria-label={ariaLabelledBy ? undefined : ariaLabel}\n\t\t\t\taria-labelledby={ariaLabelledBy}\n\t\t\t\tclassName={surveyRatingScale}\n\t\t\t\tstyle={{ maxWidth: balancedMaxWidth(options.length) }}\n\t\t\t\tonSelectionChange={handleSelectionChange}\n\t\t\t\tselectedKeys={isControlled ? toKeys(selectedKey) : undefined}\n\t\t\t\tdefaultSelectedKeys={\n\t\t\t\t\tisControlled ? undefined : toKeys(defaultSelectedKey)\n\t\t\t\t}\n\t\t\t>\n\t\t\t\t{options.map((option, index) => {\n\t\t\t\t\tconst isIcon = option.icon != null;\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<Chip\n\t\t\t\t\t\t\tkey={keyFor(option, index)}\n\t\t\t\t\t\t\tid={keyFor(option, index)}\n\t\t\t\t\t\t\tvariant={isIcon ? \"icon\" : \"number\"}\n\t\t\t\t\t\t\tsize=\"lg\"\n\t\t\t\t\t\t\taria-label={\n\t\t\t\t\t\t\t\toption[\"aria-label\"] ?? (isIcon ? option.label : undefined)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{option.icon ?? option.value}\n\t\t\t\t\t\t</Chip>\n\t\t\t\t\t);\n\t\t\t\t})}\n\t\t\t</ToggleButtonGroup>\n\t\t\t{
|
|
1
|
+
{"version":3,"file":"SurveyRating.js","names":[],"sources":["../../../src/components/SurveyRating/SurveyRating.tsx"],"sourcesContent":["import type { CSSProperties, ReactNode, Ref } from \"react\";\nimport { useId } from \"react\";\nimport type { Key } from \"react-aria-components\";\nimport { ToggleButtonGroup } from \"react-aria-components\";\nimport { cx } from \"../../utils/cx.js\";\nimport { Chip } from \"../Chip/index.js\";\nimport type { SurveyLabelProps } from \"./SurveyLabelProps.js\";\nimport {\n\tsurveyRatingLabels,\n\tsurveyRatingRoot,\n\tsurveyRatingScale,\n} from \"./SurveyRating.css.js\";\n\nexport interface SurveyRatingOption {\n\t/** Numeric value shown on the chip (e.g. 1–5, 1–10). Rendered unless `icon` is set. */\n\tvalue: number;\n\t/** Stable identity used for selection. Defaults to the option's array index. */\n\tkey?: Key;\n\t/** Endpoint label to render as selected option. */\n\tlabel?: string;\n\t/** Renders an icon chip (emoji, thumbs) instead of the numeric value. */\n\ticon?: ReactNode;\n\t/** Accessible name for the option. Overrides the numeric value / icon; icon options fall back to `label`. */\n\t\"aria-label\"?: string;\n}\n\nexport type SurveyRatingProps = SurveyLabelProps & {\n\t/** Ordered rating options, lowest value first. */\n\toptions: SurveyRatingOption[];\n\t/** Show the first and last options' labels as scale endpoints. */\n\tshowLabels?: boolean;\n\tisDisabled?: boolean;\n\t/** Selected option key (controlled). `null` means nothing selected. */\n\tselectedKey?: Key | null;\n\t/** Initially-selected option key (uncontrolled). */\n\tdefaultSelectedKey?: Key | null;\n\t/** Fires with the newly-selected key and option, or `null` when deselected. */\n\tonChange?: (key: Key | null, option: SurveyRatingOption | null) => void;\n\tclassName?: string;\n\tstyle?: CSSProperties;\n\tref?: Ref<HTMLDivElement>;\n};\n\nconst keyFor = (option: SurveyRatingOption, index: number) =>\n\toption.key ?? index;\n\n/** Most chips allowed on one row before the scale wraps to a balanced grid. */\nconst MAX_PER_ROW = 7;\n\n/** Chip footprint at `size=\"lg\"` — mirrors Chip.css.ts's fixed 48px square + gap-8. */\nconst CHIP_SIZE_LG = 48;\nconst CHIP_GAP = 8;\n\n/** Column count that splits `count` options into evenly-sized rows (11 → 6, 10 → 5, 7 → 7). */\nconst balancedColumns = (count: number) => {\n\tconst rows = Math.max(1, Math.ceil(count / MAX_PER_ROW));\n\treturn Math.max(1, Math.ceil(count / rows));\n};\n\n/**\n * Width that wraps the scale at the balanced column count. A cap rather than a fixed\n * track count, so a container narrower than this wraps further instead of overflowing\n * (the 0-10 scale is 328px wide — wider than the 320px BubbleOverlay cap).\n */\nconst balancedMaxWidth = (count: number) => {\n\tconst cols = balancedColumns(count);\n\treturn cols * CHIP_SIZE_LG + (cols - 1) * CHIP_GAP;\n};\n\n/**\n * Single-select rating scale for post-conversation surveys. Renders each option as a\n * square {@link Chip}; a numeric `value` or an `icon` (emoji, thumbs) per option supports\n * arbitrary scales. Exactly one option is selectable at a time, and tapping the selected\n * option deselects it (React Aria toggle semantics).\n */\nexport function SurveyRating({\n\toptions,\n\tshowLabels = false,\n\tisDisabled,\n\tselectedKey,\n\tdefaultSelectedKey,\n\tonChange,\n\tclassName,\n\tstyle,\n\tref,\n\t\"aria-label\": ariaLabel,\n\t\"aria-labelledby\": ariaLabelledBy,\n}: SurveyRatingProps) {\n\tconst labelIdPrefix = useId();\n\tconst lowLabel = options[0]?.label;\n\tconst highLabel = options[options.length - 1]?.label;\n\tconst showEndpoints = showLabels && (lowLabel != null || highLabel != null);\n\tconst lowLabelId =\n\t\tshowEndpoints && lowLabel != null ? `${labelIdPrefix}-low` : undefined;\n\tconst highLabelId =\n\t\tshowEndpoints && highLabel != null ? `${labelIdPrefix}-high` : undefined;\n\n\tconst describedByFor = (index: number) => {\n\t\tif (index === 0) {\n\t\t\treturn lowLabelId;\n\t\t}\n\t\treturn index === options.length - 1 ? highLabelId : undefined;\n\t};\n\n\tconst handleSelectionChange = (keys: Set<Key>) => {\n\t\tconst key = keys.size ? [...keys][0] : null;\n\t\tconst option =\n\t\t\tkey == null\n\t\t\t\t? null\n\t\t\t\t: (options.find((o, i) => keyFor(o, i) === key) ?? null);\n\t\tonChange?.(key, option);\n\t};\n\n\t// Translate the single-key facade into RAC's Set-based controlled/uncontrolled props:\n\t// a defined `selectedKey` (including `null`) is controlled; otherwise it's uncontrolled.\n\tconst toKeys = (key: Key | null | undefined) => (key == null ? [] : [key]);\n\tconst isControlled = selectedKey !== undefined;\n\n\treturn (\n\t\t<div ref={ref} className={cx(surveyRatingRoot, className)} style={style}>\n\t\t\t<ToggleButtonGroup\n\t\t\t\tselectionMode=\"single\"\n\t\t\t\tisDisabled={isDisabled}\n\t\t\t\taria-label={ariaLabelledBy ? undefined : ariaLabel}\n\t\t\t\taria-labelledby={ariaLabelledBy}\n\t\t\t\tclassName={surveyRatingScale}\n\t\t\t\tstyle={{ maxWidth: balancedMaxWidth(options.length) }}\n\t\t\t\tonSelectionChange={handleSelectionChange}\n\t\t\t\tselectedKeys={isControlled ? toKeys(selectedKey) : undefined}\n\t\t\t\tdefaultSelectedKeys={\n\t\t\t\t\tisControlled ? undefined : toKeys(defaultSelectedKey)\n\t\t\t\t}\n\t\t\t>\n\t\t\t\t{options.map((option, index) => {\n\t\t\t\t\tconst isIcon = option.icon != null;\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<Chip\n\t\t\t\t\t\t\tkey={keyFor(option, index)}\n\t\t\t\t\t\t\tid={keyFor(option, index)}\n\t\t\t\t\t\t\tvariant={isIcon ? \"icon\" : \"number\"}\n\t\t\t\t\t\t\tsize=\"lg\"\n\t\t\t\t\t\t\taria-label={\n\t\t\t\t\t\t\t\toption[\"aria-label\"] ?? (isIcon ? option.label : undefined)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taria-describedby={describedByFor(index)}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{option.icon ?? option.value}\n\t\t\t\t\t\t</Chip>\n\t\t\t\t\t);\n\t\t\t\t})}\n\t\t\t</ToggleButtonGroup>\n\t\t\t{showEndpoints && (\n\t\t\t\t<div className={surveyRatingLabels}>\n\t\t\t\t\t<span id={lowLabelId}>{lowLabel}</span>\n\t\t\t\t\t<span id={highLabelId}>{highLabel}</span>\n\t\t\t\t</div>\n\t\t\t)}\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;AA2CA,IAAM,KAAU,GAA4B,MAC3C,EAAO,OAAO,GAGT,IAAc,GAGd,IAAe,IACf,IAAW,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