forked from ptomasroos/react-native-tab-navigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Badge.js
74 lines (62 loc) · 1.46 KB
/
Badge.js
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
'use strict';
import React from 'react';
import {
StyleSheet,
Text,
} from 'react-native';
import Layout from './Layout';
export default class Badge extends React.Component {
static propTypes = Text.propTypes;
constructor(props, context) {
super(props, context);
this._handleLayout = this._handleLayout.bind(this);
}
state = {
computedSize: null,
};
render() {
let { computedSize } = this.state;
let style = {};
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) {
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',
},
});