forked from react-grid-layout/react-grid-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor with Flow and fix all type errors.
- Loading branch information
Showing
8 changed files
with
92 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,10 @@ | |
}, | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"globals": { | ||
// For Flow | ||
"ReactElement", | ||
"ReactClass" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"scripts": { | ||
"lint": "jshint ." | ||
}, | ||
"pre-commit": ["lint", "validate", "test"] | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @flow | ||
import { PropTypes, Component } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
type Props = {initialWidth: number, listenToWindowResize: boolean}; | ||
|
||
/** | ||
* A simple HOC that provides facility for listening to container resizes. | ||
*/ | ||
export default (ComposedComponent: ReactClass): ReactClass => { | ||
return class extends Component { | ||
static propTypes = { | ||
// This allows setting this on the server side | ||
initialWidth: PropTypes.number, | ||
|
||
// If false, you should supply width yourself. Good if you want to debounce resize events | ||
// or reuse a handler from somewhere else. | ||
listenToWindowResize: PropTypes.bool | ||
}; | ||
|
||
static defaultProps: Props = { | ||
initialWidth: 1280, | ||
listenToWindowResize: true | ||
}; | ||
|
||
componentDidMount() { | ||
if (this.props.listenToWindowResize) { | ||
window.addEventListener('resize', this.onWindowResize); | ||
// This is intentional. Once to properly set the breakpoint and resize the elements, | ||
// and again to compensate for any scrollbar that appeared because of the first step. | ||
this.onWindowResize(); | ||
this.onWindowResize(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('resize', this.onWindowResize); | ||
} | ||
|
||
/** | ||
* On window resize, update width of child by calling listener directly. | ||
* TODO: cleaner way to do this? | ||
*/ | ||
onWindowResize = () => { | ||
this.refs.child.onWidthChange(ReactDOM.findDOMNode(this).offsetWidth); | ||
}; | ||
|
||
render(): ReactElement { | ||
return <ComposedComponent {...this.props} ref="child" />; | ||
} | ||
}; | ||
}; |
This file contains 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
This file contains 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