Skip to content

Commit

Permalink
Refactored to use es6 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Apr 18, 2017
1 parent f98d84b commit e33a3ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Expand Up @@ -4,6 +4,7 @@
"react"
],
"plugins": [
"transform-object-rest-spread"
"transform-object-rest-spread",
"transform-class-properties"
]
}
4 changes: 2 additions & 2 deletions .eslintrc
Expand Up @@ -7,7 +7,7 @@

"parser": "babel-eslint",

"plugins": ["react"],
"plugins": ["react", "babel"],

"ecmaFeatures": {
"arrowFunctions": true,
Expand Down Expand Up @@ -104,7 +104,7 @@
"no-implicit-coercion": 2, // disallow the type conversions with shorter notations
"no-implicit-globals": 2, // disallow var and named functions in global scope
"no-implied-eval": 2, // disallow use of eval()-like methods
"no-invalid-this": 2, // disallow this keywords outside of classes or class-like objects
"babel/no-invalid-this": 2, // disallow this keywords outside of classes or class-like objects
"no-iterator": 2, // disallow usage of __iterator__ property
"no-labels": 2, // disallow use of labeled statements
"no-lone-blocks": 2, // disallow unnecessary nested blocks
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -41,20 +41,22 @@
},
"homepage": "https://github.com/nkbt/react-height",
"peerDependencies": {
"react": "^0.14 || ^15"
"react": ">=15.3"
},
"devDependencies": {
"babel-cli": "6.24.1",
"babel-core": "6.24.1",
"babel-eslint": "7.2.2",
"babel-loader": "6.4.1",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"codecov.io": "0.1.6",
"cross-env": "4.0.0",
"css-loader": "0.28.0",
"eslint": "3.19.0",
"eslint-plugin-babel": "4.1.1",
"eslint-plugin-react": "6.10.3",
"glob": "7.1.1",
"html-webpack-plugin": "2.28.0",
Expand All @@ -63,8 +65,8 @@
"json-loader": "0.5.4",
"nightwatch-autorun": "3.1.0",
"parallelshell": "2.0.0",
"react-dom": "15.5.4",
"react": "15.5.4",
"react-dom": "15.5.4",
"rimraf": "2.6.1",
"sinon": "2.1.0",
"style-loader": "0.16.1",
Expand All @@ -75,7 +77,6 @@
"webpack-dev-server": "2.4.2"
},
"dependencies": {
"create-react-class": "15.5.2",
"prop-types": "15.5.8"
}
}
53 changes: 21 additions & 32 deletions src/ReactHeight.js
Expand Up @@ -2,55 +2,47 @@
/* eslint "react/no-did-update-set-state":0 */


import React from 'react';
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import {shouldComponentUpdate} from 'react/lib/ReactComponentWithPureRenderMixin';

const getElementHeight = el => el.clientHeight;

const ReactHeight = createReactClass({
propTypes: {
export default class ReactHeight extends PureComponent {
static propTypes = {
children: PropTypes.node.isRequired,
onHeightReady: PropTypes.func.isRequired,
hidden: PropTypes.bool,
dirty: PropTypes.bool,
getElementHeight: PropTypes.func
},


getDefaultProps() {
return {
hidden: false,
dirty: true,
getElementHeight
};
},
}

static defaultProps = {
hidden: false,
dirty: true,
getElementHeight
}

getInitialState() {
return {
height: 0, dirty: this.props.dirty
constructor(props) {
super(props);
this.state = {
dirty: props.dirty,
height: 0
};
},

}

componentDidMount() {
const height = this.props.getElementHeight(this.wrapper);
const dirty = false;

this.setState({height, dirty}, () => this.props.onHeightReady(this.state.height));
},
}


componentWillReceiveProps({children, dirty}) {
if (children !== this.props.children || dirty) {
this.setState({dirty: true});
}
},


shouldComponentUpdate,
}


componentDidUpdate() {
Expand All @@ -62,12 +54,12 @@ const ReactHeight = createReactClass({
} else {
this.setState({height, dirty}, () => this.props.onHeightReady(this.state.height));
}
},
}


setWrapperRef(el) {
setWrapperRef = el => {
this.wrapper = el;
},
}


render() {
Expand Down Expand Up @@ -95,7 +87,4 @@ const ReactHeight = createReactClass({

return <div ref={this.setWrapperRef} {...props}>{children}</div>;
}
});


export default ReactHeight;
}

0 comments on commit e33a3ba

Please sign in to comment.