Skip to content

Commit c9d7754

Browse files
cmcWebCode40claude
andcommitted
feat: add Badge and FilterChip UI components
Add reusable Badge component for status indicators and FilterChip component for filter tags with theme support. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7faf8fa commit c9d7754

3 files changed

Lines changed: 264 additions & 0 deletions

File tree

‎src/components/Badge.tsx‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import React from 'react';
2+
import {
3+
StyleSheet,
4+
Text,
5+
View,
6+
type StyleProp,
7+
type ViewStyle,
8+
} from 'react-native';
9+
import { colors } from '../constants/colors';
10+
11+
export type BadgeVariant = 'success' | 'warning' | 'error' | 'info' | 'pending';
12+
13+
interface BadgeProps {
14+
label: string;
15+
variant?: BadgeVariant;
16+
size?: 'small' | 'medium';
17+
style?: StyleProp<ViewStyle>;
18+
}
19+
20+
const variantColors: Record<BadgeVariant, string> = {
21+
success: colors.success,
22+
warning: colors.warning,
23+
error: colors.error,
24+
info: colors.info,
25+
pending: colors.pending,
26+
};
27+
28+
export const Badge: React.FC<BadgeProps> = ({
29+
label,
30+
variant = 'info',
31+
size = 'medium',
32+
style,
33+
}) => {
34+
const backgroundColor = variantColors[variant];
35+
const isSmall = size === 'small';
36+
37+
return (
38+
<View
39+
style={[
40+
styles.badge,
41+
{ backgroundColor },
42+
isSmall && styles.badgeSmall,
43+
style,
44+
]}
45+
>
46+
<Text style={[styles.label, isSmall && styles.labelSmall]}>{label}</Text>
47+
</View>
48+
);
49+
};
50+
51+
interface StatusBadgeProps {
52+
status?: number;
53+
hasError?: boolean;
54+
size?: 'small' | 'medium';
55+
style?: StyleProp<ViewStyle>;
56+
}
57+
58+
export const StatusBadge: React.FC<StatusBadgeProps> = ({
59+
status,
60+
hasError,
61+
size = 'medium',
62+
style,
63+
}) => {
64+
const getVariant = (): BadgeVariant => {
65+
if (hasError) return 'error';
66+
if (!status) return 'pending';
67+
if (status >= 200 && status < 300) return 'success';
68+
if (status >= 300 && status < 400) return 'warning';
69+
if (status >= 400) return 'error';
70+
return 'pending';
71+
};
72+
73+
const getLabel = (): string => {
74+
if (hasError) return 'ERROR';
75+
if (!status) return 'PENDING';
76+
return status.toString();
77+
};
78+
79+
return (
80+
<Badge
81+
label={getLabel()}
82+
variant={getVariant()}
83+
size={size}
84+
style={style}
85+
/>
86+
);
87+
};
88+
89+
interface MethodBadgeProps {
90+
method: string;
91+
size?: 'small' | 'medium';
92+
style?: StyleProp<ViewStyle>;
93+
}
94+
95+
const methodColors: Record<string, string> = {
96+
GET: colors.success,
97+
POST: colors.warning,
98+
PUT: colors.info,
99+
PATCH: '#9C27B0',
100+
DELETE: colors.error,
101+
HEAD: '#607D8B',
102+
OPTIONS: '#795548',
103+
};
104+
105+
export const MethodBadge: React.FC<MethodBadgeProps> = ({
106+
method,
107+
size = 'medium',
108+
style,
109+
}) => {
110+
const upperMethod = method.toUpperCase();
111+
const backgroundColor = methodColors[upperMethod] || colors.info;
112+
113+
return (
114+
<View
115+
style={[
116+
styles.methodBadge,
117+
{ backgroundColor },
118+
size === 'small' && styles.badgeSmall,
119+
style,
120+
]}
121+
>
122+
<Text style={[styles.label, size === 'small' && styles.labelSmall]}>
123+
{upperMethod}
124+
</Text>
125+
</View>
126+
);
127+
};
128+
129+
const styles = StyleSheet.create({
130+
badge: {
131+
paddingHorizontal: 8,
132+
paddingVertical: 3,
133+
borderRadius: 12,
134+
alignSelf: 'flex-start',
135+
},
136+
badgeSmall: {
137+
paddingHorizontal: 6,
138+
paddingVertical: 2,
139+
borderRadius: 10,
140+
},
141+
label: {
142+
fontSize: 12,
143+
fontWeight: 'bold',
144+
color: '#fff',
145+
},
146+
labelSmall: {
147+
fontSize: 10,
148+
},
149+
methodBadge: {
150+
paddingHorizontal: 8,
151+
paddingVertical: 3,
152+
borderRadius: 4,
153+
alignSelf: 'flex-start',
154+
},
155+
});

