Last active
March 13, 2020 20:02
-
-
Save parties/a67c75c2ccc5ee6fe48a to your computer and use it in GitHub Desktop.
Responsive Wrapper for Facebook's Fixed Data Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
var {Table} = require('fixed-data-table'); | |
var _ = require('lodash'); | |
var FittedTable = React.createClass({ | |
getInitialState() { | |
return { | |
tableWidth : 400, | |
tableHeight : 400 | |
}; | |
}, | |
componentDidMount() { | |
var win = window; | |
if (win.addEventListener) { | |
win.addEventListener('resize', _.throttle(this._update, 250), false); | |
} else if (win.attachEvent) { | |
win.attachEvent('onresize', _.throttle(this._update, 250)); | |
} else { | |
win.onresize = this._update; | |
} | |
this._update(); | |
}, | |
componentWillReceiveProps(props) { | |
this._update(); | |
}, | |
componentWillUnmount() { | |
var win = window; | |
if(win.removeEventListener) { | |
win.removeEventListener('resize', _.throttle(this._update, 250), false); | |
} else if(win.removeEvent) { | |
win.removeEvent('onresize', _.throttle(this._update, 250), false); | |
} else { | |
win.onresize = null; | |
} | |
}, | |
_update() { | |
if (this.isMounted()) { | |
var node = this.getDOMNode(); | |
this.setState({ | |
tableWidth : node.clientWidth, | |
tableHeight : node.clientHeight | |
}); | |
} | |
}, | |
render() { | |
return ( | |
<div className="fitted-wrapper"> | |
<Table {...this.props} width={this.state.tableWidth} height={this.state.tableHeight}> | |
{this.props.children} | |
</Table> | |
</div> | |
); | |
}, | |
}); | |
module.exports = FittedTable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update for React 0.14+ (but the height doesn't work, it doesn't fit to the window.height)