@cerberus-design/data-grid 0.25.3 → 1.0.0-rc.5

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.
@@ -1,312 +1,290 @@
1
- 'use client';
2
- import { jsxs, jsx } from 'react/jsx-runtime';
3
- import { For, Show, useCerberusContext, Tooltip, IconButton } from '@cerberus-design/react';
4
- import { useRead } from '@cerberus-design/signals';
5
- import { useRef, memo, useMemo } from 'react';
6
- import { Scrollable, Box, HStack, Stack } from 'styled-system/jsx';
7
- import { PARTS, SCOPE } from '../const.js';
8
- import { useDataGridContext } from '../context.client.js';
9
- import { useColumnStyles, usePinnedAttribute, usePinnedState } from '../hooks.client.js';
10
- import { useVirtualizer } from '../virtualizer.client.js';
11
- import { HeaderCellOptions } from './features.client.js';
12
-
1
+ "use client";
2
+ "use client";
3
+ import { PARTS, SCOPE } from "../const.js";
4
+ import { useDataGridContext } from "../context.client.js";
5
+ import { useColumnStyles, usePinnedAttribute, usePinnedState } from "../hooks.client.js";
6
+ import { useVirtualizer } from "../virtualizer.client.js";
7
+ import { HeaderCellOptions } from "./features.client.js";
8
+ import { For, IconButton, Show, Tooltip, useCerberusContext } from "@cerberus-design/react";
9
+ import { useRead } from "@cerberus-design/signals";
10
+ import { memo, useMemo, useRef } from "react";
11
+ import { Box, HStack, Scrollable, Stack } from "styled-system/jsx";
12
+ import { jsx, jsxs } from "react/jsx-runtime";
13
+ //#region src/components/grid.client.tsx
13
14
  function GridViewport() {
14
- const viewportRef = useRef(null);
15
- const store = useDataGridContext();
16
- const { virtualRows, totalHeight } = useVirtualizer(store, viewportRef);
17
- const columns = useRead(store.columns);
18
- const isServerPaginated = useRead(store.isServerPaginated);
19
- const staticRows = useRead(store.rows);
20
- const currentPageRange = useRead(store.currentPageRange);
21
- return /* @__PURE__ */ jsxs(
22
- Scrollable,
23
- {
24
- hideScrollbar: true,
25
- "data-scope": SCOPE,
26
- "data-part": PARTS.VIEWPORT,
27
- h: "full",
28
- minH: "0",
29
- minW: "full",
30
- pos: "relative",
31
- w: "full",
32
- ref: viewportRef,
33
- children: [
34
- /* @__PURE__ */ jsx(
35
- Box,
36
- {
37
- role: "grid",
38
- "aria-rowcount": staticRows.length + 1,
39
- "aria-colcount": columns.length,
40
- h: "var(--row-height)",
41
- pos: "sticky",
42
- top: "0",
43
- w: "var(--total-grid-width)",
44
- zIndex: "sticky",
45
- children: /* @__PURE__ */ jsx(
46
- HStack,
47
- {
48
- "aria-rowIndex": "1",
49
- role: "rowgroup",
50
- gap: "0",
51
- h: "full",
52
- pos: "relative",
53
- w: "full",
54
- children: /* @__PURE__ */ jsx(For, { each: columns, children: (col) => /* @__PURE__ */ jsx(Show, { when: col.isVisible(), children: () => /* @__PURE__ */ jsx(GridHeaderCell, { column: col }) }, col.id) })
55
- }
56
- )
57
- }
58
- ),
59
- /* @__PURE__ */ jsx(
60
- Show,
61
- {
62
- when: isServerPaginated,
63
- fallback: /* @__PURE__ */ jsx(
64
- Box,
65
- {
66
- pos: "relative",
67
- w: "var(--total-grid-width)",
68
- style: {
69
- height: `${totalHeight}px`
70
- },
71
- children: /* @__PURE__ */ jsx(For, { each: virtualRows, children: (vRow) => /* @__PURE__ */ jsx(
72
- GridRow,
73
- {
74
- row: vRow.data,
75
- index: vRow.index,
76
- offsetY: vRow.offsetY
77
- },
78
- vRow.index
79
- ) })
80
- }
81
- ),
82
- children: () => /* @__PURE__ */ jsx(Stack, { direction: "column", gap: "0", w: "var(--total-grid-width)", children: /* @__PURE__ */ jsx(
83
- For,
84
- {
85
- each: staticRows.slice(
86
- currentPageRange.start,
87
- currentPageRange.end
88
- ),
89
- children: (row, index) => /* @__PURE__ */ jsx(GridRow, { row, index }, index)
90
- }
91
- ) })
92
- }
93
- )
94
- ]
95
- }
96
- );
15
+ const viewportRef = useRef(null);
16
+ const store = useDataGridContext();
17
+ const { virtualRows, totalHeight } = useVirtualizer(store, viewportRef);
18
+ const columns = useRead(store.columns);
19
+ const isServerPaginated = useRead(store.isServerPaginated);
20
+ const staticRows = useRead(store.rows);
21
+ const currentPageRange = useRead(store.currentPageRange);
22
+ return /* @__PURE__ */ jsxs(Scrollable, {
23
+ hideScrollbar: true,
24
+ "data-scope": SCOPE,
25
+ "data-part": PARTS.VIEWPORT,
26
+ h: "full",
27
+ minH: "0",
28
+ minW: "full",
29
+ pos: "relative",
30
+ w: "full",
31
+ ref: viewportRef,
32
+ children: [/* @__PURE__ */ jsx(Box, {
33
+ role: "grid",
34
+ "aria-rowcount": staticRows.length + 1,
35
+ "aria-colcount": columns.length,
36
+ h: "var(--row-height)",
37
+ pos: "sticky",
38
+ top: "0",
39
+ w: "var(--total-grid-width)",
40
+ zIndex: "sticky",
41
+ children: /* @__PURE__ */ jsx(HStack, {
42
+ "aria-rowindex": 1,
43
+ role: "rowgroup",
44
+ gap: "0",
45
+ h: "full",
46
+ pos: "relative",
47
+ w: "full",
48
+ children: /* @__PURE__ */ jsx(For, {
49
+ each: columns,
50
+ children: (col) => /* @__PURE__ */ jsx(Show, {
51
+ when: col.isVisible(),
52
+ children: () => /* @__PURE__ */ jsx(GridHeaderCell, { column: col })
53
+ }, col.id)
54
+ })
55
+ })
56
+ }), /* @__PURE__ */ jsx(Show, {
57
+ when: isServerPaginated,
58
+ fallback: /* @__PURE__ */ jsx(Box, {
59
+ pos: "relative",
60
+ w: "var(--total-grid-width)",
61
+ style: { height: `${totalHeight}px` },
62
+ children: /* @__PURE__ */ jsx(For, {
63
+ each: virtualRows,
64
+ children: (vRow) => /* @__PURE__ */ jsx(GridRow, {
65
+ row: vRow.data,
66
+ index: vRow.index,
67
+ offsetY: vRow.offsetY
68
+ }, vRow.index)
69
+ })
70
+ }),
71
+ children: () => /* @__PURE__ */ jsx(Stack, {
72
+ direction: "column",
73
+ gap: "0",
74
+ w: "var(--total-grid-width)",
75
+ children: /* @__PURE__ */ jsx(For, {
76
+ each: staticRows.slice(currentPageRange.start, currentPageRange.end),
77
+ children: (row, index) => /* @__PURE__ */ jsx(GridRow, {
78
+ row,
79
+ index
80
+ }, index)
81
+ })
82
+ })
83
+ })]
84
+ });
97
85
  }
98
- const GridHeaderCell = memo(function GridHeaderCell2(props) {
99
- const { column } = props;
100
- const store = useDataGridContext();
101
- const { icons } = useCerberusContext();
102
- const pinnedVal = useRead(column.pinned);
103
- const sortingVal = useRead(store.sorting);
104
- const pinnedState = usePinnedState(pinnedVal);
105
- const pinnedAttr = usePinnedAttribute(pinnedVal);
106
- const style = useColumnStyles(column, pinnedVal);
107
- const isSortedDesc = useMemo(() => {
108
- const result = sortingVal.find((item) => item.id === column.id);
109
- if (result) return result.desc;
110
- return false;
111
- }, [sortingVal, column.id]);
112
- const SortAscIcon = icons.sortAsc;
113
- const SortDescIcon = icons.sortDesc;
114
- function handleToggleSorting(e) {
115
- store.toggleSort(column.id, e.shiftKey);
116
- }
117
- return /* @__PURE__ */ jsxs(
118
- HStack,
119
- {
120
- role: "columnheader",
121
- "data-scope": SCOPE,
122
- "data-part": PARTS.HEAD_CELL,
123
- "data-state": pinnedState,
124
- "data-col": column.id,
125
- ...pinnedAttr,
126
- bgColor: "page.bg.initial",
127
- borderBottom: "1px solid",
128
- borderColor: "page.border.200",
129
- h: "full",
130
- justify: "space-between",
131
- pos: "relative",
132
- px: "md",
133
- userSelect: "none",
134
- textAlign: "left",
135
- textStyle: "label-md",
136
- verticalAlign: "middle",
137
- w: "full",
138
- zIndex: "10",
139
- _first: {
140
- borderTopLeftRadius: "md"
141
- },
142
- _last: {
143
- borderTopRightRadius: "md"
144
- },
145
- _pinned: {
146
- pos: "sticky",
147
- zIndex: 20
148
- },
149
- _leftPinned: {
150
- borderRightColor: "page.border.200",
151
- borderRight: "1px solid"
152
- },
153
- _rightPinned: {
154
- borderLeftColor: "page.border.200",
155
- borderLeft: "1px solid"
156
- },
157
- className: "group",
158
- style,
159
- children: [
160
- /* @__PURE__ */ jsxs(
161
- HStack,
162
- {
163
- gap: "sm",
164
- _groupHover: {
165
- "& :is(button)": {
166
- opacity: "1"
167
- }
168
- },
169
- children: [
170
- typeof column.original.header === "function" ? column.original.header({ colId: column.id }) : column.original.header,
171
- /* @__PURE__ */ jsx(Show, { when: column.sortable, children: /* @__PURE__ */ jsx(
172
- Tooltip,
173
- {
174
- content: isSortedDesc ? "Sort Ascending" : "Sort Decending",
175
- openDelay: 800,
176
- asChild: true,
177
- children: /* @__PURE__ */ jsx(
178
- IconButton,
179
- {
180
- ariaLabel: "Toggle sorting",
181
- size: "sm",
182
- opacity: {
183
- base: 1,
184
- md: 0
185
- },
186
- transitionProperty: "opacity",
187
- transitionDuration: "fast",
188
- onClick: handleToggleSorting,
189
- children: /* @__PURE__ */ jsx(Show, { when: isSortedDesc, fallback: /* @__PURE__ */ jsx(SortAscIcon, {}), children: /* @__PURE__ */ jsx(SortDescIcon, {}) })
190
- }
191
- )
192
- }
193
- ) })
194
- ]
195
- }
196
- ),
197
- /* @__PURE__ */ jsx(HeaderCellOptions, { ...column }),
198
- /* @__PURE__ */ jsx(Show, { when: pinnedState === "pinned", children: () => /* @__PURE__ */ jsx(ShadowFiller, { style, ...pinnedAttr }) })
199
- ]
200
- }
201
- );
86
+ var GridHeaderCell = memo(function GridHeaderCell(props) {
87
+ const { column } = props;
88
+ const store = useDataGridContext();
89
+ const { icons } = useCerberusContext();
90
+ const pinnedVal = useRead(column.pinned);
91
+ const sortingVal = useRead(store.sorting);
92
+ const pinnedState = usePinnedState(pinnedVal);
93
+ const pinnedAttr = usePinnedAttribute(pinnedVal);
94
+ const style = useColumnStyles(column, pinnedVal);
95
+ const isSortedDesc = useMemo(() => {
96
+ const result = sortingVal.find((item) => item.id === column.id);
97
+ if (result) return result.desc;
98
+ return false;
99
+ }, [sortingVal, column.id]);
100
+ const SortAscIcon = icons.sortAsc;
101
+ const SortDescIcon = icons.sortDesc;
102
+ function handleToggleSorting(e) {
103
+ store.toggleSort(column.id, e.shiftKey);
104
+ }
105
+ return /* @__PURE__ */ jsxs(HStack, {
106
+ role: "columnheader",
107
+ "data-scope": SCOPE,
108
+ "data-part": PARTS.HEAD_CELL,
109
+ "data-state": pinnedState,
110
+ "data-col": column.id,
111
+ ...pinnedAttr,
112
+ bgColor: "page.bg.initial",
113
+ borderBottom: "1px solid",
114
+ borderColor: "page.border.200",
115
+ h: "full",
116
+ justify: "space-between",
117
+ pos: "relative",
118
+ px: "md",
119
+ userSelect: "none",
120
+ textAlign: "left",
121
+ textStyle: "label-md",
122
+ verticalAlign: "middle",
123
+ w: "full",
124
+ zIndex: "10",
125
+ _first: { borderTopLeftRadius: "md" },
126
+ _last: { borderTopRightRadius: "md" },
127
+ _pinned: {
128
+ pos: "sticky",
129
+ zIndex: 20
130
+ },
131
+ _leftPinned: {
132
+ borderRightColor: "page.border.200",
133
+ borderRight: "1px solid"
134
+ },
135
+ _rightPinned: {
136
+ borderLeftColor: "page.border.200",
137
+ borderLeft: "1px solid"
138
+ },
139
+ className: "group",
140
+ style,
141
+ children: [
142
+ /* @__PURE__ */ jsxs(HStack, {
143
+ gap: "sm",
144
+ _groupHover: { "& :is(button)": { opacity: "1" } },
145
+ children: [typeof column.original.header === "function" ? column.original.header({ colId: column.id }) : column.original.header, /* @__PURE__ */ jsx(Show, {
146
+ when: column.sortable,
147
+ children: /* @__PURE__ */ jsx(Tooltip, {
148
+ content: isSortedDesc ? "Sort Ascending" : "Sort Decending",
149
+ openDelay: 800,
150
+ asChild: true,
151
+ children: /* @__PURE__ */ jsx(IconButton, {
152
+ ariaLabel: "Toggle sorting",
153
+ size: "sm",
154
+ opacity: {
155
+ base: 1,
156
+ md: 0
157
+ },
158
+ transitionProperty: "opacity",
159
+ transitionDuration: "fast",
160
+ onClick: handleToggleSorting,
161
+ children: /* @__PURE__ */ jsx(Show, {
162
+ when: isSortedDesc,
163
+ fallback: /* @__PURE__ */ jsx(SortAscIcon, {}),
164
+ children: /* @__PURE__ */ jsx(SortDescIcon, {})
165
+ })
166
+ })
167
+ })
168
+ })]
169
+ }),
170
+ /* @__PURE__ */ jsx(HeaderCellOptions, { ...column }),
171
+ /* @__PURE__ */ jsx(Show, {
172
+ when: pinnedState === "pinned",
173
+ children: () => /* @__PURE__ */ jsx(ShadowFiller, {
174
+ style,
175
+ ...pinnedAttr
176
+ })
177
+ })
178
+ ]
179
+ });
202
180
  });
203
- const GridCell = memo(function GridCell2(props) {
204
- const { column, row } = props;
205
- const value = column.getValue(row);
206
- const pinnedVal = useRead(column.pinned);
207
- const pinnedState = usePinnedState(pinnedVal);
208
- const pinnedAttr = usePinnedAttribute(pinnedVal);
209
- const style = useColumnStyles(column, pinnedVal);
210
- return /* @__PURE__ */ jsxs(
211
- HStack,
212
- {
213
- role: "cell",
214
- "data-scope": SCOPE,
215
- "data-part": PARTS.CELL,
216
- "data-state": pinnedState,
217
- ...pinnedAttr,
218
- bgColor: "inherit",
219
- borderColor: "page.border.200",
220
- h: "full",
221
- pos: "relative",
222
- px: "md",
223
- userSelect: "none",
224
- verticalAlign: "middle",
225
- zIndex: "base",
226
- _pinned: {
227
- pos: "sticky",
228
- zIndex: 20
229
- },
230
- _leftPinned: {
231
- borderRightColor: "page.border.200",
232
- borderRight: "1px solid"
233
- },
234
- _rightPinned: {
235
- borderLeftColor: "page.border.200",
236
- borderLeft: "1px solid"
237
- },
238
- style,
239
- children: [
240
- column.original.cell ? column.original.cell({ row, value }) : value,
241
- /* @__PURE__ */ jsx(Show, { when: pinnedState === "pinned", children: () => /* @__PURE__ */ jsx(ShadowFiller, { style, ...pinnedAttr }) })
242
- ]
243
- }
244
- );
181
+ var GridCell = memo(function GridCell(props) {
182
+ const { column, row } = props;
183
+ const value = column.getValue(row);
184
+ const pinnedVal = useRead(column.pinned);
185
+ const pinnedState = usePinnedState(pinnedVal);
186
+ const pinnedAttr = usePinnedAttribute(pinnedVal);
187
+ const style = useColumnStyles(column, pinnedVal);
188
+ return /* @__PURE__ */ jsxs(HStack, {
189
+ role: "cell",
190
+ "data-scope": SCOPE,
191
+ "data-part": PARTS.CELL,
192
+ "data-state": pinnedState,
193
+ ...pinnedAttr,
194
+ bgColor: "inherit",
195
+ borderColor: "page.border.200",
196
+ h: "full",
197
+ pos: "relative",
198
+ px: "md",
199
+ userSelect: "none",
200
+ verticalAlign: "middle",
201
+ zIndex: "base",
202
+ _pinned: {
203
+ pos: "sticky",
204
+ zIndex: 20
205
+ },
206
+ _leftPinned: {
207
+ borderRightColor: "page.border.200",
208
+ borderRight: "1px solid"
209
+ },
210
+ _rightPinned: {
211
+ borderLeftColor: "page.border.200",
212
+ borderLeft: "1px solid"
213
+ },
214
+ style,
215
+ children: [column.original.cell ? column.original.cell({
216
+ row,
217
+ value
218
+ }) : value, /* @__PURE__ */ jsx(Show, {
219
+ when: pinnedState === "pinned",
220
+ children: () => /* @__PURE__ */ jsx(ShadowFiller, {
221
+ style,
222
+ ...pinnedAttr
223
+ })
224
+ })]
225
+ });
245
226
  });
246
- const GridRow = memo(function GridRow2(props) {
247
- const store = useDataGridContext();
248
- const columns = useRead(store.columns);
249
- return /* @__PURE__ */ jsx(GridRowContainer, { offsetY: props.offsetY, children: /* @__PURE__ */ jsx(For, { each: columns, children: (col) => /* @__PURE__ */ jsx(Show, { when: col.isVisible(), children: () => /* @__PURE__ */ jsx(GridCell, { row: props.row, column: col }) }, col.id) }) });
227
+ var GridRow = memo(function GridRow(props) {
228
+ const columns = useRead(useDataGridContext().columns);
229
+ return /* @__PURE__ */ jsx(GridRowContainer, {
230
+ offsetY: props.offsetY,
231
+ children: /* @__PURE__ */ jsx(For, {
232
+ each: columns,
233
+ children: (col) => /* @__PURE__ */ jsx(Show, {
234
+ when: col.isVisible(),
235
+ children: () => /* @__PURE__ */ jsx(GridCell, {
236
+ row: props.row,
237
+ column: col
238
+ })
239
+ }, col.id)
240
+ })
241
+ });
250
242
  });
251
243
  function GridRowContainer(props) {
252
- const isVirtualized = props.offsetY !== void 0;
253
- return /* @__PURE__ */ jsx(
254
- HStack,
255
- {
256
- role: "row",
257
- "data-scope": SCOPE,
258
- "data-part": PARTS.ROW,
259
- "data-render": isVirtualized ? "virtualized" : "static",
260
- bgColor: "page.surface.100",
261
- gap: "0",
262
- h: "var(--row-height)",
263
- w: "full",
264
- _even: {
265
- bgColor: "page.surface.initial"
266
- },
267
- _hover: {
268
- bgColor: "page.surface.200"
269
- },
270
- css: {
271
- "&:is([data-render=virtualized])": {
272
- pos: "absolute",
273
- left: 0
274
- }
275
- },
276
- style: {
277
- transform: isVirtualized ? `translateY(${props.offsetY}px)` : void 0
278
- },
279
- children: props.children
280
- }
281
- );
244
+ const isVirtualized = props.offsetY !== void 0;
245
+ return /* @__PURE__ */ jsx(HStack, {
246
+ role: "row",
247
+ "data-scope": SCOPE,
248
+ "data-part": PARTS.ROW,
249
+ "data-render": isVirtualized ? "virtualized" : "static",
250
+ bgColor: "page.surface.100",
251
+ gap: "0",
252
+ h: "var(--row-height)",
253
+ w: "full",
254
+ _even: { bgColor: "page.surface.initial" },
255
+ _hover: { bgColor: "page.surface.200" },
256
+ css: { "&:is([data-render=virtualized])": {
257
+ pos: "absolute",
258
+ left: 0
259
+ } },
260
+ style: { transform: isVirtualized ? `translateY(${props.offsetY}px)` : void 0 },
261
+ children: props.children
262
+ });
282
263
  }
283
264
  function ShadowFiller(props) {
284
- const { style, ...rest } = props;
285
- return /* @__PURE__ */ jsx(
286
- Box,
287
- {
288
- bottom: "0",
289
- h: "full",
290
- pos: "absolute",
291
- w: "0.75rem",
292
- zIndex: "inherit",
293
- _leftPinned: {
294
- bgGradient: "to-r",
295
- gradientFrom: "black/10",
296
- gradientTo: "transparent"
297
- },
298
- _rightPinned: {
299
- bgGradient: "to-l",
300
- gradientFrom: "black/10",
301
- gradientTo: "transparent"
302
- },
303
- style: {
304
- left: style.left ? style.width : void 0,
305
- right: style.right ? style.width : void 0
306
- },
307
- ...rest
308
- }
309
- );
265
+ const { style, ...rest } = props;
266
+ return /* @__PURE__ */ jsx(Box, {
267
+ bottom: "0",
268
+ h: "full",
269
+ pos: "absolute",
270
+ w: "0.75rem",
271
+ zIndex: "inherit",
272
+ _leftPinned: {
273
+ bgGradient: "to-r",
274
+ gradientFrom: "black/10",
275
+ gradientTo: "transparent"
276
+ },
277
+ _rightPinned: {
278
+ bgGradient: "to-l",
279
+ gradientFrom: "black/10",
280
+ gradientTo: "transparent"
281
+ },
282
+ style: {
283
+ left: style.left ? style.width : void 0,
284
+ right: style.right ? style.width : void 0
285
+ },
286
+ ...rest
287
+ });
310
288
  }
311
-
312
- export { GridCell, GridHeaderCell, GridRow, GridViewport };
289
+ //#endregion
290
+ export { GridViewport };