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(v2): select correct tab when items are incorrectly ordered #4468

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ function Tabs(props: Props): JSX.Element {
}
}

const handleTabChange = (event) => {
const selectedTab = event.target;
const handleTabChange = (
event: React.FocusEvent<HTMLLIElement> | React.MouseEvent<HTMLLIElement>,
) => {
const selectedTab = event.currentTarget;
Copy link
Contributor Author

@armano2 armano2 Mar 19, 2021

Choose a reason for hiding this comment

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

we actually wants to use currentTarget here, instead of target
In case if tab header is not simple element (eg. swizzled) this will stop working

target: is the element that triggered the event (e.g., the user clicked on)
currentTarget: is the element that the event listener is attached to.

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

const selectedTabIndex = tabRefs.indexOf(selectedTab);
const selectedTabValue = children[selectedTabIndex].props.value;
const selectedTabValue = values[selectedTabIndex].value;
Copy link
Contributor Author

@armano2 armano2 Mar 19, 2021

Choose a reason for hiding this comment

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

instead of checking childrens we should check values, this also solves crash with missing TabItem during development


setSelectedValue(selectedTabValue);

Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ declare module '@theme/TabItem' {
export type Props = {
readonly children: ReactNode;
readonly value: string;
readonly hidden: boolean;
readonly className: string;
readonly hidden?: boolean;
readonly className?: string;
};

const TabItem: (props: Props) => JSX.Element;
Expand Down