-
Notifications
You must be signed in to change notification settings - Fork 734
/
Copy pathindex.tsx
312 lines (284 loc) · 7.92 KB
/
index.tsx
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import _ from 'lodash';
import React, {PureComponent} from 'react';
import {
ImageSourcePropType,
ImageStyle,
StyleProp,
StyleSheet,
TextStyle,
TouchableOpacityProps,
ViewStyle,
ViewProps
} from 'react-native';
import {extractAccessibilityProps} from '../../commons/modifiers';
import {asBaseComponent} from '../../commons/new';
import {BorderRadiuses, Colors, Spacings, Typography} from '../../style';
import TouchableOpacity from '../touchableOpacity';
import Image from '../image';
import View from '../view';
import Text from '../text';
const LABEL_FORMATTER_VALUES = [1, 2, 3, 4] as const;
type LabelFormatterValues = typeof LABEL_FORMATTER_VALUES[number];
export type BadgeProps = ViewProps &
TouchableOpacityProps & {
/**
* Text to show inside the badge.
* Not passing a label (undefined) will present a pimple badge.
*/
label?: string;
/**
* Color of the badge background
*/
backgroundColor?: string;
/**
* the badge size
*/
size?: number;
/**
* Press handler
*/
onPress?: (props: any) => void;
/**
* Defines how far a touch event can start away from the badge.
*/
hitSlop?: ViewProps['hitSlop'];
/**
* width of border around the badge
*/
borderWidth?: number;
/**
* radius of border around the badge
*/
borderRadius?: number;
/**
* color of border around the badge
*/
borderColor?: ImageStyle['borderColor'];
/**
* Additional styles for the top container
*/
containerStyle?: StyleProp<ViewStyle>;
/**
* Additional styles for the badge label
*/
labelStyle?: StyleProp<TextStyle>;
/**
* Receives a number from 1 to 4, representing the label's max digit length.
* Beyond the max number for that digit length, a "+" will show at the end.
* If set to a value not included in LABEL_FORMATTER_VALUES, no formatting will occur.
* Example: labelLengthFormater={2}, label={124}, label will present "99+".
*/
labelFormatterLimit?: LabelFormatterValues;
/**
* Renders an icon badge
*/
icon?: ImageSourcePropType;
/**
* Additional styling to badge icon
*/
iconStyle?: object;
/**
* Additional props passed to icon
*/
iconProps?: object;
/**
* Custom element to render instead of an icon
*/
customElement?: JSX.Element;
key?: string | number;
};
/**
* @description: Round colored badge, typically used to show a number
* @extends: View
* @image: https://user-images.githubusercontent.com/33805983/34480753-df7a868a-efb6-11e7-9072-80f5c110a4f3.png
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/BadgesScreen.tsx
*/
class Badge extends PureComponent<BadgeProps> {
static displayName = 'Badge';
styles: ReturnType<typeof createStyles>;
constructor(props: BadgeProps) {
super(props);
this.styles = createStyles(props);
}
getAccessibilityProps() {
const {onPress, icon, label, accessibilityLabel} = this.props;
return {
accessibilityLabel: accessibilityLabel || label ? `${label} new items` : 'badge',
...extractAccessibilityProps(this.props),
accessible: !_.isUndefined(label),
accessibilityRole: onPress ? 'button' : icon ? 'image' : 'text'
};
}
get size() {
const {size, label} = this.props;
if (label === undefined && size === undefined) {
return 10; // Pimple badge size
}
return size || 20;
}
isSmallBadge() {
return this.size <= 16;
}
getBadgeSizeStyle() {
const {borderWidth, icon, customElement} = this.props;
const label = this.getFormattedLabel();
const style: any = {
paddingHorizontal: this.isSmallBadge() ? 4 : 6,
height: this.size,
minWidth: this.size
};
if (icon && label) {
style.paddingRight = 6;
style.paddingLeft = 4;
style.height = Spacings.s5;
if (borderWidth) {
style.height += borderWidth * 2;
}
return style;
}
if (customElement) {
return style;
}
const isPimple = label === undefined;
if (isPimple || icon) {
style.paddingHorizontal = 0;
style.minWidth = undefined;
style.width = style.height;
if (borderWidth) {
style.height += borderWidth * 2;
style.width += borderWidth * 2;
}
return style;
}
if (borderWidth) {
style.height += borderWidth * 2;
style.minWidth += borderWidth * 2;
}
return style;
}
getFormattedLabel() {
const {labelFormatterLimit, label} = this.props;
if (_.isNaN(label)) {
return label;
}
if (LABEL_FORMATTER_VALUES.includes(labelFormatterLimit!)) {
const maxLabelNumber: any = 10 ** labelFormatterLimit! - 1;
let formattedLabel: any = label;
if (formattedLabel > maxLabelNumber) {
formattedLabel = `${maxLabelNumber}+`;
}
return formattedLabel;
} else {
return label;
}
}
getBorderStyling() {
const {borderWidth, borderColor, borderRadius} = this.props;
const style: ViewStyle = {};
if (borderWidth) {
style.borderWidth = borderWidth;
style.borderColor = borderColor;
}
if (borderRadius) {
style.borderRadius = borderRadius;
}
return style;
}
renderLabel() {
const {labelStyle, label} = this.props;
if (label) {
return (
<Text
style={[this.styles.label, this.isSmallBadge() && this.styles.labelSmall, labelStyle]}
allowFontScaling={false}
numberOfLines={1}
testID="badge"
recorderTag={'unmask'}
>
{this.getFormattedLabel()}
</Text>
);
}
}
renderCustomElement() {
const {customElement} = this.props;
return customElement;
}
renderIcon() {
const {icon, iconStyle, iconProps, borderColor, label} = this.props;
const flex = label ? 0 : 1;
return (
icon && (
<Image
source={icon!}
resizeMode="contain"
tintColor={Colors.$iconDefaultLight}
//@ts-ignore
borderColor={borderColor}
{...iconProps}
style={{
flex,
...iconStyle
}}
/>
)
);
}
render() {
const {activeOpacity, backgroundColor, containerStyle, hitSlop, onPress, testID, ...others} = this.props;
const backgroundStyle = backgroundColor && {backgroundColor};
const sizeStyle = this.getBadgeSizeStyle();
const borderStyle = this.getBorderStyling();
const Container = onPress ? TouchableOpacity : View;
return (
// The extra View wrapper is to break badge's flex-ness
// @ts-ignore
<View
style={containerStyle}
{...others}
backgroundColor={undefined}
// @ts-expect-error
borderWidth={undefined}
{...this.getAccessibilityProps()}
>
<Container
testID={testID}
pointerEvents={'none'}
style={[sizeStyle, this.styles.badge, borderStyle, backgroundStyle]}
onPress={onPress}
activeOpacity={activeOpacity}
hitSlop={hitSlop}
row
>
{this.renderCustomElement()}
{this.renderIcon()}
{this.renderLabel()}
</Container>
</View>
);
}
}
function createStyles(props: BadgeProps) {
const styles = StyleSheet.create({
badge: {
alignSelf: 'flex-start',
borderRadius: BorderRadiuses.br100,
backgroundColor: !props.icon || props.customElement ? Colors.$backgroundGeneralHeavy : undefined,
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden'
},
label: {
...Typography.text90,
color: Colors.$textDefaultLight,
backgroundColor: 'transparent'
},
labelSmall: {
...Typography.text100,
lineHeight: undefined
}
});
return styles;
}
export {Badge}; // For tests
export default asBaseComponent<BadgeProps, typeof Badge>(Badge);