Skip to content

Commit e4fee56

Browse files
Feat/support customElement in Badge (#1153)
* Feat/support customElement in Badge * customElement fixes Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 64a2a95 commit e4fee56

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

demo/src/screens/componentScreens/BadgesScreen.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {Component} from 'react';
22
import {ScrollView, StyleSheet} from 'react-native';
3-
import {Colors, View, Badge, Button, Text} from 'react-native-ui-lib'; //eslint-disable-line
3+
import {Colors, View, Badge, Button, Text, Image} from 'react-native-ui-lib'; //eslint-disable-line
44
const BadgesSpace = 30;
55
const plusIcon = require('../../assets/icons/chevronUp.png');
66
const minusIcon = require('../../assets/icons/chevronDown.png');
@@ -25,6 +25,23 @@ export default class BadgesScreen extends Component {
2525
}
2626

2727
render() {
28+
const customElement1 = (
29+
<View row>
30+
<Image source={bell}/>
31+
<Image source={bell}/>
32+
</View>
33+
);
34+
35+
const customElement2 = (
36+
<View row>
37+
<Image source={bell}/>
38+
<Text white text90>
39+
37
40+
</Text>
41+
<Image source={bell}/>
42+
</View>
43+
);
44+
2845
return (
2946
<ScrollView style={{backgroundColor: Colors.dark70}} contentContainerStyle={styles.container}>
3047
<Text text50 row center marginB-15>
@@ -141,10 +158,17 @@ export default class BadgesScreen extends Component {
141158
Counter Icon Badges
142159
</Text>
143160
<View row paddingH-15>
144-
<Badge marginR-10 label={'9999'} labelFormatterLimit={3} icon={bell} backgroundColor={Colors.red30}/>
145-
<Badge marginR-10 label={'4'} icon={bell} backgroundColor={Colors.red30}/>
161+
<Badge marginR-10 label={'9999'} labelFormatterLimit={3} icon={bell} backgroundColor={Colors.red30}/>
162+
<Badge marginR-10 label={'4'} icon={bell} backgroundColor={Colors.red30}/>
146163
</View>
147164

165+
<Text text50 marginB-10 row center marginT-25>
166+
Custom Element Badges
167+
</Text>
168+
<View row paddingH-15>
169+
<Badge marginR-10 label={'17'} customElement={customElement1}/>
170+
<Badge marginR-10 customElement={customElement2} backgroundColor={Colors.grey30}/>
171+
</View>
148172
</ScrollView>
149173
);
150174
}

generatedTypes/components/badge/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export declare type BadgeProps = ViewProps & TouchableOpacityProps & {
7272
* Additional props passed to icon
7373
*/
7474
iconProps?: object;
75+
/**
76+
* Custom element to render instead of an icon
77+
*/
78+
customElement?: JSX.Element;
7579
/**
7680
* Use to identify the badge in tests
7781
*/
@@ -101,7 +105,8 @@ declare class Badge extends PureComponent<BadgeProps> {
101105
getFormattedLabel(): any;
102106
getBorderStyling(): ViewStyle;
103107
renderLabel(): JSX.Element | undefined;
104-
renderIcon(): JSX.Element;
108+
renderCustomElement(): JSX.Element | undefined;
109+
renderIcon(): 0 | JSX.Element | undefined;
105110
render(): JSX.Element;
106111
}
107112
declare function createStyles(props: BadgeProps): {
@@ -402,6 +407,10 @@ declare const _default: React.ComponentClass<ViewProps & TouchableOpacityProps &
402407
* Additional props passed to icon
403408
*/
404409
iconProps?: object | undefined;
410+
/**
411+
* Custom element to render instead of an icon
412+
*/
413+
customElement?: JSX.Element | undefined;
405414
/**
406415
* Use to identify the badge in tests
407416
*/

src/components/badge/index.tsx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export type BadgeProps = ViewProps &
9696
* Additional props passed to icon
9797
*/
9898
iconProps?: object;
99+
/**
100+
* Custom element to render instead of an icon
101+
*/
102+
customElement?: JSX.Element;
99103
/**
100104
* Use to identify the badge in tests
101105
*/
@@ -143,7 +147,7 @@ class Badge extends PureComponent<BadgeProps> {
143147
}
144148

145149
getBadgeSizeStyle() {
146-
const {borderWidth, size, icon} = this.props;
150+
const {borderWidth, size, icon, customElement} = this.props;
147151
const label = this.getFormattedLabel();
148152
const badgeHeight = _.isNumber(size) ? size : BADGE_SIZES[size];
149153

@@ -161,7 +165,9 @@ class Badge extends PureComponent<BadgeProps> {
161165
}
162166
return style;
163167
}
164-
168+
if (customElement) {
169+
return style;
170+
}
165171
const isPimple = label === undefined;
166172
if (isPimple || icon) {
167173
style.paddingHorizontal = 0;
@@ -230,38 +236,34 @@ class Badge extends PureComponent<BadgeProps> {
230236
}
231237
}
232238

239+
renderCustomElement() {
240+
const {customElement} = this.props;
241+
return customElement;
242+
}
243+
233244
renderIcon() {
234245
const {icon, iconStyle, iconProps, borderColor, label} = this.props;
235246
const flex = label ? 0 : 1;
236247
return (
237-
<Image
238-
source={icon!}
239-
resizeMode="contain"
240-
//@ts-ignore
241-
borderColor={borderColor}
242-
{...iconProps}
243-
style={{
244-
flex,
245-
...iconStyle
246-
}}
247-
/>
248+
icon && (
249+
<Image
250+
source={icon!}
251+
resizeMode="contain"
252+
//@ts-ignore
253+
borderColor={borderColor}
254+
{...iconProps}
255+
style={{
256+
flex,
257+
...iconStyle
258+
}}
259+
/>
260+
)
248261
);
249262
}
250263

251264
render() {
252265
// TODO: remove testId after deprecation
253-
const {
254-
activeOpacity,
255-
backgroundColor,
256-
containerStyle,
257-
hitSlop,
258-
icon,
259-
label,
260-
onPress,
261-
testId,
262-
testID,
263-
...others
264-
} = this.props;
266+
const {activeOpacity, backgroundColor, containerStyle, hitSlop, onPress, testId, testID, ...others} = this.props;
265267
const backgroundStyle = backgroundColor && {backgroundColor};
266268
const sizeStyle = this.getBadgeSizeStyle();
267269
const borderStyle = this.getBorderStyling();
@@ -293,8 +295,9 @@ class Badge extends PureComponent<BadgeProps> {
293295
{...animationProps}
294296
row
295297
>
296-
{icon && this.renderIcon()}
297-
{label && this.renderLabel()}
298+
{this.renderCustomElement()}
299+
{this.renderIcon()}
300+
{this.renderLabel()}
298301
</Container>
299302
</View>
300303
);
@@ -306,7 +309,7 @@ function createStyles(props: BadgeProps) {
306309
badge: {
307310
alignSelf: 'flex-start',
308311
borderRadius: BorderRadiuses.br100,
309-
backgroundColor: !props.icon ? Colors.primary : undefined,
312+
backgroundColor: (!props.icon || props.customElement) ? Colors.primary : undefined,
310313
alignItems: 'center',
311314
justifyContent: 'center',
312315
overflow: 'hidden'

0 commit comments

Comments
 (0)