@appdirect/design-system 0.0.0-stage → 0.0.1
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 +12 -2
- package/dist/Buttons/ActionIcon.tsx +37 -0
- package/dist/Buttons/Button.module.css +5 -0
- package/dist/Buttons/Button.tsx +124 -0
- package/dist/Buttons/CloseButton.tsx +83 -0
- package/dist/Buttons/index.ts +20 -0
- package/dist/Combobox/AutocompleteClearable.tsx +128 -0
- package/dist/Combobox/Combobox.tsx +37 -0
- package/dist/Combobox/Multiselect.tsx +158 -0
- package/dist/Combobox/SearchableSelect.tsx +131 -0
- package/dist/Combobox/Select.tsx +154 -0
- package/dist/Combobox/index.ts +20 -0
- package/dist/ComplexComponents/DashboardWidget/DashboardWidget.tsx +280 -0
- package/dist/ComplexComponents/DashboardWidget/index.ts +2 -0
- package/dist/ComplexComponents/DataTable/CustomBottomToolbar/index.tsx +177 -0
- package/dist/ComplexComponents/DataTable/CustomBottomToolbar/utils.ts +35 -0
- package/dist/ComplexComponents/DataTable/CustomToolbarAlertBannerContent/index.tsx +74 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/DateRangeSelectorFilter/index.tsx +358 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterChip.tsx +48 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/DateRangeFilter.tsx +26 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/FilterRenderer.tsx +71 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/FilterSystem.tsx +158 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/MultiSelectFilter.tsx +79 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/SingleSelectFilter.tsx +65 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/index.ts +70 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/types.ts +140 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/ResultCount/index.tsx +76 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/SearchFilter.tsx +73 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/SelectFilter.tsx +50 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/index.tsx +788 -0
- package/dist/ComplexComponents/DataTable/DataTable.tsx +582 -0
- package/dist/ComplexComponents/DataTable/ErrorOverlay/index.tsx +29 -0
- package/dist/ComplexComponents/DataTable/SortableHeaderCell/index.tsx +57 -0
- package/dist/ComplexComponents/DataTable/index.ts +2 -0
- package/dist/ComplexComponents/DataTable/translations/en.ts +115 -0
- package/dist/ComplexComponents/DataTable/translations/index.ts +23 -0
- package/dist/ComplexComponents/KeyInsight/KeyInsights.tsx +221 -0
- package/dist/ComplexComponents/KeyInsight/index.ts +2 -0
- package/dist/ComplexComponents/NameValue/NameValue.tsx +175 -0
- package/dist/ComplexComponents/NameValue/NameValueItem.tsx +100 -0
- package/dist/ComplexComponents/NameValue/index.ts +4 -0
- package/dist/ComplexComponents/PageContentHeader/DescriptionBlock.tsx +65 -0
- package/dist/ComplexComponents/PageContentHeader/PageContentHeader.tsx +383 -0
- package/dist/ComplexComponents/PageContentHeader/index.ts +4 -0
- package/dist/ComplexComponents/Utilities/CopyButton.tsx +82 -0
- package/dist/ComplexComponents/Utilities/index.ts +2 -0
- package/dist/DataDisplay/Alert.tsx +130 -0
- package/dist/DataDisplay/Avatar.tsx +216 -0
- package/dist/DataDisplay/Badge.tsx +119 -0
- package/dist/DataDisplay/Card.module.css +26 -0
- package/dist/DataDisplay/Card.tsx +163 -0
- package/dist/DataDisplay/Chip.tsx +66 -0
- package/dist/DataDisplay/Indicator.tsx +122 -0
- package/dist/DataDisplay/List.tsx +90 -0
- package/dist/DataDisplay/Pill.tsx +125 -0
- package/dist/DataDisplay/Progress.tsx +77 -0
- package/dist/DataDisplay/Table.tsx +57 -0
- package/dist/DataDisplay/ThemeIcon.tsx +67 -0
- package/dist/DataDisplay/index.ts +41 -0
- package/dist/Inputs/Checkbox.tsx +94 -0
- package/dist/Inputs/ColorInput.tsx +124 -0
- package/dist/Inputs/DropZone.tsx +78 -0
- package/dist/Inputs/FileInput.tsx +120 -0
- package/dist/Inputs/NumberInput.tsx +125 -0
- package/dist/Inputs/Radio.tsx +86 -0
- package/dist/Inputs/RadioGroup.tsx +90 -0
- package/dist/Inputs/SegmentedControl.tsx +64 -0
- package/dist/Inputs/Slider.tsx +69 -0
- package/dist/Inputs/Switch.tsx +67 -0
- package/dist/Inputs/TextArea.tsx +126 -0
- package/dist/Inputs/TextInput.tsx +167 -0
- package/dist/Inputs/index.ts +51 -0
- package/dist/Layout/Box.tsx +82 -0
- package/dist/Layout/Center.tsx +17 -0
- package/dist/Layout/Container.tsx +17 -0
- package/dist/Layout/Flex.tsx +18 -0
- package/dist/Layout/Grid.tsx +31 -0
- package/dist/Layout/Group.tsx +54 -0
- package/dist/Layout/Inline.tsx +86 -0
- package/dist/Layout/SimpleGrid.tsx +50 -0
- package/dist/Layout/Stack.tsx +86 -0
- package/dist/Layout/index.ts +29 -0
- package/dist/Misc/Divider.tsx +27 -0
- package/dist/Misc/Paper.tsx +47 -0
- package/dist/Misc/index.ts +15 -0
- package/dist/Navigation/Breadcrumb.tsx +138 -0
- package/dist/Navigation/NavLink.tsx +37 -0
- package/dist/Navigation/Stepper.tsx +17 -0
- package/dist/Navigation/Tabs.tsx +29 -0
- package/dist/Navigation/index.ts +17 -0
- package/dist/Overlays/Drawer.tsx +123 -0
- package/dist/Overlays/Menu.tsx +192 -0
- package/dist/Overlays/Modal.tsx +159 -0
- package/dist/Overlays/Popover.tsx +319 -0
- package/dist/Overlays/Tooltip.tsx +131 -0
- package/dist/Overlays/index.ts +19 -0
- package/dist/Shell/AppShellLayout.tsx +56 -0
- package/dist/Shell/HeaderBar.tsx +26 -0
- package/dist/Shell/SidebarNav.tsx +47 -0
- package/dist/Shell/SidebarNavLink.tsx +50 -0
- package/dist/Shell/SingleColumnLayout.tsx +22 -0
- package/dist/Shell/TertiaryColumnLayout.tsx +32 -0
- package/dist/Shell/index.ts +17 -0
- package/dist/Typography/Code.tsx +27 -0
- package/dist/Typography/Kbd.tsx +87 -0
- package/dist/Typography/Text.tsx +27 -0
- package/dist/Typography/Title.tsx +34 -0
- package/dist/Typography/index.ts +21 -0
- package/dist/bin/ds-audit.js +521 -0
- package/dist/config.ts +226 -0
- package/dist/contracts/Alert.json +80 -0
- package/dist/contracts/Badge.json +78 -0
- package/dist/contracts/Box.json +52 -0
- package/dist/contracts/Button.json +111 -0
- package/dist/contracts/DataTable.json +68 -0
- package/dist/contracts/Grid.json +35 -0
- package/dist/contracts/Group.json +44 -0
- package/dist/contracts/Inline.json +73 -0
- package/dist/contracts/PageContentHeader.json +107 -0
- package/dist/contracts/Stack.json +62 -0
- package/dist/contracts/ds-component-contract.v1.json +23 -0
- package/dist/contracts/index.json +55 -0
- package/dist/css/foundations.css +703 -0
- package/dist/css/mantine.css +1797 -0
- package/dist/hooks/useTableHandlers.ts +179 -0
- package/dist/hooks/useTableRenderFunctions.tsx +283 -0
- package/dist/hooks/useTableState.ts +81 -0
- package/dist/index.ts +210 -0
- package/dist/types.ts +202 -0
- package/package.json +56 -4
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
MRT_PaginationState as PaginationState,
|
|
4
|
+
MRT_RowSelectionState as RowSelectionState,
|
|
5
|
+
MRT_DensityState as DensityState,
|
|
6
|
+
MRT_SortingState as SortingState,
|
|
7
|
+
} from 'mantine-react-table-open';
|
|
8
|
+
import { useTableState } from './useTableState';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generic type for state updater functions used by React state setters
|
|
12
|
+
* Can accept either a direct value or a function that takes the previous value
|
|
13
|
+
*/
|
|
14
|
+
type OnChangeFn<T> = (updater: T | ((old: T) => T)) => void;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Union type for sort directions used throughout the table
|
|
18
|
+
* null represents no sorting applied to a column
|
|
19
|
+
*/
|
|
20
|
+
type SortDirection = 'asc' | 'desc' | null;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Props interface for the useTableHandlers hook
|
|
24
|
+
*/
|
|
25
|
+
export interface UseTableHandlersProps {
|
|
26
|
+
/** Callback fired when pagination state changes (page or page size) */
|
|
27
|
+
onPaginationChange?: (pagination: PaginationState) => void;
|
|
28
|
+
/** Callback fired when row selection changes */
|
|
29
|
+
onRowSelectionChange?: (rowSelection: RowSelectionState) => void;
|
|
30
|
+
/** Callback fired when table density (row height) changes */
|
|
31
|
+
onDensityChange?: (density: DensityState) => void;
|
|
32
|
+
/** Callback fired when column sorting changes */
|
|
33
|
+
onSortChange?: (field: string, direction: SortDirection) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Custom hook for table event handlers
|
|
38
|
+
*
|
|
39
|
+
* Manages all user interactions and state updates with proper memoization.
|
|
40
|
+
* Provides handlers for pagination, row selection, density changes, and sorting.
|
|
41
|
+
* All handlers are memoized to prevent unnecessary re-renders.
|
|
42
|
+
*
|
|
43
|
+
* @param tableState - Current table state from useTableState hook
|
|
44
|
+
* @param props - Callback props passed from parent component
|
|
45
|
+
* @returns Object containing all memoized event handlers
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* const tableState = useTableState(10, 'xs');
|
|
50
|
+
* const handlers = useTableHandlers(tableState, {
|
|
51
|
+
* onPaginationChange: (pagination) => {
|
|
52
|
+
* console.log('Page changed:', pagination);
|
|
53
|
+
* },
|
|
54
|
+
* onRowSelectionChange: (selection) => {
|
|
55
|
+
* console.log('Selection changed:', selection);
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Use handlers in table configuration
|
|
60
|
+
* const { handlePaginationChange, handleRowSelectionChange } = handlers;
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* // With sorting and density change handlers
|
|
66
|
+
* const handlers = useTableHandlers(tableState, {
|
|
67
|
+
* onSortChange: (field, direction) => {
|
|
68
|
+
* console.log(`Sorting ${field} in ${direction} order`);
|
|
69
|
+
* },
|
|
70
|
+
* onDensityChange: (density) => {
|
|
71
|
+
* console.log('Density changed:', density);
|
|
72
|
+
* },
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* // Clear all selections
|
|
76
|
+
* handlers.handleClearSelection();
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* // Complete table setup with all handlers
|
|
82
|
+
* const tableState = useTableState(20, 'md');
|
|
83
|
+
* const handlers = useTableHandlers(tableState, {
|
|
84
|
+
* onPaginationChange: async (pagination) => {
|
|
85
|
+
* await fetchData(pagination.pageIndex, pagination.pageSize);
|
|
86
|
+
* },
|
|
87
|
+
* onRowSelectionChange: (selection) => {
|
|
88
|
+
* setSelectedRows(selection);
|
|
89
|
+
* updateBulkActions(Object.keys(selection));
|
|
90
|
+
* },
|
|
91
|
+
* onSortChange: async (field, direction) => {
|
|
92
|
+
* await fetchSortedData(field, direction);
|
|
93
|
+
* },
|
|
94
|
+
* onDensityChange: (density) => {
|
|
95
|
+
* localStorage.setItem('tableDensity', density);
|
|
96
|
+
* },
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export function useTableHandlers(
|
|
101
|
+
tableState: ReturnType<typeof useTableState>,
|
|
102
|
+
props: UseTableHandlersProps
|
|
103
|
+
) {
|
|
104
|
+
// Extract state and callbacks for cleaner code
|
|
105
|
+
const { pagination, setPagination, rowSelection, setRowSelection, density, setDensity } = tableState;
|
|
106
|
+
const { onPaginationChange, onRowSelectionChange, onDensityChange, onSortChange } = props;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Handles pagination state changes (page navigation, page size changes)
|
|
110
|
+
* Updates internal state and notifies parent component
|
|
111
|
+
*/
|
|
112
|
+
const handlePaginationChange: OnChangeFn<PaginationState> = useCallback((updater) => {
|
|
113
|
+
// Handle both direct values and updater functions
|
|
114
|
+
const newPagination = typeof updater === 'function' ? updater(pagination) : updater;
|
|
115
|
+
setPagination(newPagination);
|
|
116
|
+
// Notify parent component of pagination change
|
|
117
|
+
onPaginationChange?.(newPagination);
|
|
118
|
+
}, [pagination, onPaginationChange, setPagination]);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Handles row selection state changes
|
|
122
|
+
* Maintains selection state across pagination and notifies parent
|
|
123
|
+
*/
|
|
124
|
+
const handleRowSelectionChange: OnChangeFn<RowSelectionState> = useCallback((updater) => {
|
|
125
|
+
// Handle both direct values and updater functions
|
|
126
|
+
const newRowSelection = typeof updater === 'function' ? updater(rowSelection) : updater;
|
|
127
|
+
setRowSelection(newRowSelection);
|
|
128
|
+
// Notify parent component of selection change
|
|
129
|
+
onRowSelectionChange?.(newRowSelection);
|
|
130
|
+
}, [rowSelection, onRowSelectionChange, setRowSelection]);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Handles table density changes (affects row height and spacing)
|
|
134
|
+
* Updates UI density and notifies parent component
|
|
135
|
+
*/
|
|
136
|
+
const handleDensityChange: OnChangeFn<DensityState> = useCallback((updater) => {
|
|
137
|
+
// Handle both direct values and updater functions
|
|
138
|
+
const newDensity = typeof updater === 'function' ? updater(density) : updater;
|
|
139
|
+
setDensity(newDensity);
|
|
140
|
+
// Notify parent component of density change
|
|
141
|
+
onDensityChange?.(newDensity);
|
|
142
|
+
}, [density, onDensityChange, setDensity]);
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Clears all selected rows and notifies parent component
|
|
146
|
+
* Used by the "Clear Selection" button in the alert banner
|
|
147
|
+
*/
|
|
148
|
+
const handleClearSelection = useCallback(() => {
|
|
149
|
+
setRowSelection({});
|
|
150
|
+
onRowSelectionChange?.({});
|
|
151
|
+
}, [onRowSelectionChange, setRowSelection]);
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Handles column sorting changes
|
|
155
|
+
* Converts Mantine React Table sorting format to our simplified format
|
|
156
|
+
*/
|
|
157
|
+
const handleSortingChange = useCallback((updater: SortingState | ((old: SortingState) => SortingState)) => {
|
|
158
|
+
// Handle both direct values and updater functions
|
|
159
|
+
const sorting = typeof updater === 'function' ? updater([]) : updater;
|
|
160
|
+
|
|
161
|
+
if (sorting.length > 0) {
|
|
162
|
+
// Extract sorting information from first sort (single column sorting)
|
|
163
|
+
const { id, desc } = sorting[0];
|
|
164
|
+
onSortChange?.(id, desc ? 'desc' : 'asc');
|
|
165
|
+
} else {
|
|
166
|
+
// No sorting applied
|
|
167
|
+
onSortChange?.('', null);
|
|
168
|
+
}
|
|
169
|
+
}, [onSortChange]);
|
|
170
|
+
|
|
171
|
+
// Return all memoized handlers
|
|
172
|
+
return {
|
|
173
|
+
handlePaginationChange,
|
|
174
|
+
handleRowSelectionChange,
|
|
175
|
+
handleDensityChange,
|
|
176
|
+
handleClearSelection,
|
|
177
|
+
handleSortingChange,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import React, { useCallback } from 'react';
|
|
2
|
+
import { MRT_RowData } from 'mantine-react-table-open';
|
|
3
|
+
import CustomTopToolbar, { type Filters } from '../ComplexComponents/DataTable/CustomTopToolbar';
|
|
4
|
+
import CustomBottomToolbar from '../ComplexComponents/DataTable/CustomBottomToolbar';
|
|
5
|
+
import CustomToolbarAlertBannerContent from '../ComplexComponents/DataTable/CustomToolbarAlertBannerContent';
|
|
6
|
+
import ErrorOverlay from '../ComplexComponents/DataTable/ErrorOverlay';
|
|
7
|
+
import SortableHeaderCell from '../ComplexComponents/DataTable/SortableHeaderCell';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Page information interface for cursor-based pagination
|
|
11
|
+
* Used to determine navigation availability and cursor positions
|
|
12
|
+
*/
|
|
13
|
+
export interface PageInfo {
|
|
14
|
+
/** Whether there are more pages after the current one */
|
|
15
|
+
hasNextPage: boolean;
|
|
16
|
+
/** Whether there are pages before the current one */
|
|
17
|
+
hasPreviousPage: boolean;
|
|
18
|
+
/** Cursor pointing to the start of the current page (for cursor-based pagination) */
|
|
19
|
+
startCursor?: string;
|
|
20
|
+
/** Cursor pointing to the end of the current page (for cursor-based pagination) */
|
|
21
|
+
endCursor?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* ID used by Mantine React Table for the row selection column
|
|
26
|
+
* Stored as constant to avoid magic strings
|
|
27
|
+
*/
|
|
28
|
+
const SELECTION_COLUMN_ID = 'mrt-row-select';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Styling constants using Mantine design tokens
|
|
32
|
+
* Ensures consistent theming and easy maintenance
|
|
33
|
+
*/
|
|
34
|
+
const TABLE_STYLES = {
|
|
35
|
+
/** Styling for table header cells */
|
|
36
|
+
headerCell: {
|
|
37
|
+
fontWeight: 'bold',
|
|
38
|
+
},
|
|
39
|
+
} as const;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Props interface for rendering-related data
|
|
43
|
+
*/
|
|
44
|
+
export interface UseTableRenderFunctionsProps<T extends MRT_RowData> {
|
|
45
|
+
/** Current state of all applied filters */
|
|
46
|
+
filtersState: Filters;
|
|
47
|
+
/** Locale string for internationalization */
|
|
48
|
+
locale: string;
|
|
49
|
+
/** Callback fired when filters are modified */
|
|
50
|
+
onFiltersChange: (filters: Filters) => void;
|
|
51
|
+
/** Array of data objects to display in the table */
|
|
52
|
+
data: T[];
|
|
53
|
+
/** Whether to show the filters panel */
|
|
54
|
+
showFilters: boolean;
|
|
55
|
+
/** Whether the table is currently loading data */
|
|
56
|
+
isLoading: boolean;
|
|
57
|
+
/** Total number of items across all pages (for pagination display) */
|
|
58
|
+
totalCount: number;
|
|
59
|
+
/** Page information for cursor-based pagination */
|
|
60
|
+
pageInfo?: PageInfo;
|
|
61
|
+
/** Error message to display when isError is true */
|
|
62
|
+
error?: string;
|
|
63
|
+
/** Callback function to retry loading data after an error */
|
|
64
|
+
onRetry?: () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Interface for table state data needed by render functions
|
|
69
|
+
*/
|
|
70
|
+
export interface TableStateForRender {
|
|
71
|
+
/** Number of currently selected rows across all pages */
|
|
72
|
+
numberOfSelectedRows: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Interface for handlers needed by render functions
|
|
77
|
+
*/
|
|
78
|
+
export interface HandlersForRender {
|
|
79
|
+
/** Clears all selected rows and notifies parent component */
|
|
80
|
+
handleClearSelection: () => void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Custom hook for table render functions
|
|
85
|
+
*
|
|
86
|
+
* Manages all custom rendering logic with proper memoization.
|
|
87
|
+
* Provides render functions for toolbars, alert banners, empty states, and header cells.
|
|
88
|
+
* All render functions are memoized to prevent unnecessary re-renders.
|
|
89
|
+
*
|
|
90
|
+
* @param props - Props needed for rendering (data, filters, etc.)
|
|
91
|
+
* @param tableState - Current table state (selection count, etc.)
|
|
92
|
+
* @param handlers - Event handlers for user interactions
|
|
93
|
+
* @returns Object containing all memoized render functions
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```tsx
|
|
97
|
+
* const renderFunctions = useTableRenderFunctions(
|
|
98
|
+
* {
|
|
99
|
+
* filtersState: filters,
|
|
100
|
+
* locale: 'en',
|
|
101
|
+
* onFiltersChange: setFilters,
|
|
102
|
+
* data: tableData,
|
|
103
|
+
* showFilters: true,
|
|
104
|
+
* isLoading: false,
|
|
105
|
+
* totalCount: 100,
|
|
106
|
+
* pageInfo: { hasNextPage: true, hasPreviousPage: false },
|
|
107
|
+
* },
|
|
108
|
+
* { numberOfSelectedRows: 3 },
|
|
109
|
+
* { handleClearSelection: () => setSelection({}) }
|
|
110
|
+
* );
|
|
111
|
+
*
|
|
112
|
+
* // Use in table configuration
|
|
113
|
+
* const tableConfig = {
|
|
114
|
+
* renderTopToolbar: renderFunctions.renderTopToolbar,
|
|
115
|
+
* renderBottomToolbar: renderFunctions.renderBottomToolbar,
|
|
116
|
+
* // ... other render functions
|
|
117
|
+
* };
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```tsx
|
|
122
|
+
* // Complete setup with error handling
|
|
123
|
+
* const renderFunctions = useTableRenderFunctions(
|
|
124
|
+
* {
|
|
125
|
+
* filtersState: {},
|
|
126
|
+
* locale: 'en',
|
|
127
|
+
* onFiltersChange: () => {},
|
|
128
|
+
* data: [],
|
|
129
|
+
* showFilters: false,
|
|
130
|
+
* isLoading: true,
|
|
131
|
+
* totalCount: 0,
|
|
132
|
+
* error: 'Failed to load data',
|
|
133
|
+
* onRetry: () => refetch(),
|
|
134
|
+
* },
|
|
135
|
+
* { numberOfSelectedRows: 0 },
|
|
136
|
+
* { handleClearSelection: () => {} }
|
|
137
|
+
* );
|
|
138
|
+
*
|
|
139
|
+
* // Error state will be handled by renderEmptyRowsFallback
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```tsx
|
|
144
|
+
* // With custom header rendering
|
|
145
|
+
* const renderFunctions = useTableRenderFunctions(
|
|
146
|
+
* {
|
|
147
|
+
* filtersState: activeFilters,
|
|
148
|
+
* locale: userLocale,
|
|
149
|
+
* onFiltersChange: updateFilters,
|
|
150
|
+
* data: rows,
|
|
151
|
+
* showFilters: true,
|
|
152
|
+
* isLoading: false,
|
|
153
|
+
* totalCount: totalRows,
|
|
154
|
+
* pageInfo: paginationInfo,
|
|
155
|
+
* },
|
|
156
|
+
* { numberOfSelectedRows: selectedCount },
|
|
157
|
+
* { handleClearSelection: clearAll }
|
|
158
|
+
* );
|
|
159
|
+
*
|
|
160
|
+
* // Custom header cells with sorting support
|
|
161
|
+
* const headerProps = renderFunctions.renderTableHeadCell({ column });
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export function useTableRenderFunctions<T extends MRT_RowData>(
|
|
165
|
+
props: UseTableRenderFunctionsProps<T>,
|
|
166
|
+
tableState: TableStateForRender,
|
|
167
|
+
handlers: HandlersForRender
|
|
168
|
+
) {
|
|
169
|
+
// Extract props for cleaner destructuring
|
|
170
|
+
const {
|
|
171
|
+
filtersState,
|
|
172
|
+
locale,
|
|
173
|
+
onFiltersChange,
|
|
174
|
+
data,
|
|
175
|
+
showFilters,
|
|
176
|
+
isLoading,
|
|
177
|
+
totalCount,
|
|
178
|
+
pageInfo,
|
|
179
|
+
error,
|
|
180
|
+
onRetry,
|
|
181
|
+
} = props;
|
|
182
|
+
const { numberOfSelectedRows } = tableState;
|
|
183
|
+
const { handleClearSelection } = handlers;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Renders the top toolbar with filters, search, and action buttons
|
|
187
|
+
* Memoized to prevent unnecessary re-renders
|
|
188
|
+
*/
|
|
189
|
+
const renderTopToolbar = useCallback((toolbarProps: any) => (
|
|
190
|
+
<CustomTopToolbar
|
|
191
|
+
{...toolbarProps}
|
|
192
|
+
filtersState={filtersState}
|
|
193
|
+
locale={locale}
|
|
194
|
+
onChange={onFiltersChange}
|
|
195
|
+
resultCount={data.length}
|
|
196
|
+
totalCount={totalCount}
|
|
197
|
+
showFilters={showFilters}
|
|
198
|
+
numberOfSelectedRows={numberOfSelectedRows}
|
|
199
|
+
onFilterToggleChange={() => {}} // TODO: Implement filter toggle functionality
|
|
200
|
+
isLoading={isLoading}
|
|
201
|
+
/>
|
|
202
|
+
), [filtersState, locale, onFiltersChange, data.length, totalCount, showFilters, numberOfSelectedRows, isLoading]);
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Renders the bottom toolbar with pagination controls and row count
|
|
206
|
+
* Includes custom cursor-based pagination with ActionIcon chevrons
|
|
207
|
+
*/
|
|
208
|
+
const renderBottomToolbar = useCallback((toolbarProps: any) => (
|
|
209
|
+
<CustomBottomToolbar
|
|
210
|
+
{...toolbarProps}
|
|
211
|
+
totalCount={totalCount}
|
|
212
|
+
pageInfo={pageInfo}
|
|
213
|
+
numberOfSelectedRows={numberOfSelectedRows}
|
|
214
|
+
/>
|
|
215
|
+
), [totalCount, pageInfo, numberOfSelectedRows]);
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Renders the alert banner content shown when rows are selected
|
|
219
|
+
* Displays selection count and clear selection button
|
|
220
|
+
*/
|
|
221
|
+
const renderToolbarAlertBannerContent = useCallback(() => (
|
|
222
|
+
<CustomToolbarAlertBannerContent
|
|
223
|
+
numberOfSelectedRows={numberOfSelectedRows}
|
|
224
|
+
onClearSelection={handleClearSelection}
|
|
225
|
+
/>
|
|
226
|
+
), [numberOfSelectedRows, handleClearSelection]);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Renders the fallback content when the table is empty or has errors
|
|
230
|
+
* Shows error message with retry button when applicable
|
|
231
|
+
*/
|
|
232
|
+
const renderEmptyRowsFallback = useCallback(() => (
|
|
233
|
+
<ErrorOverlay error={error} onRetry={onRetry} />
|
|
234
|
+
), [error, onRetry]);
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Custom renderer for table header cells
|
|
238
|
+
* Provides custom styling while preserving column actions functionality
|
|
239
|
+
* Excludes the row selection column to let Mantine React Table handle it
|
|
240
|
+
*/
|
|
241
|
+
const renderTableHeadCell = useCallback(({ column }: any) => {
|
|
242
|
+
// Don't override the select column header - let Mantine React Table handle it
|
|
243
|
+
if (column.id === SELECTION_COLUMN_ID) {
|
|
244
|
+
return {};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Don't override the actions column header - let Mantine React Table handle it for proper pinning
|
|
248
|
+
if (column.id === 'actions') {
|
|
249
|
+
return {};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const header = column.columnDef.header;
|
|
253
|
+
|
|
254
|
+
// If header is a function (custom Header component), let it render
|
|
255
|
+
if (typeof header === 'function' || column.columnDef.Header) {
|
|
256
|
+
return {
|
|
257
|
+
style: TABLE_STYLES.headerCell,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// For string headers, provide custom styling with SortableHeaderCell
|
|
262
|
+
return {
|
|
263
|
+
style: TABLE_STYLES.headerCell,
|
|
264
|
+
children: (
|
|
265
|
+
<SortableHeaderCell
|
|
266
|
+
label={header as string}
|
|
267
|
+
sortDirection={column.getIsSorted() as "asc" | "desc" | null}
|
|
268
|
+
onSort={() => column.toggleSorting()}
|
|
269
|
+
isSortable={column.getCanSort()}
|
|
270
|
+
/>
|
|
271
|
+
),
|
|
272
|
+
};
|
|
273
|
+
}, []);
|
|
274
|
+
|
|
275
|
+
// Return all memoized render functions
|
|
276
|
+
return {
|
|
277
|
+
renderTopToolbar,
|
|
278
|
+
renderBottomToolbar,
|
|
279
|
+
renderToolbarAlertBannerContent,
|
|
280
|
+
renderEmptyRowsFallback,
|
|
281
|
+
renderTableHeadCell,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useState, useMemo } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
MRT_PaginationState as PaginationState,
|
|
4
|
+
MRT_RowSelectionState as RowSelectionState,
|
|
5
|
+
MRT_DensityState as DensityState,
|
|
6
|
+
} from 'mantine-react-table-open';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Custom hook for managing table state
|
|
10
|
+
*
|
|
11
|
+
* Manages core table state including pagination, row selection, and density settings.
|
|
12
|
+
* Provides computed values for selection count and alert banner visibility.
|
|
13
|
+
*
|
|
14
|
+
* @param defaultPageSize - Initial page size for pagination
|
|
15
|
+
* @param defaultDensity - Initial density setting for table rows
|
|
16
|
+
* @returns Object containing all state values and setters
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* const tableState = useTableState(10, 'xs');
|
|
21
|
+
*
|
|
22
|
+
* // Access state values
|
|
23
|
+
* const { pagination, rowSelection, density } = tableState;
|
|
24
|
+
*
|
|
25
|
+
* // Use state setters
|
|
26
|
+
* const { setPagination, setRowSelection, setDensity } = tableState;
|
|
27
|
+
*
|
|
28
|
+
* // Access computed values
|
|
29
|
+
* const { numberOfSelectedRows, showAlertBanner } = tableState;
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* // With custom defaults
|
|
35
|
+
* const tableState = useTableState(25, 'md');
|
|
36
|
+
*
|
|
37
|
+
* // Check if any rows are selected
|
|
38
|
+
* if (tableState.showAlertBanner) {
|
|
39
|
+
* console.log(`${tableState.numberOfSelectedRows} rows selected`);
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function useTableState(defaultPageSize: number, defaultDensity: DensityState) {
|
|
44
|
+
// ==================== STATE DECLARATIONS ====================
|
|
45
|
+
/** Pagination state containing current page index and page size */
|
|
46
|
+
const [pagination, setPagination] = useState<PaginationState>({
|
|
47
|
+
pageIndex: 0, // Start at first page (0-based)
|
|
48
|
+
pageSize: defaultPageSize,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/** Row selection state - object mapping row IDs to selection status */
|
|
52
|
+
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
53
|
+
|
|
54
|
+
/** Current table density setting affecting row height */
|
|
55
|
+
const [density, setDensity] = useState<DensityState>(defaultDensity);
|
|
56
|
+
|
|
57
|
+
// ==================== COMPUTED VALUES ====================
|
|
58
|
+
/**
|
|
59
|
+
* Number of currently selected rows across all pages
|
|
60
|
+
* Computed from the rowSelection object keys
|
|
61
|
+
*/
|
|
62
|
+
const numberOfSelectedRows = useMemo(() => Object.keys(rowSelection).length, [rowSelection]);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether to show the alert banner indicating selected rows
|
|
66
|
+
* Only shown when there are selected rows
|
|
67
|
+
*/
|
|
68
|
+
const showAlertBanner = useMemo(() => numberOfSelectedRows > 0, [numberOfSelectedRows]);
|
|
69
|
+
|
|
70
|
+
// Return all state and computed values for use in component
|
|
71
|
+
return {
|
|
72
|
+
pagination,
|
|
73
|
+
setPagination,
|
|
74
|
+
rowSelection,
|
|
75
|
+
setRowSelection,
|
|
76
|
+
density,
|
|
77
|
+
setDensity,
|
|
78
|
+
numberOfSelectedRows,
|
|
79
|
+
showAlertBanner,
|
|
80
|
+
};
|
|
81
|
+
}
|