|
| 1 | +/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */ |
| 2 | +/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */ |
| 3 | + |
| 4 | +// Implements the [Badge design pattern](https://latest-216.lightningdesignsystem.com/components/badges/) in React. |
| 5 | + |
| 6 | +// ## Dependencies |
| 7 | + |
| 8 | +// ### React |
| 9 | +import React from 'react'; |
| 10 | +import PropTypes from 'prop-types'; |
| 11 | +import classNames from 'classnames'; |
| 12 | + |
| 13 | +// ### shortid |
| 14 | +// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid) |
| 15 | +// shortid is a short, non-sequential, url-friendly, unique id generator |
| 16 | +import shortid from 'shortid'; |
| 17 | + |
| 18 | +// ## Constants |
| 19 | +import { BADGE } from '../../utilities/constants'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Badges are labels which hold small amounts of information. |
| 23 | + */ |
| 24 | +class Badge extends React.Component { |
| 25 | + constructor(props) { |
| 26 | + super(props); |
| 27 | + this.generatedId = shortid.generate(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the Badge's HTML id. Generate a new one if no ID present. |
| 32 | + */ |
| 33 | + getId() { |
| 34 | + return this.props.id || this.generatedId; |
| 35 | + } |
| 36 | + |
| 37 | + render() { |
| 38 | + const icon = ( |
| 39 | + <span |
| 40 | + className={classNames( |
| 41 | + 'slds-badge__icon', |
| 42 | + `slds-badge__icon_${this.props.iconAlignment}` |
| 43 | + )} |
| 44 | + style={this.props.style} |
| 45 | + > |
| 46 | + {this.props.icon} |
| 47 | + </span> |
| 48 | + ); |
| 49 | + |
| 50 | + return ( |
| 51 | + <span |
| 52 | + id={this.getId()} |
| 53 | + className={classNames( |
| 54 | + 'slds-badge', |
| 55 | + { |
| 56 | + 'slds-badge_inverse': this.props.color === 'inverse', |
| 57 | + 'slds-badge_lightest': this.props.color === 'light', |
| 58 | + }, |
| 59 | + this.props.className |
| 60 | + )} |
| 61 | + > |
| 62 | + {this.props.iconAlignment === 'left' ? ( |
| 63 | + <React.Fragment> |
| 64 | + {icon} |
| 65 | + {this.props.content} |
| 66 | + </React.Fragment> |
| 67 | + ) : ( |
| 68 | + <React.Fragment> |
| 69 | + {this.props.content} |
| 70 | + {icon} |
| 71 | + </React.Fragment> |
| 72 | + )} |
| 73 | + </span> |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +Badge.displayName = BADGE; |
| 79 | + |
| 80 | +Badge.propTypes = { |
| 81 | + /** |
| 82 | + * CSS classes that are applied to the component |
| 83 | + */ |
| 84 | + className: PropTypes.oneOfType([ |
| 85 | + PropTypes.array, |
| 86 | + PropTypes.object, |
| 87 | + PropTypes.string, |
| 88 | + ]), |
| 89 | + |
| 90 | + /** |
| 91 | + * Id of component, if desired. If not provided an id is automatically generated |
| 92 | + */ |
| 93 | + id: PropTypes.string, |
| 94 | + |
| 95 | + /** |
| 96 | + * Custom styles to be passed to the component |
| 97 | + */ |
| 98 | + style: PropTypes.object, |
| 99 | + |
| 100 | + /** |
| 101 | + * Color variant for the badge component |
| 102 | + */ |
| 103 | + color: PropTypes.oneOf(['default', 'inverse', 'light']), |
| 104 | + |
| 105 | + /** |
| 106 | + * Icon alignment for the badge component |
| 107 | + */ |
| 108 | + iconAlignment: PropTypes.oneOf(['left', 'right']), |
| 109 | + |
| 110 | + /** |
| 111 | + * Content to be placed inside the badge component |
| 112 | + */ |
| 113 | + content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 114 | +}; |
| 115 | + |
| 116 | +Badge.defaultProps = { |
| 117 | + iconAlignment: 'left', |
| 118 | + color: 'default', |
| 119 | +}; |
| 120 | + |
| 121 | +export default Badge; |
0 commit comments