-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFloatingLabel.js
165 lines (152 loc) · 4.94 KB
/
FloatingLabel.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
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
import React from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
Animated,
TouchableWithoutFeedback,
} from 'react-native'
import t from 'tcomb-form-native'
const Textbox = t.form.Textbox
class FloatingLabel extends Textbox {
constructor (props) {
super(props);
this.state = {
fieldFocused: (props.value) ? true : false,
value: (props.value) ? String(props.value) : undefined,
fadeAnim: (props.value) ? new Animated.Value(1) : new Animated.Value(0),
placeholderString: undefined,
};
}
getTemplate () {
let self = this
return function (locals) {
const stylesheet = locals.stylesheet;
let formGroupStyle = stylesheet.formGroup.normal;
let controlLabelStyle = stylesheet.controlLabel.normal;
let textboxStyle = stylesheet.textbox.normal;
let helpBlockStyle = stylesheet.helpBlock.normal;
let errorBlockStyle = stylesheet.errorBlock;
if (locals.hasError) {
controlLabelStyle = stylesheet.controlLabel.error;
formGroupStyle = stylesheet.formGroup.error;
textboxStyle = stylesheet.textbox.error;
helpBlockStyle = stylesheet.helpBlock.error;
}
if (locals.editable === false) {
textboxStyle = stylesheet.textbox.notEditable;
}
const help = locals.help ? <Text style={helpBlockStyle}>{locals.help}</Text> : null;
const error = locals.hasError && locals.error ? <Text style={errorBlockStyle}>{locals.error}</Text> : null;
const label =
<Animated.Text style={[controlLabelStyle, {
opacity: self.state.fadeAnim,
transform: [{
translateY: self.state.fadeAnim.interpolate({
inputRange: [0, 1],
outputRange: [10, 0]
}),
}]}]}>
{locals.label}
</Animated.Text>
const placeholderString = (self.state.fieldFocused) ? '' : self.state.placeholderString || locals.label;
return (
<TouchableWithoutFeedback onPress={() => {
if (locals.editable === false) {
return
}
self.refs.input.focus()
}}>
<View style={formGroupStyle}>
{label}
<TextInput
ref='input'
autoCapitalize={locals.autoCapitalize}
autoCorrect={locals.autoCorrect}
autoFocus={locals.autoFocus}
bufferDelay={locals.bufferDelay}
clearButtonMode={locals.clearButtonMode}
editable={locals.editable}
enablesReturnKeyAutomatically={locals.enablesReturnKeyAutomatically}
keyboardType={locals.keyboardType}
multiline={locals.multiline}
onBlur={self._onBlur.bind(self, locals)}
onEndEditing={locals.onEndEditing}
onFocus={self._onFocus.bind(self, locals)}
onSubmitEditing={locals.onSubmitEditing}
password={locals.password}
placeholderTextColor={(locals.hasError) ? locals.errorPlaceholderTextColor || 'red' : locals.placeholderTextColor || 'grey'}
returnKeyType={locals.returnKeyType}
selectTextOnFocus={locals.selectTextOnFocus}
secureTextEntry={locals.secureTextEntry}
selectionState={locals.selectionState}
onChangeText={(value) => {
locals.onChange(value)
self._onChangeText.bind(self, value, locals)
}}
onChange={locals.onChangeNative}
placeholder={placeholderString}
maxLength={locals.maxLength}
numberOfLines={locals.numberOfLines}
textAlign={locals.textAlign}
textAlignVertical={locals.textAlignVertical}
underlineColorAndroid={locals.underlineColorAndroid}
style={[styles.textInput, textboxStyle]}
value={locals.value}
/>
{help}
{error}
</View>
</TouchableWithoutFeedback>
)
}
}
_onChangeText (text, locals) {
this.setState({value: text});
}
_onFocus (locals) {
Animated.spring(
this.state.fadeAnim,
{toValue: 1, friction: 5},
).start();
this.setState({
fieldFocused: true,
placeholderString: '',
});
if (locals.onFocus) {
locals.onFocus();
}
}
_onBlur (locals) {
if (!this.state.value) {
Animated.timing(
this.state.fadeAnim,
{toValue: 0},
).start();
}
this.setState({
fieldFocused: false,
placeholderString: locals.label
});
if (locals.onBlur) {
locals.onBlur();
}
}
getLocals () {
let locals = super.getLocals();
[
'errorPlaceholderTextColor'
].forEach((name) => locals[name] = this.props.options[name]);
return locals;
}
shouldComponentUpdate (nextProps, nextState) {
return true;
}
}
const styles = StyleSheet.create({
textInput: {
height: 40,
}
});
export default FloatingLabel