Skip to content

Commit 8c18bb8

Browse files
authored
Counter Icon badge (#1081)
* add counterIcon badge * remove counterIcon prop
1 parent 68471e8 commit 8c18bb8

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

demo/src/assets/icons/bell.png

274 Bytes
Loading
454 Bytes
Loading
632 Bytes
Loading

demo/src/screens/componentScreens/BadgesScreen.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const BadgesSpace = 30;
55
const plusIcon = require('../../assets/icons/chevronUp.png');
66
const minusIcon = require('../../assets/icons/chevronDown.png');
77
const star = require('../../assets/icons/star.png');
8-
const search = require('../../assets/icons/search.png');
9-
8+
const bell = require('../../assets/icons/bell.png');
109

1110
export default class BadgesScreen extends Component {
1211
constructor(props) {
@@ -137,6 +136,15 @@ export default class BadgesScreen extends Component {
137136
</Text>
138137
</View>
139138
</View>
139+
140+
<Text text50 marginB-10 row center marginT-25>
141+
Counter Icon Badges
142+
</Text>
143+
<View row paddingH-15>
144+
<Badge marginR-10 withCounterIcon label={'9999'} labelFormatterLimit={3} icon={bell} backgroundColor={Colors.red30} iconStyle={{height: 18, width: 18}}/>
145+
<Badge marginR-10 withCounterIcon label={'4'} icon={bell} backgroundColor={Colors.red30} color={Colors.blue30} iconStyle={{height: 18, width: 18}}/>
146+
</View>
147+
140148
</ScrollView>
141149
);
142150
}

src/components/badge/index.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {ImageSourcePropType, ImageStyle, StyleProp, StyleSheet, Text, TextStyle,
44
import {View as AnimatableView} from 'react-native-animatable';
55
import {extractAccessibilityProps, extractAnimationProps} from '../../commons/modifiers';
66
import {asBaseComponent} from '../../commons/new';
7-
import {BorderRadiuses, Colors, Typography} from '../../style';
7+
import {BorderRadiuses, Colors, Spacings, Typography} from '../../style';
88
import TouchableOpacity from '../touchableOpacity';
99
import Image from '../image';
1010
import View from '../view';
@@ -143,6 +143,15 @@ class Badge extends PureComponent<BadgeProps> {
143143
height: badgeHeight,
144144
minWidth: badgeHeight
145145
};
146+
if (icon && label) {
147+
style.paddingRight = 6;
148+
style.paddingLeft = 4;
149+
style.height = Spacings.s5;
150+
if (borderWidth) {
151+
style.height += borderWidth * 2;
152+
}
153+
return style;
154+
}
146155

147156
const isPimple = label === undefined;
148157
if (isPimple || icon) {
@@ -213,7 +222,8 @@ class Badge extends PureComponent<BadgeProps> {
213222
}
214223

215224
renderIcon() {
216-
const {icon, iconStyle, iconProps, borderColor} = this.props;
225+
const {icon, iconStyle, iconProps, borderColor, label} = this.props;
226+
const flex = label ? 0 : 1;
217227
return (
218228
<Image
219229
source={icon!}
@@ -222,7 +232,7 @@ class Badge extends PureComponent<BadgeProps> {
222232
borderColor={borderColor}
223233
{...iconProps}
224234
style={{
225-
flex: 1,
235+
flex,
226236
...iconStyle
227237
}}
228238
/>
@@ -237,6 +247,7 @@ class Badge extends PureComponent<BadgeProps> {
237247
containerStyle,
238248
hitSlop,
239249
icon,
250+
label,
240251
onPress,
241252
testId,
242253
testID,
@@ -265,7 +276,10 @@ class Badge extends PureComponent<BadgeProps> {
265276
hitSlop={hitSlop}
266277
{...animationProps}
267278
>
268-
{icon ? this.renderIcon() : this.renderLabel()}
279+
<View flex row centerV>
280+
{icon && this.renderIcon()}
281+
{label && this.renderLabel()}
282+
</View>
269283
</Container>
270284
</View>
271285
);

0 commit comments

Comments
 (0)