|
| 1 | +const React = require('react'); |
| 2 | +const Typography = require('./styles/typography'); |
| 3 | +const DefaultRawTheme = require('./styles/raw-themes/light-raw-theme'); |
| 4 | +const ThemeManager = require('./styles/theme-manager'); |
| 5 | +const StylePropable = require('./mixins/style-propable'); |
| 6 | + |
| 7 | +// Badge |
| 8 | +export default React.createClass({ |
| 9 | + displayName: 'Badge', |
| 10 | + mixins: [StylePropable], |
| 11 | + contextTypes: { |
| 12 | + muiTheme: React.PropTypes.object, |
| 13 | + }, |
| 14 | + //for passing default theme context to children |
| 15 | + childContextTypes: { |
| 16 | + muiTheme: React.PropTypes.object, |
| 17 | + }, |
| 18 | + getChildContext () { |
| 19 | + return { |
| 20 | + muiTheme: this.state.muiTheme, |
| 21 | + }; |
| 22 | + }, |
| 23 | + propTypes: { |
| 24 | + className: React.PropTypes.string, |
| 25 | + badgeContent: React.PropTypes.node.isRequired, |
| 26 | + primary: React.PropTypes.bool, |
| 27 | + secondary: React.PropTypes.bool, |
| 28 | + style: React.PropTypes.object, |
| 29 | + badgeStyle: React.PropTypes.object, |
| 30 | + }, |
| 31 | + getInitialState() { |
| 32 | + return { |
| 33 | + muiTheme: this.context.muiTheme ? this.context.muiTheme : ThemeManager.getMuiTheme(DefaultRawTheme), |
| 34 | + }; |
| 35 | + }, |
| 36 | + getDefaultProps() { |
| 37 | + return { |
| 38 | + className: '', |
| 39 | + primary: false, |
| 40 | + secondary: false, |
| 41 | + style: {}, |
| 42 | + badgeStyle: {}, |
| 43 | + }; |
| 44 | + }, |
| 45 | + componentWillReceiveProps(nextProps, nextContext) { |
| 46 | + let newMuiTheme = nextContext.muiTheme ? nextContext.muiTheme : this.state.muiTheme; |
| 47 | + this.setState({ |
| 48 | + muiTheme: newMuiTheme, |
| 49 | + }); |
| 50 | + }, |
| 51 | + getStyles() { |
| 52 | + const theme = this.state.muiTheme.badge; |
| 53 | + |
| 54 | + const badgeBackgroundColor = this.props.primary |
| 55 | + ? theme.primaryColor |
| 56 | + : this.props.secondary |
| 57 | + ? theme.secondaryColor |
| 58 | + : theme.color; |
| 59 | + |
| 60 | + const badgeTextColor = this.props.primary |
| 61 | + ? theme.primaryTextColor |
| 62 | + : this.props.secondary |
| 63 | + ? theme.secondaryTextColor |
| 64 | + : theme.textColor; |
| 65 | + |
| 66 | + const radius = 12; |
| 67 | + const radius2x = Math.floor(2*radius); |
| 68 | + |
| 69 | + return { |
| 70 | + root: { |
| 71 | + position: 'relative', |
| 72 | + display: 'inline-block', |
| 73 | + padding: [radius2x+'px', radius2x+'px', radius+'px', radius+'px'].join(' '), |
| 74 | + }, |
| 75 | + badge: { |
| 76 | + display: 'flex', |
| 77 | + flexDirection: 'row', |
| 78 | + flexWrap: 'wrap', |
| 79 | + justifyContent: 'center', |
| 80 | + alignContent: 'center', |
| 81 | + alignItems: 'center', |
| 82 | + position: 'absolute', |
| 83 | + top: 0, |
| 84 | + right: 0, |
| 85 | + fontWeight: Typography.fontWeightMedium, |
| 86 | + fontSize: radius, |
| 87 | + width: radius2x, |
| 88 | + height: radius2x, |
| 89 | + borderRadius: '50%', |
| 90 | + backgroundColor: badgeBackgroundColor, |
| 91 | + color: badgeTextColor, |
| 92 | + }, |
| 93 | + }; |
| 94 | + }, |
| 95 | + render() { |
| 96 | + const styles = this.getStyles(); |
| 97 | + return ( |
| 98 | + <div style={this.prepareStyles(styles.root, this.props.style)} className={this.props.className}> |
| 99 | + {this.props.children} |
| 100 | + <span style={this.prepareStyles(styles.badge, this.props.badgeStyle)}> |
| 101 | + {this.props.badgeContent} |
| 102 | + </span> |
| 103 | + </div> |
| 104 | + ); |
| 105 | + }, |
| 106 | +}); |
0 commit comments