|
16 | 16 | import React, { useEffect, useState, useCallback } from 'react'; |
17 | 17 | import type { ObjectTableSchema, TableColumn, TableSchema, DataSource } from '@object-ui/types'; |
18 | 18 | import { SchemaRenderer } from '@object-ui/react'; |
| 19 | +import { getCellRenderer } from './field-renderers'; |
19 | 20 |
|
20 | 21 | export interface ObjectTableProps { |
21 | 22 | /** |
@@ -174,52 +175,17 @@ export const ObjectTable: React.FC<ObjectTableProps> = ({ |
174 | 175 | if (customColumn) { |
175 | 176 | generatedColumns.push(customColumn); |
176 | 177 | } else { |
177 | | - // Auto-generate column from field schema |
| 178 | + // Auto-generate column from field schema with type-aware cell renderer |
| 179 | + const CellRenderer = getCellRenderer(field.type); |
| 180 | + |
178 | 181 | const column: TableColumn = { |
179 | 182 | header: field.label || fieldName, |
180 | 183 | accessorKey: fieldName, |
| 184 | + cell: (value: any) => { |
| 185 | + return <CellRenderer value={value} field={field} />; |
| 186 | + }, |
181 | 187 | }; |
182 | 188 |
|
183 | | - // Add field type-specific formatting hints |
184 | | - if (field.type === 'date' || field.type === 'datetime') { |
185 | | - column.type = 'date'; |
186 | | - } else if (field.type === 'boolean') { |
187 | | - column.type = 'boolean'; |
188 | | - } else if (field.type === 'number' || field.type === 'currency' || field.type === 'percent') { |
189 | | - column.type = 'number'; |
190 | | - } else if (field.type === 'image' || field.type === 'file') { |
191 | | - // For file/image fields, display the name or count |
192 | | - column.cell = (value: any) => { |
193 | | - if (!value) return '-'; |
194 | | - if (Array.isArray(value)) { |
195 | | - const count = value.length; |
196 | | - const fileType = field.type === 'image' ? 'image' : 'file'; |
197 | | - return count === 1 ? `1 ${fileType}` : `${count} ${fileType}s`; |
198 | | - } |
199 | | - return value.name || value.original_name || 'File'; |
200 | | - }; |
201 | | - } else if (field.type === 'lookup' || field.type === 'master_detail') { |
202 | | - // For relationship fields, display the name property if available |
203 | | - column.cell = (value: any) => { |
204 | | - if (!value) return '-'; |
205 | | - if (typeof value === 'object' && value !== null) { |
206 | | - // Try common display properties first |
207 | | - if (value.name) return value.name; |
208 | | - if (value.label) return value.label; |
209 | | - if (value._id) return value._id; |
210 | | - // Fallback to object type indicator |
211 | | - return '[Object]'; |
212 | | - } |
213 | | - return String(value); |
214 | | - }; |
215 | | - } else if (field.type === 'url') { |
216 | | - // For URL fields, make them clickable |
217 | | - column.cell = (value: any) => { |
218 | | - if (!value) return '-'; |
219 | | - return value; // The table renderer should handle URL formatting |
220 | | - }; |
221 | | - } |
222 | | - |
223 | 189 | // Add sorting if field is sortable |
224 | 190 | if (field.sortable !== false) { |
225 | 191 | column.sortable = true; |
|
0 commit comments