Skip to content

Fix error when passing a signle item to TabController #1578

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

Merged
merged 2 commits into from
Oct 3, 2021

Conversation

ethanshar
Copy link
Collaborator

Description

Fix exception when passing a single item to TabController

Changelog

Fix exception when passing a single item to TabController

Comment on lines 104 to 105
const itemsWidthsAnimated = useSharedValue(_.times(Math.max(itemsCount, 2), () => 0));
const itemsOffsetsAnimated = useSharedValue(_.times(Math.max(itemsCount, 2), () => 0));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a simple solution, but it feels technically incorrect since we're returning an array with a length of 2 instead of 1.
However, when testing for another solution it will probably be more code change than this.
WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I preferred a simple fix.
TBH, I initially thought about not supporting it all and instead throw an appropriate error to the users that they must pass at least two items. WDYT?
I don't see a reason to use TabController for a single page..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds like a possible solution.
Is there an option in one of our apps where there can be a single tab? If it is possible, then maybe we should handle it and not force the users to have an if\else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think it's relevant..
The person who reported the issue, just played with the component and passed a single item, but it wasn't his intention so it wasn't really a problem.
So I think having an appropriate error message in this case will be suffice...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I added a console.error instead of throwing and handled the issue a little better (I think)
Let me know what u think..

@ethanshar ethanshar requested a review from M-i-k-e-l October 1, 2021 10:03
Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l left a comment

Choose a reason for hiding this comment

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

I understood where this came from; I'd consider adding something like:

<View style={[styles.selectedIndicator, {left: indicatorInsets, width: itemsWidthsAnimated.value[0] - 2 * indicatorInsets}]}/>

so the indicator is seen, but I know you prefer less code --> merge if I'm right.

@ethanshar
Copy link
Collaborator Author

I understood where this came from; I'd consider adding something like:

<View style={[styles.selectedIndicator, {left: indicatorInsets, width: itemsWidthsAnimated.value[0] - 2 * indicatorInsets}]}/>

so the indicator is seen, but I know you prefer less code --> merge if I'm right.

Either way (with the indicator or without it) it doesn't look great.
So I prefer keeping the the code cleaner..

@ethanshar ethanshar merged commit 8d716d6 into master Oct 3, 2021
@ethanshar ethanshar deleted the fix/TabControllerSingleItem branch November 30, 2021 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants