-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathObjectGrid.tsx
More file actions
355 lines (307 loc) · 11.1 KB
/
Copy pathObjectGrid.tsx
File metadata and controls
355 lines (307 loc) · 11.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
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* ObjectGrid Component
*
* A specialized grid component built on top of data-table.
* Auto-generates columns from ObjectQL schema with type-aware rendering.
* Implements the grid view type from @objectstack/spec view.zod ListView schema.
*
* Features:
* - Traditional table/grid with CRUD operations
* - Search, filters, pagination
* - Column resizing, sorting
* - Row selection
* - Inline editing support
*/
import React, { useEffect, useState, useCallback } from 'react';
import type { ObjectGridSchema, DataSource, ListColumn, ViewData } from '@object-ui/types';
import { SchemaRenderer } from '@object-ui/react';
import { getCellRenderer } from './field-renderers';
export interface ObjectGridProps {
schema: ObjectGridSchema;
dataSource?: DataSource;
className?: string;
onRowClick?: (record: any) => void;
onEdit?: (record: any) => void;
onDelete?: (record: any) => void;
onBulkDelete?: (records: any[]) => void;
onCellChange?: (rowIndex: number, columnKey: string, newValue: any) => void;
onRowSelect?: (selectedRows: any[]) => void;
}
/**
* Helper to get data configuration from schema
* Handles both new ViewData format and legacy inline data
*/
function getDataConfig(schema: ObjectGridSchema): ViewData | null {
// New format: explicit data configuration
if (schema.data) {
return schema.data;
}
// Legacy format: staticData field
if (schema.staticData) {
return {
provider: 'value',
items: schema.staticData,
};
}
// Default: use object provider with objectName
if (schema.objectName) {
return {
provider: 'object',
object: schema.objectName,
};
}
return null;
}
/**
* Helper to normalize columns configuration
* Handles both string[] and ListColumn[] formats
*/
function normalizeColumns(
columns: string[] | ListColumn[] | undefined
): ListColumn[] | string[] | undefined {
if (!columns) return undefined;
// Already in ListColumn format - check for null and object type
if (columns.length > 0 && columns[0] && typeof columns[0] === 'object') {
return columns as ListColumn[];
}
// String array format
return columns as string[];
}
export const ObjectGrid: React.FC<ObjectGridProps> = ({
schema,
dataSource,
onEdit,
onDelete,
onRowSelect,
}) => {
const [data, setData] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const [objectSchema, setObjectSchema] = useState<any>(null);
// Get data configuration (supports both new and legacy formats)
const dataConfig = getDataConfig(schema);
const hasInlineData = dataConfig?.provider === 'value';
useEffect(() => {
if (hasInlineData && dataConfig?.provider === 'value') {
setData(dataConfig.items as any[]);
setLoading(false);
}
}, [hasInlineData, dataConfig]);
useEffect(() => {
const fetchObjectSchema = async () => {
try {
if (!dataSource) {
throw new Error('DataSource required');
}
// For object provider, get the object name
const objectName = dataConfig?.provider === 'object'
? dataConfig.object
: schema.objectName;
if (!objectName) {
throw new Error('Object name required for object provider');
}
const schemaData = await dataSource.getObjectSchema(objectName);
setObjectSchema(schemaData);
} catch (err) {
setError(err as Error);
}
};
// Normalize columns (support both legacy 'fields' and new 'columns')
const cols = normalizeColumns(schema.columns) || schema.fields;
if (hasInlineData && cols) {
setObjectSchema({ name: schema.objectName, fields: {} });
} else if (schema.objectName && !hasInlineData && dataSource) {
fetchObjectSchema();
}
}, [schema.objectName, schema.columns, schema.fields, dataSource, hasInlineData, dataConfig]);
const generateColumns = useCallback(() => {
// Use normalized columns (support both new and legacy)
const cols = normalizeColumns(schema.columns);
if (cols) {
// If columns are already ListColumn objects, convert them to data-table format
if (cols.length > 0 && typeof cols[0] === 'object') {
return (cols as ListColumn[]).map((col) => ({
header: col.label || col.field.charAt(0).toUpperCase() + col.field.slice(1).replace(/_/g, ' '),
accessorKey: col.field,
...(col.width && { width: col.width }),
...(col.align && { align: col.align }),
sortable: col.sortable !== false,
}));
}
// String array format - return as-is for now
return (cols as string[]).map((fieldName) => ({
header: fieldName.charAt(0).toUpperCase() + fieldName.slice(1).replace(/_/g, ' '),
accessorKey: fieldName,
}));
}
// Legacy support: use 'fields' if columns not provided
if (hasInlineData) {
const inlineData = dataConfig?.provider === 'value' ? dataConfig.items as any[] : [];
if (inlineData.length > 0) {
const fieldsToShow = schema.fields || Object.keys(inlineData[0]);
return fieldsToShow.map((fieldName) => ({
header: fieldName.charAt(0).toUpperCase() + fieldName.slice(1).replace(/_/g, ' '),
accessorKey: fieldName,
}));
}
}
if (!objectSchema) return [];
const generatedColumns: any[] = [];
const fieldsToShow = schema.fields || Object.keys(objectSchema.fields || {});
fieldsToShow.forEach((fieldName) => {
const field = objectSchema.fields?.[fieldName];
if (!field) return;
if (field.permissions && field.permissions.read === false) return;
const CellRenderer = getCellRenderer(field.type);
generatedColumns.push({
header: field.label || fieldName,
accessorKey: fieldName,
cell: (value: any) => <CellRenderer value={value} field={field} />,
sortable: field.sortable !== false,
});
});
return generatedColumns;
}, [objectSchema, schema.fields, schema.columns, dataConfig, hasInlineData]);
const fetchData = useCallback(async () => {
if (hasInlineData || !dataSource) return;
setLoading(true);
try {
// Get object name from data config or schema
const objectName = dataConfig?.provider === 'object'
? dataConfig.object
: schema.objectName;
if (!objectName) {
throw new Error('Object name required for data fetching');
}
// Helper to get select fields
const getSelectFields = () => {
if (schema.fields) return schema.fields;
if (schema.columns && Array.isArray(schema.columns)) {
return schema.columns.map(c => typeof c === 'string' ? c : c.field);
}
return undefined;
};
const params: any = {
$select: getSelectFields(),
$top: schema.pagination?.pageSize || schema.pageSize || 50,
};
// Support new filter format
if (schema.filter && Array.isArray(schema.filter)) {
params.$filter = schema.filter;
} else if ('defaultFilters' in schema && schema.defaultFilters) {
// Legacy support
params.$filter = schema.defaultFilters;
}
// Support new sort format
if (schema.sort) {
if (typeof schema.sort === 'string') {
// Legacy string format
params.$orderby = schema.sort;
} else if (Array.isArray(schema.sort)) {
// New array format
params.$orderby = schema.sort
.map(s => `${s.field} ${s.order}`)
.join(', ');
}
} else if ('defaultSort' in schema && schema.defaultSort) {
// Legacy support
params.$orderby = `${schema.defaultSort.field} ${schema.defaultSort.order}`;
}
const result = await dataSource.find(objectName, params);
setData(result.data || []);
} catch (err) {
setError(err as Error);
} finally {
setLoading(false);
}
}, [schema, dataSource, hasInlineData, dataConfig]);
useEffect(() => {
if (objectSchema || hasInlineData) {
fetchData();
}
}, [objectSchema, hasInlineData, fetchData]);
if (error) {
return (
<div className="p-4 border border-red-300 bg-red-50 rounded-md">
<h3 className="text-red-800 font-semibold">Error loading grid</h3>
<p className="text-red-600 text-sm mt-1">{error.message}</p>
</div>
);
}
if (loading && data.length === 0) {
return (
<div className="p-8 text-center">
<div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"></div>
<p className="mt-2 text-sm text-gray-600">Loading grid...</p>
</div>
);
}
const columns = generateColumns();
const operations = 'operations' in schema ? schema.operations : undefined;
const hasActions = operations && (operations.update || operations.delete);
const columnsWithActions = hasActions ? [
...columns,
{
header: 'Actions',
accessorKey: '_actions',
cell: (_value: any, row: any) => (
<div className="flex gap-2">
{operations?.update && onEdit && (
<button onClick={() => onEdit(row)} className="text-blue-600 hover:text-blue-800 text-sm">
Edit
</button>
)}
{operations?.delete && onDelete && (
<button onClick={() => onDelete(row)} className="text-red-600 hover:text-red-800 text-sm">
Delete
</button>
)}
</div>
),
sortable: false,
},
] : columns;
// Determine selection mode (support both new and legacy formats)
let selectionMode: 'none' | 'single' | 'multiple' | boolean = false;
if (schema.selection?.type) {
selectionMode = schema.selection.type === 'none' ? false : schema.selection.type;
} else if (schema.selectable !== undefined) {
// Legacy support
selectionMode = schema.selectable;
}
// Determine pagination settings (support both new and legacy formats)
const paginationEnabled = schema.pagination !== undefined
? true
: (schema.showPagination !== undefined ? schema.showPagination : true);
const pageSize = schema.pagination?.pageSize
|| schema.pageSize
|| 10;
// Determine search settings
const searchEnabled = schema.searchableFields !== undefined
? schema.searchableFields.length > 0
: (schema.showSearch !== undefined ? schema.showSearch : true);
const dataTableSchema: any = {
type: 'data-table',
caption: schema.label || schema.title,
columns: columnsWithActions,
data,
pagination: paginationEnabled,
pageSize: pageSize,
searchable: searchEnabled,
selectable: selectionMode,
sortable: true,
exportable: operations?.export,
rowActions: hasActions,
resizableColumns: schema.resizable ?? schema.resizableColumns ?? true,
className: schema.className,
onSelectionChange: onRowSelect,
};
return <SchemaRenderer schema={dataTableSchema} />;
};