Skip to content

Commit 065a1ab

Browse files
authored
Support rendering prefix in TextField (#765)
1 parent 5aa2b5f commit 065a1ab

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

demo/src/screens/componentScreens/TextFieldScreen/BasicTextFieldScreen.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class BasicTextFieldScreen extends Component {
2727

2828
this.state = {
2929
hideUnderline: false,
30+
withPrefix: false,
3031
underlineColor: undefined,
3132
guidingText: GUIDING_TEXTS.none,
3233
disabled: false,
@@ -43,6 +44,7 @@ export default class BasicTextFieldScreen extends Component {
4344
render() {
4445
const {
4546
hideUnderline,
47+
withPrefix,
4648
underlineColor,
4749
guidingText,
4850
titleColor,
@@ -64,6 +66,7 @@ export default class BasicTextFieldScreen extends Component {
6466
placeholder={disabled ? 'Disabled' : 'Placeholder'}
6567
hideUnderline={hideUnderline}
6668
underlineColor={underlineColor}
69+
prefix={withPrefix ? 'prefix://' : undefined}
6770
title={guidingText === GUIDING_TEXTS.useTitle ? 'Title' : undefined}
6871
titleColor={titleColor}
6972
floatingPlaceholder={guidingText === GUIDING_TEXTS.floatingPlaceholder}
@@ -94,6 +97,7 @@ export default class BasicTextFieldScreen extends Component {
9497
{renderBooleanOption.call(this, 'Disabled', 'disabled')}
9598
{renderBooleanOption.call(this, 'Centered', 'centered')}
9699
{renderBooleanOption.call(this, 'Hide Underline', 'hideUnderline')}
100+
{renderBooleanOption.call(this, 'With Prefix', 'withPrefix')}
97101
{renderColorOption.call(this, 'Underline Color', 'underlineColor')}
98102
{renderRadioGroup.call(this, 'Guiding Text', 'guidingText', GUIDING_TEXTS)}
99103
{renderColorOption.call(this, 'Title Color', 'titleColor')}

src/components/inputs/TextField.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import PropTypes from 'prop-types';
99
import React from 'react';
1010
import {StyleSheet, Animated, TextInput as RNTextInput, Image as RNImage} from 'react-native';
1111
import {Constants} from '../../helpers';
12-
import {Colors, Typography} from '../../style';
12+
import {Colors, Typography, Spacings} from '../../style';
1313
import BaseInput from './BaseInput';
1414
import Modal from '../modal';
1515
import TextArea from './TextArea';
@@ -125,6 +125,14 @@ export default class TextField extends BaseInput {
125125
* transform function executed on value and return transformed value
126126
*/
127127
transformer: PropTypes.func,
128+
/**
129+
* Pass to render a prefix text as part of the input
130+
*/
131+
prefix: PropTypes.string,
132+
/**
133+
* The prefix style
134+
*/
135+
prefixStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
128136
/**
129137
* Fixed title that will displayed above the input (note: floatingPlaceholder MUST be 'false')
130138
*/
@@ -417,6 +425,14 @@ export default class TextField extends BaseInput {
417425
}
418426
}
419427

428+
renderPrefix() {
429+
const {prefix, prefixStyle} = this.props;
430+
if (prefix) {
431+
const typography = this.getTypography();
432+
return <Text style={[this.styles.prefix, typography, {lineHeight: undefined}, prefixStyle]}>{prefix}</Text>;
433+
}
434+
}
435+
420436
renderTitle() {
421437
const {floatingPlaceholder, title, titleColor, titleStyle} = this.getThemeProps();
422438
const color = this.getStateColor(titleColor || PLACEHOLDER_COLOR_BY_STATE);
@@ -525,7 +541,7 @@ export default class TextField extends BaseInput {
525541
style,
526542
placeholderTextColor,
527543
multiline,
528-
hideUnderline,
544+
// hideUnderline,
529545
numberOfLines,
530546
expandable,
531547
rightIconSource,
@@ -542,7 +558,7 @@ export default class TextField extends BaseInput {
542558
const inputStyle = [
543559
hasRightElement && this.styles.rightElement,
544560
this.styles.input,
545-
hideUnderline && this.styles.inputWithoutUnderline,
561+
// hideUnderline && this.styles.inputWithoutUnderline,
546562
{...typographyStyle},
547563
Constants.isAndroid && {lineHeight},
548564
expandable && {maxHeight: lineHeight * (Constants.isAndroid ? 3 : 3.3)},
@@ -627,6 +643,7 @@ export default class TextField extends BaseInput {
627643
{paddingTop: this.getTopPaddings()}
628644
]}
629645
>
646+
{this.renderPrefix()}
630647
{this.renderPlaceholder()}
631648
{expandable ? this.renderExpandableInput() : this.renderTextInput()}
632649
{this.renderRightButton()}
@@ -697,34 +714,40 @@ function createStyles({centered, multiline, expandable}) {
697714
flexDirection: 'row',
698715
justifyContent: centered ? 'center' : undefined,
699716
borderBottomWidth: 1,
700-
borderColor: Colors.dark70
717+
borderColor: Colors.dark70,
718+
paddingBottom: Constants.isIOS ? 10 : 5
701719
},
702720
innerContainerWithoutUnderline: {
703-
borderBottomWidth: 0
721+
borderBottomWidth: 0,
722+
paddingBottom: 0
704723
},
705724
input: {
706725
flexGrow: 1,
707726
textAlign: centered ? 'center' : inputTextAlign,
708727
backgroundColor: 'transparent',
709-
marginBottom: Constants.isIOS ? 10 : 5,
728+
// marginBottom: Constants.isIOS ? 10 : 5,
710729
padding: 0, // for Android
711730
textAlignVertical: 'top', // for Android
712731
borderColor: 'transparent', // borderColor & borderWidth is a fix for collapsing issue on Android
713-
borderWidth: 1 // for Android
732+
borderWidth: Constants.isAndroid ? 1 : undefined // for Android
714733
},
715734
expandableInput: {
716735
flexGrow: 1,
717736
flexDirection: 'row',
718737
alignItems: 'center'
719738
},
720-
inputWithoutUnderline: {
721-
marginBottom: undefined
722-
},
739+
// inputWithoutUnderline: {
740+
// marginBottom: undefined
741+
// },
723742
expandableModalContent: {
724743
flex: 1,
725744
paddingTop: 15,
726745
paddingHorizontal: 20
727746
},
747+
prefix: {
748+
color: Colors.grey30,
749+
marginRight: Spacings.s1
750+
},
728751
placeholder: {
729752
textAlign: 'left'
730753
},

0 commit comments

Comments
 (0)