Last active
October 3, 2017 04:02
-
-
Save RafalFilipek/7cd7363f203a0778ebd5ca5b7d752517 to your computer and use it in GitHub Desktop.
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
import Loader from './Loader' | |
// Simple ... | |
<Loader /> | |
// Custom text | |
<Loader text="Wait for it" /> | |
// Custom sign | |
<Loader sign="❤" /> | |
// You can change max number of `dots` | |
<Loader max={10} /> | |
// To slow? To fast? | |
<Loader interval={100} /> |
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
import React, { PropTypes } from 'react' | |
import { pure, mapProps, compose, withState, lifecycle, setDisplayName, setPropTypes, defaultProps, onlyUpdateForPropTypes } from 'recompose' | |
import { repeat } from 'lodash' | |
export default pure( | |
compose( | |
setDisplayName('Loader'), | |
onlyUpdateForPropTypes, | |
setPropTypes({ | |
sign: PropTypes.string, | |
interval: PropTypes.number, | |
max: PropTypes.number, | |
text: PropTypes.string | |
}), | |
defaultProps({ | |
sign: '.', | |
interval: 200, | |
max: 3, | |
text: 'Loading' | |
}), | |
withState('dotsCount', 'setDotsCount', 1), | |
withState('timer', 'setTimer', null), | |
mapProps( | |
({ setDotsCount, max, ...rest }) => ({ | |
tick: () => setDotsCount(n => (n + 1) % (max + 1)), | |
...rest | |
}) | |
), | |
lifecycle( | |
(c) => setTimeout(() => c.props.setTimer(setInterval(c.props.tick, c.props.interval)), 0), | |
(c) => clearInterval(c.props.timer) | |
) | |
)(({ text, sign, dotsCount }) => <div>{ text } { repeat(sign, dotsCount) }</div>) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment