Last active
April 28, 2016 00:47
-
-
Save narqo/856ddcb73d5e7bad80ffa715720ddebb 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 React from 'react'; | |
import Control from '../Control'; | |
import pressable from '../pressable'; | |
class Button extends Control { | |
constructor(props, context) { | |
super(props, context); | |
this.children = null; | |
} | |
componentWillUpdate(nextProps, nextState) { | |
if (super.componentWillUpdate) { | |
super.componentWillUpdate(nextProps, nextState); | |
} | |
if (this.props.children !== nextProps.children) { | |
this.children = null; | |
} | |
} | |
render() { | |
if (!this.children) { | |
this.children = React.Children.map(this.props.children, child => { | |
if (React.isValidElement(child)) { | |
return child; | |
} else { | |
return <span className="button__text">{child}</span> | |
} | |
}); | |
} | |
if (this.props.type === 'link') { | |
const url = this.props.disabled ? null : this.props.url; | |
return ( | |
<a className={this.className()} {...this.getControlHandlers()} ref="control" role="link" href={url}> | |
{this.children} | |
</a> | |
); | |
} else { | |
return ( | |
<button className={this.className()} {...this.getControlHandlers()} ref="control" name={this.props.name} disabled={this.props.disabled}> | |
{this.children} | |
</button> | |
); | |
} | |
} | |
className() { | |
var className = 'button'; | |
const theme = this.props.theme || this.context.theme; | |
if (theme) { | |
className += ' button_theme_' + theme; | |
} | |
if (this.props.size) { | |
className += ' button_size_' + this.props.size; | |
} | |
if (this.props.type) { | |
className += ' button_type_' + this.props.type; | |
} | |
if (this.props.view) { | |
className += ' button_view_' + this.props.view; | |
} | |
if (this.props.disabled) { | |
className += ' button_disabled'; | |
} | |
if (this.state.hovered) { | |
className += ' button_hovered'; | |
} | |
if (this.state.pressed) { | |
className += ' button_pressed'; | |
} | |
if (this.state.focused === 'hard') { | |
className += ' button_focused button_focused-hard'; | |
} else if (this.state.focused) { | |
className += ' button_focused'; | |
} | |
if (this.props.checked) { | |
className += ' button_checked'; | |
} | |
if (this.props.className) { | |
className += ' ' + this.props.className; | |
} | |
return className; | |
} | |
} | |
Button.propTypes = { | |
size: React.PropTypes.oneOf(['s', 'm', 'l', 'xl']) | |
}; | |
Button.contextTypes = { | |
theme: React.PropTypes.string | |
}; | |
export default pressable(Button); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment