mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	[PUI] Column Refactoring (#7242)
* Refactor column helpers * Make reference column switchable in BomTable * Make 'accessor' a required field againt * Update props * Fix c0d3
This commit is contained in:
		| @@ -1,8 +1,5 @@ | ||||
| /** | ||||
|  * Interface for the table column definition | ||||
|  */ | ||||
| export type TableColumn<T = any> = { | ||||
|   accessor: string; // The key in the record to access | ||||
| export type TableColumnProps<T = any> = { | ||||
|   accessor?: string; // The key in the record to access | ||||
|   title?: string; // The title of the column - Note: this may be supplied by the API, and is not required, but it can be overridden if required | ||||
|   ordering?: string; // The key in the record to sort by (defaults to accessor) | ||||
|   sortable?: boolean; // Whether the column is sortable | ||||
| @@ -18,3 +15,10 @@ export type TableColumn<T = any> = { | ||||
|   cellsStyle?: any; // The style of the cells in the column | ||||
|   extra?: any; // Extra data to pass to the render function | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Interface for the table column definition | ||||
|  */ | ||||
| export type TableColumn<T = any> = { | ||||
|   accessor: string; // The key in the record to access | ||||
| } & TableColumnProps<T>; | ||||
|   | ||||
| @@ -13,7 +13,7 @@ import { formatCurrency, renderDate } from '../defaults/formatters'; | ||||
| import { ModelType } from '../enums/ModelType'; | ||||
| import { resolveItem } from '../functions/conversion'; | ||||
| import { cancelEvent } from '../functions/events'; | ||||
| import { TableColumn } from './Column'; | ||||
| import { TableColumn, TableColumnProps } from './Column'; | ||||
| import { ProjectCodeHoverCard } from './TableHoverCard'; | ||||
|  | ||||
| // Render a Part instance within a table | ||||
| @@ -26,24 +26,14 @@ export function PartColumn(part: any, full_name?: boolean) { | ||||
|   ); | ||||
| } | ||||
|  | ||||
| export function LocationColumn({ | ||||
|   accessor, | ||||
|   title, | ||||
|   sortable, | ||||
|   ordering | ||||
| }: { | ||||
|   accessor: string; | ||||
|   title?: string; | ||||
|   sortable?: boolean; | ||||
|   ordering?: string; | ||||
| }): TableColumn { | ||||
| export function LocationColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: accessor, | ||||
|     title: title ?? t`Location`, | ||||
|     sortable: sortable ?? true, | ||||
|     ordering: ordering ?? 'location', | ||||
|     accessor: 'location', | ||||
|     title: t`Location`, | ||||
|     sortable: true, | ||||
|     ordering: 'location', | ||||
|     render: (record: any) => { | ||||
|       let location = resolveItem(record, accessor); | ||||
|       let location = resolveItem(record, props.accessor ?? ''); | ||||
|  | ||||
|       if (!location) { | ||||
|         return ( | ||||
| @@ -52,62 +42,38 @@ export function LocationColumn({ | ||||
|       } | ||||
|  | ||||
|       return <Text>{location.name}</Text>; | ||||
|     } | ||||
|     }, | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function BooleanColumn({ | ||||
|   accessor, | ||||
|   title, | ||||
|   sortable, | ||||
|   switchable, | ||||
|   ordering | ||||
| }: { | ||||
|   accessor: string; | ||||
|   title?: string; | ||||
|   ordering?: string; | ||||
|   sortable?: boolean; | ||||
|   switchable?: boolean; | ||||
| }): TableColumn { | ||||
| export function BooleanColumn(props: TableColumn): TableColumn { | ||||
|   return { | ||||
|     accessor: accessor, | ||||
|     title: title, | ||||
|     ordering: ordering, | ||||
|     sortable: sortable ?? true, | ||||
|     switchable: switchable ?? true, | ||||
|     sortable: true, | ||||
|     switchable: true, | ||||
|     render: (record: any) => ( | ||||
|       <YesNoButton value={resolveItem(record, accessor)} /> | ||||
|     ) | ||||
|       <YesNoButton value={resolveItem(record, props.accessor ?? '')} /> | ||||
|     ), | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function DescriptionColumn({ | ||||
|   accessor, | ||||
|   sortable, | ||||
|   switchable | ||||
| }: { | ||||
|   accessor?: string; | ||||
|   sortable?: boolean; | ||||
|   switchable?: boolean; | ||||
| }): TableColumn { | ||||
| export function DescriptionColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: accessor ?? 'description', | ||||
|     accessor: 'description', | ||||
|     title: t`Description`, | ||||
|     sortable: sortable ?? false, | ||||
|     switchable: switchable ?? true | ||||
|     sortable: false, | ||||
|     switchable: true, | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function LinkColumn({ | ||||
|   accessor = 'link' | ||||
| }: { | ||||
|   accessor?: string; | ||||
| }): TableColumn { | ||||
| export function LinkColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: accessor, | ||||
|     accessor: 'link', | ||||
|     sortable: false, | ||||
|     render: (record: any) => { | ||||
|       let url = resolveItem(record, accessor); | ||||
|       let url = resolveItem(record, props.accessor ?? 'link'); | ||||
|  | ||||
|       if (!url) { | ||||
|         return '-'; | ||||
| @@ -127,24 +93,28 @@ export function LinkColumn({ | ||||
|           {url} | ||||
|         </Anchor> | ||||
|       ); | ||||
|     } | ||||
|     }, | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function ReferenceColumn(): TableColumn { | ||||
| export function ReferenceColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: 'reference', | ||||
|     title: t`Reference`, | ||||
|     sortable: true, | ||||
|     switchable: false | ||||
|     switchable: true, | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function NoteColumn(): TableColumn { | ||||
| export function NoteColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: 'note', | ||||
|     sortable: false, | ||||
|     title: t`Note`, | ||||
|     render: (record: any) => record.note ?? record.notes | ||||
|     render: (record: any) => record.note ?? record.notes, | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| @@ -162,13 +132,14 @@ export function LineItemsProgressColumn(): TableColumn { | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function ProjectCodeColumn(): TableColumn { | ||||
| export function ProjectCodeColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: 'project_code', | ||||
|     sortable: true, | ||||
|     render: (record: any) => ( | ||||
|       <ProjectCodeHoverCard projectCode={record.project_code_detail} /> | ||||
|     ) | ||||
|     ), | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| @@ -188,62 +159,52 @@ export function StatusColumn({ | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function ResponsibleColumn(): TableColumn { | ||||
| export function ResponsibleColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: 'responsible', | ||||
|     sortable: true, | ||||
|     switchable: true, | ||||
|     render: (record: any) => | ||||
|       record.responsible && RenderOwner({ instance: record.responsible_detail }) | ||||
|       record.responsible && | ||||
|       RenderOwner({ instance: record.responsible_detail }), | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function DateColumn({ | ||||
|   accessor, | ||||
|   sortable, | ||||
|   switchable, | ||||
|   ordering, | ||||
|   title | ||||
| }: { | ||||
|   accessor?: string; | ||||
|   ordering?: string; | ||||
|   sortable?: boolean; | ||||
|   switchable?: boolean; | ||||
|   title?: string; | ||||
| }): TableColumn { | ||||
| export function DateColumn(props: TableColumnProps): TableColumn { | ||||
|   return { | ||||
|     accessor: accessor ?? 'date', | ||||
|     sortable: sortable ?? true, | ||||
|     ordering: ordering, | ||||
|     title: title ?? t`Date`, | ||||
|     switchable: switchable, | ||||
|     render: (record: any) => renderDate(resolveItem(record, accessor ?? 'date')) | ||||
|     accessor: 'date', | ||||
|     sortable: true, | ||||
|     title: t`Date`, | ||||
|     switchable: true, | ||||
|     render: (record: any) => | ||||
|       renderDate(resolveItem(record, props.accessor ?? 'date')), | ||||
|     ...props | ||||
|   }; | ||||
| } | ||||
|  | ||||
| export function TargetDateColumn(): TableColumn { | ||||
|   return { | ||||
| export function TargetDateColumn(props: TableColumnProps): TableColumn { | ||||
|   return DateColumn({ | ||||
|     accessor: 'target_date', | ||||
|     sortable: true, | ||||
|     title: t`Target Date`, | ||||
|     // TODO: custom renderer which alerts user if target date is overdue | ||||
|     render: (record: any) => renderDate(record.target_date) | ||||
|   }; | ||||
|     ...props | ||||
|   }); | ||||
| } | ||||
|  | ||||
| export function CreationDateColumn(): TableColumn { | ||||
|   return { | ||||
| export function CreationDateColumn(props: TableColumnProps): TableColumn { | ||||
|   return DateColumn({ | ||||
|     accessor: 'creation_date', | ||||
|     sortable: true, | ||||
|     render: (record: any) => renderDate(record.creation_date) | ||||
|   }; | ||||
|     title: t`Creation Date`, | ||||
|     ...props | ||||
|   }); | ||||
| } | ||||
|  | ||||
| export function ShipmentDateColumn(): TableColumn { | ||||
|   return { | ||||
| export function ShipmentDateColumn(props: TableColumnProps): TableColumn { | ||||
|   return DateColumn({ | ||||
|     accessor: 'shipment_date', | ||||
|     sortable: true, | ||||
|     render: (record: any) => renderDate(record.shipment_date) | ||||
|   }; | ||||
|     title: t`Shipment Date`, | ||||
|     ...props | ||||
|   }); | ||||
| } | ||||
|  | ||||
| export function CurrencyColumn({ | ||||
|   | ||||
| @@ -99,7 +99,9 @@ export function BomTable({ | ||||
|       DescriptionColumn({ | ||||
|         accessor: 'sub_part_detail.description' | ||||
|       }), | ||||
|       ReferenceColumn(), | ||||
|       ReferenceColumn({ | ||||
|         switchable: true | ||||
|       }), | ||||
|       { | ||||
|         accessor: 'quantity', | ||||
|         switchable: false, | ||||
| @@ -248,7 +250,7 @@ export function BomTable({ | ||||
|           ); | ||||
|         } | ||||
|       }, | ||||
|       NoteColumn() | ||||
|       NoteColumn({}) | ||||
|     ]; | ||||
|   }, [partId, params]); | ||||
|  | ||||
|   | ||||
| @@ -52,7 +52,7 @@ export function UsedInTable({ | ||||
|           ); | ||||
|         } | ||||
|       }, | ||||
|       ReferenceColumn() | ||||
|       ReferenceColumn({}) | ||||
|     ]; | ||||
|   }, [partId]); | ||||
|  | ||||
|   | ||||
| @@ -36,7 +36,7 @@ import { InvenTreeTable } from '../InvenTreeTable'; | ||||
|  */ | ||||
| function buildOrderTableColumns(): TableColumn[] { | ||||
|   return [ | ||||
|     ReferenceColumn(), | ||||
|     ReferenceColumn({}), | ||||
|     { | ||||
|       accessor: 'part', | ||||
|       sortable: true, | ||||
| @@ -60,13 +60,13 @@ function buildOrderTableColumns(): TableColumn[] { | ||||
|       ) | ||||
|     }, | ||||
|     StatusColumn({ model: ModelType.build }), | ||||
|     ProjectCodeColumn(), | ||||
|     ProjectCodeColumn({}), | ||||
|     { | ||||
|       accessor: 'priority', | ||||
|       sortable: true | ||||
|     }, | ||||
|     CreationDateColumn(), | ||||
|     TargetDateColumn(), | ||||
|     CreationDateColumn({}), | ||||
|     TargetDateColumn({}), | ||||
|     DateColumn({ | ||||
|       accessor: 'completion_date', | ||||
|       sortable: true | ||||
| @@ -78,7 +78,7 @@ function buildOrderTableColumns(): TableColumn[] { | ||||
|         <RenderUser instance={record?.issued_by_detail} /> | ||||
|       ) | ||||
|     }, | ||||
|     ResponsibleColumn() | ||||
|     ResponsibleColumn({}) | ||||
|   ]; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -88,7 +88,7 @@ export function PurchaseOrderLineItemTable({ | ||||
|         sortable: false, | ||||
|         render: (record: any) => record?.part_detail?.description | ||||
|       }, | ||||
|       ReferenceColumn(), | ||||
|       ReferenceColumn({}), | ||||
|       { | ||||
|         accessor: 'quantity', | ||||
|         title: t`Quantity`, | ||||
| @@ -170,7 +170,7 @@ export function PurchaseOrderLineItemTable({ | ||||
|         title: t`Unit Price` | ||||
|       }), | ||||
|       TotalPriceColumn(), | ||||
|       TargetDateColumn(), | ||||
|       TargetDateColumn({}), | ||||
|       { | ||||
|         accessor: 'destination', | ||||
|         title: t`Destination`, | ||||
| @@ -180,7 +180,7 @@ export function PurchaseOrderLineItemTable({ | ||||
|             ? RenderStockLocation({ instance: record.destination_detail }) | ||||
|             : '-' | ||||
|       }, | ||||
|       NoteColumn(), | ||||
|       NoteColumn({}), | ||||
|       LinkColumn({}) | ||||
|     ]; | ||||
|   }, [orderId, user]); | ||||
|   | ||||
| @@ -81,7 +81,7 @@ export function PurchaseOrderTable({ | ||||
|  | ||||
|   const tableColumns = useMemo(() => { | ||||
|     return [ | ||||
|       ReferenceColumn(), | ||||
|       ReferenceColumn({}), | ||||
|       DescriptionColumn({}), | ||||
|       { | ||||
|         accessor: 'supplier__name', | ||||
| @@ -104,9 +104,9 @@ export function PurchaseOrderTable({ | ||||
|       }, | ||||
|       LineItemsProgressColumn(), | ||||
|       StatusColumn({ model: ModelType.purchaseorder }), | ||||
|       ProjectCodeColumn(), | ||||
|       CreationDateColumn(), | ||||
|       TargetDateColumn(), | ||||
|       ProjectCodeColumn({}), | ||||
|       CreationDateColumn({}), | ||||
|       TargetDateColumn({}), | ||||
|       { | ||||
|         accessor: 'total_price', | ||||
|         title: t`Total Price`, | ||||
| @@ -117,7 +117,7 @@ export function PurchaseOrderTable({ | ||||
|           }); | ||||
|         } | ||||
|       }, | ||||
|       ResponsibleColumn() | ||||
|       ResponsibleColumn({}) | ||||
|     ]; | ||||
|   }, []); | ||||
|  | ||||
|   | ||||
| @@ -134,7 +134,7 @@ export function SupplierPartTable({ params }: { params: any }): ReactNode { | ||||
|         } | ||||
|       }, | ||||
|       LinkColumn({}), | ||||
|       NoteColumn(), | ||||
|       NoteColumn({}), | ||||
|       { | ||||
|         accessor: 'available', | ||||
|         sortable: true, | ||||
|   | ||||
| @@ -72,7 +72,7 @@ export function ReturnOrderTable({ params }: { params?: any }) { | ||||
|  | ||||
|   const tableColumns = useMemo(() => { | ||||
|     return [ | ||||
|       ReferenceColumn(), | ||||
|       ReferenceColumn({}), | ||||
|       { | ||||
|         accessor: 'customer__name', | ||||
|         title: t`Customer`, | ||||
| @@ -95,10 +95,10 @@ export function ReturnOrderTable({ params }: { params?: any }) { | ||||
|       DescriptionColumn({}), | ||||
|       LineItemsProgressColumn(), | ||||
|       StatusColumn({ model: ModelType.returnorder }), | ||||
|       ProjectCodeColumn(), | ||||
|       CreationDateColumn(), | ||||
|       TargetDateColumn(), | ||||
|       ResponsibleColumn(), | ||||
|       ProjectCodeColumn({}), | ||||
|       CreationDateColumn({}), | ||||
|       TargetDateColumn({}), | ||||
|       ResponsibleColumn({}), | ||||
|       { | ||||
|         accessor: 'total_price', | ||||
|         title: t`Total Price`, | ||||
|   | ||||
| @@ -101,7 +101,7 @@ export function SalesOrderTable({ | ||||
|  | ||||
|   const tableColumns = useMemo(() => { | ||||
|     return [ | ||||
|       ReferenceColumn(), | ||||
|       ReferenceColumn({}), | ||||
|       { | ||||
|         accessor: 'customer__name', | ||||
|         title: t`Customer`, | ||||
| @@ -125,10 +125,10 @@ export function SalesOrderTable({ | ||||
|       DescriptionColumn({}), | ||||
|       LineItemsProgressColumn(), | ||||
|       StatusColumn({ model: ModelType.salesorder }), | ||||
|       ProjectCodeColumn(), | ||||
|       CreationDateColumn(), | ||||
|       TargetDateColumn(), | ||||
|       ShipmentDateColumn(), | ||||
|       ProjectCodeColumn({}), | ||||
|       CreationDateColumn({}), | ||||
|       TargetDateColumn({}), | ||||
|       ShipmentDateColumn({}), | ||||
|       { | ||||
|         accessor: 'total_price', | ||||
|         title: t`Total Price`, | ||||
|   | ||||
| @@ -33,7 +33,7 @@ export default function ProjectCodeTable() { | ||||
|         sortable: true | ||||
|       }, | ||||
|       DescriptionColumn({}), | ||||
|       ResponsibleColumn() | ||||
|       ResponsibleColumn({}) | ||||
|     ]; | ||||
|   }, []); | ||||
|  | ||||
|   | ||||
| @@ -187,7 +187,7 @@ export default function StockItemTestResultTable({ | ||||
|         render: (record: any) => | ||||
|           record.attachment && <AttachmentLink attachment={record.attachment} /> | ||||
|       }, | ||||
|       NoteColumn(), | ||||
|       NoteColumn({}), | ||||
|       DateColumn({}), | ||||
|       { | ||||
|         accessor: 'user', | ||||
|   | ||||
		Reference in New Issue
	
	Block a user