-
Notifications
You must be signed in to change notification settings - Fork 434
Patch request for Badge
component
#1805
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
028bf3e
Update README.md to run install command in right directory while sett…
pradumangoyal dc58b2e
Add `Badge` component at initial stage
pradumangoyal e13826a
Refactor code according to the changes suggested
pradumangoyal 032888b
Remove check-props file
pradumangoyal 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This object is imported into the documentation site. An example for the documentation site should be part of the pull request for the component. The object key is the kabob case of the "URL folder". In the case of `http://localhost:8080/components/app-launcher/`, `app-launcher` is the `key`. The folder name is created by `components.component` value in `package.json`. The following uses webpack's raw-loader plugin to get "text files" that will be eval()'d by CodeMirror within the documentation site on page load. | ||
|
||
/* eslint-env node */ | ||
/* eslint-disable global-require */ | ||
|
||
const siteStories = [ | ||
require('raw-loader!@salesforce/design-system-react/components/badge/__examples__/default.jsx'), | ||
]; | ||
|
||
module.exports = siteStories; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Base from '../__examples__/base'; | ||
import Darker from '../__examples__/darker.jsx'; | ||
import Lightest from '../__examples__/lightest.jsx'; | ||
import LeftIcon from '../__examples__/left-icon.jsx'; | ||
import InverseRightIcon from '../__examples__/inverse-right-icon.jsx'; | ||
import Nested from '../__examples__/nested.jsx'; | ||
|
||
import { BADGE } from '../../../utilities/constants'; | ||
|
||
/* eslint-disable react/display-name */ | ||
|
||
storiesOf(BADGE, module) | ||
.addDecorator((getStory) => ( | ||
<div className="slds-p-around_medium">{getStory()}</div> | ||
)) | ||
.add('Base', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}>Base Badge</h1> | ||
<Base /> | ||
</div> | ||
)) | ||
.add('Darker', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}>Darker Badger</h1> | ||
<Darker /> | ||
</div> | ||
)) | ||
.add('Lightest', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}>Lightest Badge</h1> | ||
<Lightest /> | ||
</div> | ||
)) | ||
.add('Left Icon', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}>Icon on the left</h1> | ||
<LeftIcon /> | ||
</div> | ||
)) | ||
.add('Inverse Right Icon', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}> | ||
Inverse badge with the icon on the left | ||
</h1> | ||
<InverseRightIcon /> | ||
</div> | ||
)) | ||
.add('Nested Elements', () => ( | ||
<div> | ||
<h1 style={{ marginBottom: '10px' }}>Badge with Nested Elements</h1> | ||
<Nested /> | ||
</div> | ||
)); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return <Badge>Badge Label</Badge>; | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return <Badge color="inverse">Badge Label</Badge>; | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
import IconSettings from '~/components/icon-settings'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return ( | ||
<IconSettings iconPath="/assets/icons"> | ||
<Badge | ||
color="inverse" | ||
iconName="moneybag" | ||
iconCategory="utility" | ||
iconPosition="right" | ||
> | ||
423 Credits Available | ||
</Badge> | ||
</IconSettings> | ||
); | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
import IconSettings from '~/components/icon-settings'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return ( | ||
<IconSettings iconPath="/assets/icons"> | ||
<Badge iconName="moneybag" iconCategory="utility"> | ||
423 Credits Available | ||
</Badge> | ||
</IconSettings> | ||
); | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return <Badge color="lightest">Badge Label</Badge>; | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import Badge from '~/components/badge'; | ||
|
||
class Example extends React.Component { | ||
static displayName = 'BadgeExample'; | ||
|
||
render() { | ||
return ( | ||
<Badge> | ||
Component In: | ||
<strong>Lightning</strong> | ||
</Badge> | ||
); | ||
} | ||
} | ||
|
||
export default Example; // export is replaced with `ReactDOM.render(<Example />, mountNode);` at runtime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"component": "badge", | ||
"status": "prod", | ||
"display-name": "Badges", | ||
"SLDS-component-path": "/components/badges", | ||
"url-slug": "badges" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */ | ||
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */ | ||
|
||
// # Badges Component | ||
|
||
// Implements the [Badges design pattern](https://www.lightningdesignsystem.com/components/badges/) in React. | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import requiredIf from 'react-required-if'; | ||
import classNames from 'classnames'; | ||
|
||
import BadgeIcon from '../icon/badge-icon'; | ||
|
||
import { BADGE } from '../../utilities/constants'; | ||
|
||
const propTypes = { | ||
/** | ||
* Change badge color and that of text accordingly | ||
*/ | ||
color: PropTypes.oneOf(['inverse', 'lightest']), | ||
/** | ||
* CSS classes that are applied to the span. | ||
*/ | ||
className: PropTypes.oneOfType([ | ||
PropTypes.array, | ||
PropTypes.object, | ||
PropTypes.string, | ||
]), | ||
/** | ||
* Name of the icon category. Visit <a href="http://www.lightningdesignsystem.com/resources/icons">Lightning Design System Icons</a> to reference icon categories. | ||
*/ | ||
iconCategory: requiredIf( | ||
PropTypes.oneOf(['action', 'custom', 'doctype', 'standard', 'utility']), | ||
(props) => !!props.iconName | ||
), | ||
/** | ||
* CSS classes to be added to icon. | ||
*/ | ||
iconClassName: PropTypes.oneOfType([ | ||
PropTypes.array, | ||
PropTypes.object, | ||
PropTypes.string, | ||
]), | ||
/** | ||
* Name of the icon. Visit <a href="http://www.lightningdesignsystem.com/resources/icons">Lightning Design System Icons</a> to reference icon names. | ||
*/ | ||
iconName: PropTypes.string, | ||
/** | ||
* Path to the icon. This will override any global icon settings. | ||
*/ | ||
iconPath: PropTypes.string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is usually handled by an higher order component called |
||
/** | ||
* Position of the icon. If omitted icon will be positioned to the left. | ||
*/ | ||
iconPosition: PropTypes.oneOf(['left', 'right']), | ||
/** | ||
* HTML id applied on the badge span. | ||
*/ | ||
id: PropTypes.string, | ||
/** | ||
* Description of icon if required. | ||
*/ | ||
iconTite: PropTypes.string, | ||
}; | ||
|
||
const defaultProps = { | ||
assistiveText: {}, | ||
color: '', | ||
iconPosition: 'left', | ||
}; | ||
|
||
/** | ||
* Badges are labels which hold small amounts of information. | ||
*/ | ||
class Badge extends React.Component { | ||
/** | ||
* ID as a string | ||
* @returns {string} id | ||
*/ | ||
getId() { | ||
return this.props.id; | ||
} | ||
|
||
/** | ||
* Render icon according to the passed props. | ||
*/ | ||
renderIcon = () => ( | ||
<BadgeIcon | ||
category={this.props.iconCategory || 'utility'} | ||
className={classNames( | ||
{ | ||
'slds-global-header__icon': | ||
this.props.iconVariant === 'global-header', | ||
}, | ||
this.props.iconClassName | ||
)} | ||
inverse={this.props.color === 'inverse'} | ||
name={this.props.iconName} | ||
path={this.props.iconPath} | ||
position={this.props.iconPosition} | ||
title={this.props.iconTitle} | ||
/> | ||
); | ||
|
||
/** | ||
* Render data other than icon in a badge. | ||
*/ | ||
renderLabel = () => { | ||
if (this.props.iconPosition === 'right' || !this.props.iconName) { | ||
return this.props.children; | ||
} | ||
return <span>{this.props.children}</span>; | ||
}; | ||
|
||
/** | ||
* Render badge component | ||
*/ | ||
renderBadge = () => ( | ||
<span | ||
className={classNames( | ||
'slds-badge', | ||
{ | ||
[`slds-badge_${this.props.color}`]: this.props.color, | ||
}, | ||
this.props.className | ||
)} | ||
id={this.getId()} | ||
> | ||
{this.props.iconPosition === 'right' && this.renderLabel()} | ||
{this.props.iconName && this.renderIcon()} | ||
{this.props.iconPosition === 'left' && this.renderLabel()} | ||
</span> | ||
); | ||
|
||
render() { | ||
return this.renderBadge(); | ||
} | ||
} | ||
|
||
Badge.displayName = BADGE; | ||
Badge.propTypes = propTypes; | ||
Badge.defaultProps = defaultProps; | ||
|
||
export default Badge; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Many of the older components do use
iconCategory
andiconName
but the best pattern is to pass in a whole<Icon>
so that theIcon
props do not have to be duplicated within badge also https://github.com/salesforce/design-system-react/blob/master/docs/codebase-overview.md#reuse-existing-component-props-by-using-component-no-button-iconclassname-It looks like there are special classnames for badge that are on placed on the icon, so you may want to duplicate the icon passed into a prop and the classNames that way before render.
Please update.