-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathFloatingLabel.js
More file actions
98 lines (94 loc) · 2.48 KB
/
FloatingLabel.js
File metadata and controls
98 lines (94 loc) · 2.48 KB
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
'use strict';
import React, {Component} from "react";
import PropTypes from 'prop-types';
import {StyleSheet, Animated, View, ViewPropTypes} from "react-native";
export default class FloatingLabel extends Component {
constructor(props: Object) {
super(props);
if(props.dense) {
this.posTop = 12;
this.posBottom = 32;
this.fontLarge = 13;
this.fontSmall = 13;
} else {
this.posTop = ( props.posTop != null ) ? props.posTop : 16;
this.posBottom = ( props.posBottom != null ) ? props.posBottom : 37;
this.fontLarge = 16;
this.fontSmall = 12;
}
let posTop = (props.hasValue) ? this.posTop : this.posBottom;
let fontSize = (props.hasValue) ? this.fontSmall : this.fontLarge;
this.state = {
top: new Animated.Value(posTop),
fontSize: new Animated.Value(fontSize)
};
}
shouldComponentUpdate(nextProps: Object, nextState: Object) : bool {
return (this.props.hasValue !== nextProps.hasValue) ? false : true;
}
floatLabel() {
Animated.parallel([
Animated.timing(this.state.top, {
toValue: this.posTop,
duration: this.props.duration
}),
Animated.timing(this.state.fontSize, {
toValue: this.fontSmall,
duration: this.props.duration
})
]).start();
}
sinkLabel() {
Animated.parallel([
Animated.timing(this.state.top, {
toValue: this.posBottom,
duration: this.props.duration
}),
Animated.timing(this.state.fontSize, {
toValue: this.fontLarge,
duration: this.props.duration
})
]).start();
}
render() : Object {
let {
label,
labelColor,
highlightColor,
style
} = this.props;
return (
<Animated.Text
style={[{
fontSize: this.state.fontSize,
top: this.state.top,
color: labelColor
}, styles.labelText, this.props.isFocused && {
color: highlightColor
}, style]}
onPress={this.props.focusHandler}
>
{label}
</Animated.Text>
);
}
}
FloatingLabel.propTypes = {
duration: PropTypes.number,
label: PropTypes.string,
labelColor: PropTypes.string,
highlightColor: PropTypes.string,
dense: PropTypes.bool,
style: PropTypes.oneOfType([
PropTypes.object,
ViewPropTypes.style
]),
focusHandler: PropTypes.func.isRequired
};
const styles = StyleSheet.create({
labelText: {
position: 'absolute',
left: 0,
backgroundColor: 'rgba(0,0,0,0)'
}
});