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

[Tabs] Fix indicator update issue #8388

Merged
merged 2 commits into from
Sep 25, 2017
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"size-limit": [
{
"path": "build/index.js",
"limit": "91 KB"
"limit": "88 KB"
}
],
"nyc": {
Expand Down
5 changes: 3 additions & 2 deletions src/Grid/Grid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import React from 'react';
import { assert } from 'chai';
import forOwn from 'lodash/forOwn';
import { createShallow, getClasses } from '../test-utils';
import Hidden from '../Hidden';
import Grid from './Grid';
Expand Down Expand Up @@ -93,7 +92,9 @@ describe('<Grid />', () => {
xlDownHidden: true,
};

forOwn(hiddenProps, (value, key) => {
Object.keys(hiddenProps).forEach(key => {
const value = hiddenProps[key];

it(`should render ${key} with Hidden`, () => {
const wrapper = shallow(<Grid hidden={{ [key]: value }} />);
assert.strictEqual(wrapper.type(), Hidden);
Expand Down
23 changes: 6 additions & 17 deletions src/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import EventListener from 'react-event-listener';
import debounce from 'lodash/debounce';
import ScrollbarSize from 'react-scrollbar-size';
import scroll from 'scroll';
import pick from 'lodash/pick';
import withStyles from '../styles/withStyles';
import TabIndicator from './TabIndicator';
import TabScrollButton from './TabScrollButton';
Expand Down Expand Up @@ -124,11 +123,7 @@ type TabsMeta = {
scrollLeft: number,
// ClientRect
left: number,
width: number,
right: number,
top: number,
bottom: number,
height: number,
};

/**
Expand Down Expand Up @@ -162,12 +157,11 @@ class Tabs extends React.Component<DefaultProps & Props, State> {
}

componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
this.updateIndicatorState(this.props);
}
this.updateScrollButtonState();
if (this.state.indicatorStyle !== prevState.indicatorStyle) {
this.scrollSelectedIntoView();
} else {
this.updateIndicatorState(this.props);
}
}

Expand Down Expand Up @@ -254,17 +248,12 @@ class Tabs extends React.Component<DefaultProps & Props, State> {
getTabsMeta = (value): { tabsMeta: ?TabsMeta, tabMeta: ?ClientRect } => {
let tabsMeta;
if (this.tabs) {
const rect = this.tabs.getBoundingClientRect();
// create a new object with ClientRect class props + scrollLeft
tabsMeta = {
scrollLeft: this.tabs.scrollLeft,
...pick(this.tabs.getBoundingClientRect(), [
'left',
'width',
'right',
'top',
'bottom',
'height',
]),
scrollLeft: this.tabs ? this.tabs.scrollLeft : 0,
left: rect.left,
right: rect.right,
};
}

Expand Down
21 changes: 20 additions & 1 deletion src/Tabs/Tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import TabIndicator from './TabIndicator';
import Tab from './Tab';

const noop = () => {};
const fakeTabs = {
getBoundingClientRect: () => ({}),
children: [
{
children: [
{
getBoundingClientRect: () => ({}),
},
],
},
],
};

describe('<Tabs />', () => {
let mount;
Expand Down Expand Up @@ -250,7 +262,10 @@ describe('<Tabs />', () => {

it('should response to scroll events', () => {
const instance = wrapper.instance();
instance.tabs = { scrollLeft: 100 };
instance.tabs = {
scrollLeft: 100,
...fakeTabs,
};
spy(instance, 'updateScrollButtonState');
const selector = `.${classes.scrollingContainer}.${classes.scrollable}`;
wrapper.find(selector).simulate('scroll');
Expand Down Expand Up @@ -373,6 +388,7 @@ describe('<Tabs />', () => {
scrollLeft: 0,
scrollWidth: 90,
clientWidth: 100,
...fakeTabs,
};
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, false, 'left scroll should be false');
Expand All @@ -382,6 +398,7 @@ describe('<Tabs />', () => {
it('should set only left scroll button state', () => {
instance.tabs = {
scrollLeft: 1,
...fakeTabs,
};
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, true, 'left scroll should be true');
Expand All @@ -393,6 +410,7 @@ describe('<Tabs />', () => {
scrollLeft: 0,
scrollWidth: 110,
clientWidth: 100,
...fakeTabs,
};
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, false, 'left scroll should be false');
Expand All @@ -404,6 +422,7 @@ describe('<Tabs />', () => {
scrollLeft: 1,
scrollWidth: 110,
clientWidth: 100,
...fakeTabs,
};
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, true, 'left scroll should be true');
Expand Down