Skip to content

TextField - Right Icon #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions demo/src/screens/componentScreens/InputsScreen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, {Component} from 'react';
import {ScrollView, StyleSheet} from 'react-native';
import {ScrollView, StyleSheet, Alert} from 'react-native';
import {Colors, Typography, View, Text, TextField, TextArea, Modal, Button} from 'react-native-ui-lib'; //eslint-disable-line
import {KeyboardAwareInsetsView} from 'react-native-keyboard-tracking-view';
import icon from '../../assets/icons/richText.png';
import dropDown from '../../assets/icons/chevronDown.png';


const richText = require('../../assets/icons/richText.png');
const dropDown = require('../../assets/icons/chevronDown.png');
const star = require('../../assets/icons/star.png');

const LONG_TEXT =
'Concept, edition and design direction for the editorial piece “La Forma Bruta” by the photographer' +
'Martín Bollati. In this piece';
Expand Down Expand Up @@ -37,7 +39,7 @@ export default class InputsScreen extends Component {
}

onPressInfo = () => {
console.warn('onPressInfo');
Alert.alert('Info button press');
}

onChangeText = (text) => {
Expand All @@ -60,6 +62,7 @@ export default class InputsScreen extends Component {
<View flex>
<ScrollView
contentContainerStyle={styles.container}
showsVerticalScrollIndicator={false}
keyboardShouldPersistTaps="always"
getTextInputRefs={() => [this.noUnderline, this.hugeText]}
>
Expand All @@ -78,7 +81,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="floatingPlaceholder & error"
placeholder="FloatingPlaceholder & error"
onChangeText={this.onChangeText}
error={this.state.error}
useTopErrors={this.state.topError}
Expand All @@ -102,7 +105,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="& helperText"
placeholder="With helperText"
helperText="this is an helper text"
onChangeText={this.onChangeText}
error={this.state.error}
Expand All @@ -113,7 +116,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="multiline & helperText"
placeholder="Multiline & helperText"
multiline
helperText="this is an helper text"
/>
Expand All @@ -122,7 +125,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
title="title"
placeholder="character counter & error"
placeholder="Character counter & error"
maxLength={3}
showCharacterCounter
onChangeText={this.onChangeText}
Expand All @@ -135,7 +138,7 @@ export default class InputsScreen extends Component {
containerStyle={{marginBottom: INPUT_SPACING}}
title="Title"
titleStyle={{fontSize: Typography.text70.fontSize}}
placeholder="multiline & titleStyle"
placeholder="Multiline & titleStyle"
multiline
maxLength={32}
showCharacterCounter
Expand All @@ -149,7 +152,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="character counter & expandable"
placeholder="Character counter & expandable"
expandable
maxLength={20}
showCharacterCounter
Expand All @@ -161,7 +164,7 @@ export default class InputsScreen extends Component {
floatingPlaceholder
placeholderTextColor={Colors.cyan30}
floatingPlaceholderColor={Colors.cyan30}
placeholder="underline colors & error"
placeholder="Underline colors & error"
onChangeText={this.onChangeText}
error={this.state.error}
useTopErrors={this.state.topError}
Expand All @@ -171,14 +174,14 @@ export default class InputsScreen extends Component {
<TextField
text40
containerStyle={{marginBottom: INPUT_SPACING}}
placeholder="write something.."
placeholder="Write something.."
hideUnderline
/>

<TextField
text30
containerStyle={{marginBottom: INPUT_SPACING}}
placeholder="write something.."
placeholder="Write something.."
centered
hideUnderline
/>
Expand Down Expand Up @@ -234,7 +237,7 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="with price transformer"
placeholder="With price transformer"
value={this.state.value}
transformer={transformPrice}
/>
Expand All @@ -243,24 +246,32 @@ export default class InputsScreen extends Component {
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="Right button"
rightButtonProps={{iconSource: icon, onPress: this.onPressInfo}}
placeholder="With right button"
rightButtonProps={{iconSource: richText, onPress: this.onPressInfo}}
/>

<TextField
text70
containerStyle={{marginBottom: INPUT_SPACING, width: 210}}
floatingPlaceholder
placeholder="Multiline & Right button"
placeholder="Multiline & right button"
multiline
rightButtonProps={{iconSource: icon, onPress: this.onPressInfo, style: {tintColor: Colors.red30}}}
rightButtonProps={{iconSource: richText, onPress: this.onPressInfo, iconColor: Colors.red30}}
/>

<TextField
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="Expandable & rightIconSource"
placeholder="With right icon"
rightIconSource={star}
/>

<TextField
text70
containerStyle={{marginBottom: INPUT_SPACING}}
floatingPlaceholder
placeholder="Expandable & right icon"
expandable
rightIconSource={dropDown}
/>
Expand All @@ -275,7 +286,7 @@ export default class InputsScreen extends Component {
borderColor: Colors.dark60,
}}
>
<TextArea placeholder="write something.."/>
<TextArea placeholder="Write something.."/>
</View>

<TextField
Expand Down
45 changes: 33 additions & 12 deletions src/components/inputs/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default class TextField extends BaseInput {
*/
rightButtonProps: PropTypes.shape({
iconSource: PropTypes.number,
iconColor: PropTypes.string,
onPress: PropTypes.func,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.number])
})
Expand Down Expand Up @@ -433,7 +434,7 @@ export default class TextField extends BaseInput {
}

renderExpandableInput() {
const {renderExpandableInput, rightIconSource, testID} = this.getThemeProps();
const {renderExpandableInput, testID} = this.getThemeProps();

if (_.isFunction(renderExpandableInput)) {
return renderExpandableInput(this.getThemeProps());
Expand All @@ -447,7 +448,6 @@ export default class TextField extends BaseInput {
testID={`${testID}.expandable`}
>
{this.renderTextInput()}
{rightIconSource && <Image pointerEvents="none" source={rightIconSource} style={this.styles.rightIcon}/>}
</TouchableOpacity>
);
}
Expand All @@ -465,13 +465,15 @@ export default class TextField extends BaseInput {
numberOfLines,
helperText,
expandable,
rightIconSource,
...others
} = this.getThemeProps();
const typography = this.getTypography();
const {lineHeight, ...typographyStyle} = typography;
const color = this.getStateColor(this.props.color || this.extractColorValue());
const hasRightElement = this.shouldDisplayRightButton() || rightIconSource;
const inputStyle = [
this.shouldDisplayRightButton() && {paddingRight: (ICON_SIZE + ICON_RIGHT_PADDING + ICON_LEFT_PADDING)},
hasRightElement && this.styles.rightElement,
this.styles.input,
hideUnderline && this.styles.inputWithoutUnderline,
{...typographyStyle},
Expand Down Expand Up @@ -509,23 +511,35 @@ export default class TextField extends BaseInput {
}

renderRightButton() {
const {rightButtonProps} = this.getThemeProps();
const tintColor = _.get(rightButtonProps, 'style.tintColor', Colors.blue30);

if (this.shouldDisplayRightButton()) {
const {rightButtonProps} = this.getThemeProps();
const {style, iconSource, iconColor} = rightButtonProps;

return (
<TouchableOpacity style={[this.styles.rightButton, rightButtonProps.style]} onPress={this.onPressRightButton}>
<TouchableOpacity style={[this.styles.rightButton, style]} onPress={this.onPressRightButton}>
<Image
pointerEvents="none"
source={rightButtonProps.iconSource}
source={iconSource}
resizeMode={'contain'}
style={[this.styles.rightButtonImage, {tintColor}]}
style={[this.styles.rightButtonImage, {tintColor: iconColor || Colors.blue30}]}
/>
</TouchableOpacity>
);
}
}

renderRightIcon() {
const {rightIconSource} = this.getThemeProps();

if (rightIconSource) {
return (
<View style={this.styles.rightIcon} pointerEvents="none">
<Image source={rightIconSource} resizeMode={'center'} style={this.styles.rightButtonImage}/>
</View>
);
}
}

render() {
const {expandable, containerStyle, underlineColor, useTopErrors, hideUnderline} = this.getThemeProps();
const underlineStateColor = this.getStateColor(underlineColor, true);
Expand All @@ -545,6 +559,7 @@ export default class TextField extends BaseInput {
{this.renderPlaceholder()}
{expandable ? this.renderExpandableInput() : this.renderTextInput()}
{this.renderRightButton()}
{this.renderRightIcon()}
{expandable && this.renderExpandableModal()}
</View>

Expand Down Expand Up @@ -600,8 +615,9 @@ export default class TextField extends BaseInput {
}
}

function createStyles({placeholderTextColor, centered, multiline}) {
function createStyles({placeholderTextColor, centered, multiline, expandable}) {
const inputTextAlign = (Constants.isRTL ? 'right' : 'left');

return StyleSheet.create({
container: {
},
Expand Down Expand Up @@ -661,9 +677,14 @@ function createStyles({placeholderTextColor, centered, multiline}) {
...LABEL_TYPOGRAPHY,
height: LABEL_TYPOGRAPHY.lineHeight
},
rightElement: {
paddingRight: ICON_SIZE + ICON_RIGHT_PADDING + ICON_LEFT_PADDING
},
rightIcon: {
alignSelf: 'center',
marginLeft: 8
position: 'absolute',
right: ICON_RIGHT_PADDING,
alignSelf: 'flex-end',
paddingBottom: expandable ? 14 : 8
},
rightButton: {
position: 'absolute',
Expand Down