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

fix(tab): out of bounds issue for screen size change #226

Closed
wants to merge 3 commits into from

Conversation

tatata96
Copy link
Contributor

@tatata96 tatata96 commented Jan 27, 2023

Related Issue;

I tried recreate problem by updating the items (it had 5 elements, I changed it to 2) for screens smaller than 400, using useWindowSize (this will be removed, only there for testing purposes).

Screen.Recording.2023-01-26.at.22.38.00.mov

After adding the out of bounds check and setting index to 0:

Screen.Recording.2023-01-26.at.23.02.23.mov

@tatata96 tatata96 self-assigned this Jan 27, 2023
@tatata96 tatata96 changed the base branch from main to next-release January 27, 2023 05:54
@github-actions
Copy link

github-actions bot commented Jan 27, 2023

PR Preview Action v1.4.6
Preview removed because the pull request was closed.
2024-01-11 08:04 UTC

Copy link
Collaborator

@jamcry jamcry left a comment

Choose a reason for hiding this comment

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

Hi @tatata96 , I realized code formatting on your PRs are a bit weird. Can you make sure your VSCode/IDE is using the correct linter/formatter?

For example, when I save this file, I get this diff (it formats according to our linter config files):
Screenshot 2023-01-27 at 12 47 36

Your ide should be using the local prettier config
Screenshot 2023-01-27 at 12 49 01

@jamcry
Copy link
Collaborator

jamcry commented Jan 27, 2023

@tatata96 I believe issue link should be this: #193

src/tab/Tab.tsx Outdated Show resolved Hide resolved
@@ -0,0 +1,28 @@
import {useState, useEffect} from "react";
Copy link
Collaborator

Choose a reason for hiding this comment

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

using useWindowSize (this will be removed, only there for testing purposes).

I saw this part, but this hook is not actually used at the moment, so maybe you can remove from remote, but keep it on your local if you need it

We could add an example of changing items to the story, but I don't think that's necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added that for testing purposes, it will be removed before the pr is merged

Copy link
Contributor

@Anlerkan Anlerkan left a comment

Choose a reason for hiding this comment

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

Not related to that issue but it came to mind while thinking about it,
Maybe we can simplify the handleChangeActiveTab function.
Here is my suggestion:

const tabChangeHandler = onTabChange || setActiveTabIndex;

  function handleChangeActiveTab(index: number) {
    if (index !== activeTabIndexToUse) {
      tabChangeHandler(index);
    }
  }

What do you think? @tatata96 @Hipo/frontend-team

src/tab/Tab.tsx Outdated
Comment on lines 52 to 54
let activeTabIndexToUse = activeTabIndexFromProps === undefined
? activeTabIndex
: activeTabIndexFromProps;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would that work?

Suggested change
let activeTabIndexToUse = activeTabIndexFromProps === undefined
? activeTabIndex
: activeTabIndexFromProps;
let activeTabIndexToUse = activeTabIndexFromProps || activeTabIndex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this works 👍

Copy link
Contributor

@edizcelik edizcelik Feb 23, 2023

Choose a reason for hiding this comment

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

What if activeTabIndexFromProps is 0? Does it still work as expected? This may be more reliable:

let activeTabIndexToUse = activeTabIndexFromProps ?? activeTabIndex;

@tatata96
Copy link
Contributor Author

tatata96 commented Feb 2, 2023

I think yours is more readable but it may cause issues for cases where we want to use onTabChange for uncontrolled tabs.
I think we can use your version of handleChangeActiveTab but change tabChangeHandler like below maybe:

const tabChangeHandler = activeTabIndexFromProps && onTabChange ? onTabChange : setActiveTabIndex;

What do you think? @Anlerkan

Comment on lines +52 to +60
let activeTabIndexToUse = activeTabIndexFromProps || activeTabIndex;

/**
* In case activeTabIndex is out of bounds, we need to reset it to 0.
* This can happen if items are changes after the mount
*/
if (activeTabIndexToUse > items.length) {
activeTabIndexToUse = 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be reflected to the state and also we should call onTabChange if the Tab is being used as a controlled component

@tatata96 tatata96 closed this Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants