framework/js/old/common/components/Badge.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

import Component from '../Component';
import icon from '../helpers/icon';
import extract from '../utils/extract';
2015-04-25 20:58:39 +08:00
/**
* The `Badge` component represents a user/discussion badge, indicating some
* status (e.g. a discussion is stickied, a user is an admin).
*
* A badge may have the following special props:
*
* - `type` The type of badge this is. This will be used to give the badge a
* class name of `Badge--{type}`.
* - `icon` The name of an icon to show inside the badge.
* - `label`
*
* All other props will be assigned as attributes on the badge element.
*/
2015-04-25 20:58:39 +08:00
export default class Badge extends Component {
view() {
const attrs = Object.assign({}, this.props);
const type = extract(attrs, 'type');
const iconName = extract(attrs, 'icon');
2015-04-25 20:58:39 +08:00
attrs.className = 'Badge ' + (type ? 'Badge--' + type : '') + ' ' + (attrs.className || '');
attrs.title = extract(attrs, 'label') || '';
2020-04-17 17:57:55 +08:00
return <span {...attrs}>{iconName ? icon(iconName, { className: 'Badge-icon' }) : m.trust('&nbsp;')}</span>;
}
config(isInitialized) {
if (isInitialized) return;
2020-04-17 17:57:55 +08:00
if (this.props.label) this.$().tooltip({ container: 'body' });
2015-04-25 20:58:39 +08:00
}
}