Tabs pointer not being updated (#1224) (#1325)

* Fixes #1224

* add jest setup file to mock requestAnimationFrame

* fix duplicated jest config
old
Rubén Moya 2017-04-02 14:08:25 +02:00 committed by Javi Velasco
parent 3e834cb63b
commit 5de4e7391c
3 changed files with 24 additions and 13 deletions

View File

@ -51,8 +51,13 @@ const factory = (Tab, TabContent, FontIcon) => {
this.handleResize();
}
componentWillReceiveProps(nextProps) {
this.updatePointer(nextProps.index);
componentDidUpdate(prevProps) {
const { index, children } = this.props;
const { index: prevIndex, children: prevChildren } = prevProps;
if (index !== prevIndex || children !== prevChildren) {
this.updatePointer(index);
}
}
componentWillUnmount() {
@ -76,15 +81,17 @@ const factory = (Tab, TabContent, FontIcon) => {
updatePointer = (idx) => {
if (this.navigationNode && this.navigationNode.children[idx]) {
const nav = this.navigationNode.getBoundingClientRect();
const label = this.navigationNode.children[idx].getBoundingClientRect();
const scrollLeft = this.navigationNode.scrollLeft;
this.setState({
pointer: {
top: `${nav.height}px`,
left: `${label.left - (nav.left + scrollLeft)}px`,
width: `${label.width}px`,
},
requestAnimationFrame(() => {
const nav = this.navigationNode.getBoundingClientRect();
const label = this.navigationNode.children[idx].getBoundingClientRect();
const scrollLeft = this.navigationNode.scrollLeft;
this.setState({
pointer: {
top: `${nav.height}px`,
left: `${(label.left + scrollLeft) - nav.left}px`,
width: `${label.width}px`,
},
});
});
}
}

1
jest.config.js Normal file
View File

@ -0,0 +1 @@
Object.defineProperty(window, 'requestAnimationFrame', { value: fn => fn() });

View File

@ -112,14 +112,17 @@
},
"license": "MIT",
"jest": {
"modulePaths": [
"<rootDir>"
],
"moduleDirectories": [
"node_modules"
],
"moduleNameMapper": {
"(\\.css$)|(normalize.css/normalize)|(^exports-loader)": "identity-obj-proxy"
},
"modulePaths": [
"<rootDir>"
"setupFiles": [
"./jest.config.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"