/
Badge.tsx
79 lines (69 loc) · 1.72 KB
/
Badge.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import PropTypes from 'prop-types';
import classNames from 'classnames';
import * as React from 'react';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { Color, Variant } from './types';
export interface BadgeProps
extends BsPrefixProps,
React.HTMLAttributes<HTMLElement> {
bg?: Variant;
pill?: boolean;
text?: Color;
}
const propTypes = {
/** @default 'badge' */
bsPrefix: PropTypes.string,
/**
* The visual style of the badge
*
* @type {('primary'|'secondary'|'success'|'danger'|'warning'|'info'|'light'|'dark')}
*/
bg: PropTypes.string,
/**
* Add the `pill` modifier to make badges more rounded with
* some additional horizontal padding
*/
pill: PropTypes.bool,
/**
* Sets badge text color
*
* @type {('primary'|'secondary'|'success'|'danger'|'warning'|'info'|'light'|'dark')}
*/
text: PropTypes.string,
/** @default span */
as: PropTypes.elementType,
};
const Badge: BsPrefixRefForwardingComponent<'span', BadgeProps> =
React.forwardRef<HTMLElement, BadgeProps>(
(
{
bsPrefix,
bg = 'primary',
pill = false,
text,
className,
as: Component = 'span',
...props
},
ref,
) => {
const prefix = useBootstrapPrefix(bsPrefix, 'badge');
return (
<Component
ref={ref}
{...props}
className={classNames(
className,
prefix,
pill && `rounded-pill`,
text && `text-${text}`,
bg && `bg-${bg}`,
)}
/>
);
},
);
Badge.displayName = 'Badge';
Badge.propTypes = propTypes;
export default Badge;