2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { View, StyleSheet, Text, TextInput, ViewPropTypes, Platform } from 'react-native';
|
2018-03-29 17:55:37 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2018-04-01 00:45:15 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { COLOR_DANGER, COLOR_TEXT } from '../constants/colors';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
inputContainer: {
|
2018-04-24 19:34:03 +00:00
|
|
|
marginBottom: 15
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
|
|
|
label: {
|
2018-04-24 19:34:03 +00:00
|
|
|
marginBottom: 10,
|
|
|
|
color: COLOR_TEXT,
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: '700'
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
|
|
|
input: {
|
2018-04-24 19:34:03 +00:00
|
|
|
fontSize: 14,
|
2018-03-29 17:55:37 +00:00
|
|
|
paddingTop: 12,
|
|
|
|
paddingBottom: 12,
|
2018-04-24 19:34:03 +00:00
|
|
|
// paddingTop: 5,
|
|
|
|
// paddingBottom: 5,
|
2018-03-29 17:55:37 +00:00
|
|
|
paddingHorizontal: 10,
|
|
|
|
borderWidth: 2,
|
2018-04-24 19:34:03 +00:00
|
|
|
borderRadius: 4,
|
2018-03-29 17:55:37 +00:00
|
|
|
backgroundColor: 'white',
|
|
|
|
borderColor: 'rgba(0,0,0,.15)',
|
|
|
|
color: 'black'
|
|
|
|
},
|
|
|
|
labelError: {
|
|
|
|
color: COLOR_DANGER
|
|
|
|
},
|
|
|
|
inputError: {
|
|
|
|
color: COLOR_DANGER,
|
|
|
|
borderColor: COLOR_DANGER
|
2018-04-01 00:45:15 +00:00
|
|
|
},
|
|
|
|
wrap: {
|
|
|
|
position: 'relative'
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
position: 'absolute',
|
2018-04-24 19:34:03 +00:00
|
|
|
color: 'rgba(0,0,0,.45)',
|
|
|
|
height: 45,
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
...Platform.select({
|
|
|
|
ios: {
|
|
|
|
padding: 12
|
|
|
|
},
|
|
|
|
android: {
|
|
|
|
paddingHorizontal: 12,
|
|
|
|
paddingTop: 18,
|
|
|
|
paddingBottom: 6
|
|
|
|
}
|
|
|
|
})
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-01 00:45:15 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
export default class RCTextInput extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
label: PropTypes.string,
|
|
|
|
error: PropTypes.object,
|
2018-04-24 19:34:03 +00:00
|
|
|
secureTextEntry: PropTypes.bool,
|
|
|
|
containerStyle: ViewPropTypes.style,
|
|
|
|
inputStyle: PropTypes.object,
|
|
|
|
inputRef: PropTypes.func
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
static defaultProps = {
|
|
|
|
error: {}
|
|
|
|
}
|
2018-04-01 00:45:15 +00:00
|
|
|
state = {
|
|
|
|
showPassword: false
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
icon = ({
|
|
|
|
name,
|
|
|
|
onPress,
|
|
|
|
style,
|
|
|
|
testID
|
|
|
|
}) => <Icon name={name} style={[styles.icon, style]} size={20} onPress={onPress} testID={testID} />
|
2018-04-01 00:45:15 +00:00
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
iconLeft = name => this.icon({
|
|
|
|
name,
|
|
|
|
onPress: null,
|
|
|
|
style: { left: 0 },
|
|
|
|
testID: this.props.testID ? `${ this.props.testID }-icon-left` : null
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
iconPassword = name => this.icon({
|
|
|
|
name,
|
|
|
|
onPress: () => this.tooglePassword(),
|
|
|
|
style: { right: 0 },
|
|
|
|
testID: this.props.testID ? `${ this.props.testID }-icon-right` : null
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
tooglePassword = () => this.setState({ showPassword: !this.state.showPassword });
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2018-05-23 13:39:18 +00:00
|
|
|
label, error, secureTextEntry, containerStyle, inputRef, iconLeft, inputStyle, testID, placeholder, ...inputProps
|
2018-03-29 17:55:37 +00:00
|
|
|
} = this.props;
|
2018-04-01 00:45:15 +00:00
|
|
|
const { showPassword } = this.state;
|
2018-03-29 17:55:37 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={[styles.inputContainer, containerStyle]}>
|
2018-05-23 13:39:18 +00:00
|
|
|
{label && <Text contentDescription={null} accessibilityLabel={null} style={[styles.label, error.error && styles.labelError]}>{label}</Text> }
|
2018-04-01 00:45:15 +00:00
|
|
|
<View style={styles.wrap}>
|
|
|
|
<TextInput
|
2018-04-24 19:34:03 +00:00
|
|
|
style={[
|
|
|
|
styles.input,
|
|
|
|
error.error && styles.inputError,
|
|
|
|
inputStyle,
|
|
|
|
iconLeft && { paddingLeft: 40 },
|
|
|
|
secureTextEntry && { paddingRight: 40 }
|
|
|
|
]}
|
|
|
|
ref={inputRef}
|
2018-04-01 00:45:15 +00:00
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
secureTextEntry={secureTextEntry && !showPassword}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={testID}
|
|
|
|
accessibilityLabel={placeholder}
|
|
|
|
placeholder={placeholder}
|
|
|
|
contentDescription={placeholder}
|
2018-04-01 00:45:15 +00:00
|
|
|
{...inputProps}
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
{iconLeft && this.iconLeft(iconLeft)}
|
|
|
|
{secureTextEntry && this.iconPassword(showPassword ? 'eye-off' : 'eye')}
|
2018-04-01 00:45:15 +00:00
|
|
|
</View>
|
2018-03-29 17:55:37 +00:00
|
|
|
{error.error && <Text style={sharedStyles.error}>{error.reason}</Text>}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|