1
- import React , { FC } from 'react' ;
2
- import { Flex , FlexProps } from 'rebass' ;
1
+ import React from 'react' ;
2
+ import { Flex , FlexProps , SxStyleProp } from 'rebass' ;
3
3
import { useTheme } from '../../theme/theme' ;
4
4
import { getIcon , IconName } from '../icon/list' ;
5
5
import Value from '../typography/value' ;
6
- import getBorderAnimation from './animation ' ;
7
- import { ITheme } from '../../theme/types ' ;
6
+ import { BadgeVariant } from './types ' ;
7
+ import { getBorderColor , getBorderAnimation } from './utils ' ;
8
8
9
9
type Mode = 'default' | 'bordered' ;
10
- type Variant =
11
- | 'light'
12
- | 'default'
13
- | 'label'
14
- | 'success'
15
- | 'warning'
16
- | 'notice'
17
- | 'fail' ;
18
-
19
- export interface BadgeProps extends Omit < FlexProps , 'css' > {
10
+ export interface Props extends Omit < FlexProps , 'css' > {
11
+ /** Value for the badge. E.g. 'pending' or 18 */
20
12
value : string | number ;
21
- variant ?: Variant ;
13
+ /** One of badge variants. Defined by `BadgeVariant` type. E.g. `light` or `warning`... */
14
+ variant ?: BadgeVariant ;
15
+ /** Badge mode: `default` or `bordered` */
22
16
mode ?: Mode ;
17
+ /** Whether to show loading spinner */
23
18
loading ?: boolean ;
19
+ /** Icon, used as `IconName.nameOfTheIcon` e.g. `IconName.download` */
24
20
icon ?: IconName ;
25
21
}
26
22
27
- // this is a workaround because styled-system does not support theme colors for `linear-gradient`
28
- const getBorderColor = ( theme : ITheme , variant : Variant ) => {
29
- switch ( variant ) {
30
- case 'light' :
31
- return theme . colors . grayShade1 ;
32
- case 'default' :
33
- return theme . colors . black ;
34
- case 'label' :
35
- return theme . colors . black ;
36
- case 'success' :
37
- return theme . colors . labels . green ;
38
- case 'warning' :
39
- return theme . colors . labels . orange ;
40
- case 'notice' :
41
- return theme . colors . labels . yellow ;
42
- case 'fail' :
43
- return theme . colors . labels . red ;
44
- default :
45
- return theme . colors . black ;
46
- }
47
- } ;
48
-
49
23
const THEME_PATH : Record < Mode , string > = {
50
24
default : 'variants.badges.primary' ,
51
25
bordered : 'variants.badges.bordered' ,
52
26
} ;
53
27
54
- const Badge : FC < BadgeProps > = ( {
28
+ export const Badge = ( {
55
29
value,
56
30
variant = 'light' ,
57
31
mode = 'default' ,
58
32
loading = false ,
59
33
icon,
34
+ sx,
60
35
...props
61
- } : BadgeProps ) => {
36
+ } : Props ) => {
62
37
const theme = useTheme ( ) ;
63
38
64
39
const borderColor = getBorderColor ( theme , variant ) ;
@@ -73,7 +48,7 @@ const Badge: FC<BadgeProps> = ({
73
48
justifyContent = "center"
74
49
px = "6px"
75
50
height = "19px"
76
- sx = { borderStyle }
51
+ sx = { { ... borderStyle , ... sx } as SxStyleProp }
77
52
variant = { variant }
78
53
tx = { loading ? THEME_PATH . bordered : THEME_PATH [ mode ] }
79
54
{ ...props }
@@ -99,5 +74,3 @@ const Badge: FC<BadgeProps> = ({
99
74
</ Flex >
100
75
) ;
101
76
} ;
102
-
103
- export default Badge ;
0 commit comments