-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
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 detectedLatest commit: 1c33895 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
size-limit report 📦
|
it('updates selected state when clicked', async () => { | ||
component.click(); | ||
await component.updateComplete; | ||
expect(component.selected).to.be.true; | ||
}); |
There was a problem hiding this comment.
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
private _handleClick(): void { | ||
this.selected = true; | ||
this._triggerSelectedEvent(); | ||
} |
There was a problem hiding this comment.
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.
private _selectInitialTab(): void { | ||
const selected: PharosTab | null = this.querySelector(`${_allTabsSelector}[selected]`); | ||
const selectedTab: PharosTab = selected ? selected : this._tabs[0]; | ||
this._handleTabSelected(selectedTab); | ||
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
* 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)
* 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
This change: (check at least one)
Is this a breaking change? (check one)
Is the: (complete all)
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.