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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

CarlosNZ
Copy link
Contributor

Basically a pre-requisite for #5990, but just wanted to make this is separate PR as it's essentially a separate refactor of existing functionality and didn't want to over-complicate #5990 proper.

👩🏻‍💻 What does this PR do?

It's been an ongoing hassle when making Table cells that require more props than are available from the table provider. If you try and define a new Component inline, you get problems with losing input focus on re-render. So you have to define a component outside the table column definitions. But even with that, I was having problems implementing what I need for #5990 without getting input focus issues on the Number Input.

So this PR just provides a very simple mechanism to pass props into whatever cell is defined in the Cell field of a table column definitions -- the column definition can take an additional cellProps property, which is then passed into the Cell component where it is rendered (inside DataRow)

I've implemented it just in one place to demonstrate that it works as expected -- in StocktakeLineEditTables. There will be a more substantial implementation in #5990 proper,

💌 Any notes for the reviewer?

Any potential problems? I don't think so, but haven't tried too hard to break it.

🧪 Testing

Check that the "Counted Number Of Packs" field in the Stocktake Line Edit modal behaves exactly the same as before.

@github-actions github-actions bot added this to the v2.6.0 milestone Jan 20, 2025
@github-actions github-actions bot added refactor Team Piwakawaka James, Carl, John, Zachariah labels Jan 20, 2025
Copy link

Bundle size difference

Comparing this PR to main

Old size New size Diff
5.16 MB 5.16 MB -5 B (-0.00%)

@lache-melvin lache-melvin self-assigned this Jan 20, 2025
Copy link
Contributor

@lache-melvin lache-melvin left a comment

Choose a reason for hiding this comment

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

Happy with this as it is, better than the separately defined Cells (is there a plan to clean these up?) but if we can type it, that would be my preference 🙏

@@ -90,6 +90,9 @@ export interface Column<T extends RecordWithId> {
backgroundColor?: string;

Cell: JSXElementConstructor<CellProps<T>>;
// For passing additional props to the above Cell
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Team Piwakawaka James, Carl, John, Zachariah
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants