forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableWrapper.tsx
More file actions
394 lines (362 loc) · 15.1 KB
/
tableWrapper.tsx
File metadata and controls
394 lines (362 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import React, {
FC,
Fragment,
useMemo,
useRef,
useEffect,
} from 'react';
import { filterVisibility, calculateDefaultColumns, convertRowDimensionsToHeight, convertRowStylingBoxToPadding, convertRowBorderStyleToBorder } from './utils';
import { getStyle } from '@/providers/form/utils';
import { ITableComponentProps } from './models';
import { getShadowStyle } from '@/designer-components/_settings/utils/shadow/utils';
import {
SidebarContainer,
DataTable,
DatatableAdvancedFilter,
DatatableColumnsSelector,
} from '@/components';
import {
useDataTable,
useDataTableStore,
useForm,
useFormData,
useGlobalState,
useSheshaApplication,
} from '@/providers';
import { GlobalTableStyles } from './styles/styles';
import { useDeepCompareEffect } from '@/hooks/useDeepCompareEffect';
import { FilterList } from '../filterList/filterList';
import { useStyles } from './styles';
import { useMetadata } from '@/providers/metadata';
import { useFormDesignerOrUndefined } from '@/providers/formDesigner';
import { Popover } from 'antd';
import { InfoCircleOutlined } from '@ant-design/icons';
import { useTheme } from '@/providers/theme';
import { StandaloneTable } from './standaloneTable';
import { useDatatableHintPopoverStyles } from './hintPopoverStyles';
export const TableWrapper: FC<ITableComponentProps> = (props) => {
const { id, items, useMultiselect, selectionMode, tableStyle, containerStyle } = props;
const { formMode } = useForm();
const { data: formData } = useFormData();
const { globalState } = useGlobalState();
const { anyOfPermissionsGranted } = useSheshaApplication();
const isDesignMode = formMode === 'designer';
const metadata = useMetadata(false); // Don't require - DataTable may not be in a DataSource
const formDesigner = useFormDesignerOrUndefined();
const hasAutoConfiguredRef = useRef(false);
const componentIdRef = useRef(id);
const { theme } = useTheme();
// Reset auto-config flag when component ID changes (new DataTable instance)
useEffect(() => {
if (componentIdRef.current !== id) {
componentIdRef.current = id;
hasAutoConfiguredRef.current = false;
}
}, [id]);
// Inject CSS for hint popover arrow styling
useDatatableHintPopoverStyles();
// Process shadow settings using getShadowStyle utility
const shadowStyles = useMemo(() => getShadowStyle(props?.shadow), [props?.shadow]);
const finalBoxShadow = useMemo(() => {
// If there's a shadow object, use the processed styles, otherwise use the boxShadow string
return props?.shadow ? shadowStyles?.boxShadow : props?.boxShadow;
}, [props?.shadow, shadowStyles?.boxShadow, props?.boxShadow]);
// Convert new property structures to old format for backward compatibility
const effectiveRowHeight = useMemo(() => {
// Prefer new rowDimensions over old rowHeight
const converted = convertRowDimensionsToHeight(props?.rowDimensions);
console.log('Row Height - rowDimensions:', props?.rowDimensions, 'converted:', converted, 'fallback:', props?.rowHeight);
return converted || props?.rowHeight;
}, [props?.rowDimensions, props?.rowHeight]);
const effectiveRowPadding = useMemo(() => {
// Prefer new rowStylingBox over old rowPadding
const converted = convertRowStylingBoxToPadding(props?.rowStylingBox);
console.log('Row Padding - rowStylingBox:', props?.rowStylingBox, 'converted:', converted, 'fallback:', props?.rowPadding);
return converted || props?.rowPadding;
}, [props?.rowStylingBox, props?.rowPadding]);
const effectiveRowBorder = useMemo(() => {
// Prefer new rowBorderStyle over old rowBorder
const converted = convertRowBorderStyleToBorder(props?.rowBorderStyle);
console.log('Row Border - rowBorderStyle:', props?.rowBorderStyle, 'converted:', converted, 'fallback:', props?.rowBorder);
return converted || props?.rowBorder;
}, [props?.rowBorderStyle, props?.rowBorder]);
const { styles } = useStyles({
fontFamily: props?.font?.type,
fontWeight: props?.font?.weight,
textAlign: props?.font?.align,
color: props?.font?.color,
fontSize: props?.font?.size,
striped: props?.striped,
hoverHighlight: props?.hoverHighlight,
stickyHeader: props?.stickyHeader,
enableStyleOnReadonly: props?.enableStyleOnReadonly,
readOnly: props?.readOnly,
rowBackgroundColor: props?.rowBackgroundColor,
rowAlternateBackgroundColor: props?.rowAlternateBackgroundColor,
rowHoverBackgroundColor: props?.rowHoverBackgroundColor,
rowSelectedBackgroundColor: props?.rowSelectedBackgroundColor,
border: props?.border,
backgroundColor: props?.background?.color,
headerFontSize: props?.headerFontSize,
headerFontWeight: props?.headerFontWeight,
headerBackgroundColor: props?.headerBackgroundColor,
headerTextColor: props?.headerTextColor,
rowHeight: effectiveRowHeight,
rowPadding: effectiveRowPadding,
rowBorder: effectiveRowBorder,
boxShadow: finalBoxShadow,
sortableIndicatorColor: props?.sortableIndicatorColor,
});
const finalStyle = useMemo(() => {
if (props.allStyles) {
let baseStyle;
if (!props.enableStyleOnReadonly && props.readOnly) {
baseStyle = {
...props.allStyles.fontStyles,
...props.allStyles.dimensionsStyles,
};
} else {
baseStyle = props.allStyles.fullStyle;
}
// Remove border properties from the outer container when border is being passed to DataTable
// This prevents double borders
if (props.border && baseStyle) {
const {
border,
borderTop,
borderRight,
borderBottom,
borderLeft,
borderWidth,
borderStyle,
borderColor,
borderRadius,
borderTopWidth,
borderRightWidth,
borderBottomWidth,
borderLeftWidth,
borderTopStyle,
borderRightStyle,
borderBottomStyle,
borderLeftStyle,
borderTopColor,
borderRightColor,
borderBottomColor,
borderLeftColor,
borderTopLeftRadius,
borderTopRightRadius,
borderBottomLeftRadius,
borderBottomRightRadius,
...styleWithoutBorder
} = baseStyle;
return styleWithoutBorder;
}
return baseStyle;
}
return {};
}, [props.enableStyleOnReadonly, props.readOnly, props.allStyles, props.border]);
const {
getRepository,
isInProgress: { isFiltering, isSelectingColumns },
setIsInProgressFlag,
registerConfigurableColumns,
selectedRow,
setMultiSelectedRow,
requireColumns,
allowReordering,
clearFilters,
removeColumnFilter,
tableFilter,
} = useDataTableStore();
const { totalRows } = useDataTable();
requireColumns(); // our component requires columns loading. it's safe to call on each render
const repository = getRepository();
useDeepCompareEffect(() => {
// register columns
const permissibleColumns = isDesignMode
? items
: items
?.filter(({ permissions }) => anyOfPermissionsGranted(permissions || []))
.filter(filterVisibility({ data: formData, globalState }));
registerConfigurableColumns(id, permissibleColumns);
}, [items, isDesignMode]);
// Auto-configure columns when DataTable is dropped into a DataContext
useEffect(() => {
// Only attempt auto-config if we have empty items and haven't tried yet
if (hasAutoConfiguredRef.current || !isDesignMode || !formDesigner) {
return;
}
// Check if we should auto-configure
const hasNoColumns = !items || items.length === 0;
const hasMetadata = metadata?.metadata != null;
if (!hasNoColumns || !hasMetadata) {
return;
}
// Mark as attempted to prevent multiple triggers
hasAutoConfiguredRef.current = true;
const autoConfigureColumns = async (): Promise<void> => {
try {
const defaultColumns = await calculateDefaultColumns(metadata.metadata);
if (defaultColumns.length > 0) {
formDesigner.updateComponent({
componentId: id,
settings: {
...props,
items: defaultColumns,
} as ITableComponentProps,
});
}
} catch (error) {
console.warn('Failed to auto-configure DataTable columns:', error);
// Reset flag to allow retry if it failed
hasAutoConfiguredRef.current = false;
}
};
autoConfigureColumns();
}, [isDesignMode, formDesigner, metadata?.metadata, items, id]);
const renderSidebarContent = (): JSX.Element => {
if (isFiltering) {
return <DatatableAdvancedFilter />;
}
if (isSelectingColumns) {
return <DatatableColumnsSelector />;
}
return <Fragment />;
};
const hasNoColumns = !items || items.length === 0;
const hasNoRepository = !repository;
const toggleFieldPropertiesSidebar = (): void => {
if (!isSelectingColumns && !isFiltering) setIsInProgressFlag({ isFiltering: true });
else setIsInProgressFlag({ isFiltering: false, isSelectingColumns: false });
};
// In designer mode, show StandaloneTable if columns were deliberately deleted
// (hasAutoConfiguredRef.current means auto-config was attempted, but we still have no columns)
if (isDesignMode && hasNoColumns && hasAutoConfiguredRef.current) {
return <StandaloneTable {...props} />;
}
return (
<SidebarContainer
rightSidebarProps={{
onOpen: toggleFieldPropertiesSidebar,
open: Boolean(isSelectingColumns || isFiltering),
onClose: toggleFieldPropertiesSidebar,
title: 'Table Columns',
content: renderSidebarContent,
}}
allowFullCollapse
>
<GlobalTableStyles />
{tableFilter?.length > 0 && <FilterList filters={tableFilter} rows={totalRows} clearFilters={clearFilters} removeColumnFilter={removeColumnFilter} />}
<div style={{ position: 'relative' }}>
{/* Show info icon in top-right corner in designer mode for configuration issues */}
{isDesignMode && (hasNoRepository || hasNoColumns) && (
<Popover
placement="left"
title="Hint:"
classNames={{ root: "sha-datatable-hint-popover" }}
styles={{ body: { backgroundColor: '#D9DCDC' } }}
content={hasNoRepository ? (
<p>
This Data Table is not inside a Data Context.<br />
Drag it into a Data Context component to<br />
connect it to data.
<br /><br />
<a href="https://docs.shesha.io/docs/category/tables-and-lists" target="_blank" rel="noopener noreferrer">
See component documentation
</a>
<br />for setup and usage.
</p>
) : (
<p>
This Data Table has no columns configured.<br />
Click the Settings icon in the Properties Panel<br />
to configure columns.
<br /><br />
<a href="https://docs.shesha.io/docs/category/tables-and-lists" target="_blank" rel="noopener noreferrer">
See component documentation
</a>
<br />for setup and usage.
</p>
)}
>
<InfoCircleOutlined
role="button"
tabIndex={0}
aria-label="Data table configuration help"
style={{
position: 'absolute',
top: '4px',
right: '4px',
color: theme?.application?.warningColor || '#faad14',
fontSize: '20px',
zIndex: 9999,
cursor: 'help',
backgroundColor: '#fff',
borderRadius: '50%',
padding: '4px',
boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
}}
/>
</Popover>
)}
<div className={styles.dataTable} style={finalStyle}>
<DataTable
onRowDeleteSuccessAction={props.onRowDeleteSuccessAction}
onMultiRowSelect={setMultiSelectedRow}
selectedRowIndex={selectedRow?.index}
useMultiselect={useMultiselect}
selectionMode={selectionMode}
freezeHeaders={props.stickyHeader || props.freezeHeaders}
allowReordering={allowReordering}
tableStyle={getStyle(tableStyle, formData, globalState)}
containerStyle={getStyle(containerStyle, formData, globalState)}
canAddInline={props.canAddInline}
canAddInlineExpression={props.canAddInlineExpression}
customCreateUrl={props.customCreateUrl}
newRowCapturePosition={props.newRowCapturePosition}
onNewRowInitialize={props.onNewRowInitialize}
canEditInline={props.canEditInline}
canEditInlineExpression={props.canEditInlineExpression}
customUpdateUrl={props.customUpdateUrl}
canDeleteInline={props.canDeleteInline}
canDeleteInlineExpression={props.canDeleteInlineExpression}
customDeleteUrl={props.customDeleteUrl}
onRowSave={props.onRowSave}
onRowSaveSuccessAction={props.onRowSaveSuccessAction}
onDblClick={props.dblClickActionConfiguration}
inlineSaveMode={props.inlineSaveMode}
inlineEditMode={props.inlineEditMode}
minHeight={props.minHeight}
maxHeight={props.maxHeight}
noDataText={props.noDataText}
noDataSecondaryText={props.noDataSecondaryText}
noDataIcon={props.noDataIcon}
showExpandedView={props.showExpandedView}
onRowClick={props.onRowClick}
onRowDoubleClick={props.onRowDoubleClick}
onRowHover={props.onRowHover}
onRowSelect={props.onRowSelect}
onSelectionChange={props.onSelectionChange}
rowBackgroundColor={props.rowBackgroundColor}
rowAlternateBackgroundColor={props.rowAlternateBackgroundColor}
rowHoverBackgroundColor={props.rowHoverBackgroundColor}
rowSelectedBackgroundColor={props.rowSelectedBackgroundColor}
border={props.border}
striped={props.striped}
hoverHighlight={props.hoverHighlight}
backgroundColor={props.background?.color}
headerFontSize={props.headerFontSize}
headerFontWeight={props.headerFontWeight}
headerBackgroundColor={props.headerBackgroundColor}
headerTextColor={props.headerTextColor}
rowHeight={effectiveRowHeight}
rowPadding={effectiveRowPadding}
rowBorder={effectiveRowBorder}
boxShadow={finalBoxShadow}
sortableIndicatorColor={props.sortableIndicatorColor}
/>
</div>
</div>
</SidebarContainer>
);
};