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

Tabs: fix initial tab selection in more cases #449

Merged
merged 4 commits into from
Nov 10, 2022

Conversation

daneah
Copy link
Member

@daneah daneah commented Nov 10, 2022

This change: (check at least one)

  • Adds a new feature
  • Fixes a bug
  • Improves maintainability
  • Improves documentation
  • Is a release activity

Is this a breaking change? (check one)

  • Yes
  • No

Is the: (complete all)

  • Title of this pull request clear, concise, and indicative of the issue number it addresses, if any?
  • Test suite(s) passing?
  • Code coverage maximal?
  • Changeset added?
  • Component status page up to date?

What does this change address?

Fixes #346
Supersedes #448

How does this change work?

The design flaw in tabs was letting each individual tab manage its own state when clicked. This created issues with state management centralization and reasoning. This change instead moves the job of state management to the Tabs component, which coordinates the individual Tab and TabPanel components it contains. This results in smoother management and, ultimately, a more correct implementation.

The design flaw in tabs was letting each individual tab manage its own
state when clicked. This created issues with state management
centralization and reasoning. This change instead moves the job of state
management to the Tabs component, which coordinates the individual Tab
and TabPanel components it contains. This results in smoother management
and, ultimately, a more correct implementation.
@changeset-bot
Copy link

changeset-bot bot commented Nov 10, 2022

🦋 Changeset detected

Latest commit: 1c33895

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@ithaka/pharos Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Nov 10, 2022

size-limit report 📦

Path Size
packages/pharos/lib/index.js 46.53 KB (+0.16% 🔺)

Comment on lines -25 to -29
it('updates selected state when clicked', async () => {
component.click();
await component.updateComplete;
expect(component.selected).to.be.true;
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Individual tabs no longer manage this state themselves

Comment on lines 65 to 67
private _handleClick(): void {
this.selected = true;
this._triggerSelectedEvent();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

On click, individual tabs now only fire the event saying that they got selected; they don't manage the transition to actual selected state themselves.

Comment on lines 55 to 59
private _selectInitialTab(): void {
const selected: PharosTab | null = this.querySelector(`${_allTabsSelector}[selected]`);
const selectedTab: PharosTab = selected ? selected : this._tabs[0];
this._handleTabSelected(selectedTab);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified this function by adjusting _handleTabSelected to work agnostic of how/why the tab got selected, instead of managing different cases of selection different ways.

const previous: PharosTab | null = this.querySelector(
`[data-pharos-component="PharosTab"][selected]:not([id="${selected.id}"])`
private _handleTabSelected(selectedTab: PharosTab): void {
selectedTab.selected = true;
Copy link
Member Author

Choose a reason for hiding this comment

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

Tabs component now sets selected for the Tab component.

previous.selected = false;
const panel: PharosTabPanel | null = this.querySelector(
`[data-pharos-component="PharosTabPanel"][id="${previous.getAttribute('aria-controls')}"]`
if (previousTab && previousTab != selectedTab) {
Copy link
Member Author

Choose a reason for hiding this comment

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

There was a bug where you could click a tab repeatedly and it would select/deselect/select/deselect.

@daneah daneah marked this pull request as ready for review November 10, 2022 19:29
@daneah daneah requested a review from a team as a code owner November 10, 2022 19:29
@daneah daneah requested review from eslawski, SMQuazi and satya-achanta-venkata and removed request for a team November 10, 2022 19:29
@daneah daneah merged commit 50e665d into develop Nov 10, 2022
@daneah daneah deleted the fix/tabs-initial-selection branch November 10, 2022 22:51
daneah added a commit that referenced this pull request Nov 10, 2022
* develop:
  Tabs: fix initial tab selection in more cases (#449)
  Storybook: add eslint-plugin-storybook and fix issues (#446)
  feat(storybook): install Measure and Outline addons (#445)
  Docs: update copy for Button's on-background usage (#444)
  Storybook: Convert stories to CSF 3 (#435)
  feat(tabs): fire event when tab selected programmatically (#442)
  ToggleButton: indicate toggle button state on underlying button elements (#438)
  Tooltip, DropdownMenu: replace popperjs with floating-ui (#434)
  Site: improve imports (#433)
@github-actions github-actions bot mentioned this pull request Nov 10, 2022
sirrah-tam pushed a commit to sirrah-tam/pharos that referenced this pull request Dec 1, 2023
* fix(tabs): fix initial tab selection in more cases

The design flaw in tabs was letting each individual tab manage its own
state when clicked. This created issues with state management
centralization and reasoning. This change instead moves the job of state
management to the Tabs component, which coordinates the individual Tab
and TabPanel components it contains. This results in smoother management
and, ultimately, a more correct implementation.

* chore: add changeset

* fix(tabs): only trigger event when selected prop changes

* refactor(tabs): shorten ternary logic
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.

Tab: First tab always gets selected disregarding the "selected" attribute
3 participants