-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathPillBadge.tsx
More file actions
138 lines (128 loc) · 3.5 KB
/
Copy pathPillBadge.tsx
File metadata and controls
138 lines (128 loc) · 3.5 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/** @jsxImportSource @antv/infographic-jsx */
import { ComponentType, Defs, Group, Rect } from '@antv/infographic-jsx';
import { ItemDesc, ItemLabel } from '../components';
import { DropShadow, LinearGradient } from '../defs';
import { getItemProps } from '../utils';
import { registerItem } from './registry';
import type { BaseItemProps } from './types';
export interface PillBadgeProps extends BaseItemProps {
width?: number;
pillWidth?: number;
pillHeight?: number;
gap?: number;
}
export const PillBadge: ComponentType<PillBadgeProps> = (props) => {
const [
{
datum,
indexes,
width = 300,
pillWidth = 120,
pillHeight = 36,
gap = 16,
positionH = 'normal',
themeColors,
},
restProps,
] = getItemProps(props, ['width', 'pillWidth', 'pillHeight', 'gap']);
// Optimize: when no description, use pillWidth as component width
const hasDesc = !!datum.desc;
const componentWidth = hasDesc ? width : pillWidth;
// Calculate pill position based on alignment
const pillX = hasDesc
? positionH === 'center'
? (componentWidth - pillWidth) / 2
: positionH === 'flipped'
? componentWidth - pillWidth
: 0
: 0; // Always 0 when no description
const pillY = 0;
// Calculate content position (only needed when hasDesc is true)
const contentX = hasDesc
? positionH === 'center'
? 0
: positionH === 'flipped'
? 0
: 0
: 0;
const contentY = pillHeight + gap;
const contentWidth = componentWidth;
const dropShadowId = `drop-shadow-${themeColors.colorPrimary}`;
const linearGradientId = `linear-gradient-white-top-bottom`;
return (
<Group {...restProps}>
<Defs>
<DropShadow id={dropShadowId} color={themeColors.colorPrimary} />
<LinearGradient
id={linearGradientId}
startColor="#fff"
stopColor="#ffffff33"
direction="top-bottom"
/>
</Defs>
{/* Pill-shaped badge */}
<Rect
x={pillX}
y={pillY}
width={pillWidth}
height={pillHeight}
fill={themeColors.colorPrimaryBg}
stroke={themeColors.colorPrimary}
rx={pillHeight / 2}
ry={pillHeight / 2}
filter={`url(#${dropShadowId})`}
/>
<Rect
x={pillX}
y={pillY}
width={pillWidth}
height={pillHeight}
fill={`url(#${linearGradientId})`}
opacity={themeColors.isDarkMode ? 0.4 : 0.7}
rx={pillHeight / 2}
ry={pillHeight / 2}
/>
{/* Pill label */}
<ItemLabel
indexes={indexes}
x={pillX}
y={pillY}
width={pillWidth}
height={pillHeight}
alignHorizontal="center"
alignVertical="center"
fontSize={14}
fontWeight="500"
fill={themeColors.colorText}
>
{datum.label}
</ItemLabel>
{/* Description below */}
{datum.desc && (
<ItemDesc
indexes={indexes}
x={contentX}
y={contentY}
width={contentWidth}
alignHorizontal={
positionH === 'center'
? 'center'
: positionH === 'flipped'
? 'right'
: 'left'
}
fontSize={12}
fill={themeColors.colorTextSecondary}
lineNumber={2}
wordWrap={true}
>
{datum.desc}
</ItemDesc>
)}
</Group>
);
};
registerItem('pill-badge', {
component: PillBadge,
composites: ['label', 'desc'],
});