Skip to content

Commit 4cbb7a2

Browse files
Fix/text field right icon alignment (#997)
* remove duplicated style * TextField - fix right icon/button alignment * fix right icon alignment * lint fix * fix right icon position when 'useTopError' and 'floatingPlaceholder' are true * adding examples * moving method up Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 5554e43 commit 4cbb7a2

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,26 +215,37 @@ export default class InputsScreen extends Component {
215215
<TextField
216216
text70
217217
containerStyle={{marginBottom: INPUT_SPACING}}
218-
floatingPlaceholder
219-
placeholder="With right button"
220-
rightButtonProps={{iconSource: richText, onPress: this.onPressInfo, accessibilityLabel: 'TextField Info'}}
218+
title="Title"
219+
placeholder="With right icon"
220+
rightIconSource={star}
221+
useTopErrors
222+
validate={'required'}
223+
errorMessage="Please fill this input"
221224
/>
222225

223226
<TextField
224227
text70
225-
containerStyle={{marginBottom: INPUT_SPACING, width: 210}}
228+
containerStyle={{marginBottom: INPUT_SPACING}}
226229
floatingPlaceholder
227-
placeholder="Multiline & right button"
228-
multiline
229-
rightButtonProps={{iconSource: richText, onPress: this.onPressInfo, iconColor: Colors.red30}}
230+
placeholder="With right button"
231+
rightButtonProps={{
232+
iconSource: richText,
233+
onPress: this.onPressInfo,
234+
accessibilityLabel: 'TextField Info',
235+
iconColor: Colors.red30
236+
}}
237+
useTopErrors
238+
validate={'required'}
239+
errorMessage="Please fill this input"
230240
/>
231241

232242
<TextField
233243
text70
234-
containerStyle={{marginBottom: INPUT_SPACING}}
244+
containerStyle={{marginBottom: INPUT_SPACING, width: 210}}
235245
floatingPlaceholder
236-
placeholder="With right icon"
237-
rightIconSource={star}
246+
placeholder="Multiline & right button"
247+
multiline
248+
rightButtonProps={{iconSource: richText, onPress: this.onPressInfo}}
238249
/>
239250

240251
<TextField

src/components/textField/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ export default class TextField extends BaseInput {
340340
return this.shouldFakePlaceholder() ? (this.shouldShowTopError() ? undefined : 25) : undefined;
341341
}
342342

343+
getTopErrorsPosition() {
344+
return !this.props.title && this.shouldShowTopError() ? {top: Constants.isIOS ? -25 : -27} : undefined;
345+
}
346+
343347
isDisabled() {
344348
return this.props.editable === false;
345349
}
@@ -397,6 +401,11 @@ export default class TextField extends BaseInput {
397401
return !expandable && rightButtonProps && rightButtonProps.iconSource;
398402
}
399403

404+
shouldRenderTitle() {
405+
const {floatingPlaceholder, title} = this.getThemeProps();
406+
return !floatingPlaceholder && title;
407+
}
408+
400409
onPressRightButton = () => {
401410
_.invoke(this.props, 'rightButtonProps.onPress');
402411
};
@@ -419,6 +428,7 @@ export default class TextField extends BaseInput {
419428
onLayout={this.onPlaceholderLayout}
420429
style={[
421430
this.styles.placeholder,
431+
this.getTopErrorsPosition(),
422432
typography,
423433
// TODO: we need to exclude completely any dependency on line height
424434
// in this component since it always breaks alignments
@@ -467,10 +477,10 @@ export default class TextField extends BaseInput {
467477
}
468478

469479
renderTitle() {
470-
const {floatingPlaceholder, title, titleColor, titleStyle} = this.getThemeProps();
480+
const {title, titleColor, titleStyle} = this.getThemeProps();
471481
const color = this.getStateColor(titleColor || PLACEHOLDER_COLOR_BY_STATE);
472482

473-
if (!floatingPlaceholder && title) {
483+
if (this.shouldRenderTitle()) {
474484
return <Text style={[{color}, this.styles.topLabel, this.styles.label, titleStyle]}>{title}</Text>;
475485
}
476486
}
@@ -636,7 +646,7 @@ export default class TextField extends BaseInput {
636646
return (
637647
<TouchableOpacity
638648
{...others} accessibilityLabel={accessibilityLabel}
639-
style={[this.styles.rightButton, style]} onPress={this.onPressRightButton}
649+
style={[this.styles.rightButton, this.getTopErrorsPosition(), style]} onPress={this.onPressRightButton}
640650
>
641651
<Image
642652
pointerEvents="none"
@@ -654,7 +664,7 @@ export default class TextField extends BaseInput {
654664

655665
if (rightIconSource) {
656666
return (
657-
<View style={this.styles.rightIcon} pointerEvents="none">
667+
<View style={[this.styles.rightButton, this.getTopErrorsPosition()]} pointerEvents="none">
658668
<Image source={rightIconSource} resizeMode={'center'} style={[this.styles.rightButtonImage, rightIconStyle]}/>
659669
</View>
660670
);
@@ -739,7 +749,7 @@ export default class TextField extends BaseInput {
739749
};
740750
}
741751

742-
function createStyles({centered, multiline, hideUnderline}, rightItemTopPadding = 0) {
752+
function createStyles({centered, multiline, title, floatingPlaceholder}, rightItemTopPadding = 0) {
743753
const itemTopPadding = Constants.isIOS ? (rightItemTopPadding - 3) : (rightItemTopPadding - 1);
744754

745755
return StyleSheet.create({
@@ -805,12 +815,6 @@ function createStyles({centered, multiline, hideUnderline}, rightItemTopPadding
805815
rightElement: {
806816
paddingRight: ICON_SIZE + ICON_LEFT_PADDING
807817
},
808-
rightIcon: {
809-
position: 'absolute',
810-
right: 0,
811-
alignSelf: 'flex-start',
812-
paddingTop: itemTopPadding
813-
},
814818
rightButton: {
815819
position: 'absolute',
816820
right: 0,

0 commit comments

Comments
 (0)