Skip to content

Commit 7abbe57

Browse files
committed
🎉 first commit
1 parent c6e443c commit 7abbe57

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# react-automatic-width
2-
Automatically sets `width` property on child components
2+
3+
Reacts on window.onResize and sets its current width as property on its children. Use this to keep components such as [fixed-data-table](https://facebook.github.io/fixed-data-table/) or [react-d3-components](https://github.com/codesuki/react-d3-components) responsive.
4+
5+
# Usage
6+
7+
**TODO**
8+
9+
# License
10+
11+
Apache 2.0

src/react-automatic-width.jsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
3+
class AutomaticWidth extends React.Component {
4+
constructor(props) {
5+
super();
6+
this.state = {
7+
listener: null,
8+
width: props.initialWidth || 1000
9+
};
10+
}
11+
12+
_resizeHandler() {
13+
let dom = this.refs.autowidthWrapper.getDOMNode();
14+
if (dom.clientWidth !== this.state.width) {
15+
this.setState({
16+
width: dom.clientWidth
17+
});
18+
}
19+
}
20+
21+
componentDidMount() {
22+
let boundListener = this._resizeHandler.bind(this);
23+
window.addEventListener('resize', boundListener);
24+
this.setState({
25+
listener: boundListener
26+
});
27+
boundListener();
28+
}
29+
30+
componentWillUnmount() {
31+
window.removeEventListener('resize', this.state.listener);
32+
}
33+
34+
render() {
35+
return <div ref='autowidthWrapper' {...this.props}>
36+
{React.Children.map(this.props.children, c =>
37+
React.addons.cloneWithProps(c, {
38+
width: this.state.width
39+
})
40+
)}
41+
</div>;
42+
}
43+
}
44+
AutomaticWidth.displayName='Automatic Width';
45+
export default AutomaticWidth;
46+

0 commit comments

Comments
 (0)