@mrktg-common/internal-ui 1.3.1 → 1.4.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 +1 -0
- package/dist/MultiSelectMenu.d.ts +8 -0
- package/dist/MultiSelectMenu.js +37 -3
- package/package.json +2 -2
- package/tokens/tokens.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
- `design/README.md` — бренд-бук, `design/sections/*.md` — разделы (контраст, паттерны, переход продуктов);
|
|
22
22
|
- `design/components/<Comp>/` — карточка компонента: `README.md` с правилами и `preview.html` с превью в обеих темах;
|
|
23
23
|
- `design/cover.html` — обложка, `design/canvas.css` — классы для холстов;
|
|
24
|
+
- `design/assets/` — фавиконка библиотеки для страницы системы; в пакет npm не входит;
|
|
24
25
|
- `CHANGELOG.md` — общий changelog пакета и системы.
|
|
25
26
|
|
|
26
27
|
`npm run design` собирает артефакт в `design-dist/project/` (в гит не попадает): токены с версией из
|
|
@@ -12,6 +12,14 @@ export type TMultiSelectOption<T extends string> = {
|
|
|
12
12
|
export type TMultiSelectGroup<T extends string> = {
|
|
13
13
|
/** Заголовок группы, не кликается. */
|
|
14
14
|
label: string | null;
|
|
15
|
+
/**
|
|
16
|
+
* Группу выбирают целиком (бренды одного рынка): первой строкой под заголовком — «All INT brands»
|
|
17
|
+
* с тремя состояниями, на кнопке одна целая группа называется button («Brand: All INT»). Без label не действует.
|
|
18
|
+
*/
|
|
19
|
+
wholeGroup?: {
|
|
20
|
+
option: string;
|
|
21
|
+
button: string;
|
|
22
|
+
};
|
|
15
23
|
/** Оговорка про всю группу одной строкой под её вариантами. */
|
|
16
24
|
note?: ReactNode;
|
|
17
25
|
options: TMultiSelectOption<T>[];
|
package/dist/MultiSelectMenu.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { Fragment, useState } from 'react';
|
|
4
|
-
import { ChevronDown } from 'lucide-react';
|
|
3
|
+
import { Fragment, useEffect, useRef, useState } from 'react';
|
|
4
|
+
import { ChevronDown, Layers } from 'lucide-react';
|
|
5
5
|
import { buttonClass } from './buttonClass.js';
|
|
6
6
|
import { Popover } from './Popover.js';
|
|
7
7
|
/**
|
|
@@ -12,6 +12,40 @@ export function MultiSelectMenu({ label, groups, value, onChange, size = 'md', w
|
|
|
12
12
|
const [open, setOpen] = useState(false);
|
|
13
13
|
const all = groups.flatMap((group) => group.options);
|
|
14
14
|
const single = value.length === 1 ? all.find((option) => option.value === value[0]) : undefined;
|
|
15
|
+
const whole = wholeGroupOf(groups, value);
|
|
15
16
|
const toggle = (option) => onChange(value.includes(option) ? value.filter((item) => item !== option) : [...value, option]);
|
|
16
|
-
|
|
17
|
+
const toggleGroup = (group) => {
|
|
18
|
+
const options = group.options.map((option) => option.value);
|
|
19
|
+
onChange(groupState(group, value) === 'all' ? value.filter((item) => !options.includes(item)) : [...value, ...options.filter((option) => !value.includes(option))]);
|
|
20
|
+
};
|
|
21
|
+
return (_jsxs(Popover, { open: open, onClose: () => setOpen(false), width: width, role: "listbox", "aria-label": label, "aria-multiselectable": true, scroll: true, trigger: _jsxs("button", { type: "button", className: buttonClass({ variant: 'secondary', size, active: value.length > 0 }), "aria-haspopup": "listbox", "aria-expanded": open, onClick: () => setOpen((current) => !current), children: [whole !== null ? `${label}: ${whole}` : single === undefined ? label : `${label}: ${single.label}`, whole === null && value.length > 1 && _jsx("span", { className: "count-pill", children: value.length }), _jsx(ChevronDown, { className: "ic ic-sm", "aria-hidden": true })] }), children: [groups.map((group, index) => (_jsxs(Fragment, { children: [index > 0 && _jsx("div", { className: "menu-sep" }), group.label !== null && _jsx("div", { className: "menu-label", children: group.label }), group.label !== null && group.wholeGroup !== undefined && (_jsx(WholeGroupRow, { option: group.wholeGroup.option, state: groupState(group, value), options: group.options, onToggle: () => toggleGroup(group) })), group.options.map((option) => (_jsxs(Fragment, { children: [_jsxs("label", { className: "menu-item", role: "option", "aria-selected": value.includes(option.value), children: [_jsx("input", { className: "checkbox", type: "checkbox", checked: value.includes(option.value), onChange: () => toggle(option.value) }), option.icon === undefined ? option.label : (_jsxs("span", { className: "brand", children: [option.icon, _jsx("span", { className: "name", children: option.label })] })), option.count !== undefined && _jsx("span", { className: "count", children: option.count })] }), option.hint !== undefined && _jsx("div", { className: "menu-hint", children: option.hint })] }, option.value))), group.note !== undefined && _jsx("div", { className: "menu-hint", children: group.note })] }, group.label ?? `group-${index}`))), _jsxs("div", { className: "menu-foot", style: { position: 'sticky', bottom: 'calc(-1 * var(--space-1))', background: 'var(--surface)', paddingBottom: 'var(--space-1)' }, children: [_jsx("button", { type: "button", className: "btn btn-ghost btn-sm", disabled: value.length === 0, onClick: () => onChange([]), children: "Clear" }), _jsx("span", { className: "hint", children: "Applies as you tick" })] })] }));
|
|
22
|
+
}
|
|
23
|
+
function groupState(group, value) {
|
|
24
|
+
const picked = group.options.filter((option) => value.includes(option.value)).length;
|
|
25
|
+
if (picked === 0)
|
|
26
|
+
return 'none';
|
|
27
|
+
return picked === group.options.length ? 'all' : 'some';
|
|
28
|
+
}
|
|
29
|
+
/** Подпись кнопки для ровно одной целой группы и ничего больше; null — подпись как обычно. */
|
|
30
|
+
function wholeGroupOf(groups, value) {
|
|
31
|
+
const group = groups.find((candidate) => candidate.label !== null && candidate.wholeGroup !== undefined && candidate.options.length > 0 && groupState(candidate, value) === 'all');
|
|
32
|
+
return group?.wholeGroup !== undefined && value.length === group.options.length ? group.wholeGroup.button : null;
|
|
33
|
+
}
|
|
34
|
+
/** Сумма count вариантов группы; если хоть один — «≥ N», сумма тоже «≥». */
|
|
35
|
+
function groupCount(options) {
|
|
36
|
+
if (options.every((option) => option.count === undefined))
|
|
37
|
+
return undefined;
|
|
38
|
+
const atLeast = options.some((option) => typeof option.count === 'string');
|
|
39
|
+
const sum = options.reduce((total, option) => total + (typeof option.count === 'number' ? option.count : Number.parseInt(String(option.count ?? 0).replace(/\D/g, ''), 10) || 0), 0);
|
|
40
|
+
return atLeast ? `≥ ${sum}` : sum;
|
|
41
|
+
}
|
|
42
|
+
function WholeGroupRow({ option, state, options, onToggle }) {
|
|
43
|
+
const ref = useRef(null);
|
|
44
|
+
// indeterminate у чекбокса — только свойство DOM, атрибутом React его не ставит
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (ref.current)
|
|
47
|
+
ref.current.indeterminate = state === 'some';
|
|
48
|
+
}, [state]);
|
|
49
|
+
const count = groupCount(options);
|
|
50
|
+
return (_jsxs("label", { className: "menu-item", role: "option", "aria-selected": state === 'all', children: [_jsx("input", { ref: ref, className: "checkbox", type: "checkbox", checked: state === 'all', "aria-checked": state === 'some' ? 'mixed' : state === 'all', onChange: onToggle }), options.some((item) => item.icon !== undefined) && _jsx(Layers, { className: "ic", "aria-hidden": true }), option, count !== undefined && _jsx("span", { className: "count", children: count })] }));
|
|
17
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrktg-common/internal-ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Общий интерфейс внутренних инструментов маркетинга (Landings Admin, LP Constructor): токены, классы компонентов и тонкие React-обёртки над ними.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"repository": {
|
|
41
41
|
"type": "git",
|
|
42
|
-
"url": "git+ssh://git@gitlab.srv.team/marketing-frontend/
|
|
42
|
+
"url": "git+ssh://git@gitlab.srv.team/marketing-frontend/common/internal-ui.git"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"lucide-react": "^1",
|