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

feat(Tabs): add control for autoscroll on component rerender #7410

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
4 changes: 4 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5080,6 +5080,7 @@ Map {
},
"Tabs" => Object {
"defaultProps": Object {
"scrollIntoView": true,
"selected": 0,
"selectionMode": "automatic",
"type": "default",
Expand All @@ -5106,6 +5107,9 @@ Map {
"onSelectionChange": Object {
"type": "func",
},
"scrollIntoView": Object {
"type": "bool",
},
"selected": Object {
"type": "number",
},
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/Tabs/Tabs-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const props = {
'The className for the child `<TabContent>` components',
'tab-content'
),
scrollIntoView: boolean(
'Scroll to selected tab on component rerender (scrollIntoView)',
true
),
selectionMode: select(
'Selection mode (selectionMode)',
selectionModes,
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/components/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export default class Tabs extends React.Component {
*/
onSelectionChange: PropTypes.func,

/**
* Choose whether or not to automatically scroll to newly selected tabs
* on component rerender
*/
scrollIntoView: PropTypes.bool,

/**
* Optionally provide an index for the currently selected <Tab>
*/
Expand All @@ -82,6 +88,7 @@ export default class Tabs extends React.Component {

static defaultProps = {
type: 'default',
scrollIntoView: true,
selected: 0,
selectionMode: 'automatic',
};
Expand Down Expand Up @@ -205,7 +212,7 @@ export default class Tabs extends React.Component {
});
}

if (prevState.selected !== selected) {
if (this.props.scrollIntoView && prevState.selected !== selected) {
this.getTabAt(selected)?.tabAnchor?.scrollIntoView({
block: 'nearest',
inline: 'nearest',
Expand Down Expand Up @@ -379,6 +386,7 @@ export default class Tabs extends React.Component {
type,
light,
onSelectionChange,
scrollIntoView, // eslint-disable-line no-unused-vars
selectionMode, // eslint-disable-line no-unused-vars
tabContentClassName,
...other
Expand Down