Skip to content

Commit

Permalink
fix(react-tabs): focus selected tab by default (#33236)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrokirpa authored Nov 13, 2024
1 parent ea90f38 commit 4046b33
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: focus selected tab by default",
"packageName": "@fluentui/react-tabs",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`Tab renders correctly 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -23,6 +24,7 @@ exports[`Tab renders correctly when disabled 1`] = `
<div>
<button
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
disabled=""
role="tab"
type="button"
Expand All @@ -42,6 +44,7 @@ exports[`Tab renders default correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -79,6 +82,7 @@ exports[`Tab renders large size and vertical correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -116,6 +120,7 @@ exports[`Tab renders large size correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -153,6 +158,7 @@ exports[`Tab renders medium size and vertical correctly with icon slotted 1`] =
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -190,6 +196,7 @@ exports[`Tab renders medium size correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -227,6 +234,7 @@ exports[`Tab renders small size and vertical correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -264,6 +272,7 @@ exports[`Tab renders small size correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -301,6 +310,7 @@ exports[`Tab renders subtle appearance correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down Expand Up @@ -338,6 +348,7 @@ exports[`Tab renders vertical correctly with icon slotted 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useTabsterAttributes } from '@fluentui/react-tabster';
import {
getIntrinsicElementProps,
mergeCallbacks,
Expand Down Expand Up @@ -40,6 +41,10 @@ export const useTab_unstable = (props: TabProps, ref: React.Ref<HTMLElement>): T
const onTabClick = useEventCallback(mergeCallbacks(onClick, onSelectCallback));
const onTabFocus = useEventCallback(mergeCallbacks(onFocus, onSelectCallback));

const focusProps = useTabsterAttributes({
focusable: { isDefault: selected },
});

React.useEffect(() => {
onRegister({
value,
Expand Down Expand Up @@ -72,6 +77,7 @@ export const useTab_unstable = (props: TabProps, ref: React.Ref<HTMLElement>): T
// aria-selected undefined indicates it is not selectable
// according to https://www.w3.org/TR/wai-aria-1.1/#aria-selected
'aria-selected': disabled ? undefined : (`${selected}` as 'true' | 'false'),
...focusProps,
...props,
disabled,
onClick: onTabClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ exports[`TabList renders tabs when disabled 1`] = `
<div
aria-orientation="horizontal"
class="fui-TabList"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":true}}"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":false,\\"hasDefault\\":true}}"
role="tablist"
>
<button
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
disabled=""
role="tab"
type="button"
Expand All @@ -28,6 +29,7 @@ exports[`TabList renders tabs when disabled 1`] = `
</button>
<button
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":true}}"
disabled=""
role="tab"
type="button"
Expand All @@ -41,6 +43,7 @@ exports[`TabList renders tabs when disabled 1`] = `
</button>
<button
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
disabled=""
role="tab"
type="button"
Expand All @@ -66,12 +69,13 @@ exports[`TabList renders tabs with default selected tab 1`] = `
<div
aria-orientation="horizontal"
class="fui-TabList"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":true}}"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":false,\\"hasDefault\\":true}}"
role="tablist"
>
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -91,6 +95,7 @@ exports[`TabList renders tabs with default selected tab 1`] = `
<button
aria-selected="true"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":true}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -105,6 +110,7 @@ exports[`TabList renders tabs with default selected tab 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -130,7 +136,7 @@ exports[`TabList renders with no tabs 1`] = `
<div
aria-orientation="horizontal"
class="fui-TabList"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":true}}"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":false,\\"hasDefault\\":true}}"
role="tablist"
/>
</div>
Expand All @@ -141,12 +147,13 @@ exports[`TabList renders with tabs 1`] = `
<div
aria-orientation="horizontal"
class="fui-TabList"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":true}}"
data-tabster="{\\"mover\\":{\\"cyclic\\":true,\\"direction\\":2,\\"memorizeCurrent\\":false,\\"hasDefault\\":true}}"
role="tablist"
>
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -166,6 +173,7 @@ exports[`TabList renders with tabs 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand All @@ -185,6 +193,7 @@ exports[`TabList renders with tabs 1`] = `
<button
aria-selected="false"
class="fui-Tab"
data-tabster="{\\"focusable\\":{\\"isDefault\\":false}}"
role="tab"
style="--fui-Tab__indicator--offset: 0px; --fui-Tab__indicator--scale: 1;"
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const useTabList_unstable = (props: TabListProps, ref: React.Ref<HTMLElem
const focusAttributes = useArrowNavigationGroup({
circular: true,
axis: vertical ? 'vertical' : 'horizontal',
memorizeCurrent: true,
memorizeCurrent: false,
// eslint-disable-next-line @typescript-eslint/naming-convention
unstable_hasDefault: true,
});

const [selectedValue, setSelectedValue] = useControllableState({
Expand Down

0 comments on commit 4046b33

Please sign in to comment.