‎src/components/FilterChip.tsx‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React from 'react';
2+
import {
3+
StyleSheet,
4+
Text,
5+
TouchableOpacity,
6+
type StyleProp,
7+
type ViewStyle,
8+
} from 'react-native';
9+
import type { ThemeMode } from '../constants/colors';
10+
import { colors, getThemeColors } from '../constants/colors';
11+
12+
interface FilterChipProps {
13+
label: string;
14+
isActive?: boolean;
15+
onPress: () => void;
16+
variant?: 'default' | 'error';
17+
theme?: ThemeMode;
18+
style?: StyleProp<ViewStyle>;
19+
}
20+
21+
export const FilterChip: React.FC<FilterChipProps> = ({
22+
label,
23+
isActive = false,
24+
onPress,
25+
variant = 'default',
26+
theme = 'light',
27+
style,
28+
}) => {
29+
const themeColors = getThemeColors(theme);
30+
const isError = variant === 'error' && isActive;
31+
32+
const chipStyle = [
33+
styles.chip,
34+
{
35+
backgroundColor: isActive
36+
? isError
37+
? colors.error
38+
: themeColors.primary
39+
: themeColors.background,
40+
borderColor: isActive
41+
? isError
42+
? colors.error
43+
: themeColors.primary
44+
: themeColors.border,
45+
},
46+
style,
47+
];
48+
49+
const textStyle = [
50+
styles.chipText,
51+
{
52+
color: isActive ? '#fff' : themeColors.textSecondary,
53+
},
54+
];
55+
56+
return (
57+
<TouchableOpacity style={chipStyle} onPress={onPress} activeOpacity={0.7}>
58+
<Text style={textStyle}>{label}</Text>
59+
</TouchableOpacity>
60+
);
61+
};
62+
63+
interface FilterChipGroupProps {
64+
options: { key: string; label: string }[];
65+
selectedKey: string;
66+
onSelect: (key: string) => void;
67+
theme?: ThemeMode;
68+
errorKeys?: string[];
69+
}
70+
71+
export const FilterChipGroup: React.FC<FilterChipGroupProps> = ({
72+
options,
73+
selectedKey,
74+
onSelect,
75+
theme = 'light',
76+
errorKeys = ['error'],
77+
}) => {
78+
return (
79+
<>
80+
{options.map((option) => (
81+
<FilterChip
82+
key={option.key}
83+
label={option.label}
84+
isActive={selectedKey === option.key}
85+
onPress={() => onSelect(option.key)}
86+
variant={errorKeys.includes(option.key) ? 'error' : 'default'}
87+
theme={theme}
88+
/>
89+
))}
90+
</>
91+
);
92+
};
93+
94+
const styles = StyleSheet.create({
95+
chip: {
96+
paddingHorizontal: 12,
97+
paddingVertical: 6,
98+
borderRadius: 16,
99+
borderWidth: 1,
100+
},
101+
chipText: {
102+
fontSize: 12,
103+
fontWeight: '600',
104+
},
105+
});

‎src/components/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './Badge';
2+
export * from './FilterChip';
3+
export * from './JsonViewer';
4+
export * from './ExportModal';

0 commit comments

Comments
 (0)