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 1 commit
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
46 changes: 28 additions & 18 deletions demo/src/screens/componentScreens/InputsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import React, {Component} from 'react';
import {ScrollView, StyleSheet} 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 @@ -78,7 +80,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 +104,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 +115,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 +124,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 +137,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 +151,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 +163,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 +173,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 +236,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 +245,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, style: {tintColor: 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 +285,7 @@ export default class InputsScreen extends Component {
borderColor: Colors.dark60,
}}
>
<TextArea placeholder="write something.."/>
<TextArea placeholder="Write something.."/>
</View>

<TextField
Expand Down
43 changes: 32 additions & 11 deletions src/components/inputs/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,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 +447,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 +464,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,15 +510,17 @@ 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 tintColor = _.get(rightButtonProps, 'style.tintColor', Colors.blue30);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is kinda weird.
first, accessing to style as an object is not so great, cause it can be a number or an array
second, tintColor is an image style prop, not a button.
maybe to add an explicit prop to rightButtonProps for tintColor or iconColor

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

const style = _.get(rightButtonProps, 'style');
const iconSource = _.get(rightButtonProps, 'iconSource');

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}]}
/>
Expand All @@ -526,6 +529,18 @@ export default class TextField extends BaseInput {
}
}

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

if (rightIconSource) {
return (
<View style={this.styles.rightIcon}>
<Image pointerEvents="none" 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 +560,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,7 +616,7 @@ export default class TextField extends BaseInput {
}
}

function createStyles({placeholderTextColor, centered, multiline}) {
function createStyles({placeholderTextColor, centered, multiline, expandable}) {
return StyleSheet.create({
container: {
},
Expand Down Expand Up @@ -659,9 +675,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