Skip to content

Commit a973af0

Browse files
fix: add support for textAlign: center on TextInput (callstack#2369)
1 parent eda72db commit a973af0

File tree

6 files changed

+402
-4
lines changed

6 files changed

+402
-4
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ CHANGELOG.md
6262

6363
# generated by bob
6464
lib/
65+
66+
.expo

Diff for: example/src/Examples/TextInputExample.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,23 @@ const TextInputExample = () => {
417417
Error: Only letters are allowed
418418
</HelperText>
419419
</View>
420+
<View style={styles.inputContainerStyle}>
421+
<TextInput
422+
label="Input with text align center"
423+
style={{
424+
textAlign: 'center',
425+
}}
426+
/>
427+
</View>
428+
<View style={styles.inputContainerStyle}>
429+
<TextInput
430+
mode="outlined"
431+
label="Outlined input with text align center"
432+
style={{
433+
textAlign: 'center',
434+
}}
435+
/>
436+
</View>
420437
</ScrollView>
421438
</TextInputAvoidingView>
422439
);

Diff for: src/components/TextInput/TextInputFlat.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class TextInputFlat extends React.Component<ChildTextInputProps> {
9393
fontWeight,
9494
height,
9595
paddingHorizontal,
96+
textAlign,
9697
...viewStyle
9798
} = (StyleSheet.flatten(style) || {}) as TextStyle;
9899
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
@@ -350,6 +351,11 @@ class TextInputFlat extends React.Component<ChildTextInputProps> {
350351
fontWeight,
351352
color: inputTextColor,
352353
textAlignVertical: multiline ? 'top' : 'center',
354+
textAlign: textAlign
355+
? textAlign
356+
: I18nManager.isRTL
357+
? 'right'
358+
: 'left',
353359
},
354360
adornmentStyleAdjustmentForNativeInput,
355361
],
@@ -415,7 +421,6 @@ const styles = StyleSheet.create({
415421
input: {
416422
flexGrow: 1,
417423
margin: 0,
418-
textAlign: I18nManager.isRTL ? 'right' : 'left',
419424
zIndex: 1,
420425
},
421426
inputFlat: {

Diff for: src/components/TextInput/TextInputOutlined.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class TextInputOutlined extends React.Component<ChildTextInputProps> {
9292
fontWeight,
9393
height,
9494
backgroundColor = colors.background,
95+
textAlign,
9596
...viewStyle
9697
} = (StyleSheet.flatten(style) || {}) as TextStyle;
9798
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
@@ -329,6 +330,11 @@ class TextInputOutlined extends React.Component<ChildTextInputProps> {
329330
fontWeight,
330331
color: inputTextColor,
331332
textAlignVertical: multiline ? 'top' : 'center',
333+
textAlign: textAlign
334+
? textAlign
335+
: I18nManager.isRTL
336+
? 'right'
337+
: 'left',
332338
},
333339
adornmentStyleAdjustmentForNativeInput,
334340
],
@@ -390,7 +396,6 @@ const styles = StyleSheet.create({
390396
flexGrow: 1,
391397
paddingHorizontal: INPUT_PADDING_HORIZONTAL,
392398
margin: 0,
393-
textAlign: I18nManager.isRTL ? 'right' : 'left',
394399
zIndex: 1,
395400
},
396401
inputOutlined: {

Diff for: src/components/__tests__/TextInput.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,28 @@ it('correctly renders left-side icon adornment, and right-side affix adornment '
6060
expect(() => getByTestId('left-affix-adornment')).not.toThrow();
6161
expect(toJSON()).toMatchSnapshot();
6262
});
63+
64+
it('correctly applies default textAlign based on default RTL', () => {
65+
const { toJSON } = render(
66+
<TextInput
67+
label="Flat input"
68+
placeholder="Type something"
69+
value={'Some test value'}
70+
/>
71+
);
72+
73+
expect(toJSON()).toMatchSnapshot();
74+
});
75+
76+
it('correctly applies textAlign center', () => {
77+
const { toJSON } = render(
78+
<TextInput
79+
label="Flat input"
80+
placeholder="Type something"
81+
value={'Some test value'}
82+
style={{ textAlign: 'center' }}
83+
/>
84+
);
85+
86+
expect(toJSON()).toMatchSnapshot();
87+
});

0 commit comments

Comments
 (0)