forked from mui/material-ui
-
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.
Merge pull request mui#2045 from rhythnic/master
[Badge] Add a new component
- Loading branch information
Showing
7 changed files
with
225 additions
and
0 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
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,88 @@ | ||
let React = require('react'); | ||
let { FontIcon, IconButton, Badge } = require('material-ui'); | ||
let ComponentDoc = require('../../component-doc'); | ||
let Code = require('badge-code'); | ||
let CodeExample = require('../../code-example/code-example'); | ||
const NotificationsIcon = require('svg-icons/social/notifications'); | ||
const ShoppingCartIcon = require('svg-icons/action/shopping-cart'); | ||
const FolderIcon = require('svg-icons/file/folder-open'); | ||
const UploadIcon = require('svg-icons/file/cloud-upload'); | ||
|
||
export default class BadgePage extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.desc = 'This component generates a small badge to the top-right of it\'s child(ren)'; | ||
|
||
this.componentInfo = [ | ||
{ | ||
name: 'Props', | ||
infoArray: [ | ||
{ | ||
name: 'badgeContent', | ||
type: 'node', | ||
header: 'required', | ||
desc: 'This is the content rendered within the badge.', | ||
}, | ||
{ | ||
name: 'primary', | ||
type: 'bool', | ||
header: 'default: false', | ||
desc: 'If true, the badge will use the primary badge colors.', | ||
}, | ||
{ | ||
name: 'secondary', | ||
type: 'bool', | ||
header: 'default: false', | ||
desc: 'If true, the badge will use the secondary badge colors.', | ||
}, | ||
{ | ||
name: 'style', | ||
type: 'object', | ||
header: 'optional', | ||
desc: 'Override the inline-styles of the root element.', | ||
}, | ||
{ | ||
name: 'badgeStyle', | ||
type: 'object', | ||
header: 'optional', | ||
desc: 'Override the inline-styles of the badge element.', | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
|
||
render() { | ||
return ( | ||
<ComponentDoc | ||
name="Badge" | ||
desc={this.desc} | ||
componentInfo={this.componentInfo}> | ||
<CodeExample code={Code}> | ||
|
||
<Badge badgeContent={4} primary={true}> | ||
<NotificationsIcon /> | ||
</Badge> | ||
|
||
<Badge badgeContent={10} secondary={true} badgeStyle={{top:12, right:12}}> | ||
<IconButton tooltip="Go To Cart"> | ||
<ShoppingCartIcon/> | ||
</IconButton> | ||
</Badge> | ||
|
||
<Badge backgroundColor="#d8d8d8" | ||
badgeContent={<IconButton tooltip="Backup"><UploadIcon/></IconButton>}> | ||
<FolderIcon /> | ||
</Badge> | ||
|
||
<Badge badgeContent="©" badgeStyle={{fontSize:20}}> | ||
<h3>Company Name</h3> | ||
</Badge> | ||
|
||
</CodeExample> | ||
</ComponentDoc> | ||
); | ||
} | ||
|
||
} |
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,19 @@ | ||
<Badge badgeContent={4} primary={true}> | ||
<NotificationsIcon /> | ||
</Badge> | ||
|
||
//override badgeStyle to account for padding of child element | ||
<Badge badgeContent={10} secondary={true} badgeStyle={{top:12, right:12}}> | ||
<IconButton tooltip="Go To Cart"> | ||
<ShoppingCartIcon/> | ||
</IconButton> | ||
</Badge> | ||
|
||
<Badge backgroundColor="#d8d8d8" | ||
badgeContent={<IconButton tooltip="Backup"><UploadIcon/></IconButton>}> | ||
<FolderIcon /> | ||
</Badge> | ||
|
||
<Badge badgeContent="©" badgeStyle={{fontSize:20}}> | ||
<h3>Company Name</h3> | ||
</Badge> |
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,106 @@ | ||
const React = require('react'); | ||
const Typography = require('./styles/typography'); | ||
const DefaultRawTheme = require('./styles/raw-themes/light-raw-theme'); | ||
const ThemeManager = require('./styles/theme-manager'); | ||
const StylePropable = require('./mixins/style-propable'); | ||
|
||
// Badge | ||
export default React.createClass({ | ||
displayName: 'Badge', | ||
mixins: [StylePropable], | ||
contextTypes: { | ||
muiTheme: React.PropTypes.object, | ||
}, | ||
//for passing default theme context to children | ||
childContextTypes: { | ||
muiTheme: React.PropTypes.object, | ||
}, | ||
getChildContext () { | ||
return { | ||
muiTheme: this.state.muiTheme, | ||
}; | ||
}, | ||
propTypes: { | ||
className: React.PropTypes.string, | ||
badgeContent: React.PropTypes.node.isRequired, | ||
primary: React.PropTypes.bool, | ||
secondary: React.PropTypes.bool, | ||
style: React.PropTypes.object, | ||
badgeStyle: React.PropTypes.object, | ||
}, | ||
getInitialState() { | ||
return { | ||
muiTheme: this.context.muiTheme ? this.context.muiTheme : ThemeManager.getMuiTheme(DefaultRawTheme), | ||
}; | ||
}, | ||
getDefaultProps() { | ||
return { | ||
className: '', | ||
primary: false, | ||
secondary: false, | ||
style: {}, | ||
badgeStyle: {}, | ||
}; | ||
}, | ||
componentWillReceiveProps(nextProps, nextContext) { | ||
let newMuiTheme = nextContext.muiTheme ? nextContext.muiTheme : this.state.muiTheme; | ||
this.setState({ | ||
muiTheme: newMuiTheme, | ||
}); | ||
}, | ||
getStyles() { | ||
const theme = this.state.muiTheme.badge; | ||
|
||
const badgeBackgroundColor = this.props.primary | ||
? theme.primaryColor | ||
: this.props.secondary | ||
? theme.secondaryColor | ||
: theme.color; | ||
|
||
const badgeTextColor = this.props.primary | ||
? theme.primaryTextColor | ||
: this.props.secondary | ||
? theme.secondaryTextColor | ||
: theme.textColor; | ||
|
||
const radius = 12; | ||
const radius2x = Math.floor(2*radius); | ||
|
||
return { | ||
root: { | ||
position: 'relative', | ||
display: 'inline-block', | ||
padding: [radius2x+'px', radius2x+'px', radius+'px', radius+'px'].join(' '), | ||
}, | ||
badge: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap', | ||
justifyContent: 'center', | ||
alignContent: 'center', | ||
alignItems: 'center', | ||
position: 'absolute', | ||
top: 0, | ||
right: 0, | ||
fontWeight: Typography.fontWeightMedium, | ||
fontSize: radius, | ||
width: radius2x, | ||
height: radius2x, | ||
borderRadius: '50%', | ||
backgroundColor: badgeBackgroundColor, | ||
color: badgeTextColor, | ||
}, | ||
}; | ||
}, | ||
render() { | ||
const styles = this.getStyles(); | ||
return ( | ||
<div style={this.prepareStyles(styles.root, this.props.style)} className={this.props.className}> | ||
{this.props.children} | ||
<span style={this.prepareStyles(styles.badge, this.props.badgeStyle)}> | ||
{this.props.badgeContent} | ||
</span> | ||
</div> | ||
); | ||
}, | ||
}); |
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