@gridui/data-explorer 1.1.1 → 1.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/dist/index.js CHANGED
@@ -1,4 +1,8 @@
1
- // src/data-explorer/model.ts
1
+ // src/shared/control-row-style.ts
2
+ import { cva } from "class-variance-authority";
3
+ var explorerControlRowStyles = cva("gridui-u-flex gridui-u-min-w-0 gridui-u-flex-wrap gridui-u-items-end gridui-u-gap-gridui-16 gridui-u-p-gridui-16 gridui-u-[&_label]:min-h-11");
4
+
5
+ // src/shared/model.ts
2
6
  var compareValues = (left, right, direction) => {
3
7
  if (left === null || left === void 0) {
4
8
  return right === null || right === void 0 ? 0 : 1;
@@ -110,10 +114,166 @@ var resolveExplorerWidth = (column, width = column.width ?? 200) => {
110
114
  return Math.max(min, Math.min(max, width));
111
115
  };
112
116
 
113
- // src/data-explorer/column-resize-handle.tsx
117
+ // src/shared/explorer-context.ts
118
+ import { createContext, useContext, useId } from "react";
119
+ var useExplorerController = (options) => {
120
+ const { rows, columns, state, onStateChange, filter, pageSize = 10, selectedIds = [], busy = false, columnSettings } = options;
121
+ const tableId = useId();
122
+ const view = selectExplorerRows(rows, columns, state, pageSize, filter);
123
+ const existingRowIds = new Set(rows.map((row) => {
124
+ return row.id;
125
+ }));
126
+ const selectedRowIds = [...new Set(selectedIds)].filter((id) => {
127
+ return existingRowIds.has(id);
128
+ });
129
+ const pageIds = view.rows.map((row) => {
130
+ return row.id;
131
+ });
132
+ const selectedPageCount = pageIds.filter((id) => {
133
+ return selectedRowIds.includes(id);
134
+ }).length;
135
+ const updateState = (changes) => {
136
+ if (!busy) {
137
+ onStateChange({
138
+ ...state,
139
+ ...changes
140
+ });
141
+ }
142
+ };
143
+ const visibleColumns = columns.filter((column) => {
144
+ return !columnSettings?.hiddenKeys.includes(column.key);
145
+ });
146
+ if (columns.length > 0 && visibleColumns.length === 0) {
147
+ throw new RangeError("DataExplorer needs at least one visible column.");
148
+ }
149
+ return {
150
+ ...options,
151
+ busy,
152
+ tableId,
153
+ view,
154
+ selectedRowIds,
155
+ pageIds,
156
+ selectedPageCount,
157
+ updateState,
158
+ visibleColumns
159
+ };
160
+ };
161
+ var explorerContext = createContext(null);
162
+ var useExplorer = () => {
163
+ const explorer = useContext(explorerContext);
164
+ if (!explorer) {
165
+ throw new Error("DataExplorer parts require a DataExplorer.");
166
+ }
167
+ return explorer;
168
+ };
169
+
170
+ // src/data-explorer-bulk-actions/data-explorer-bulk-actions.tsx
171
+ import { Button } from "@gridui/button";
172
+ import { Checkbox } from "@gridui/choice";
173
+ import { cn } from "@gridui/style-utils";
174
+ import { jsx, jsxs } from "react/jsx-runtime";
175
+ var DataExplorerBulkActions = (props) => {
176
+ const { actions = [], busy, labels, onSelectionChange, pageIds, selectedPageCount, selectedRowIds } = useExplorer();
177
+ if (!onSelectionChange) {
178
+ return null;
179
+ }
180
+ return /* @__PURE__ */ jsxs(
181
+ "div",
182
+ {
183
+ className: cn(explorerControlRowStyles(), props.className),
184
+ children: [
185
+ /* @__PURE__ */ jsxs("label", { children: [
186
+ /* @__PURE__ */ jsx(
187
+ Checkbox,
188
+ {
189
+ checked: pageIds.length > 0 && selectedPageCount === pageIds.length,
190
+ indeterminate: selectedPageCount > 0 && selectedPageCount < pageIds.length,
191
+ disabled: busy || pageIds.length === 0,
192
+ onCheckedChange: (checked) => {
193
+ onSelectionChange(selectExplorerPage(selectedRowIds, pageIds, checked));
194
+ }
195
+ }
196
+ ),
197
+ " ",
198
+ labels.selectPage
199
+ ] }),
200
+ /* @__PURE__ */ jsx(
201
+ "span",
202
+ {
203
+ role: "status",
204
+ children: labels.selected(selectedRowIds.length)
205
+ }
206
+ ),
207
+ actions.map((action) => {
208
+ return /* @__PURE__ */ jsx(
209
+ Button,
210
+ {
211
+ variant: "secondary",
212
+ disabled: busy || selectedRowIds.length === 0 || action.disabled,
213
+ onClick: () => {
214
+ action.onAction(selectedRowIds);
215
+ },
216
+ children: action.label
217
+ },
218
+ action.label
219
+ );
220
+ })
221
+ ]
222
+ }
223
+ );
224
+ };
225
+
226
+ // src/data-explorer-pagination/data-explorer-pagination.tsx
227
+ import { Button as Button2 } from "@gridui/button";
228
+ import { cn as cn2 } from "@gridui/style-utils";
229
+ import { cva as cva2 } from "class-variance-authority";
230
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
231
+ var explorerFooterStyles = cva2("gridui-u-flex gridui-u-flex-wrap gridui-u-items-center gridui-u-justify-between gridui-u-gap-gridui-8 gridui-u-p-gridui-16");
232
+ var DataExplorerPagination = (props) => {
233
+ const { busy, labels, updateState, view } = useExplorer();
234
+ return /* @__PURE__ */ jsxs2(
235
+ "div",
236
+ {
237
+ className: cn2(explorerFooterStyles(), props.className),
238
+ children: [
239
+ /* @__PURE__ */ jsx2(
240
+ Button2,
241
+ {
242
+ variant: "secondary",
243
+ disabled: busy || view.page === 0,
244
+ onClick: () => {
245
+ updateState({ page: view.page - 1 });
246
+ },
247
+ children: labels.previous
248
+ }
249
+ ),
250
+ /* @__PURE__ */ jsx2(
251
+ "span",
252
+ {
253
+ role: "status",
254
+ children: labels.page(view.page + 1, view.pageCount, view.total)
255
+ }
256
+ ),
257
+ /* @__PURE__ */ jsx2(
258
+ Button2,
259
+ {
260
+ variant: "secondary",
261
+ disabled: busy || view.page + 1 >= view.pageCount,
262
+ onClick: () => {
263
+ updateState({ page: view.page + 1 });
264
+ },
265
+ children: labels.next
266
+ }
267
+ )
268
+ ]
269
+ }
270
+ );
271
+ };
272
+
273
+ // src/data-explorer-table/column-resize-handle.tsx
114
274
  import { isComposingKeyEvent, listenForDragCancellation } from "@gridui/interaction";
115
275
  import { useEffect, useLayoutEffect, useRef } from "react";
116
- import { jsx } from "react/jsx-runtime";
276
+ import { jsx as jsx3 } from "react/jsx-runtime";
117
277
  var ColumnResizeHandle = (props) => {
118
278
  const { column, width, label, disabled, onChange } = props;
119
279
  const handle = useRef(null);
@@ -154,7 +314,7 @@ var ColumnResizeHandle = (props) => {
154
314
  }
155
315
  };
156
316
  }, []);
157
- return /* @__PURE__ */ jsx(
317
+ return /* @__PURE__ */ jsx3(
158
318
  "div",
159
319
  {
160
320
  ref: handle,
@@ -235,22 +395,203 @@ var ColumnResizeHandle = (props) => {
235
395
  );
236
396
  };
237
397
 
238
- // src/data-explorer/style.ts
239
- import { cva } from "class-variance-authority";
240
- var explorerStyles = cva("gridui-u-min-w-0 gridui-u-border gridui-u-border-border gridui-u-bg-background gridui-u-text-foreground");
241
- var explorerToolbarStyles = cva("gridui-u-flex gridui-u-min-w-0 gridui-u-flex-wrap gridui-u-items-end gridui-u-gap-gridui-16 gridui-u-p-gridui-16 gridui-u-[&_label]:min-h-11");
242
- var explorerLabelStyles = cva("gridui-u-flex gridui-u-min-w-0 gridui-u-flex-[1_1_10rem] gridui-u-flex-col gridui-u-gap-gridui-4 gridui-u-text-muted gridui-u-type-caption");
243
- var explorerFooterStyles = cva("gridui-u-flex gridui-u-flex-wrap gridui-u-items-center gridui-u-justify-between gridui-u-gap-gridui-8 gridui-u-p-gridui-16");
398
+ // src/data-explorer-table/data-explorer-table.tsx
399
+ import { Button as Button3 } from "@gridui/button";
400
+ import { Checkbox as Checkbox2 } from "@gridui/choice";
401
+ import { Split } from "@gridui/grid";
402
+ import { cn as cn3 } from "@gridui/style-utils";
403
+ import { DataTable } from "@gridui/table";
404
+ import { Children } from "react";
405
+ import { Fragment, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
406
+ var DataExplorerTable = (props) => {
407
+ const { busy, columnSizing, details, labels, notice, onSelectionChange, renderCell, selectedRowIds, tableId, view, visibleColumns } = useExplorer();
408
+ const hasNotice = Children.toArray(notice).some((child) => {
409
+ return child !== "";
410
+ });
411
+ const fixedWidths = columnSizing !== void 0 || visibleColumns.some((column) => {
412
+ return column.width !== void 0 || column.sticky !== void 0;
413
+ });
414
+ const widths = visibleColumns.map((column) => {
415
+ return resolveExplorerWidth(column, columnSizing?.widths[column.key] ?? column.width);
416
+ });
417
+ const tableColumns = visibleColumns.map((column, index) => {
418
+ const width = widths[index];
419
+ const stickyOffset = visibleColumns.reduce((offset, current, currentIndex) => {
420
+ return current.sticky === column.sticky && (column.sticky === "start" ? currentIndex < index : currentIndex > index) ? offset + widths[currentIndex] : offset;
421
+ }, 0);
422
+ return {
423
+ key: `column:${column.key}`,
424
+ label: column.label,
425
+ ...column.numeric !== void 0 ? { numeric: column.numeric } : {},
426
+ ...fixedWidths ? {
427
+ sizing: {
428
+ width,
429
+ minWidth: width,
430
+ maxWidth: width
431
+ }
432
+ } : {},
433
+ ...column.sticky ? {
434
+ sticky: column.sticky,
435
+ stickyOffset
436
+ } : {},
437
+ ...columnSizing ? {
438
+ header: /* @__PURE__ */ jsxs3(
439
+ "div",
440
+ {
441
+ className: "gridui-u-flex gridui-u-min-w-0 gridui-u-items-center gridui-u-justify-between gridui-u-gap-gridui-8",
442
+ children: [
443
+ /* @__PURE__ */ jsx4("span", { children: column.label }),
444
+ /* @__PURE__ */ jsx4(
445
+ ColumnResizeHandle,
446
+ {
447
+ column,
448
+ width,
449
+ label: columnSizing.resizeLabel(column),
450
+ disabled: busy,
451
+ onChange: (nextWidth) => {
452
+ columnSizing.onWidthsChange({
453
+ ...columnSizing.widths,
454
+ [column.key]: nextWidth
455
+ });
456
+ }
457
+ }
458
+ )
459
+ ]
460
+ }
461
+ )
462
+ } : {}
463
+ };
464
+ });
465
+ if (onSelectionChange) {
466
+ tableColumns.unshift({
467
+ key: "control:selection",
468
+ label: labels.selection,
469
+ ...fixedWidths ? { sizing: { width: 56 } } : {}
470
+ });
471
+ }
472
+ if (details) {
473
+ tableColumns.unshift({
474
+ key: "control:details",
475
+ label: details.columnLabel,
476
+ ...fixedWidths ? { sizing: { width: 96 } } : {}
477
+ });
478
+ }
479
+ const tableWidth = widths.reduce((total, width) => {
480
+ return total + width;
481
+ }, 0) + (onSelectionChange ? 56 : 0) + (details ? 96 : 0);
482
+ const detailId = (row) => {
483
+ return `${tableId}-details-${encodeURIComponent(row.id)}`;
484
+ };
485
+ const tableRows = view.rows.map((row) => {
486
+ const cells = visibleColumns.map((column) => {
487
+ const value = row.values[column.key];
488
+ if (renderCell) {
489
+ return renderCell(value, row, column);
490
+ }
491
+ return value ?? "";
492
+ });
493
+ if (onSelectionChange) {
494
+ cells.unshift(/* @__PURE__ */ jsx4(
495
+ Checkbox2,
496
+ {
497
+ "aria-label": labels.selectRow(row),
498
+ checked: selectedRowIds.includes(row.id),
499
+ disabled: busy,
500
+ onCheckedChange: (checked) => {
501
+ const nextSelection = selectExplorerPage(selectedRowIds, [row.id], checked);
502
+ onSelectionChange(nextSelection);
503
+ }
504
+ },
505
+ "select"
506
+ ));
507
+ }
508
+ if (details) {
509
+ const expanded = details.expandedIds.includes(row.id);
510
+ cells.unshift(/* @__PURE__ */ jsx4(
511
+ Button3,
512
+ {
513
+ variant: "quiet",
514
+ disabled: busy,
515
+ "aria-expanded": expanded,
516
+ "aria-controls": expanded ? detailId(row) : void 0,
517
+ onClick: () => {
518
+ details.onExpandedIdsChange(expanded ? details.expandedIds.filter((key) => {
519
+ return key !== row.id;
520
+ }) : [
521
+ .../* @__PURE__ */ new Set([
522
+ ...details.expandedIds,
523
+ row.id
524
+ ])
525
+ ]);
526
+ },
527
+ children: expanded ? details.collapseLabel(row) : details.expandLabel(row)
528
+ },
529
+ "details"
530
+ ));
531
+ }
532
+ const detail = details?.expandedIds.includes(row.id) ? {
533
+ id: detailId(row),
534
+ content: details.render(row)
535
+ } : void 0;
536
+ return {
537
+ key: row.id,
538
+ cells,
539
+ ...detail ? { detail } : {}
540
+ };
541
+ });
542
+ return /* @__PURE__ */ jsxs3(Fragment, { children: [
543
+ hasNotice && /* @__PURE__ */ jsx4("div", { children: notice }),
544
+ /* @__PURE__ */ jsxs3(
545
+ Split,
546
+ {
547
+ direction: "column",
548
+ "aria-busy": busy,
549
+ className: cn3(props.className),
550
+ children: [
551
+ /* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(
552
+ DataTable,
553
+ {
554
+ label: labels.table,
555
+ variant: "structured",
556
+ blockEdges: "none",
557
+ inlineEdges: "none",
558
+ columns: tableColumns,
559
+ rows: tableRows,
560
+ ...fixedWidths ? {
561
+ style: {
562
+ width: tableWidth,
563
+ tableLayout: "fixed"
564
+ },
565
+ minWidth: tableWidth
566
+ } : {}
567
+ }
568
+ ) }),
569
+ view.rows.length === 0 && !busy && /* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(
570
+ "p",
571
+ {
572
+ className: "gridui-u-m-0 gridui-u-p-gridui-16",
573
+ children: labels.empty
574
+ }
575
+ ) })
576
+ ]
577
+ }
578
+ )
579
+ ] });
580
+ };
244
581
 
245
- // src/data-explorer/filter-editor.tsx
246
- import { Button } from "@gridui/button";
582
+ // src/data-explorer-toolbar/style.ts
583
+ import { cva as cva3 } from "class-variance-authority";
584
+ var explorerLabelStyles = cva3("gridui-u-flex gridui-u-min-w-0 gridui-u-flex-[1_1_10rem] gridui-u-flex-col gridui-u-gap-gridui-4 gridui-u-text-muted gridui-u-type-caption");
585
+
586
+ // src/data-explorer-toolbar/filter-editor.tsx
587
+ import { Button as Button4 } from "@gridui/button";
247
588
  import { Input } from "@gridui/input";
248
589
  import { Select } from "@gridui/select";
249
- import { useId, useRef as useRef2 } from "react";
250
- import { jsx as jsx2, jsxs } from "react/jsx-runtime";
590
+ import { useId as useId2, useRef as useRef2 } from "react";
591
+ import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
251
592
  var FilterEditor = (props) => {
252
593
  const { filtering, conditions, disabled, onChange } = props;
253
- const id = useId();
594
+ const id = useId2();
254
595
  const sequence = useRef2(0);
255
596
  const addButton = useRef2(null);
256
597
  const { definitions, labels } = filtering;
@@ -260,7 +601,7 @@ var FilterEditor = (props) => {
260
601
  return currentIndex === index ? condition : current;
261
602
  }));
262
603
  };
263
- return /* @__PURE__ */ jsxs("div", { children: [
604
+ return /* @__PURE__ */ jsxs4("div", { children: [
264
605
  conditions.map((condition, index) => {
265
606
  const definition = definitions.find((entry) => {
266
607
  return entry.key === condition.key;
@@ -287,18 +628,18 @@ var FilterEditor = (props) => {
287
628
  "empty"
288
629
  ];
289
630
  }
290
- return /* @__PURE__ */ jsxs(
631
+ return /* @__PURE__ */ jsxs4(
291
632
  "div",
292
633
  {
293
- className: explorerToolbarStyles(),
634
+ className: explorerControlRowStyles(),
294
635
  children: [
295
- /* @__PURE__ */ jsxs(
636
+ /* @__PURE__ */ jsxs4(
296
637
  "label",
297
638
  {
298
639
  className: explorerLabelStyles(),
299
640
  children: [
300
641
  labels.field,
301
- /* @__PURE__ */ jsx2(
642
+ /* @__PURE__ */ jsx5(
302
643
  Select,
303
644
  {
304
645
  "aria-label": labels.field,
@@ -326,13 +667,13 @@ var FilterEditor = (props) => {
326
667
  ]
327
668
  }
328
669
  ),
329
- /* @__PURE__ */ jsxs(
670
+ /* @__PURE__ */ jsxs4(
330
671
  "label",
331
672
  {
332
673
  className: explorerLabelStyles(),
333
674
  children: [
334
675
  labels.operator,
335
- /* @__PURE__ */ jsx2(
676
+ /* @__PURE__ */ jsx5(
336
677
  Select,
337
678
  {
338
679
  "aria-label": labels.operator,
@@ -360,13 +701,13 @@ var FilterEditor = (props) => {
360
701
  ]
361
702
  }
362
703
  ),
363
- condition.operator !== "empty" && /* @__PURE__ */ jsxs(
704
+ condition.operator !== "empty" && /* @__PURE__ */ jsxs4(
364
705
  "label",
365
706
  {
366
707
  className: explorerLabelStyles(),
367
708
  children: [
368
709
  labels.value,
369
- definition?.type === "enum" ? /* @__PURE__ */ jsx2(
710
+ definition?.type === "enum" ? /* @__PURE__ */ jsx5(
370
711
  Select,
371
712
  {
372
713
  "aria-label": labels.value,
@@ -381,7 +722,7 @@ var FilterEditor = (props) => {
381
722
  });
382
723
  }
383
724
  }
384
- ) : /* @__PURE__ */ jsx2(
725
+ ) : /* @__PURE__ */ jsx5(
385
726
  Input,
386
727
  {
387
728
  "aria-label": labels.value,
@@ -400,8 +741,8 @@ var FilterEditor = (props) => {
400
741
  ]
401
742
  }
402
743
  ),
403
- /* @__PURE__ */ jsx2(
404
- Button,
744
+ /* @__PURE__ */ jsx5(
745
+ Button4,
405
746
  {
406
747
  variant: "quiet",
407
748
  disabled,
@@ -419,12 +760,12 @@ var FilterEditor = (props) => {
419
760
  condition.id
420
761
  );
421
762
  }),
422
- /* @__PURE__ */ jsx2(
763
+ /* @__PURE__ */ jsx5(
423
764
  "div",
424
765
  {
425
- className: explorerToolbarStyles(),
426
- children: /* @__PURE__ */ jsx2(
427
- Button,
766
+ className: explorerControlRowStyles(),
767
+ children: /* @__PURE__ */ jsx5(
768
+ Button4,
428
769
  {
429
770
  ref: addButton,
430
771
  variant: "secondary",
@@ -452,16 +793,210 @@ var FilterEditor = (props) => {
452
793
  ] });
453
794
  };
454
795
 
455
- // src/data-explorer/data-explorer.tsx
456
- import { Button as Button2 } from "@gridui/button";
457
- import { Checkbox } from "@gridui/choice";
458
- import { Split } from "@gridui/grid";
796
+ // src/data-explorer-toolbar/data-explorer-toolbar.tsx
797
+ import { Button as Button5 } from "@gridui/button";
798
+ import { Checkbox as Checkbox3 } from "@gridui/choice";
459
799
  import { Input as Input2 } from "@gridui/input";
460
800
  import { Select as Select2 } from "@gridui/select";
461
- import { cn } from "@gridui/style-utils";
462
- import { DataTable } from "@gridui/table";
463
- import { Children, useId as useId2 } from "react";
464
- import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
801
+ import { cn as cn4 } from "@gridui/style-utils";
802
+ import { Fragment as Fragment2, jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
803
+ var DataExplorerToolbar = (props) => {
804
+ const { busy, columnSettings, columns, filter, filtering, labels, state, updateState, visibleColumns } = useExplorer();
805
+ return /* @__PURE__ */ jsxs5(Fragment2, { children: [
806
+ /* @__PURE__ */ jsxs5(
807
+ "div",
808
+ {
809
+ className: cn4(explorerControlRowStyles(), props.className),
810
+ children: [
811
+ /* @__PURE__ */ jsxs5(
812
+ "label",
813
+ {
814
+ className: explorerLabelStyles(),
815
+ children: [
816
+ labels.search,
817
+ /* @__PURE__ */ jsx6(
818
+ Input2,
819
+ {
820
+ type: "search",
821
+ value: state.query,
822
+ disabled: busy,
823
+ onValueChange: (query) => {
824
+ updateState({
825
+ query,
826
+ page: 0
827
+ });
828
+ }
829
+ }
830
+ )
831
+ ]
832
+ }
833
+ ),
834
+ filter && /* @__PURE__ */ jsxs5(
835
+ "label",
836
+ {
837
+ className: explorerLabelStyles(),
838
+ children: [
839
+ filter.label,
840
+ /* @__PURE__ */ jsx6(
841
+ Select2,
842
+ {
843
+ "aria-label": filter.label,
844
+ value: state.filterValue,
845
+ disabled: busy,
846
+ options: [
847
+ {
848
+ value: "",
849
+ label: labels.all
850
+ },
851
+ ...filter.options
852
+ ],
853
+ onValueChange: (filterValue) => {
854
+ updateState({
855
+ filterValue,
856
+ page: 0
857
+ });
858
+ }
859
+ }
860
+ )
861
+ ]
862
+ }
863
+ ),
864
+ /* @__PURE__ */ jsxs5(
865
+ "label",
866
+ {
867
+ className: explorerLabelStyles(),
868
+ children: [
869
+ labels.sort,
870
+ /* @__PURE__ */ jsx6(
871
+ Select2,
872
+ {
873
+ "aria-label": labels.sort,
874
+ value: state.sort?.key ?? "",
875
+ disabled: busy,
876
+ options: [
877
+ {
878
+ value: "",
879
+ label: labels.unsorted
880
+ },
881
+ ...columns.map((column) => {
882
+ return {
883
+ value: column.key,
884
+ label: column.label
885
+ };
886
+ })
887
+ ],
888
+ onValueChange: (key) => {
889
+ updateState({
890
+ sort: key ? {
891
+ key,
892
+ direction: "ascending"
893
+ } : null,
894
+ page: 0
895
+ });
896
+ }
897
+ }
898
+ )
899
+ ]
900
+ }
901
+ ),
902
+ /* @__PURE__ */ jsx6(
903
+ Button5,
904
+ {
905
+ variant: "secondary",
906
+ disabled: busy || !state.sort,
907
+ onClick: () => {
908
+ if (state.sort) {
909
+ updateState({
910
+ sort: {
911
+ ...state.sort,
912
+ direction: state.sort.direction === "ascending" ? "descending" : "ascending"
913
+ },
914
+ page: 0
915
+ });
916
+ }
917
+ },
918
+ children: state.sort?.direction === "descending" ? labels.descending : labels.ascending
919
+ }
920
+ )
921
+ ]
922
+ }
923
+ ),
924
+ columnSettings && /* @__PURE__ */ jsxs5(
925
+ "details",
926
+ {
927
+ className: "gridui-u-p-gridui-16",
928
+ children: [
929
+ /* @__PURE__ */ jsx6(
930
+ "summary",
931
+ {
932
+ className: "gridui-u-cursor-pointer gridui-u-type-caption",
933
+ children: columnSettings.label
934
+ }
935
+ ),
936
+ /* @__PURE__ */ jsx6(
937
+ "div",
938
+ {
939
+ className: "gridui-u-flex gridui-u-flex-wrap gridui-u-gap-gridui-16 gridui-u-pt-gridui-16",
940
+ children: columns.map((column) => {
941
+ const visible = !columnSettings.hiddenKeys.includes(column.key);
942
+ return /* @__PURE__ */ jsxs5(
943
+ "label",
944
+ {
945
+ className: "gridui-u-flex gridui-u-min-h-11 gridui-u-items-center gridui-u-gap-gridui-8",
946
+ children: [
947
+ /* @__PURE__ */ jsx6(
948
+ Checkbox3,
949
+ {
950
+ checked: visible,
951
+ disabled: busy || visible && visibleColumns.length === 1,
952
+ onCheckedChange: (checked) => {
953
+ columnSettings.onHiddenKeysChange(checked ? columnSettings.hiddenKeys.filter((key) => {
954
+ return key !== column.key;
955
+ }) : [
956
+ .../* @__PURE__ */ new Set([
957
+ ...columnSettings.hiddenKeys,
958
+ column.key
959
+ ])
960
+ ]);
961
+ }
962
+ }
963
+ ),
964
+ column.label
965
+ ]
966
+ },
967
+ column.key
968
+ );
969
+ })
970
+ }
971
+ )
972
+ ]
973
+ }
974
+ ),
975
+ filtering && /* @__PURE__ */ jsx6(
976
+ FilterEditor,
977
+ {
978
+ filtering,
979
+ conditions: state.filters ?? [],
980
+ disabled: busy,
981
+ onChange: (filters) => {
982
+ updateState({
983
+ filters,
984
+ page: 0
985
+ });
986
+ }
987
+ }
988
+ )
989
+ ] });
990
+ };
991
+
992
+ // src/data-explorer/style.ts
993
+ import { cva as cva4 } from "class-variance-authority";
994
+ var explorerStyles = cva4("gridui-u-min-w-0 gridui-u-border gridui-u-border-border gridui-u-bg-background gridui-u-text-foreground");
995
+
996
+ // src/data-explorer/data-explorer.tsx
997
+ import { Split as Split2 } from "@gridui/grid";
998
+ import { cn as cn5 } from "@gridui/style-utils";
999
+ import { Fragment as Fragment3, jsx as jsx7, jsxs as jsxs6 } from "react/jsx-runtime";
465
1000
  var DataExplorer = (props) => {
466
1001
  const {
467
1002
  rows,
@@ -473,497 +1008,69 @@ var DataExplorer = (props) => {
473
1008
  columnSizing,
474
1009
  details,
475
1010
  filtering,
476
- pageSize = 10,
477
- selectedIds = [],
1011
+ pageSize,
1012
+ selectedIds,
478
1013
  onSelectionChange,
479
- actions = [],
1014
+ actions,
480
1015
  renderCell,
481
- busy = false,
1016
+ busy,
482
1017
  notice,
483
1018
  labels,
484
1019
  className,
1020
+ children,
485
1021
  ...restProps
486
1022
  } = props;
487
- const tableId = useId2();
488
- const hasNotice = Children.toArray(notice).some((child) => {
489
- return child !== "";
490
- });
491
- const view = selectExplorerRows(rows, columns, state, pageSize, filter);
492
- const existingRowIds = new Set(rows.map((row) => {
493
- return row.id;
494
- }));
495
- const selectedRowIds = [...new Set(selectedIds)].filter((id) => {
496
- return existingRowIds.has(id);
497
- });
498
- const pageIds = view.rows.map((row) => {
499
- return row.id;
500
- });
501
- const selectedPageCount = pageIds.filter((id) => {
502
- return selectedRowIds.includes(id);
503
- }).length;
504
- const updateState = (changes) => {
505
- if (!busy) {
506
- onStateChange({
507
- ...state,
508
- ...changes
509
- });
510
- }
511
- };
512
- const visibleColumns = columns.filter((column) => {
513
- return !columnSettings?.hiddenKeys.includes(column.key);
514
- });
515
- if (columns.length > 0 && visibleColumns.length === 0) {
516
- throw new RangeError("DataExplorer needs at least one visible column.");
517
- }
518
- const fixedWidths = columnSizing !== void 0 || visibleColumns.some((column) => {
519
- return column.width !== void 0 || column.sticky !== void 0;
520
- });
521
- const widths = visibleColumns.map((column) => {
522
- return resolveExplorerWidth(column, columnSizing?.widths[column.key] ?? column.width);
523
- });
524
- const tableColumns = visibleColumns.map((column, index) => {
525
- const width = widths[index];
526
- const stickyOffset = visibleColumns.reduce((offset, current, currentIndex) => {
527
- return current.sticky === column.sticky && (column.sticky === "start" ? currentIndex < index : currentIndex > index) ? offset + widths[currentIndex] : offset;
528
- }, 0);
529
- return {
530
- key: `column:${column.key}`,
531
- label: column.label,
532
- ...column.numeric !== void 0 ? { numeric: column.numeric } : {},
533
- ...fixedWidths ? {
534
- sizing: {
535
- width,
536
- minWidth: width,
537
- maxWidth: width
538
- }
539
- } : {},
540
- ...column.sticky ? {
541
- sticky: column.sticky,
542
- stickyOffset
543
- } : {},
544
- ...columnSizing ? {
545
- header: /* @__PURE__ */ jsxs2(
546
- "div",
547
- {
548
- className: "gridui-u-flex gridui-u-min-w-0 gridui-u-items-center gridui-u-justify-between gridui-u-gap-gridui-8",
549
- children: [
550
- /* @__PURE__ */ jsx3("span", { children: column.label }),
551
- /* @__PURE__ */ jsx3(
552
- ColumnResizeHandle,
553
- {
554
- column,
555
- width,
556
- label: columnSizing.resizeLabel(column),
557
- disabled: busy,
558
- onChange: (nextWidth) => {
559
- columnSizing.onWidthsChange({
560
- ...columnSizing.widths,
561
- [column.key]: nextWidth
562
- });
563
- }
564
- }
565
- )
566
- ]
567
- }
568
- )
569
- } : {}
570
- };
571
- });
572
- if (onSelectionChange) {
573
- tableColumns.unshift({
574
- key: "control:selection",
575
- label: labels.selection,
576
- ...fixedWidths ? { sizing: { width: 56 } } : {}
577
- });
578
- }
579
- if (details) {
580
- tableColumns.unshift({
581
- key: "control:details",
582
- label: details.columnLabel,
583
- ...fixedWidths ? { sizing: { width: 96 } } : {}
584
- });
585
- }
586
- const tableWidth = widths.reduce((total, width) => {
587
- return total + width;
588
- }, 0) + (onSelectionChange ? 56 : 0) + (details ? 96 : 0);
589
- const detailId = (row) => {
590
- return `${tableId}-details-${encodeURIComponent(row.id)}`;
591
- };
592
- const tableRows = view.rows.map((row) => {
593
- const cells = visibleColumns.map((column) => {
594
- const value = row.values[column.key];
595
- if (renderCell) {
596
- return renderCell(value, row, column);
597
- }
598
- return value ?? "";
599
- });
600
- if (onSelectionChange) {
601
- cells.unshift(/* @__PURE__ */ jsx3(
602
- Checkbox,
603
- {
604
- "aria-label": labels.selectRow(row),
605
- checked: selectedRowIds.includes(row.id),
606
- disabled: busy,
607
- onCheckedChange: (checked) => {
608
- const nextSelection = selectExplorerPage(selectedRowIds, [row.id], checked);
609
- onSelectionChange(nextSelection);
610
- }
611
- },
612
- "select"
613
- ));
614
- }
615
- if (details) {
616
- const expanded = details.expandedIds.includes(row.id);
617
- cells.unshift(/* @__PURE__ */ jsx3(
618
- Button2,
619
- {
620
- variant: "quiet",
621
- disabled: busy,
622
- "aria-expanded": expanded,
623
- "aria-controls": expanded ? detailId(row) : void 0,
624
- onClick: () => {
625
- details.onExpandedIdsChange(expanded ? details.expandedIds.filter((key) => {
626
- return key !== row.id;
627
- }) : [
628
- .../* @__PURE__ */ new Set([
629
- ...details.expandedIds,
630
- row.id
631
- ])
632
- ]);
633
- },
634
- children: expanded ? details.collapseLabel(row) : details.expandLabel(row)
635
- },
636
- "details"
637
- ));
638
- }
639
- return cells;
1023
+ const explorer = useExplorerController({
1024
+ rows,
1025
+ columns,
1026
+ state,
1027
+ onStateChange,
1028
+ filter,
1029
+ columnSettings,
1030
+ columnSizing,
1031
+ details,
1032
+ filtering,
1033
+ pageSize,
1034
+ selectedIds,
1035
+ onSelectionChange,
1036
+ actions,
1037
+ renderCell,
1038
+ busy,
1039
+ notice,
1040
+ labels
640
1041
  });
641
- return /* @__PURE__ */ jsx3(
642
- "section",
1042
+ return /* @__PURE__ */ jsx7(
1043
+ explorerContext.Provider,
643
1044
  {
644
- ...restProps,
645
- "aria-label": labels.table,
646
- className: cn(explorerStyles(), className),
647
- children: /* @__PURE__ */ jsxs2(
648
- Split,
1045
+ value: explorer,
1046
+ children: /* @__PURE__ */ jsx7(
1047
+ "section",
649
1048
  {
650
- direction: "column",
651
- children: [
652
- /* @__PURE__ */ jsxs2(
653
- "div",
654
- {
655
- className: explorerToolbarStyles(),
656
- children: [
657
- /* @__PURE__ */ jsxs2(
658
- "label",
659
- {
660
- className: explorerLabelStyles(),
661
- children: [
662
- labels.search,
663
- /* @__PURE__ */ jsx3(
664
- Input2,
665
- {
666
- type: "search",
667
- value: state.query,
668
- disabled: busy,
669
- onValueChange: (query) => {
670
- updateState({
671
- query,
672
- page: 0
673
- });
674
- }
675
- }
676
- )
677
- ]
678
- }
679
- ),
680
- filter && /* @__PURE__ */ jsxs2(
681
- "label",
682
- {
683
- className: explorerLabelStyles(),
684
- children: [
685
- filter.label,
686
- /* @__PURE__ */ jsx3(
687
- Select2,
688
- {
689
- "aria-label": filter.label,
690
- value: state.filterValue,
691
- disabled: busy,
692
- options: [
693
- {
694
- value: "",
695
- label: labels.all
696
- },
697
- ...filter.options
698
- ],
699
- onValueChange: (filterValue) => {
700
- updateState({
701
- filterValue,
702
- page: 0
703
- });
704
- }
705
- }
706
- )
707
- ]
708
- }
709
- ),
710
- /* @__PURE__ */ jsxs2(
711
- "label",
712
- {
713
- className: explorerLabelStyles(),
714
- children: [
715
- labels.sort,
716
- /* @__PURE__ */ jsx3(
717
- Select2,
718
- {
719
- "aria-label": labels.sort,
720
- value: state.sort?.key ?? "",
721
- disabled: busy,
722
- options: [
723
- {
724
- value: "",
725
- label: labels.unsorted
726
- },
727
- ...columns.map((column) => {
728
- return {
729
- value: column.key,
730
- label: column.label
731
- };
732
- })
733
- ],
734
- onValueChange: (key) => {
735
- updateState({
736
- sort: key ? {
737
- key,
738
- direction: "ascending"
739
- } : null,
740
- page: 0
741
- });
742
- }
743
- }
744
- )
745
- ]
746
- }
747
- ),
748
- /* @__PURE__ */ jsx3(
749
- Button2,
750
- {
751
- variant: "secondary",
752
- disabled: busy || !state.sort,
753
- onClick: () => {
754
- if (state.sort) {
755
- updateState({
756
- sort: {
757
- ...state.sort,
758
- direction: state.sort.direction === "ascending" ? "descending" : "ascending"
759
- },
760
- page: 0
761
- });
762
- }
763
- },
764
- children: state.sort?.direction === "descending" ? labels.descending : labels.ascending
765
- }
766
- )
767
- ]
768
- }
769
- ),
770
- columnSettings && /* @__PURE__ */ jsxs2(
771
- "details",
772
- {
773
- className: "gridui-u-p-gridui-16",
774
- children: [
775
- /* @__PURE__ */ jsx3(
776
- "summary",
777
- {
778
- className: "gridui-u-cursor-pointer gridui-u-type-caption",
779
- children: columnSettings.label
780
- }
781
- ),
782
- /* @__PURE__ */ jsx3(
783
- "div",
784
- {
785
- className: "gridui-u-flex gridui-u-flex-wrap gridui-u-gap-gridui-16 gridui-u-pt-gridui-16",
786
- children: columns.map((column) => {
787
- const visible = !columnSettings.hiddenKeys.includes(column.key);
788
- return /* @__PURE__ */ jsxs2(
789
- "label",
790
- {
791
- className: "gridui-u-flex gridui-u-min-h-11 gridui-u-items-center gridui-u-gap-gridui-8",
792
- children: [
793
- /* @__PURE__ */ jsx3(
794
- Checkbox,
795
- {
796
- checked: visible,
797
- disabled: busy || visible && visibleColumns.length === 1,
798
- onCheckedChange: (checked) => {
799
- columnSettings.onHiddenKeysChange(checked ? columnSettings.hiddenKeys.filter((key) => {
800
- return key !== column.key;
801
- }) : [
802
- .../* @__PURE__ */ new Set([
803
- ...columnSettings.hiddenKeys,
804
- column.key
805
- ])
806
- ]);
807
- }
808
- }
809
- ),
810
- column.label
811
- ]
812
- },
813
- column.key
814
- );
815
- })
816
- }
817
- )
818
- ]
819
- }
820
- ),
821
- filtering && /* @__PURE__ */ jsx3(
822
- FilterEditor,
823
- {
824
- filtering,
825
- conditions: state.filters ?? [],
826
- disabled: busy,
827
- onChange: (filters) => {
828
- updateState({
829
- filters,
830
- page: 0
831
- });
832
- }
833
- }
834
- ),
835
- onSelectionChange && /* @__PURE__ */ jsxs2(
836
- "div",
837
- {
838
- className: explorerToolbarStyles(),
839
- children: [
840
- /* @__PURE__ */ jsxs2("label", { children: [
841
- /* @__PURE__ */ jsx3(
842
- Checkbox,
843
- {
844
- checked: pageIds.length > 0 && selectedPageCount === pageIds.length,
845
- indeterminate: selectedPageCount > 0 && selectedPageCount < pageIds.length,
846
- disabled: busy || pageIds.length === 0,
847
- onCheckedChange: (checked) => {
848
- onSelectionChange(selectExplorerPage(selectedRowIds, pageIds, checked));
849
- }
850
- }
851
- ),
852
- " ",
853
- labels.selectPage
854
- ] }),
855
- /* @__PURE__ */ jsx3(
856
- "span",
857
- {
858
- role: "status",
859
- children: labels.selected(selectedRowIds.length)
860
- }
861
- ),
862
- actions.map((action) => {
863
- return /* @__PURE__ */ jsx3(
864
- Button2,
865
- {
866
- variant: "secondary",
867
- disabled: busy || selectedRowIds.length === 0 || action.disabled,
868
- onClick: () => {
869
- action.onAction(selectedRowIds);
870
- },
871
- children: action.label
872
- },
873
- action.label
874
- );
875
- })
876
- ]
877
- }
878
- ),
879
- hasNotice && /* @__PURE__ */ jsx3("div", { children: notice }),
880
- /* @__PURE__ */ jsxs2(
881
- Split,
882
- {
883
- direction: "column",
884
- "aria-busy": busy,
885
- children: [
886
- /* @__PURE__ */ jsx3("div", { children: /* @__PURE__ */ jsx3(
887
- DataTable,
888
- {
889
- label: labels.table,
890
- variant: "structured",
891
- blockEdges: "none",
892
- inlineEdges: "none",
893
- columns: tableColumns,
894
- rows: tableRows,
895
- rowKeys: pageIds,
896
- ...fixedWidths ? {
897
- style: {
898
- width: tableWidth,
899
- tableLayout: "fixed"
900
- },
901
- minWidth: tableWidth
902
- } : {},
903
- ...details ? {
904
- rowDetails: view.rows.map((row) => {
905
- return details.expandedIds.includes(row.id) ? {
906
- id: detailId(row),
907
- content: details.render(row)
908
- } : null;
909
- })
910
- } : {}
911
- }
912
- ) }),
913
- view.rows.length === 0 && !busy && /* @__PURE__ */ jsx3("div", { children: /* @__PURE__ */ jsx3(
914
- "p",
915
- {
916
- className: "gridui-u-m-0 gridui-u-p-gridui-16",
917
- children: labels.empty
918
- }
919
- ) })
920
- ]
921
- }
922
- ),
923
- /* @__PURE__ */ jsxs2(
924
- "div",
925
- {
926
- className: explorerFooterStyles(),
927
- children: [
928
- /* @__PURE__ */ jsx3(
929
- Button2,
930
- {
931
- variant: "secondary",
932
- disabled: busy || view.page === 0,
933
- onClick: () => {
934
- updateState({ page: view.page - 1 });
935
- },
936
- children: labels.previous
937
- }
938
- ),
939
- /* @__PURE__ */ jsx3(
940
- "span",
941
- {
942
- role: "status",
943
- children: labels.page(view.page + 1, view.pageCount, view.total)
944
- }
945
- ),
946
- /* @__PURE__ */ jsx3(
947
- Button2,
948
- {
949
- variant: "secondary",
950
- disabled: busy || view.page + 1 >= view.pageCount,
951
- onClick: () => {
952
- updateState({ page: view.page + 1 });
953
- },
954
- children: labels.next
955
- }
956
- )
957
- ]
958
- }
959
- )
960
- ]
1049
+ ...restProps,
1050
+ "aria-label": labels.table,
1051
+ className: cn5(explorerStyles(), className),
1052
+ children: /* @__PURE__ */ jsx7(
1053
+ Split2,
1054
+ {
1055
+ direction: "column",
1056
+ children: children ?? /* @__PURE__ */ jsxs6(Fragment3, { children: [
1057
+ /* @__PURE__ */ jsx7(DataExplorerToolbar, {}),
1058
+ /* @__PURE__ */ jsx7(DataExplorerBulkActions, {}),
1059
+ /* @__PURE__ */ jsx7(DataExplorerTable, {}),
1060
+ /* @__PURE__ */ jsx7(DataExplorerPagination, {})
1061
+ ] })
1062
+ }
1063
+ )
961
1064
  }
962
1065
  )
963
1066
  }
964
1067
  );
965
1068
  };
966
1069
  export {
967
- DataExplorer
1070
+ DataExplorer,
1071
+ DataExplorerBulkActions,
1072
+ DataExplorerPagination,
1073
+ DataExplorerTable,
1074
+ DataExplorerToolbar
968
1075
  };
969
1076
  //# sourceMappingURL=index.js.map