-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Badge component. #2045
Merged
+225
−0
Merged
Added Badge component. #2045
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the default value with
getDefaultProps
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added getDefaultProps cfd972d