From 552669c7961084755605f4fdcd61446b0822ca30 Mon Sep 17 00:00:00 2001 From: Garrett Brustkern Date: Thu, 5 Nov 2020 12:11:40 -0600 Subject: [PATCH] feat(react): add option to not automatically scroll Tab(s) into view Co-authored-by: Garrett Brustkern Co-authored-by: James Catalano <35548103+jcatalan-cisco@users.noreply.github.com> --- react/src/lib/Tab/index.js | 10 ++++++---- .../Tab/tests/__snapshots__/index.spec.js.snap | 17 +++++++++-------- react/src/lib/Tab/tests/index.spec.js | 2 +- .../tests/__snapshots__/index.spec.js.snap | 5 +++++ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/react/src/lib/Tab/index.js b/react/src/lib/Tab/index.js index ce26768f91..0a8358d3bb 100644 --- a/react/src/lib/Tab/index.js +++ b/react/src/lib/Tab/index.js @@ -4,9 +4,8 @@ import React from 'react'; import PropTypes from 'prop-types'; class Tab extends React.PureComponent { - componentDidUpdate() { - this.props.focus && this.tabLink.focus(); + this.props.focus && !this.props.preventFocus && this.tabLink.focus(); } render() { @@ -36,9 +35,9 @@ class Tab extends React.PureComponent { (this.tabLink = ref)} role={role} - href='javascript:void(0)' onKeyDown={onKeyDown} - onClick={onPress} + href="#" + onClick={(e) => {e.preventDefault(); onPress(e)}} tabIndex={focus ? 0 : -1} aria-current={active} > @@ -60,6 +59,8 @@ Tab.propTypes = { disabled: PropTypes.bool, /** @prop Specifies if Tab should automatically get focus when page loads | false */ focus: PropTypes.bool, + /** @prop Prevent focusing the Tab (which would scroll it into view if not already) when selected | false */ + preventFocus: PropTypes.bool, /** @prop Tab Heading Text */ heading: PropTypes.string.isRequired, /** @prop Currently unused prop myKey | 0 */ @@ -78,6 +79,7 @@ Tab.defaultProps = { className: '', disabled: false, focus: false, + preventFocus: false, myKey: 0, onKeyDown: null, onPress: null, diff --git a/react/src/lib/Tab/tests/__snapshots__/index.spec.js.snap b/react/src/lib/Tab/tests/__snapshots__/index.spec.js.snap index a2c9e8de37..a4dd0b4589 100644 --- a/react/src/lib/Tab/tests/__snapshots__/index.spec.js.snap +++ b/react/src/lib/Tab/tests/__snapshots__/index.spec.js.snap @@ -13,6 +13,7 @@ ShallowWrapper { myKey={0} onKeyDown={null} onPress={null} + preventFocus={false} role="tab" />, Symbol(enzyme.__renderer__): Object { @@ -31,8 +32,8 @@ ShallowWrapper { "props": Object { "children": ', () => { it('should handle onClick event', () => { let count = 0; const countUp = () => count++; - const container = shallow(); + const container = mount(); container.find('a').simulate('click'); expect(count).toEqual(1); diff --git a/react/src/lib/TabList/tests/__snapshots__/index.spec.js.snap b/react/src/lib/TabList/tests/__snapshots__/index.spec.js.snap index b319ab0615..6133f7748f 100644 --- a/react/src/lib/TabList/tests/__snapshots__/index.spec.js.snap +++ b/react/src/lib/TabList/tests/__snapshots__/index.spec.js.snap @@ -17,6 +17,7 @@ ShallowWrapper { myKey={0} onKeyDown={null} onPress={null} + preventFocus={false} role="tab" /> , @@ -45,6 +46,7 @@ ShallowWrapper { myKey={0} onKeyDown={[Function]} onPress={[Function]} + preventFocus={false} refName="navLink" role="tab" />, @@ -68,6 +70,7 @@ ShallowWrapper { "myKey": 0, "onKeyDown": [Function], "onPress": [Function], + "preventFocus": false, "refName": "navLink", "role": "tab", }, @@ -95,6 +98,7 @@ ShallowWrapper { myKey={0} onKeyDown={[Function]} onPress={[Function]} + preventFocus={false} refName="navLink" role="tab" />, @@ -118,6 +122,7 @@ ShallowWrapper { "myKey": 0, "onKeyDown": [Function], "onPress": [Function], + "preventFocus": false, "refName": "navLink", "role": "tab", },