Skip to content

Commit

Permalink
feat(react): add option to not automatically scroll Tab(s) into view
Browse files Browse the repository at this point in the history
Co-authored-by: Garrett Brustkern <gbrustke@cisco.com>
Co-authored-by: James Catalano <35548103+jcatalan-cisco@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 5, 2020
1 parent 4efa074 commit 552669c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
10 changes: 6 additions & 4 deletions react/src/lib/Tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -36,9 +35,9 @@ class Tab extends React.PureComponent {
<a
ref={ref => (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}
>
Expand All @@ -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 */
Expand All @@ -78,6 +79,7 @@ Tab.defaultProps = {
className: '',
disabled: false,
focus: false,
preventFocus: false,
myKey: 0,
onKeyDown: null,
onPress: null,
Expand Down
17 changes: 9 additions & 8 deletions react/src/lib/Tab/tests/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ShallowWrapper {
myKey={0}
onKeyDown={null}
onPress={null}
preventFocus={false}
role="tab"
/>,
Symbol(enzyme.__renderer__): Object {
Expand All @@ -31,8 +32,8 @@ ShallowWrapper {
"props": Object {
"children": <a
aria-current={false}
href="javascript:void(0)"
onClick={null}
href="#"
onClick={[Function]}
onKeyDown={null}
role="tab"
tabIndex={-1}
Expand All @@ -50,8 +51,8 @@ ShallowWrapper {
"props": Object {
"aria-current": false,
"children": "test",
"href": "javascript:void(0)",
"onClick": null,
"href": "#",
"onClick": [Function],
"onKeyDown": null,
"role": "tab",
"tabIndex": -1,
Expand All @@ -70,8 +71,8 @@ ShallowWrapper {
"props": Object {
"children": <a
aria-current={false}
href="javascript:void(0)"
onClick={null}
href="#"
onClick={[Function]}
onKeyDown={null}
role="tab"
tabIndex={-1}
Expand All @@ -89,8 +90,8 @@ ShallowWrapper {
"props": Object {
"aria-current": false,
"children": "test",
"href": "javascript:void(0)",
"onClick": null,
"href": "#",
"onClick": [Function],
"onKeyDown": null,
"role": "tab",
"tabIndex": -1,
Expand Down
2 changes: 1 addition & 1 deletion react/src/lib/Tab/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('tests for <Tab />', () => {
it('should handle onClick event', () => {
let count = 0;
const countUp = () => count++;
const container = shallow(<Tab heading="test" onPress={countUp} />);
const container = mount(<Tab heading="test" onPress={countUp} />);

container.find('a').simulate('click');
expect(count).toEqual(1);
Expand Down
5 changes: 5 additions & 0 deletions react/src/lib/TabList/tests/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ShallowWrapper {
myKey={0}
onKeyDown={null}
onPress={null}
preventFocus={false}
role="tab"
/>
</TabList>,
Expand Down Expand Up @@ -45,6 +46,7 @@ ShallowWrapper {
myKey={0}
onKeyDown={[Function]}
onPress={[Function]}
preventFocus={false}
refName="navLink"
role="tab"
/>,
Expand All @@ -68,6 +70,7 @@ ShallowWrapper {
"myKey": 0,
"onKeyDown": [Function],
"onPress": [Function],
"preventFocus": false,
"refName": "navLink",
"role": "tab",
},
Expand Down Expand Up @@ -95,6 +98,7 @@ ShallowWrapper {
myKey={0}
onKeyDown={[Function]}
onPress={[Function]}
preventFocus={false}
refName="navLink"
role="tab"
/>,
Expand All @@ -118,6 +122,7 @@ ShallowWrapper {
"myKey": 0,
"onKeyDown": [Function],
"onPress": [Function],
"preventFocus": false,
"refName": "navLink",
"role": "tab",
},
Expand Down

0 comments on commit 552669c

Please sign in to comment.