-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLabel.tsx
71 lines (62 loc) · 1.65 KB
/
Label.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
import React, { useCallback, useState, useMemo } from "react";
import { StyleSheet, Text, View, LayoutChangeEvent } from "react-native";
import { LabelProps } from "./types";
const MIN_HEIGHT = 30;
export default function Label({
position,
selected,
style,
textStyle,
}: LabelProps) {
const [height, setHeight] = useState(30);
const onLayout = useCallback((event: LayoutChangeEvent) => {
const height = Math.max(event.nativeEvent.layout.height, MIN_HEIGHT);
setHeight(height);
}, []);
const containerStyles = useMemo(() => {
return [styles.container, { height: height }];
}, [height]);
const labelStyles = useMemo(
() => [styles.inner, selected && styles.selected, style],
[styles, selected, style]
);
const textStyles = useMemo(() => [styles.text, textStyle], [textStyle]);
if (typeof position?.value === "undefined") {
return <></>;
}
return (
<View>
<View style={containerStyles}>
<View style={labelStyles} onLayout={onLayout}>
<Text style={textStyles}>{position.value}</Text>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
// The container positions the label above the marker thumb.
container: {
marginBottom: 8,
height: MIN_HEIGHT,
alignItems: "center",
justifyContent: "center",
},
// The inner container allows the label to grow with the text
inner: {
position: "absolute",
top: 0,
paddingVertical: 7,
paddingHorizontal: 12,
backgroundColor: "#f1f1f1",
},
selected: {
borderWidth: 2,
paddingVertical: 5,
paddingHorizontal: 10,
borderColor: "#888",
},
text: {
fontSize: 12,
},
});