2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
2018-09-25 19:28:42 +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,
|
|
|
|
paddingHorizontal: 10,
|
2018-08-10 17:26:36 +00:00
|
|
|
borderWidth: 1.5,
|
|
|
|
borderRadius: 2,
|
2018-03-29 17:55:37 +00:00
|
|
|
backgroundColor: 'white',
|
2018-08-10 17:26:36 +00:00
|
|
|
borderColor: '#E7EBF2',
|
2018-03-29 17:55:37 +00:00
|
|
|
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,
|
2018-07-10 13:40:32 +00:00
|
|
|
inputRef: PropTypes.func,
|
|
|
|
testID: PropTypes.string,
|
|
|
|
iconLeft: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
static defaultProps = {
|
|
|
|
error: {}
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
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-09-25 19:28:42 +00:00
|
|
|
iconLeft = (name) => {
|
|
|
|
const { testID } = this.props;
|
|
|
|
return this.icon({
|
|
|
|
name,
|
|
|
|
onPress: null,
|
|
|
|
style: { left: 0 },
|
|
|
|
testID: testID ? `${ testID }-icon-left` : null
|
|
|
|
});
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
iconPassword = (name) => {
|
|
|
|
const { testID } = this.props;
|
|
|
|
return this.icon({
|
|
|
|
name,
|
|
|
|
onPress: () => this.tooglePassword(),
|
|
|
|
style: { right: 0 },
|
|
|
|
testID: testID ? `${ testID }-icon-right` : null
|
|
|
|
});
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
tooglePassword = () => {
|
|
|
|
const { showPassword } = this.state;
|
|
|
|
this.setState({ showPassword: !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-06-13 01:33:00 +00:00
|
|
|
{label ? <Text contentDescription={null} accessibilityLabel={null} style={[styles.label, error.error && styles.labelError]}>{label}</Text> : null }
|
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-06-13 01:33:00 +00:00
|
|
|
{iconLeft ? this.iconLeft(iconLeft) : null}
|
|
|
|
{secureTextEntry ? this.iconPassword(showPassword ? 'eye-off' : 'eye') : null}
|
2018-04-01 00:45:15 +00:00
|
|
|
</View>
|
2018-06-13 01:33:00 +00:00
|
|
|
{error.error ? <Text style={sharedStyles.error}>{error.reason}</Text> : null}
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|