Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5990(1) Allow custom props for table cells without creating new component. #6172

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/packages/common/src/ui/layout/tables/columns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export interface Column<T extends RecordWithId> {
backgroundColor?: string;

Cell: JSXElementConstructor<CellProps<T>>;
// For passing additional props to the above Cell -- ⚠️ use with caution as
// these are not properly typed
cellProps?: Record<string, unknown>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm ... I played around with something like this:

cellProps?: Omit<ComponentProps<Column<T>['Cell']>, keyof CellProps<T>>;

It's not quite right but might be good to experiment with for a sec? Would be nice for cellProps to be typed based on the Cell

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had it almost working too. I'll have another look, won't spend too long on it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - that was my concern too. allowing untyped spreading and working around the type system isn't ideal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difficulty is that the Cell prop itself isn't actually correctly defined: JSXElementConstructor<CellProps<T>> but it can can have props beyond CellProps<T>, so it's hard to define props type for props the Cell property isn't actually aware of (if that makes any sense).

I tried passing in a second generic U to the Column definition, but this requires every column to have the same props for its "Cell" which is obviously not the case.

I think we inherently lose type information by defining Cell like this. Any other suggestions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I've just added a comment to use with caution. I think a more significant refactor is in order to properly type these 🤔


Header: JSXElementConstructor<HeaderProps<T>>;

formatter: ColumnDataFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const DataRowComponent = <T extends RecordWithId>({
autocompleteName={column.autocompleteProvider?.(rowData)}
localisedText={localisedText}
localisedDate={localisedDate}
{...column.cellProps}
/>
}
</TableCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ export const BatchTable: FC<StocktakeLineEditTableProps> = ({
getIsError: rowData =>
errorsContext.getError(rowData)?.__typename ===
'StockLineReducedBelowZero',
Cell: props => <NumberInputCell {...props} decimalLimit={2} min={0} />,
Cell: NumberInputCell,
cellProps: { decimalLimit: 2, min: 0 },
setter: patch => {
// If counted number of packs was changed to result in no adjustment we
// should remove inventoryAdjustmentReason, otherwise could have a
Expand Down
Loading