forked from ptomasroos/react-native-tab-navigator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBadge.tsx
86 lines (73 loc) · 1.66 KB
/
Badge.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
'use strict';
import React from 'react';
import {
LayoutChangeEvent,
StyleSheet,
Text,
TextProps,
ViewStyle,
} from 'react-native';
import Layout from './Layout';
type IBadgeState = {
computedSize: {
width: number,
height: number,
},
}
export default class Badge extends React.Component<TextProps, IBadgeState> {
constructor(props: TextProps) {
super(props);
this._handleLayout = this._handleLayout.bind(this);
this.state = {
computedSize: {
width: 0,
height: 0,
},
};
}
render() {
let { computedSize } = this.state;
let style:ViewStyle = {};
if (!computedSize) {
style.opacity = 0;
} else {
style.width = Math.max(computedSize?.height, computedSize?.width);
}
return (
<Text
{...this.props}
numberOfLines={1}
onLayout={this._handleLayout}
style={[styles.container, this.props.style, style]}>
{this.props.children}
</Text>
);
}
_handleLayout(event: LayoutChangeEvent) {
let { width, height } = event.nativeEvent.layout;
let { computedSize } = this.state;
if (computedSize && computedSize.height === height &&
computedSize.width === width) {
return;
}
this.setState({
computedSize: { width, height },
});
if (this.props.onLayout) {
this.props.onLayout(event);
}
}
}
let styles = StyleSheet.create({
container: {
fontSize: 12,
color: '#fff',
backgroundColor: 'rgb(0, 122, 255)',
lineHeight: 15,
textAlign: 'center',
borderWidth: 1 + Layout.pixel,
borderColor: '#fefefe',
borderRadius: 17 / 2,
overflow: 'hidden',
},
});