@valu/block-components 0.1.0 → 0.2.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 +4 -0
- package/dist/components/matrix/index.cjs +714 -0
- package/dist/components/matrix/index.cjs.map +1 -0
- package/dist/components/matrix/index.d.cts +195 -0
- package/dist/components/matrix/index.d.ts +195 -0
- package/dist/components/matrix/index.js +704 -0
- package/dist/components/matrix/index.js.map +1 -0
- package/dist/index.cjs +696 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +694 -2
- package/dist/index.js.map +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { useId, useMemo, useState, useCallback } from 'react';
|
|
2
|
+
import styled from '@emotion/styled';
|
|
3
|
+
import { css, CacheProvider } from '@emotion/react';
|
|
4
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
5
|
+
import { ToggleControl, CheckboxControl } from '@wordpress/components';
|
|
6
|
+
import createCache from '@emotion/cache';
|
|
7
|
+
|
|
8
|
+
// components/matrix/Matrix.tsx
|
|
9
|
+
|
|
10
|
+
// components/matrix/styles/tokens.ts
|
|
11
|
+
var tokens = {
|
|
12
|
+
accent: "var(--wp-admin-theme-color, #3858e9)",
|
|
13
|
+
accentHover: "color-mix(in srgb, var(--wp-components-color-accent, var(--wp-admin-theme-color, #3858e9)) 4%, transparent)",
|
|
14
|
+
bg: "var(--wp-components-color-background, #ffffff)",
|
|
15
|
+
bgMuted: "var(--wp-components-color-gray-100, #f0f0f0)",
|
|
16
|
+
fg: "var(--wp-components-color-foreground, #1e1e1e)",
|
|
17
|
+
gray200: "var(--wp-components-color-gray-200, #ebebeb)",
|
|
18
|
+
gray300: "var(--wp-components-color-gray-300, #dcdcde)",
|
|
19
|
+
gray600: "var(--wp-components-color-gray-600, #757575)",
|
|
20
|
+
gray900: "var(--wp-components-color-gray-900, #1e1e1e)",
|
|
21
|
+
radiusSm: "2px",
|
|
22
|
+
radiusMd: "4px",
|
|
23
|
+
rowMinHeight: "44px",
|
|
24
|
+
shadowStickyTop: "inset 0 -1px 0 rgba(0, 0, 0, 0.08)",
|
|
25
|
+
shadowStickyLeft: "inset -1px 0 0 rgba(0, 0, 0, 0.08)",
|
|
26
|
+
spaceXs: "4px",
|
|
27
|
+
spaceSm: "8px",
|
|
28
|
+
spaceMd: "12px",
|
|
29
|
+
transitionFast: "120ms ease-out",
|
|
30
|
+
fontSizeBody: "13px",
|
|
31
|
+
fontSizeSmall: "11px",
|
|
32
|
+
fontWeightRegular: 400,
|
|
33
|
+
fontWeightMedium: 500,
|
|
34
|
+
fontWeightBold: 600,
|
|
35
|
+
/** Sticky z-index layering. Row-header > master > top headers > leaf cells. */
|
|
36
|
+
zRowHeader: 4,
|
|
37
|
+
zMaster: 3,
|
|
38
|
+
zHeader: 3};
|
|
39
|
+
|
|
40
|
+
// components/matrix/styles/index.ts
|
|
41
|
+
var visuallyHiddenCss = css`
|
|
42
|
+
position: absolute !important;
|
|
43
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
44
|
+
width: 1px;
|
|
45
|
+
height: 1px;
|
|
46
|
+
overflow: hidden;
|
|
47
|
+
white-space: nowrap;
|
|
48
|
+
`;
|
|
49
|
+
var Root = styled.div`
|
|
50
|
+
color: ${tokens.fg};
|
|
51
|
+
font-size: ${tokens.fontSizeBody};
|
|
52
|
+
|
|
53
|
+
* {
|
|
54
|
+
box-sizing: border-box;
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
var Viewport = styled.div`
|
|
58
|
+
position: relative;
|
|
59
|
+
overflow: auto;
|
|
60
|
+
max-height: ${(p) => p.$maxHeight};
|
|
61
|
+
border: 1px solid ${tokens.gray300};
|
|
62
|
+
border-radius: ${tokens.radiusMd};
|
|
63
|
+
background: ${tokens.bg};
|
|
64
|
+
`;
|
|
65
|
+
var Table = styled.table`
|
|
66
|
+
border-collapse: separate;
|
|
67
|
+
border-spacing: 0;
|
|
68
|
+
width: 100%;
|
|
69
|
+
min-width: max-content;
|
|
70
|
+
|
|
71
|
+
caption {
|
|
72
|
+
${visuallyHiddenCss};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
th,
|
|
76
|
+
td {
|
|
77
|
+
text-align: left;
|
|
78
|
+
padding: ${tokens.spaceSm} ${tokens.spaceMd};
|
|
79
|
+
border-bottom: 1px solid ${tokens.gray200};
|
|
80
|
+
font-weight: ${tokens.fontWeightRegular};
|
|
81
|
+
vertical-align: middle;
|
|
82
|
+
background: ${tokens.bg};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
tbody tr:last-of-type th,
|
|
86
|
+
tbody tr:last-of-type td {
|
|
87
|
+
border-bottom: none;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
tbody tr:hover th,
|
|
91
|
+
tbody tr:hover td {
|
|
92
|
+
background: ${tokens.bgMuted};
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
var HeaderCell = styled.th`
|
|
96
|
+
font-weight: ${tokens.fontWeightBold};
|
|
97
|
+
font-size: ${tokens.fontSizeSmall};
|
|
98
|
+
text-transform: uppercase;
|
|
99
|
+
letter-spacing: 0.02em;
|
|
100
|
+
color: ${tokens.gray900};
|
|
101
|
+
white-space: nowrap;
|
|
102
|
+
text-align: ${(p) => p.$align ?? "left"};
|
|
103
|
+
|
|
104
|
+
${(p) => p.$sticky && css`
|
|
105
|
+
position: sticky;
|
|
106
|
+
top: ${p.$top ?? "0"};
|
|
107
|
+
z-index: ${p.$z ?? tokens.zHeader};
|
|
108
|
+
box-shadow: ${tokens.shadowStickyTop};
|
|
109
|
+
`};
|
|
110
|
+
|
|
111
|
+
${(p) => p.$leftSticky && css`
|
|
112
|
+
position: sticky;
|
|
113
|
+
left: ${p.$leftOffset ?? "0"};
|
|
114
|
+
z-index: ${(p.$z ?? tokens.zHeader) + 1};
|
|
115
|
+
box-shadow: ${tokens.shadowStickyLeft}, ${tokens.shadowStickyTop};
|
|
116
|
+
`};
|
|
117
|
+
`;
|
|
118
|
+
var GroupHeaderButton = styled.button`
|
|
119
|
+
display: inline-flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
gap: ${tokens.spaceXs};
|
|
122
|
+
background: transparent;
|
|
123
|
+
border: 1px solid transparent;
|
|
124
|
+
border-radius: ${tokens.radiusSm};
|
|
125
|
+
padding: 2px ${tokens.spaceXs};
|
|
126
|
+
font: inherit;
|
|
127
|
+
color: inherit;
|
|
128
|
+
cursor: pointer;
|
|
129
|
+
text-transform: inherit;
|
|
130
|
+
letter-spacing: inherit;
|
|
131
|
+
|
|
132
|
+
&:hover {
|
|
133
|
+
background: ${tokens.accentHover};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
&:focus-visible {
|
|
137
|
+
outline: 2px solid ${tokens.accent};
|
|
138
|
+
outline-offset: 1px;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
&[disabled] {
|
|
142
|
+
cursor: default;
|
|
143
|
+
&:hover {
|
|
144
|
+
background: transparent;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
`;
|
|
148
|
+
var Caret = styled.span`
|
|
149
|
+
display: inline-block;
|
|
150
|
+
width: 0;
|
|
151
|
+
height: 0;
|
|
152
|
+
border-left: 4px solid transparent;
|
|
153
|
+
border-right: 4px solid transparent;
|
|
154
|
+
border-top: 5px solid currentColor;
|
|
155
|
+
transition: transform ${tokens.transitionFast};
|
|
156
|
+
transform: ${(p) => p.$open ? "rotate(0deg)" : "rotate(-90deg)"};
|
|
157
|
+
`;
|
|
158
|
+
var RowHeaderCell = styled.th`
|
|
159
|
+
min-width: 220px;
|
|
160
|
+
max-width: 320px;
|
|
161
|
+
${(p) => p.$leftSticky && css`
|
|
162
|
+
position: sticky;
|
|
163
|
+
left: 0;
|
|
164
|
+
z-index: ${tokens.zRowHeader};
|
|
165
|
+
box-shadow: ${tokens.shadowStickyLeft};
|
|
166
|
+
`};
|
|
167
|
+
`;
|
|
168
|
+
var RowHeaderInner = styled.div`
|
|
169
|
+
display: flex;
|
|
170
|
+
align-items: center;
|
|
171
|
+
gap: ${tokens.spaceMd};
|
|
172
|
+
min-height: ${tokens.rowMinHeight};
|
|
173
|
+
`;
|
|
174
|
+
var RowHeaderText = styled.div`
|
|
175
|
+
display: flex;
|
|
176
|
+
flex-direction: column;
|
|
177
|
+
gap: 2px;
|
|
178
|
+
min-width: 0;
|
|
179
|
+
`;
|
|
180
|
+
var RowLabel = styled.div`
|
|
181
|
+
font-weight: ${tokens.fontWeightMedium};
|
|
182
|
+
color: ${tokens.gray900};
|
|
183
|
+
overflow: hidden;
|
|
184
|
+
text-overflow: ellipsis;
|
|
185
|
+
`;
|
|
186
|
+
var RowSublabel = styled.div`
|
|
187
|
+
color: ${tokens.gray600};
|
|
188
|
+
font-size: ${tokens.fontSizeSmall};
|
|
189
|
+
overflow: hidden;
|
|
190
|
+
text-overflow: ellipsis;
|
|
191
|
+
`;
|
|
192
|
+
var RowMeta = styled.div`
|
|
193
|
+
display: flex;
|
|
194
|
+
flex-wrap: wrap;
|
|
195
|
+
gap: ${tokens.spaceXs};
|
|
196
|
+
margin-left: auto;
|
|
197
|
+
`;
|
|
198
|
+
var MasterCell = styled.td`
|
|
199
|
+
min-width: 96px;
|
|
200
|
+
text-align: center;
|
|
201
|
+
${(p) => p.$leftSticky && css`
|
|
202
|
+
position: sticky;
|
|
203
|
+
left: ${p.$leftOffset};
|
|
204
|
+
z-index: ${tokens.zMaster};
|
|
205
|
+
box-shadow: ${tokens.shadowStickyLeft};
|
|
206
|
+
`};
|
|
207
|
+
`;
|
|
208
|
+
var LeafCell = styled.td`
|
|
209
|
+
min-width: 88px;
|
|
210
|
+
text-align: center;
|
|
211
|
+
|
|
212
|
+
&[data-disabled='true'] {
|
|
213
|
+
opacity: 0.45;
|
|
214
|
+
}
|
|
215
|
+
`;
|
|
216
|
+
var SummaryCell = styled.td`
|
|
217
|
+
min-width: 88px;
|
|
218
|
+
text-align: center;
|
|
219
|
+
color: ${tokens.gray600};
|
|
220
|
+
font-variant-numeric: tabular-nums;
|
|
221
|
+
`;
|
|
222
|
+
var CellControlWrap = styled.div`
|
|
223
|
+
display: inline-flex;
|
|
224
|
+
align-items: center;
|
|
225
|
+
justify-content: center;
|
|
226
|
+
|
|
227
|
+
/* Trim @wordpress/components default vertical margins inside table cells. */
|
|
228
|
+
.components-checkbox-control,
|
|
229
|
+
.components-toggle-control,
|
|
230
|
+
.components-base-control,
|
|
231
|
+
.components-base-control__field {
|
|
232
|
+
margin: 0;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.components-checkbox-control__input-container {
|
|
236
|
+
margin: 0;
|
|
237
|
+
}
|
|
238
|
+
`;
|
|
239
|
+
var Caption = styled.caption`
|
|
240
|
+
${visuallyHiddenCss};
|
|
241
|
+
`;
|
|
242
|
+
var isExpanded = (group, expanded) => group.collapsible === false ? true : expanded.has(group.id);
|
|
243
|
+
var MatrixHeader = ({
|
|
244
|
+
groups,
|
|
245
|
+
master,
|
|
246
|
+
flat,
|
|
247
|
+
stickyHeader,
|
|
248
|
+
stickyFirstColumn,
|
|
249
|
+
masterLeftOffset,
|
|
250
|
+
expandedGroups,
|
|
251
|
+
onToggleGroup,
|
|
252
|
+
labels,
|
|
253
|
+
renderHeader,
|
|
254
|
+
rowHeaderLabel
|
|
255
|
+
}) => {
|
|
256
|
+
if (flat) {
|
|
257
|
+
const cols = groups[0]?.columns ?? [];
|
|
258
|
+
return /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
259
|
+
/* @__PURE__ */ jsx(
|
|
260
|
+
HeaderCell,
|
|
261
|
+
{
|
|
262
|
+
scope: "col",
|
|
263
|
+
$sticky: stickyHeader,
|
|
264
|
+
$leftSticky: stickyFirstColumn,
|
|
265
|
+
$leftOffset: "0",
|
|
266
|
+
children: rowHeaderLabel ?? ""
|
|
267
|
+
}
|
|
268
|
+
),
|
|
269
|
+
master && /* @__PURE__ */ jsx(
|
|
270
|
+
HeaderCell,
|
|
271
|
+
{
|
|
272
|
+
scope: "col",
|
|
273
|
+
$sticky: stickyHeader,
|
|
274
|
+
$leftSticky: stickyFirstColumn,
|
|
275
|
+
$leftOffset: masterLeftOffset,
|
|
276
|
+
$align: "center",
|
|
277
|
+
children: master.label
|
|
278
|
+
}
|
|
279
|
+
),
|
|
280
|
+
cols.map((col) => /* @__PURE__ */ jsx(
|
|
281
|
+
HeaderCell,
|
|
282
|
+
{
|
|
283
|
+
scope: "col",
|
|
284
|
+
$sticky: stickyHeader,
|
|
285
|
+
$align: "center",
|
|
286
|
+
"aria-description": col.description,
|
|
287
|
+
children: renderHeader ? renderHeader(col) : col.label
|
|
288
|
+
},
|
|
289
|
+
col.id
|
|
290
|
+
))
|
|
291
|
+
] }) });
|
|
292
|
+
}
|
|
293
|
+
const TOP_ROW_HEIGHT = "36px";
|
|
294
|
+
return /* @__PURE__ */ jsxs("thead", { children: [
|
|
295
|
+
/* @__PURE__ */ jsxs("tr", { children: [
|
|
296
|
+
/* @__PURE__ */ jsx(
|
|
297
|
+
HeaderCell,
|
|
298
|
+
{
|
|
299
|
+
scope: "col",
|
|
300
|
+
rowSpan: 2,
|
|
301
|
+
$sticky: stickyHeader,
|
|
302
|
+
$leftSticky: stickyFirstColumn,
|
|
303
|
+
$leftOffset: "0",
|
|
304
|
+
children: rowHeaderLabel ?? ""
|
|
305
|
+
}
|
|
306
|
+
),
|
|
307
|
+
master && /* @__PURE__ */ jsx(
|
|
308
|
+
HeaderCell,
|
|
309
|
+
{
|
|
310
|
+
scope: "col",
|
|
311
|
+
rowSpan: 2,
|
|
312
|
+
$sticky: stickyHeader,
|
|
313
|
+
$leftSticky: stickyFirstColumn,
|
|
314
|
+
$leftOffset: masterLeftOffset,
|
|
315
|
+
$align: "center",
|
|
316
|
+
children: master.label
|
|
317
|
+
}
|
|
318
|
+
),
|
|
319
|
+
groups.map((group) => {
|
|
320
|
+
const open = isExpanded(group, expandedGroups);
|
|
321
|
+
const span = open ? group.columns.length : 1;
|
|
322
|
+
const collapsible = group.collapsible !== false;
|
|
323
|
+
return /* @__PURE__ */ jsx(
|
|
324
|
+
HeaderCell,
|
|
325
|
+
{
|
|
326
|
+
scope: "colgroup",
|
|
327
|
+
colSpan: span,
|
|
328
|
+
rowSpan: open ? 1 : 2,
|
|
329
|
+
$sticky: stickyHeader,
|
|
330
|
+
$align: "center",
|
|
331
|
+
children: collapsible ? /* @__PURE__ */ jsxs(
|
|
332
|
+
GroupHeaderButton,
|
|
333
|
+
{
|
|
334
|
+
type: "button",
|
|
335
|
+
"aria-expanded": open,
|
|
336
|
+
"aria-label": open ? labels.collapseGroup : labels.expandGroup,
|
|
337
|
+
onClick: () => onToggleGroup(group.id),
|
|
338
|
+
children: [
|
|
339
|
+
/* @__PURE__ */ jsx(Caret, { $open: open, "aria-hidden": true }),
|
|
340
|
+
group.label
|
|
341
|
+
]
|
|
342
|
+
}
|
|
343
|
+
) : /* @__PURE__ */ jsx("span", { children: group.label })
|
|
344
|
+
},
|
|
345
|
+
group.id
|
|
346
|
+
);
|
|
347
|
+
})
|
|
348
|
+
] }),
|
|
349
|
+
/* @__PURE__ */ jsx("tr", { children: groups.map((group) => {
|
|
350
|
+
const open = isExpanded(group, expandedGroups);
|
|
351
|
+
if (!open) return null;
|
|
352
|
+
return group.columns.map((col) => /* @__PURE__ */ jsx(
|
|
353
|
+
HeaderCell,
|
|
354
|
+
{
|
|
355
|
+
scope: "col",
|
|
356
|
+
$sticky: stickyHeader,
|
|
357
|
+
$top: TOP_ROW_HEIGHT,
|
|
358
|
+
$align: "center",
|
|
359
|
+
"aria-description": col.description,
|
|
360
|
+
children: renderHeader ? renderHeader(col) : col.label
|
|
361
|
+
},
|
|
362
|
+
`${group.id}::${col.id}`
|
|
363
|
+
));
|
|
364
|
+
}) })
|
|
365
|
+
] });
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// components/matrix/utils.ts
|
|
369
|
+
var cellKey = (rowId, colId) => `${rowId}::${colId}`;
|
|
370
|
+
var selectionByRow = (value, rowId) => {
|
|
371
|
+
const prefix = `${rowId}::`;
|
|
372
|
+
const out = [];
|
|
373
|
+
for (const key of value) {
|
|
374
|
+
if (key.startsWith(prefix)) out.push(key.slice(prefix.length));
|
|
375
|
+
}
|
|
376
|
+
return out;
|
|
377
|
+
};
|
|
378
|
+
var toggleCell = (value, rowId, colId, checked) => {
|
|
379
|
+
const next = new Set(value);
|
|
380
|
+
if (checked) next.add(cellKey(rowId, colId));
|
|
381
|
+
else next.delete(cellKey(rowId, colId));
|
|
382
|
+
return next;
|
|
383
|
+
};
|
|
384
|
+
var normalizeGroups = (input) => {
|
|
385
|
+
if (input.groups) return { groups: input.groups, flat: false };
|
|
386
|
+
return {
|
|
387
|
+
groups: [
|
|
388
|
+
{
|
|
389
|
+
id: "__flat__",
|
|
390
|
+
label: "",
|
|
391
|
+
columns: input.columns ?? [],
|
|
392
|
+
collapsible: false,
|
|
393
|
+
defaultExpanded: true
|
|
394
|
+
}
|
|
395
|
+
],
|
|
396
|
+
flat: true
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
var defaultLabels = (overrides = {}) => ({
|
|
400
|
+
expandGroup: overrides.expandGroup ?? "Expand group",
|
|
401
|
+
collapseGroup: overrides.collapseGroup ?? "Collapse group",
|
|
402
|
+
groupSummary: overrides.groupSummary ?? ((selected, total) => `${selected} / ${total}`),
|
|
403
|
+
cellToggled: overrides.cellToggled ?? ((rowLabel, colLabel, checked) => checked ? `${colLabel} added to ${rowLabel}` : `${colLabel} removed from ${rowLabel}`)
|
|
404
|
+
});
|
|
405
|
+
var toPlainText = (node) => {
|
|
406
|
+
if (node === null || node === void 0 || node === false || node === true) return "";
|
|
407
|
+
if (typeof node === "string" || typeof node === "number") return String(node);
|
|
408
|
+
if (Array.isArray(node)) return node.map(toPlainText).join(" ");
|
|
409
|
+
if (typeof node === "object" && "props" in node) {
|
|
410
|
+
const props = node.props;
|
|
411
|
+
if (props && "children" in props) return toPlainText(props.children);
|
|
412
|
+
}
|
|
413
|
+
return "";
|
|
414
|
+
};
|
|
415
|
+
var MatrixCell = ({
|
|
416
|
+
row,
|
|
417
|
+
column,
|
|
418
|
+
value,
|
|
419
|
+
disabled,
|
|
420
|
+
variant,
|
|
421
|
+
onChange,
|
|
422
|
+
renderCell
|
|
423
|
+
}) => {
|
|
424
|
+
const key = cellKey(row.id, column.id);
|
|
425
|
+
const checked = value.has(key);
|
|
426
|
+
const toggle = useCallback(
|
|
427
|
+
(nextChecked) => {
|
|
428
|
+
if (disabled) return;
|
|
429
|
+
const next = toggleCell(value, row.id, column.id, nextChecked);
|
|
430
|
+
onChange(next, { rowId: row.id, colId: column.id, checked: nextChecked });
|
|
431
|
+
},
|
|
432
|
+
[column.id, disabled, onChange, row.id, value]
|
|
433
|
+
);
|
|
434
|
+
const ariaLabel = `${toPlainText(row.label)} \u2014 ${toPlainText(column.label)}`;
|
|
435
|
+
const content = renderCell ? renderCell({ row, column, checked, disabled, toggle, selectionKey: key }) : variant === "toggle" ? /* @__PURE__ */ jsx(
|
|
436
|
+
ToggleControl,
|
|
437
|
+
{
|
|
438
|
+
__nextHasNoMarginBottom: true,
|
|
439
|
+
label: "",
|
|
440
|
+
checked,
|
|
441
|
+
disabled,
|
|
442
|
+
"aria-label": ariaLabel,
|
|
443
|
+
onChange: toggle
|
|
444
|
+
}
|
|
445
|
+
) : /* @__PURE__ */ jsx(
|
|
446
|
+
CheckboxControl,
|
|
447
|
+
{
|
|
448
|
+
__nextHasNoMarginBottom: true,
|
|
449
|
+
label: "",
|
|
450
|
+
checked,
|
|
451
|
+
disabled,
|
|
452
|
+
"aria-label": ariaLabel,
|
|
453
|
+
onChange: toggle
|
|
454
|
+
}
|
|
455
|
+
);
|
|
456
|
+
return /* @__PURE__ */ jsx(LeafCell, { "data-disabled": disabled || void 0, children: /* @__PURE__ */ jsx(CellControlWrap, { children: content }) });
|
|
457
|
+
};
|
|
458
|
+
var MatrixGroupSummary = ({ ctx, labels, render }) => {
|
|
459
|
+
if (render) return /* @__PURE__ */ jsx(SummaryCell, { children: render(ctx) });
|
|
460
|
+
return /* @__PURE__ */ jsx(SummaryCell, { children: labels.groupSummary(ctx.selectedCount, ctx.totalCount) });
|
|
461
|
+
};
|
|
462
|
+
var isGroupExpanded = (group, expanded) => group.collapsible === false ? true : expanded.has(group.id);
|
|
463
|
+
var countSelected = (value, rowId, cols) => {
|
|
464
|
+
let n = 0;
|
|
465
|
+
for (const c of cols) if (value.has(cellKey(rowId, c.id))) n += 1;
|
|
466
|
+
return n;
|
|
467
|
+
};
|
|
468
|
+
var MatrixRowView = ({
|
|
469
|
+
row,
|
|
470
|
+
groups,
|
|
471
|
+
master,
|
|
472
|
+
flat,
|
|
473
|
+
value,
|
|
474
|
+
expandedGroups,
|
|
475
|
+
stickyFirstColumn,
|
|
476
|
+
masterLeftOffset,
|
|
477
|
+
variant,
|
|
478
|
+
onChange,
|
|
479
|
+
labels,
|
|
480
|
+
renderCell,
|
|
481
|
+
renderRowHeader,
|
|
482
|
+
renderGroupSummary
|
|
483
|
+
}) => {
|
|
484
|
+
const masterChecked = master ? value.has(cellKey(row.id, master.id)) : true;
|
|
485
|
+
const masterDisablesRow = master ? master.disablesRow !== false : false;
|
|
486
|
+
const rowDisabled = master ? masterDisablesRow && !masterChecked : false;
|
|
487
|
+
const toggleMaster = useCallback(
|
|
488
|
+
(checked) => {
|
|
489
|
+
if (!master) return;
|
|
490
|
+
const next = toggleCell(value, row.id, master.id, checked);
|
|
491
|
+
onChange(next, { rowId: row.id, colId: master.id, checked });
|
|
492
|
+
},
|
|
493
|
+
[master, onChange, row.id, value]
|
|
494
|
+
);
|
|
495
|
+
return /* @__PURE__ */ jsxs("tr", { children: [
|
|
496
|
+
/* @__PURE__ */ jsx(RowHeaderCell, { scope: "row", $leftSticky: stickyFirstColumn, children: renderRowHeader ? renderRowHeader(row) : /* @__PURE__ */ jsxs(RowHeaderInner, { children: [
|
|
497
|
+
row.avatar,
|
|
498
|
+
/* @__PURE__ */ jsxs(RowHeaderText, { children: [
|
|
499
|
+
/* @__PURE__ */ jsx(RowLabel, { children: row.label }),
|
|
500
|
+
row.sublabel && /* @__PURE__ */ jsx(RowSublabel, { children: row.sublabel })
|
|
501
|
+
] }),
|
|
502
|
+
row.meta && /* @__PURE__ */ jsx(RowMeta, { children: row.meta })
|
|
503
|
+
] }) }),
|
|
504
|
+
master && /* @__PURE__ */ jsx(MasterCell, { $leftSticky: stickyFirstColumn, $leftOffset: masterLeftOffset, children: /* @__PURE__ */ jsx(CellControlWrap, { children: /* @__PURE__ */ jsx(
|
|
505
|
+
ToggleControl,
|
|
506
|
+
{
|
|
507
|
+
__nextHasNoMarginBottom: true,
|
|
508
|
+
label: "",
|
|
509
|
+
checked: masterChecked,
|
|
510
|
+
"aria-label": `${toPlainText(master.label)} \u2014 ${toPlainText(row.label)}`,
|
|
511
|
+
onChange: toggleMaster
|
|
512
|
+
}
|
|
513
|
+
) }) }),
|
|
514
|
+
flat ? groups[0].columns.map((col) => /* @__PURE__ */ jsx(
|
|
515
|
+
MatrixCell,
|
|
516
|
+
{
|
|
517
|
+
row,
|
|
518
|
+
column: col,
|
|
519
|
+
value,
|
|
520
|
+
disabled: rowDisabled,
|
|
521
|
+
variant,
|
|
522
|
+
onChange,
|
|
523
|
+
renderCell
|
|
524
|
+
},
|
|
525
|
+
col.id
|
|
526
|
+
)) : groups.flatMap((group) => {
|
|
527
|
+
const open = isGroupExpanded(group, expandedGroups);
|
|
528
|
+
if (!open) {
|
|
529
|
+
return [
|
|
530
|
+
/* @__PURE__ */ jsx(
|
|
531
|
+
MatrixGroupSummary,
|
|
532
|
+
{
|
|
533
|
+
ctx: {
|
|
534
|
+
row,
|
|
535
|
+
group,
|
|
536
|
+
selectedCount: countSelected(value, row.id, group.columns),
|
|
537
|
+
totalCount: group.columns.length
|
|
538
|
+
},
|
|
539
|
+
labels,
|
|
540
|
+
render: renderGroupSummary
|
|
541
|
+
},
|
|
542
|
+
group.id
|
|
543
|
+
)
|
|
544
|
+
];
|
|
545
|
+
}
|
|
546
|
+
return group.columns.map((col) => /* @__PURE__ */ jsx(
|
|
547
|
+
MatrixCell,
|
|
548
|
+
{
|
|
549
|
+
row,
|
|
550
|
+
column: col,
|
|
551
|
+
value,
|
|
552
|
+
disabled: rowDisabled,
|
|
553
|
+
variant,
|
|
554
|
+
onChange,
|
|
555
|
+
renderCell
|
|
556
|
+
},
|
|
557
|
+
`${group.id}::${col.id}`
|
|
558
|
+
));
|
|
559
|
+
})
|
|
560
|
+
] });
|
|
561
|
+
};
|
|
562
|
+
var EmotionCacheProvider = ({ children }) => {
|
|
563
|
+
const [head, setHead] = useState(null);
|
|
564
|
+
const captureDocument = useCallback((node) => {
|
|
565
|
+
if (!node) return;
|
|
566
|
+
const owner = node.ownerDocument ?? document;
|
|
567
|
+
setHead((prev) => prev === owner.head ? prev : owner.head);
|
|
568
|
+
}, []);
|
|
569
|
+
const cache = useMemo(() => {
|
|
570
|
+
if (!head) return null;
|
|
571
|
+
return createCache({ key: "valu-mx", container: head, prepend: true });
|
|
572
|
+
}, [head]);
|
|
573
|
+
if (!cache) {
|
|
574
|
+
return /* @__PURE__ */ jsx("div", { ref: captureDocument, style: { display: "contents" } });
|
|
575
|
+
}
|
|
576
|
+
return /* @__PURE__ */ jsx(CacheProvider, { value: cache, children: /* @__PURE__ */ jsx("div", { ref: captureDocument, style: { display: "contents" }, children }) });
|
|
577
|
+
};
|
|
578
|
+
var useExpandedGroups = ({
|
|
579
|
+
expanded: controlled,
|
|
580
|
+
defaultExpanded,
|
|
581
|
+
onChange
|
|
582
|
+
}) => {
|
|
583
|
+
const [uncontrolled, setUncontrolled] = useState(
|
|
584
|
+
() => defaultExpanded ?? /* @__PURE__ */ new Set()
|
|
585
|
+
);
|
|
586
|
+
const isControlled = controlled !== void 0;
|
|
587
|
+
const expanded = isControlled ? controlled : uncontrolled;
|
|
588
|
+
const apply = useCallback(
|
|
589
|
+
(next) => {
|
|
590
|
+
if (!isControlled) setUncontrolled(next);
|
|
591
|
+
onChange?.(next);
|
|
592
|
+
},
|
|
593
|
+
[isControlled, onChange]
|
|
594
|
+
);
|
|
595
|
+
const isExpanded2 = useCallback((groupId) => expanded.has(groupId), [expanded]);
|
|
596
|
+
const toggle = useCallback(
|
|
597
|
+
(groupId) => {
|
|
598
|
+
const next = new Set(expanded);
|
|
599
|
+
if (next.has(groupId)) next.delete(groupId);
|
|
600
|
+
else next.add(groupId);
|
|
601
|
+
apply(next);
|
|
602
|
+
},
|
|
603
|
+
[apply, expanded]
|
|
604
|
+
);
|
|
605
|
+
const expand = useCallback(
|
|
606
|
+
(groupId) => {
|
|
607
|
+
if (expanded.has(groupId)) return;
|
|
608
|
+
const next = new Set(expanded);
|
|
609
|
+
next.add(groupId);
|
|
610
|
+
apply(next);
|
|
611
|
+
},
|
|
612
|
+
[apply, expanded]
|
|
613
|
+
);
|
|
614
|
+
const collapse = useCallback(
|
|
615
|
+
(groupId) => {
|
|
616
|
+
if (!expanded.has(groupId)) return;
|
|
617
|
+
const next = new Set(expanded);
|
|
618
|
+
next.delete(groupId);
|
|
619
|
+
apply(next);
|
|
620
|
+
},
|
|
621
|
+
[apply, expanded]
|
|
622
|
+
);
|
|
623
|
+
return { expanded, isExpanded: isExpanded2, toggle, expand, collapse };
|
|
624
|
+
};
|
|
625
|
+
var DEFAULT_MAX_HEIGHT = "calc(100vh - 260px)";
|
|
626
|
+
var Matrix = (props) => {
|
|
627
|
+
const {
|
|
628
|
+
rows,
|
|
629
|
+
master,
|
|
630
|
+
value,
|
|
631
|
+
onChange,
|
|
632
|
+
expanded,
|
|
633
|
+
defaultExpanded,
|
|
634
|
+
onExpandedChange,
|
|
635
|
+
variant = "checkbox",
|
|
636
|
+
renderCell,
|
|
637
|
+
renderRowHeader,
|
|
638
|
+
renderGroupSummary,
|
|
639
|
+
renderHeader,
|
|
640
|
+
stickyHeader = true,
|
|
641
|
+
stickyFirstColumn = true,
|
|
642
|
+
maxHeight = DEFAULT_MAX_HEIGHT,
|
|
643
|
+
caption,
|
|
644
|
+
labels,
|
|
645
|
+
className,
|
|
646
|
+
id
|
|
647
|
+
} = props;
|
|
648
|
+
const reactId = useId();
|
|
649
|
+
const rootId = id ?? `valu-mx-${reactId.replace(/:/g, "")}`;
|
|
650
|
+
const { groups, flat } = useMemo(
|
|
651
|
+
() => normalizeGroups({ columns: props.columns, groups: props.groups }),
|
|
652
|
+
[props.columns, props.groups]
|
|
653
|
+
);
|
|
654
|
+
const resolvedLabels = useMemo(() => defaultLabels(labels), [labels]);
|
|
655
|
+
const { expanded: expandedGroups, toggle: toggleGroup } = useExpandedGroups({
|
|
656
|
+
expanded,
|
|
657
|
+
defaultExpanded,
|
|
658
|
+
onChange: onExpandedChange
|
|
659
|
+
});
|
|
660
|
+
const maxHeightCss = typeof maxHeight === "number" ? `${maxHeight}px` : maxHeight;
|
|
661
|
+
const masterLeftOffset = "220px";
|
|
662
|
+
return /* @__PURE__ */ jsx(EmotionCacheProvider, { children: /* @__PURE__ */ jsx(Root, { id: rootId, className, children: /* @__PURE__ */ jsx(Viewport, { $maxHeight: maxHeightCss, children: /* @__PURE__ */ jsxs(Table, { children: [
|
|
663
|
+
caption && /* @__PURE__ */ jsx(Caption, { children: caption }),
|
|
664
|
+
/* @__PURE__ */ jsx(
|
|
665
|
+
MatrixHeader,
|
|
666
|
+
{
|
|
667
|
+
groups,
|
|
668
|
+
master,
|
|
669
|
+
flat,
|
|
670
|
+
stickyHeader,
|
|
671
|
+
stickyFirstColumn,
|
|
672
|
+
masterLeftOffset,
|
|
673
|
+
expandedGroups,
|
|
674
|
+
onToggleGroup: toggleGroup,
|
|
675
|
+
labels: resolvedLabels,
|
|
676
|
+
renderHeader
|
|
677
|
+
}
|
|
678
|
+
),
|
|
679
|
+
/* @__PURE__ */ jsx("tbody", { children: rows.map((row) => /* @__PURE__ */ jsx(
|
|
680
|
+
MatrixRowView,
|
|
681
|
+
{
|
|
682
|
+
row,
|
|
683
|
+
groups,
|
|
684
|
+
master,
|
|
685
|
+
flat,
|
|
686
|
+
value,
|
|
687
|
+
expandedGroups,
|
|
688
|
+
stickyFirstColumn,
|
|
689
|
+
masterLeftOffset,
|
|
690
|
+
variant,
|
|
691
|
+
onChange,
|
|
692
|
+
labels: resolvedLabels,
|
|
693
|
+
renderCell,
|
|
694
|
+
renderRowHeader,
|
|
695
|
+
renderGroupSummary
|
|
696
|
+
},
|
|
697
|
+
row.id
|
|
698
|
+
)) })
|
|
699
|
+
] }) }) }) });
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
export { Matrix, cellKey, selectionByRow, toggleCell };
|
|
703
|
+
//# sourceMappingURL=index.js.map
|
|
704
|
+
//# sourceMappingURL=index.js.map
|