Last active
January 18, 2018 20:04
-
-
Save tortillaj/7a3cddeed09189cde9841b6fd05df1b4 to your computer and use it in GitHub Desktop.
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
import {compose, setPropTypes, defaultProps, withHandlers} from 'recompose' | |
const enhance = compose( | |
setPropTypes({ | |
onClick: PropTypes.func, | |
type: PropTypes.oneOf(['submit', 'button']), | |
className: PropTypes.func, | |
children: PropTypes.func.isRequired, | |
isLoading: PropTypes.bool, | |
}), | |
defaultProps({ | |
type: 'button', | |
}), | |
withHandlers({ | |
onClick: props => e => { | |
// first do something other than what was passed | |
doSomethingDifferentHere() | |
// if an onClick method was passed, do it | |
props.onClick && props.onClick(e) | |
}, | |
onHover: props => e => { | |
// this function was not originally passed as a prop; | |
// it was created here. when recompose did it's magic, | |
// the component had it available as a prop! | |
lookMomImHovering() | |
}, | |
}), | |
) | |
const Button = ({ type, className, children, onClick, isLoading, onHover }) => { | |
return ( | |
<button type={type} className={className} onClick={onClick} onMouseMove={onHover}> | |
{isLoading && <Loading />} | |
{!isLoading && children} | |
</button> | |
) | |
} | |
export default enhance(Button) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment