@kivacore/ui-pro-shared 0.0.9 → 0.0.10
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/package.json +187 -21
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import {
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState
|
|
9
|
+
} from 'react';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
getCoreRowModel,
|
|
13
|
+
useReactTable,
|
|
14
|
+
type ColumnDef,
|
|
15
|
+
type ColumnPinningState,
|
|
16
|
+
type ColumnSizingState
|
|
17
|
+
} from '@tanstack/react-table';
|
|
18
|
+
import { useVirtualizer } from '@tanstack/react-virtual';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
buildPivotGridCsv,
|
|
22
|
+
downloadTextFile,
|
|
23
|
+
exportPivotGridExcel,
|
|
24
|
+
exportPivotGridPdf,
|
|
25
|
+
type PivotGridExportSnapshot
|
|
26
|
+
} from '../lib/pivot-grid-export';
|
|
27
|
+
import {
|
|
28
|
+
buildPivotGridHeaderRows,
|
|
29
|
+
buildPivotGridStructure,
|
|
30
|
+
buildPivotGridView
|
|
31
|
+
} from '../lib/pivot-grid-model';
|
|
32
|
+
import {
|
|
33
|
+
compilePivotGridCellColorRules,
|
|
34
|
+
evaluatePivotGridCellColorRules
|
|
35
|
+
} from '../lib/pivot-grid-color-rules';
|
|
36
|
+
import {
|
|
37
|
+
type PivotGridController,
|
|
38
|
+
type PivotGridDrilldownState,
|
|
39
|
+
type PivotGridLeafColumn,
|
|
40
|
+
type PivotGridPinPosition,
|
|
41
|
+
type PivotGridRenderedCell,
|
|
42
|
+
type PivotGridRenderedRow,
|
|
43
|
+
type UsePivotGridProps
|
|
44
|
+
} from '../types';
|
|
45
|
+
|
|
46
|
+
const OVERSCAN = 8;
|
|
47
|
+
const DEFAULT_ROW_HEIGHT = 40;
|
|
48
|
+
const DEFAULT_VALUE_COLUMN_WIDTH = 136;
|
|
49
|
+
const DEFAULT_SUBTOTAL_COLUMN_WIDTH = 144;
|
|
50
|
+
const DEFAULT_GRAND_TOTAL_COLUMN_WIDTH = 156;
|
|
51
|
+
const DEFAULT_ROW_HEADER_WIDTH = 220;
|
|
52
|
+
const DEFAULT_CHILD_ROW_HEADER_WIDTH = 180;
|
|
53
|
+
const GRAND_TOTAL_LABEL = 'Grand Total';
|
|
54
|
+
|
|
55
|
+
function createRowHeaderColumnId(index: number): string {
|
|
56
|
+
return `__pivot_row_header_${index}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function arraysEqual(left: Array<string>, right: Array<string>): boolean {
|
|
60
|
+
if (left.length !== right.length) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return left.every((value, index) => value === right[index]);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createExpandedState(
|
|
68
|
+
ids: Array<string>,
|
|
69
|
+
initial?: Record<string, boolean>
|
|
70
|
+
): Record<string, boolean> {
|
|
71
|
+
const nextState: Record<string, boolean> = {};
|
|
72
|
+
|
|
73
|
+
ids.forEach((id) => {
|
|
74
|
+
nextState[id] = initial?.[id] ?? true;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return nextState;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function reconcileExpandedState(
|
|
81
|
+
previousState: Record<string, boolean>,
|
|
82
|
+
ids: Array<string>
|
|
83
|
+
): Record<string, boolean> {
|
|
84
|
+
const nextState: Record<string, boolean> = {};
|
|
85
|
+
|
|
86
|
+
ids.forEach((id) => {
|
|
87
|
+
nextState[id] = previousState[id] ?? true;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return nextState;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function deriveValueColumnWidth<TData>(
|
|
94
|
+
column: PivotGridLeafColumn<TData>
|
|
95
|
+
): number {
|
|
96
|
+
switch (column.kind) {
|
|
97
|
+
case 'grand-total':
|
|
98
|
+
return DEFAULT_GRAND_TOTAL_COLUMN_WIDTH;
|
|
99
|
+
|
|
100
|
+
case 'subtotal':
|
|
101
|
+
return DEFAULT_SUBTOTAL_COLUMN_WIDTH;
|
|
102
|
+
|
|
103
|
+
default:
|
|
104
|
+
return DEFAULT_VALUE_COLUMN_WIDTH;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildDefaultDrilldownColumns<TData>(
|
|
109
|
+
rows: Array<TData>
|
|
110
|
+
): Array<ColumnDef<TData>> {
|
|
111
|
+
const sampleRow = rows[0];
|
|
112
|
+
|
|
113
|
+
if (!sampleRow || typeof sampleRow !== 'object') {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return Object.keys(sampleRow as Record<string, unknown>).map((key) => {
|
|
118
|
+
const value = (sampleRow as Record<string, unknown>)[key];
|
|
119
|
+
const variant
|
|
120
|
+
= typeof value === 'number' ? 'number' : typeof value === 'boolean' ? 'checkbox' : 'short-text';
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
id: key,
|
|
124
|
+
accessorFn: (row: TData) => {
|
|
125
|
+
if (row && typeof row === 'object' && key in (row as Record<string, unknown>)) {
|
|
126
|
+
return (row as Record<string, unknown>)[key];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return '';
|
|
130
|
+
},
|
|
131
|
+
header: key.replaceAll('_', ' '),
|
|
132
|
+
meta: {
|
|
133
|
+
label: key.replaceAll('_', ' '),
|
|
134
|
+
cell: { variant }
|
|
135
|
+
}
|
|
136
|
+
} as ColumnDef<TData>;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function usePivotGrid<TData>(
|
|
141
|
+
props: UsePivotGridProps<TData>
|
|
142
|
+
): PivotGridController<TData> {
|
|
143
|
+
const {
|
|
144
|
+
data,
|
|
145
|
+
rowDimensions,
|
|
146
|
+
columnDimensions,
|
|
147
|
+
measures,
|
|
148
|
+
getRowId,
|
|
149
|
+
initialState,
|
|
150
|
+
cellColorRules,
|
|
151
|
+
drilldown,
|
|
152
|
+
height = 600,
|
|
153
|
+
className
|
|
154
|
+
} = props;
|
|
155
|
+
|
|
156
|
+
const dataGridRef = useRef<HTMLDivElement>(null);
|
|
157
|
+
const getRowIdRef = useRef(getRowId);
|
|
158
|
+
|
|
159
|
+
getRowIdRef.current = getRowId;
|
|
160
|
+
|
|
161
|
+
const rowHeaderColumnIds = useMemo(
|
|
162
|
+
() => rowDimensions.map((_, index) => createRowHeaderColumnId(index)),
|
|
163
|
+
[rowDimensions]
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const structure = useMemo(
|
|
167
|
+
() => buildPivotGridStructure({
|
|
168
|
+
data,
|
|
169
|
+
rowDimensions,
|
|
170
|
+
columnDimensions,
|
|
171
|
+
measures,
|
|
172
|
+
getRowId: getRowIdRef.current
|
|
173
|
+
}),
|
|
174
|
+
[
|
|
175
|
+
data,
|
|
176
|
+
rowDimensions,
|
|
177
|
+
columnDimensions,
|
|
178
|
+
measures
|
|
179
|
+
]
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const [expandedRowIds, setExpandedRowIds] = useState<Record<string, boolean>>(
|
|
183
|
+
() => createExpandedState(
|
|
184
|
+
structure.allRowToggleIds,
|
|
185
|
+
initialState?.expandedRowIds
|
|
186
|
+
)
|
|
187
|
+
);
|
|
188
|
+
const [expandedColumnIds, setExpandedColumnIds] = useState<Record<string, boolean>>(
|
|
189
|
+
() => createExpandedState(
|
|
190
|
+
structure.allColumnToggleIds,
|
|
191
|
+
initialState?.expandedColumnIds
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
const [columnPinning, setColumnPinning] = useState<ColumnPinningState>(
|
|
195
|
+
() => ({
|
|
196
|
+
left: [
|
|
197
|
+
...rowHeaderColumnIds,
|
|
198
|
+
...((initialState?.columnPinning?.left ?? []).filter(
|
|
199
|
+
columnId => !rowHeaderColumnIds.includes(columnId)
|
|
200
|
+
))
|
|
201
|
+
],
|
|
202
|
+
right: (initialState?.columnPinning?.right ?? []).filter(
|
|
203
|
+
columnId => !rowHeaderColumnIds.includes(columnId)
|
|
204
|
+
)
|
|
205
|
+
})
|
|
206
|
+
);
|
|
207
|
+
const [columnSizing, setColumnSizing] = useState<ColumnSizingState>(
|
|
208
|
+
() => initialState?.columnSizing ?? {}
|
|
209
|
+
);
|
|
210
|
+
const [drilldownState, setDrilldownState] = useState<PivotGridDrilldownState<TData>>({
|
|
211
|
+
open: false,
|
|
212
|
+
cell: null
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const prevAllRowToggleIdsRef = useRef(structure.allRowToggleIds);
|
|
216
|
+
|
|
217
|
+
if (prevAllRowToggleIdsRef.current !== structure.allRowToggleIds) {
|
|
218
|
+
prevAllRowToggleIdsRef.current = structure.allRowToggleIds;
|
|
219
|
+
setExpandedRowIds((previousState) => {
|
|
220
|
+
const nextState = reconcileExpandedState(
|
|
221
|
+
previousState,
|
|
222
|
+
structure.allRowToggleIds
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
return JSON.stringify(previousState) === JSON.stringify(nextState) ? previousState : nextState;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const prevAllColumnToggleIdsRef = useRef(structure.allColumnToggleIds);
|
|
230
|
+
|
|
231
|
+
if (prevAllColumnToggleIdsRef.current !== structure.allColumnToggleIds) {
|
|
232
|
+
prevAllColumnToggleIdsRef.current = structure.allColumnToggleIds;
|
|
233
|
+
setExpandedColumnIds((previousState) => {
|
|
234
|
+
const nextState = reconcileExpandedState(
|
|
235
|
+
previousState,
|
|
236
|
+
structure.allColumnToggleIds
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
return JSON.stringify(previousState) === JSON.stringify(nextState) ? previousState : nextState;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const prevRowHeaderColumnIdsRef = useRef(rowHeaderColumnIds);
|
|
244
|
+
|
|
245
|
+
if (prevRowHeaderColumnIdsRef.current !== rowHeaderColumnIds) {
|
|
246
|
+
prevRowHeaderColumnIdsRef.current = rowHeaderColumnIds;
|
|
247
|
+
|
|
248
|
+
const rowHeaderSet = new Set(rowHeaderColumnIds);
|
|
249
|
+
|
|
250
|
+
setColumnPinning((previousState) => {
|
|
251
|
+
const nextLeft = [
|
|
252
|
+
...rowHeaderColumnIds,
|
|
253
|
+
...((previousState.left ?? []).filter(
|
|
254
|
+
columnId => !rowHeaderSet.has(columnId)
|
|
255
|
+
))
|
|
256
|
+
];
|
|
257
|
+
const nextRight = (previousState.right ?? []).filter(
|
|
258
|
+
columnId => !rowHeaderSet.has(columnId)
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
if (
|
|
262
|
+
arraysEqual(nextLeft, previousState.left ?? [])
|
|
263
|
+
&& arraysEqual(nextRight, previousState.right ?? [])
|
|
264
|
+
) {
|
|
265
|
+
return previousState;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
left: nextLeft,
|
|
270
|
+
right: nextRight
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const prevDrilldownKeyRef = useRef({ open: drilldownState.open, dataLen: structure.data.length });
|
|
276
|
+
|
|
277
|
+
if (
|
|
278
|
+
drilldownState.open && (
|
|
279
|
+
prevDrilldownKeyRef.current.open !== drilldownState.open
|
|
280
|
+
|| prevDrilldownKeyRef.current.dataLen !== structure.data.length
|
|
281
|
+
)
|
|
282
|
+
) {
|
|
283
|
+
prevDrilldownKeyRef.current = { open: drilldownState.open, dataLen: structure.data.length };
|
|
284
|
+
setDrilldownState((previousState) => {
|
|
285
|
+
if (!previousState.cell) {
|
|
286
|
+
return previousState;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const nextCell = previousState.cell.rawRowIndices.every(
|
|
290
|
+
rawRowIndex => rawRowIndex < structure.data.length
|
|
291
|
+
) ? previousState.cell : null;
|
|
292
|
+
|
|
293
|
+
return nextCell ? previousState : {
|
|
294
|
+
open: false,
|
|
295
|
+
cell: null
|
|
296
|
+
};
|
|
297
|
+
});
|
|
298
|
+
} else if (
|
|
299
|
+
prevDrilldownKeyRef.current.open !== drilldownState.open
|
|
300
|
+
|| prevDrilldownKeyRef.current.dataLen !== structure.data.length
|
|
301
|
+
) {
|
|
302
|
+
prevDrilldownKeyRef.current = { open: drilldownState.open, dataLen: structure.data.length };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const viewModel = useMemo(
|
|
306
|
+
() => buildPivotGridView({
|
|
307
|
+
structure,
|
|
308
|
+
expandedRowIds,
|
|
309
|
+
expandedColumnIds
|
|
310
|
+
}),
|
|
311
|
+
[structure, expandedRowIds, expandedColumnIds]
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const valueColumnsById = useMemo(
|
|
315
|
+
() => new Map(viewModel.visibleLeafColumns.map(column => [column.id, column])),
|
|
316
|
+
[viewModel.visibleLeafColumns]
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const columns = useMemo<Array<ColumnDef<PivotGridRenderedRow<TData>>>>(
|
|
320
|
+
() => [
|
|
321
|
+
...rowDimensions.map((dimension, index) => ({
|
|
322
|
+
id: rowHeaderColumnIds[index] ?? createRowHeaderColumnId(index),
|
|
323
|
+
accessorFn: (row: PivotGridRenderedRow<TData>) => row.headerCells[index]?.label ?? '',
|
|
324
|
+
header: dimension.label,
|
|
325
|
+
size: index === 0 ? DEFAULT_ROW_HEADER_WIDTH : DEFAULT_CHILD_ROW_HEADER_WIDTH,
|
|
326
|
+
minSize: 120,
|
|
327
|
+
maxSize: 360,
|
|
328
|
+
enableHiding: false,
|
|
329
|
+
enableResizing: true,
|
|
330
|
+
enablePinning: false
|
|
331
|
+
})),
|
|
332
|
+
...viewModel.visibleLeafColumns.map(column => ({
|
|
333
|
+
id: column.id,
|
|
334
|
+
accessorFn: (row: PivotGridRenderedRow<TData>) => {
|
|
335
|
+
return row.cellMap.get(column.id)?.formattedValue ?? '';
|
|
336
|
+
},
|
|
337
|
+
header: column.headerPath[column.headerPath.length - 1] ?? column.measure.label,
|
|
338
|
+
size: deriveValueColumnWidth(column),
|
|
339
|
+
minSize: 92,
|
|
340
|
+
maxSize: 420,
|
|
341
|
+
enableHiding: false,
|
|
342
|
+
enableResizing: true,
|
|
343
|
+
enablePinning: true
|
|
344
|
+
}))
|
|
345
|
+
],
|
|
346
|
+
[rowDimensions, rowHeaderColumnIds, viewModel.visibleLeafColumns]
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
const table = useReactTable({
|
|
350
|
+
data: viewModel.visibleRows,
|
|
351
|
+
columns,
|
|
352
|
+
state: {
|
|
353
|
+
columnPinning,
|
|
354
|
+
columnSizing
|
|
355
|
+
},
|
|
356
|
+
onColumnPinningChange: setColumnPinning,
|
|
357
|
+
onColumnSizingChange: setColumnSizing,
|
|
358
|
+
enableColumnPinning: true,
|
|
359
|
+
columnResizeMode: 'onChange',
|
|
360
|
+
getCoreRowModel: getCoreRowModel()
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const visibleLeafTableColumns = table.getVisibleLeafColumns();
|
|
364
|
+
const valueColumnIds = useMemo(
|
|
365
|
+
() => visibleLeafTableColumns
|
|
366
|
+
.map(column => column.id)
|
|
367
|
+
.filter(columnId => !rowHeaderColumnIds.includes(columnId)),
|
|
368
|
+
[visibleLeafTableColumns, rowHeaderColumnIds]
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
const pinPositionByColumnId = useMemo(() => {
|
|
372
|
+
const map = new Map<string, PivotGridPinPosition>();
|
|
373
|
+
|
|
374
|
+
valueColumnIds.forEach((columnId) => {
|
|
375
|
+
map.set(columnId, table.getColumn(columnId)?.getIsPinned() ?? false);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
return map;
|
|
379
|
+
}, [table, valueColumnIds]);
|
|
380
|
+
|
|
381
|
+
const headerRows = useMemo(
|
|
382
|
+
() => buildPivotGridHeaderRows({
|
|
383
|
+
columnsById: valueColumnsById,
|
|
384
|
+
orderedColumnIds: valueColumnIds,
|
|
385
|
+
pinPositionByColumnId,
|
|
386
|
+
headerDepth: structure.headerDepth
|
|
387
|
+
}),
|
|
388
|
+
[
|
|
389
|
+
valueColumnsById,
|
|
390
|
+
valueColumnIds,
|
|
391
|
+
pinPositionByColumnId,
|
|
392
|
+
structure.headerDepth
|
|
393
|
+
]
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
const rowVirtualizer = useVirtualizer({
|
|
397
|
+
count: viewModel.visibleRows.length,
|
|
398
|
+
getScrollElement: () => dataGridRef.current,
|
|
399
|
+
estimateSize: () => DEFAULT_ROW_HEIGHT,
|
|
400
|
+
overscan: OVERSCAN
|
|
401
|
+
});
|
|
402
|
+
const virtualItems = rowVirtualizer.getVirtualItems();
|
|
403
|
+
const virtualTotalSize = rowVirtualizer.getTotalSize();
|
|
404
|
+
|
|
405
|
+
const compiledColorRules = useMemo(
|
|
406
|
+
() => compilePivotGridCellColorRules(cellColorRules ?? []),
|
|
407
|
+
[cellColorRules]
|
|
408
|
+
);
|
|
409
|
+
const visibleCells = useMemo(
|
|
410
|
+
() => viewModel.visibleRows.flatMap((row) => {
|
|
411
|
+
if (row.kind === 'group') {
|
|
412
|
+
return [];
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return [...row.cellMap.values()];
|
|
416
|
+
}),
|
|
417
|
+
[viewModel.visibleRows]
|
|
418
|
+
);
|
|
419
|
+
const [cellColorMap, setCellColorMap] = useState<Map<string, string>>(
|
|
420
|
+
() => new Map()
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
useEffect(() => {
|
|
424
|
+
if (compiledColorRules.length === 0) {
|
|
425
|
+
queueMicrotask(() => setCellColorMap(new Map()));
|
|
426
|
+
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let cancelled = false;
|
|
431
|
+
|
|
432
|
+
evaluatePivotGridCellColorRules(compiledColorRules, visibleCells).then((nextMap) => {
|
|
433
|
+
if (!cancelled) {
|
|
434
|
+
setCellColorMap(nextMap);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
return () => {
|
|
439
|
+
cancelled = true;
|
|
440
|
+
};
|
|
441
|
+
}, [compiledColorRules, visibleCells]);
|
|
442
|
+
|
|
443
|
+
const toggleRow = useCallback((nodeId: string) => {
|
|
444
|
+
setExpandedRowIds(previousState => ({
|
|
445
|
+
...previousState,
|
|
446
|
+
[nodeId]: previousState[nodeId] === false
|
|
447
|
+
}));
|
|
448
|
+
}, []);
|
|
449
|
+
|
|
450
|
+
const toggleColumn = useCallback((nodeId: string) => {
|
|
451
|
+
setExpandedColumnIds(previousState => ({
|
|
452
|
+
...previousState,
|
|
453
|
+
[nodeId]: previousState[nodeId] === false
|
|
454
|
+
}));
|
|
455
|
+
}, []);
|
|
456
|
+
|
|
457
|
+
const expandAllRows = useCallback(() => {
|
|
458
|
+
setExpandedRowIds(createExpandedState(structure.allRowToggleIds));
|
|
459
|
+
}, [structure.allRowToggleIds]);
|
|
460
|
+
|
|
461
|
+
const collapseAllRows = useCallback(() => {
|
|
462
|
+
setExpandedRowIds(
|
|
463
|
+
Object.fromEntries(
|
|
464
|
+
structure.allRowToggleIds.map(nodeId => [nodeId, false])
|
|
465
|
+
)
|
|
466
|
+
);
|
|
467
|
+
}, [structure.allRowToggleIds]);
|
|
468
|
+
|
|
469
|
+
const expandAllColumns = useCallback(() => {
|
|
470
|
+
setExpandedColumnIds(createExpandedState(structure.allColumnToggleIds));
|
|
471
|
+
}, [structure.allColumnToggleIds]);
|
|
472
|
+
|
|
473
|
+
const collapseAllColumns = useCallback(() => {
|
|
474
|
+
setExpandedColumnIds(
|
|
475
|
+
Object.fromEntries(
|
|
476
|
+
structure.allColumnToggleIds.map(nodeId => [nodeId, false])
|
|
477
|
+
)
|
|
478
|
+
);
|
|
479
|
+
}, [structure.allColumnToggleIds]);
|
|
480
|
+
|
|
481
|
+
const pinColumns = useCallback(
|
|
482
|
+
(
|
|
483
|
+
requestedIds: Array<string>,
|
|
484
|
+
position: Exclude<PivotGridPinPosition, false>
|
|
485
|
+
) => {
|
|
486
|
+
setColumnPinning((previousState) => {
|
|
487
|
+
const rowHeaderSet = new Set(rowHeaderColumnIds);
|
|
488
|
+
const requestedSet = new Set(requestedIds);
|
|
489
|
+
const orderedIds = [...requestedIds];
|
|
490
|
+
const nextLeft = (previousState.left ?? []).filter(
|
|
491
|
+
columnId => !rowHeaderSet.has(columnId) && !requestedSet.has(columnId)
|
|
492
|
+
);
|
|
493
|
+
const nextRight = (previousState.right ?? []).filter(
|
|
494
|
+
columnId => !requestedSet.has(columnId)
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
return position === 'left' ? {
|
|
498
|
+
left: [...rowHeaderColumnIds, ...nextLeft, ...orderedIds],
|
|
499
|
+
right: nextRight
|
|
500
|
+
} : {
|
|
501
|
+
left: [...rowHeaderColumnIds, ...nextLeft],
|
|
502
|
+
right: [...orderedIds, ...nextRight]
|
|
503
|
+
};
|
|
504
|
+
});
|
|
505
|
+
},
|
|
506
|
+
[rowHeaderColumnIds]
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
const unpinColumns = useCallback((requestedIds: Array<string>) => {
|
|
510
|
+
const requestedSet = new Set(requestedIds);
|
|
511
|
+
|
|
512
|
+
setColumnPinning(previousState => ({
|
|
513
|
+
left: (previousState.left ?? []).filter(
|
|
514
|
+
columnId => !requestedSet.has(columnId)
|
|
515
|
+
),
|
|
516
|
+
right: (previousState.right ?? []).filter(
|
|
517
|
+
columnId => !requestedSet.has(columnId)
|
|
518
|
+
)
|
|
519
|
+
}));
|
|
520
|
+
}, []);
|
|
521
|
+
|
|
522
|
+
const openDrilldown = useCallback((cell: PivotGridRenderedCell<TData>) => {
|
|
523
|
+
setDrilldownState({
|
|
524
|
+
open: true,
|
|
525
|
+
cell
|
|
526
|
+
});
|
|
527
|
+
}, []);
|
|
528
|
+
|
|
529
|
+
const closeDrilldown = useCallback(() => {
|
|
530
|
+
setDrilldownState({
|
|
531
|
+
open: false,
|
|
532
|
+
cell: null
|
|
533
|
+
});
|
|
534
|
+
}, []);
|
|
535
|
+
|
|
536
|
+
const getDrilldownRows = useCallback(
|
|
537
|
+
(cell: PivotGridRenderedCell<TData>) => cell.rawRowIndices.map(
|
|
538
|
+
rawRowIndex => structure.data[rawRowIndex]
|
|
539
|
+
).filter((row): row is TData => row !== undefined),
|
|
540
|
+
[structure.data]
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
const getDrilldownTitle = useCallback(
|
|
544
|
+
(cell: PivotGridRenderedCell<TData>) => {
|
|
545
|
+
if (drilldown?.getTitle) {
|
|
546
|
+
return drilldown.getTitle(cell);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const rowLabel = cell.rowPath.filter(Boolean).join(' / ') || GRAND_TOTAL_LABEL;
|
|
550
|
+
const columnLabel = cell.columnPath.filter(Boolean).join(' / ') || GRAND_TOTAL_LABEL;
|
|
551
|
+
|
|
552
|
+
return `${rowLabel} x ${columnLabel}`;
|
|
553
|
+
},
|
|
554
|
+
[drilldown]
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
const buildExportSnapshot = useCallback((): PivotGridExportSnapshot<TData> => ({
|
|
558
|
+
fileName: 'pivot-grid',
|
|
559
|
+
headerDepth: structure.headerDepth,
|
|
560
|
+
rowHeaderLabels: rowDimensions.map(dimension => dimension.label),
|
|
561
|
+
rowHeaderWidths: rowHeaderColumnIds.map((columnId, index) => {
|
|
562
|
+
return table.getColumn(columnId)?.getSize()
|
|
563
|
+
?? (index === 0 ? DEFAULT_ROW_HEADER_WIDTH : DEFAULT_CHILD_ROW_HEADER_WIDTH);
|
|
564
|
+
}),
|
|
565
|
+
valueColumns: valueColumnIds.map((columnId) => {
|
|
566
|
+
const column = valueColumnsById.get(columnId);
|
|
567
|
+
|
|
568
|
+
return {
|
|
569
|
+
id: columnId,
|
|
570
|
+
headerPath: column?.headerPath ?? [],
|
|
571
|
+
width: table.getColumn(columnId)?.getSize()
|
|
572
|
+
?? DEFAULT_VALUE_COLUMN_WIDTH,
|
|
573
|
+
kind: column?.kind ?? 'leaf'
|
|
574
|
+
};
|
|
575
|
+
}),
|
|
576
|
+
rows: viewModel.visibleRows
|
|
577
|
+
}), [
|
|
578
|
+
structure.headerDepth,
|
|
579
|
+
rowDimensions,
|
|
580
|
+
rowHeaderColumnIds,
|
|
581
|
+
valueColumnIds,
|
|
582
|
+
valueColumnsById,
|
|
583
|
+
table,
|
|
584
|
+
viewModel.visibleRows
|
|
585
|
+
]);
|
|
586
|
+
|
|
587
|
+
const exportCsv = useCallback(async () => {
|
|
588
|
+
const csv = buildPivotGridCsv(buildExportSnapshot());
|
|
589
|
+
|
|
590
|
+
downloadTextFile(csv, 'pivot-grid.csv');
|
|
591
|
+
}, [buildExportSnapshot]);
|
|
592
|
+
|
|
593
|
+
const exportExcel = useCallback(async () => {
|
|
594
|
+
await exportPivotGridExcel(buildExportSnapshot());
|
|
595
|
+
}, [buildExportSnapshot]);
|
|
596
|
+
|
|
597
|
+
const exportPdf = useCallback(async () => {
|
|
598
|
+
if (!dataGridRef.current) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
await exportPivotGridPdf({
|
|
603
|
+
element: dataGridRef.current,
|
|
604
|
+
fileName: 'pivot-grid'
|
|
605
|
+
});
|
|
606
|
+
}, []);
|
|
607
|
+
|
|
608
|
+
const drilldownColumns = useMemo(() => {
|
|
609
|
+
if (drilldown?.columns) {
|
|
610
|
+
return drilldown.columns;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const rows = drilldownState.cell ? getDrilldownRows(drilldownState.cell) : [];
|
|
614
|
+
|
|
615
|
+
return buildDefaultDrilldownColumns(rows);
|
|
616
|
+
}, [drilldown?.columns, drilldownState.cell, getDrilldownRows]);
|
|
617
|
+
|
|
618
|
+
return useMemo(
|
|
619
|
+
() => ({
|
|
620
|
+
table,
|
|
621
|
+
dataGridRef,
|
|
622
|
+
height,
|
|
623
|
+
className,
|
|
624
|
+
rowHeaderColumnIds,
|
|
625
|
+
valueColumnIds,
|
|
626
|
+
visibleLeafColumns: viewModel.visibleLeafColumns,
|
|
627
|
+
headerDepth: structure.headerDepth,
|
|
628
|
+
headerRows,
|
|
629
|
+
visibleRows: viewModel.visibleRows,
|
|
630
|
+
virtualItems,
|
|
631
|
+
virtualTotalSize,
|
|
632
|
+
cellColorMap,
|
|
633
|
+
drilldownColumns,
|
|
634
|
+
drilldownState,
|
|
635
|
+
toggleRow,
|
|
636
|
+
toggleColumn,
|
|
637
|
+
expandAllRows,
|
|
638
|
+
collapseAllRows,
|
|
639
|
+
expandAllColumns,
|
|
640
|
+
collapseAllColumns,
|
|
641
|
+
pinColumns,
|
|
642
|
+
unpinColumns,
|
|
643
|
+
openDrilldown,
|
|
644
|
+
closeDrilldown,
|
|
645
|
+
getDrilldownRows,
|
|
646
|
+
getDrilldownTitle,
|
|
647
|
+
exportCsv,
|
|
648
|
+
exportExcel,
|
|
649
|
+
exportPdf
|
|
650
|
+
}),
|
|
651
|
+
[
|
|
652
|
+
table,
|
|
653
|
+
height,
|
|
654
|
+
className,
|
|
655
|
+
rowHeaderColumnIds,
|
|
656
|
+
valueColumnIds,
|
|
657
|
+
viewModel.visibleLeafColumns,
|
|
658
|
+
structure.headerDepth,
|
|
659
|
+
headerRows,
|
|
660
|
+
viewModel.visibleRows,
|
|
661
|
+
virtualItems,
|
|
662
|
+
virtualTotalSize,
|
|
663
|
+
cellColorMap,
|
|
664
|
+
drilldownColumns,
|
|
665
|
+
drilldownState,
|
|
666
|
+
toggleRow,
|
|
667
|
+
toggleColumn,
|
|
668
|
+
expandAllRows,
|
|
669
|
+
collapseAllRows,
|
|
670
|
+
expandAllColumns,
|
|
671
|
+
collapseAllColumns,
|
|
672
|
+
pinColumns,
|
|
673
|
+
unpinColumns,
|
|
674
|
+
openDrilldown,
|
|
675
|
+
closeDrilldown,
|
|
676
|
+
getDrilldownRows,
|
|
677
|
+
getDrilldownTitle,
|
|
678
|
+
exportCsv,
|
|
679
|
+
exportExcel,
|
|
680
|
+
exportPdf
|
|
681
|
+
]
|
|
682
|
+
);
|
|
683
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
export { PivotGrid, PivotGridView } from './pivot-grid';
|
|
6
|
+
export { PivotGridToolbar } from './pivot-grid-toolbar';
|
|
7
|
+
export { PivotGridExportMenu } from './pivot-grid-export-menu';
|
|
8
|
+
|
|
9
|
+
export type {
|
|
10
|
+
PivotGridCellColorRule,
|
|
11
|
+
PivotGridController,
|
|
12
|
+
PivotGridDimension,
|
|
13
|
+
PivotGridDrilldown,
|
|
14
|
+
PivotGridLeafColumn,
|
|
15
|
+
PivotGridMeasure,
|
|
16
|
+
PivotGridProps,
|
|
17
|
+
PivotGridRenderedCell,
|
|
18
|
+
PivotGridRenderedRow,
|
|
19
|
+
PivotGridState,
|
|
20
|
+
UsePivotGridProps
|
|
21
|
+
} from './types';
|
|
22
|
+
|
|
23
|
+
export { usePivotGrid } from './hooks/use-pivot-grid';
|