2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
2019-12-04 16:39:53 +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-04 16:39:53 +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';
|
2020-02-11 14:01:35 +00:00
|
|
|
import ActivityIndicator from './ActivityIndicator';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2019-12-04 16:39:53 +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-04 16:39:53 +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-04 16:39:53 +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,
|
2020-02-11 14:01:35 +00:00
|
|
|
loading: PropTypes.bool,
|
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,
|
2020-05-08 17:36:10 +00:00
|
|
|
iconRight: PropTypes.string,
|
2019-12-04 16:39:53 +00:00
|
|
|
placeholder: PropTypes.string,
|
2020-03-30 20:19:01 +00:00
|
|
|
left: PropTypes.element,
|
2020-05-08 17:36:10 +00:00
|
|
|
onIconRightPress: PropTypes.func,
|
2019-12-04 16:39:53 +00:00
|
|
|
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-04 16:39:53 +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-04 16:39:53 +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-04 16:39:53 +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
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
get iconRight() {
|
|
|
|
const { iconRight, onIconRightPress, theme } = this.props;
|
|
|
|
return (
|
|
|
|
<BorderlessButton onPress={onIconRightPress} style={[styles.iconContainer, styles.iconRight]}>
|
|
|
|
<CustomIcon
|
|
|
|
name={iconRight}
|
|
|
|
style={{ color: themes[theme].bodyText }}
|
|
|
|
size={20}
|
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
get iconPassword() {
|
|
|
|
const { showPassword } = this.state;
|
2019-12-04 16:39:53 +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-04 16:39:53 +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
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
get loading() {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <ActivityIndicator style={[styles.iconContainer, styles.iconRight, { color: themes[theme].bodyText }]} />;
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-05-08 17:36:10 +00:00
|
|
|
label, left, error, loading, secureTextEntry, containerStyle, inputRef, iconLeft, iconRight, inputStyle, testID, placeholder, theme, ...inputProps
|
2018-03-29 17:55:37 +00:00
|
|
|
} = this.props;
|
2019-12-04 16:39:53 +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-04 16:39:53 +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,
|
2018-11-14 21:42:03 +00:00
|
|
|
iconLeft && styles.inputIconLeft,
|
2020-05-08 17:36:10 +00:00
|
|
|
(secureTextEntry || iconRight) && styles.inputIconRight,
|
2019-12-04 16:39:53 +00:00
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].backgroundColor,
|
|
|
|
borderColor: themes[theme].separatorColor,
|
|
|
|
color: themes[theme].titleText
|
|
|
|
},
|
2020-02-11 14:01:35 +00:00
|
|
|
error.error && {
|
|
|
|
color: dangerColor,
|
|
|
|
borderColor: dangerColor
|
|
|
|
},
|
2019-12-04 16:39:53 +00:00
|
|
|
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-04 16:39:53 +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}
|
2020-05-08 17:36:10 +00:00
|
|
|
{iconRight ? this.iconRight : null}
|
2018-11-14 21:42:03 +00:00
|
|
|
{secureTextEntry ? this.iconPassword : null}
|
2020-02-11 14:01:35 +00:00
|
|
|
{loading ? this.loading : null}
|
2020-03-30 20:19:01 +00:00
|
|
|
{left}
|
2018-04-01 00:45:15 +00:00
|
|
|
</View>
|
2020-02-11 14:01:35 +00:00
|
|
|
{error && error.reason ? <Text style={[styles.error, { color: dangerColor }]}>{error.reason}</Text> : null}
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|