2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { View, StyleSheet, Text } from 'react-native';
|
2018-03-29 17:55:37 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
2018-04-01 00:45:15 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
2019-12-11 23:01:12 +00:00
|
|
|
import TextInput from '../presentation/TextInput';
|
|
|
|
import { themes } from '../constants/colors';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2019-12-11 23:01:12 +00:00
|
|
|
error: {
|
|
|
|
textAlign: 'center',
|
|
|
|
paddingTop: 5
|
|
|
|
},
|
2018-03-29 17:55:37 +00:00
|
|
|
inputContainer: {
|
2018-11-14 21:42:03 +00:00
|
|
|
marginBottom: 10
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
|
|
|
label: {
|
2018-04-24 19:34:03 +00:00
|
|
|
marginBottom: 10,
|
|
|
|
fontSize: 14,
|
2019-12-11 23:01:12 +00:00
|
|
|
...sharedStyles.textSemibold
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
|
|
|
input: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...sharedStyles.textRegular,
|
|
|
|
height: 48,
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 16,
|
2018-11-14 21:42:03 +00:00
|
|
|
paddingLeft: 14,
|
|
|
|
paddingRight: 14,
|
2019-12-11 23:01:12 +00:00
|
|
|
borderWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderRadius: 2
|
2018-11-14 21:42:03 +00:00
|
|
|
},
|
|
|
|
inputIconLeft: {
|
|
|
|
paddingLeft: 45
|
|
|
|
},
|
|
|
|
inputIconRight: {
|
|
|
|
paddingRight: 45
|
2018-03-29 17:55:37 +00:00
|
|
|
},
|
2018-04-01 00:45:15 +00:00
|
|
|
wrap: {
|
|
|
|
position: 'relative'
|
|
|
|
},
|
2018-11-14 21:42:03 +00:00
|
|
|
iconContainer: {
|
2018-04-01 00:45:15 +00:00
|
|
|
position: 'absolute',
|
2018-11-14 21:42:03 +00:00
|
|
|
top: 14
|
|
|
|
},
|
|
|
|
iconLeft: {
|
|
|
|
left: 15
|
|
|
|
},
|
|
|
|
iconRight: {
|
|
|
|
right: 15
|
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,
|
2019-05-22 20:15:35 +00:00
|
|
|
containerStyle: PropTypes.any,
|
2018-04-24 19:34:03 +00:00
|
|
|
inputStyle: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
inputRef: PropTypes.func,
|
|
|
|
testID: PropTypes.string,
|
|
|
|
iconLeft: PropTypes.string,
|
2019-12-11 23:01:12 +00:00
|
|
|
placeholder: PropTypes.string,
|
|
|
|
theme: 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 = {
|
2019-12-11 23:01:12 +00:00
|
|
|
error: {},
|
|
|
|
theme: 'light'
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-01 00:45:15 +00:00
|
|
|
state = {
|
|
|
|
showPassword: false
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
get iconLeft() {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { testID, iconLeft, theme } = this.props;
|
2018-11-14 21:42:03 +00:00
|
|
|
return (
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon
|
|
|
|
name={iconLeft}
|
2018-11-14 21:42:03 +00:00
|
|
|
testID={testID ? `${ testID }-icon-left` : null}
|
2019-12-11 23:01:12 +00:00
|
|
|
style={[styles.iconContainer, styles.iconLeft, { color: themes[theme].bodyText }]}
|
2019-03-01 16:49:11 +00:00
|
|
|
size={20}
|
2018-11-14 21:42:03 +00:00
|
|
|
/>
|
|
|
|
);
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
get iconPassword() {
|
|
|
|
const { showPassword } = this.state;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { testID, theme } = this.props;
|
2018-11-14 21:42:03 +00:00
|
|
|
return (
|
|
|
|
<BorderlessButton onPress={this.tooglePassword} style={[styles.iconContainer, styles.iconRight]}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon
|
|
|
|
name={showPassword ? 'Eye' : 'eye-off'}
|
2018-11-14 21:42:03 +00:00
|
|
|
testID={testID ? `${ testID }-icon-right` : null}
|
2019-12-11 23:01:12 +00:00
|
|
|
style={{ color: themes[theme].auxiliaryText }}
|
2019-03-01 16:49:11 +00:00
|
|
|
size={20}
|
2018-11-14 21:42:03 +00:00
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
tooglePassword = () => {
|
2018-11-14 21:42:03 +00:00
|
|
|
this.setState(prevState => ({ showPassword: !prevState.showPassword }));
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
render() {
|
2018-11-14 21:42:03 +00:00
|
|
|
const { showPassword } = this.state;
|
2018-03-29 17:55:37 +00:00
|
|
|
const {
|
2019-12-11 23:01:12 +00:00
|
|
|
label, error, secureTextEntry, containerStyle, inputRef, iconLeft, inputStyle, testID, placeholder, theme, ...inputProps
|
2018-03-29 17:55:37 +00:00
|
|
|
} = this.props;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { dangerColor } = themes[theme];
|
2018-03-29 17:55:37 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={[styles.inputContainer, containerStyle]}>
|
2019-12-11 23:01:12 +00:00
|
|
|
{label ? (
|
|
|
|
<Text
|
|
|
|
contentDescription={null}
|
|
|
|
accessibilityLabel={null}
|
|
|
|
style={[
|
|
|
|
styles.label,
|
|
|
|
{ color: themes[theme].titleText },
|
|
|
|
error.error && { color: dangerColor }
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{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,
|
2019-12-11 23:01:12 +00:00
|
|
|
error.error && {
|
|
|
|
color: dangerColor,
|
|
|
|
borderColor: dangerColor
|
|
|
|
},
|
2018-11-14 21:42:03 +00:00
|
|
|
iconLeft && styles.inputIconLeft,
|
2019-12-11 23:01:12 +00:00
|
|
|
secureTextEntry && styles.inputIconRight,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].backgroundColor,
|
|
|
|
borderColor: themes[theme].separatorColor,
|
|
|
|
color: themes[theme].titleText
|
|
|
|
},
|
|
|
|
inputStyle
|
2018-04-24 19:34:03 +00:00
|
|
|
]}
|
|
|
|
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}
|
2019-12-11 23:01:12 +00:00
|
|
|
theme={theme}
|
2018-04-01 00:45:15 +00:00
|
|
|
{...inputProps}
|
|
|
|
/>
|
2018-11-14 21:42:03 +00:00
|
|
|
{iconLeft ? this.iconLeft : null}
|
|
|
|
{secureTextEntry ? this.iconPassword : null}
|
2018-04-01 00:45:15 +00:00
|
|
|
</View>
|
2019-12-11 23:01:12 +00:00
|
|
|
{error.error ? <Text style={[styles.error, { color: dangerColor }]}>{error.reason}</Text> : null}
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|