Skip to content

Commit eab02b4

Browse files
committed
fix: textinput rtl for label, helper and accessories
1 parent f21a604 commit eab02b4

5 files changed

Lines changed: 124 additions & 23 deletions

File tree

‎src/components/TextInput/hooks.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {
332332

333333
const isControlled = props.value !== undefined;
334334
const isRTL = direction === 'rtl';
335-
const hasAccessory = isRTL ? !!props.endAccessory : !!props.startAccessory;
335+
const hasAccessory = !!props.startAccessory;
336336

337337
const { hasValue, inputLength, getHasText, onChangeText } =
338338
useTextInputInput(props);
@@ -430,12 +430,8 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {
430430

431431
const counterText = `${inputLength}/${props.maxLength}`;
432432

433-
const renderLeadingAccessory = flags.isRTL
434-
? props.endAccessory
435-
: props.startAccessory;
436-
const renderTrailingAccessory = flags.isRTL
437-
? props.startAccessory
438-
: props.endAccessory;
433+
const renderLeadingAccessory = props.startAccessory;
434+
const renderTrailingAccessory = props.endAccessory;
439435

440436
// https://github.com/facebook/react-native/issues/31573
441437
const placeholder =

‎src/components/TextInput/styles.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export const styles = StyleSheet.create({
6363
alignItems: 'center',
6464
marginStart: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
6565
},
66+
leadingAccessoryRTL: {
67+
marginStart: 0,
68+
marginEnd: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
69+
},
70+
trailingAccessoryRTL: {
71+
marginEnd: 0,
72+
marginStart: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
73+
},
6674
disabled: {
6775
opacity: tokens.md.sys.state.opacity.disabled,
6876
},

‎src/components/TextInput/utils.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export const getSharedTextInputStyleData = (
247247
styles.supportingText,
248248
{
249249
color: supportingTextColor,
250+
textAlign: isRTL && Platform.OS === 'web' ? 'right' : 'left',
250251
writingDirection: isRTL ? 'rtl' : 'ltr',
251252
},
252253
isDisabled && styles.disabled,
@@ -256,6 +257,7 @@ export const getSharedTextInputStyleData = (
256257
styles.counter,
257258
{
258259
color: supportingTextColor,
260+
textAlign: isRTL && Platform.OS === 'web' ? 'left' : 'right',
259261
writingDirection: isRTL ? 'rtl' : 'ltr',
260262
},
261263
isDisabled && styles.disabled,
@@ -283,11 +285,13 @@ export const getSharedTextInputStyleData = (
283285

284286
const leadingAccessoryStyles: StyleProp<ViewStyle> = [
285287
styles.leadingAccessory,
288+
isRTL && Platform.OS === 'web' && styles.leadingAccessoryRTL,
286289
isDisabled && styles.disabled,
287290
];
288291

289292
const trailingAccessoryStyles: StyleProp<ViewStyle> = [
290293
styles.trailingAccessory,
294+
isRTL && Platform.OS === 'web' && styles.trailingAccessoryRTL,
291295
isDisabled && styles.disabled,
292296
];
293297

@@ -353,12 +357,15 @@ export const getFilledTextInputData = (
353357
* Variant-specific styles
354358
*/
355359

360+
const labelInlineProp =
361+
shared.isRTL && Platform.OS === 'web' ? 'end' : 'start';
362+
356363
const animatedLabelWrapperStyles: StyleProp<
357364
AnimatedStyle<StyleProp<ViewStyle>>
358365
> = [
359366
filledStyles.labelWrapper,
360367
{
361-
left: hasAccessory
368+
[labelInlineProp]: hasAccessory
362369
? FILLED_LABEL_START_OFFSET_WITH_ACCESSORY
363370
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
364371
},
@@ -518,12 +525,15 @@ export const getOutlinedTextInputData = (
518525
isDisabled && { opacity: OUTLINED_DISABLED_OUTLINE_OPACITY },
519526
];
520527

528+
const labelInlineProp =
529+
shared.isRTL && Platform.OS === 'web' ? 'end' : 'start';
530+
521531
const animatedLabelWrapperStyles: StyleProp<
522532
AnimatedStyle<StyleProp<ViewStyle>>
523533
> = [
524534
outlinedStyles.labelWrapper,
525535
{
526-
left: hasAccessory
536+
[labelInlineProp]: hasAccessory
527537
? OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY
528538
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
529539
backgroundColor: labelBackgroundColor,

‎src/components/__tests__/TextInput.test.tsx‎

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ it('applies RTL text alignment and writing direction to the TextInput (outlined)
970970
});
971971
});
972972

973-
it('applies RTL writing direction to supporting text', async () => {
973+
it('applies RTL writing direction and start alignment to supporting text on native', async () => {
974974
I18nManager.isRTL = true;
975975

976976
await render(
@@ -983,11 +983,88 @@ it('applies RTL writing direction to supporting text', async () => {
983983
);
984984

985985
expect(screen.getByText('Hint', includeHiddenElements)).toHaveStyle({
986+
textAlign: 'left',
987+
writingDirection: 'rtl',
988+
});
989+
});
990+
991+
it('aligns the counter to the end side in RTL on native', async () => {
992+
I18nManager.isRTL = true;
993+
994+
await render(
995+
<TextInput
996+
label="Email"
997+
value="ab"
998+
maxLength={10}
999+
counter
1000+
onChangeText={() => {}}
1001+
/>
1002+
);
1003+
1004+
expect(screen.getByText('2/10', includeHiddenElements)).toHaveStyle({
1005+
textAlign: 'right',
9861006
writingDirection: 'rtl',
9871007
});
9881008
});
9891009

990-
it('places EndAccessory before StartAccessory in the tree when RTL', async () => {
1010+
it('places the label on the right side when RTL', async () => {
1011+
I18nManager.isRTL = true;
1012+
1013+
await render(<TextInput label="Email" value="" onChangeText={() => {}} />);
1014+
1015+
const labelStyle = StyleSheet.flatten(
1016+
// eslint-disable-next-line no-restricted-syntax -- label wrapper has no accessible handle.
1017+
screen.getByText('Email', includeHiddenElements).parent?.props.style
1018+
);
1019+
expect(labelStyle).toHaveProperty('start');
1020+
});
1021+
1022+
it('places the helper on the right side when RTL', async () => {
1023+
I18nManager.isRTL = true;
1024+
1025+
await render(
1026+
<TextInput
1027+
label="Email"
1028+
value=""
1029+
supportingText="Helper"
1030+
onChangeText={() => {}}
1031+
/>
1032+
);
1033+
1034+
expect(screen.getByText('Helper', includeHiddenElements)).toHaveStyle({
1035+
textAlign: 'left',
1036+
writingDirection: 'rtl',
1037+
});
1038+
});
1039+
1040+
it('places the accessories on the right side when RTL', async () => {
1041+
I18nManager.isRTL = true;
1042+
1043+
await render(
1044+
<TextInput
1045+
label="Email"
1046+
value=""
1047+
onChangeText={() => {}}
1048+
startAccessory={({ style }) => (
1049+
<View testID="rtl-leading" style={style} />
1050+
)}
1051+
endAccessory={({ style }) => <View testID="rtl-trailing" style={style} />}
1052+
/>
1053+
);
1054+
1055+
const leadingStyle = StyleSheet.flatten(
1056+
// eslint-disable-next-line no-restricted-syntax -- reading resolved style.
1057+
screen.getByTestId('rtl-leading').props.style
1058+
);
1059+
const trailingStyle = StyleSheet.flatten(
1060+
// eslint-disable-next-line no-restricted-syntax -- reading resolved style.
1061+
screen.getByTestId('rtl-trailing').props.style
1062+
);
1063+
expect(leadingStyle).toHaveProperty('marginStart');
1064+
expect(trailingStyle).toHaveProperty('marginEnd');
1065+
});
1066+
1067+
it('keeps StartAccessory before EndAccessory in the tree when RTL (the row mirrors)', async () => {
9911068
I18nManager.isRTL = true;
9921069

9931070
function StartAccessory() {
@@ -1010,9 +1087,9 @@ it('places EndAccessory before StartAccessory in the tree when RTL', async () =>
10101087
);
10111088

10121089
const tree = toJSON();
1013-
expect(firstIndexOfTestIdInTree(tree, 'rtl-acc-from-end-prop')).toBeLessThan(
1090+
expect(
10141091
firstIndexOfTestIdInTree(tree, 'rtl-acc-from-start-prop')
1015-
);
1092+
).toBeLessThan(firstIndexOfTestIdInTree(tree, 'rtl-acc-from-end-prop'));
10161093
});
10171094

10181095
it('places StartAccessory before EndAccessory in the tree when LTR', async () => {
@@ -1164,7 +1241,7 @@ it('does not expose the placeholder string again after the TextInput loses focus
11641241
expect(screen.getByTestId('tf-input').props.placeholder).toBe(' ');
11651242
});
11661243

1167-
it('maps a lone StartAccessory to leading in LTR and trailing in RTL (tree order)', async () => {
1244+
it('keeps a lone StartAccessory in the leading slot in LTR and RTL (tree order)', async () => {
11681245
function LoneStartAccessory() {
11691246
return <View testID="lone-start-acc" />;
11701247
}
@@ -1199,8 +1276,8 @@ it('maps a lone StartAccessory to leading in LTR and trailing in RTL (tree order
11991276
);
12001277

12011278
const rtlTree = toJsonRtl();
1202-
expect(firstIndexOfTestIdInTree(rtlTree, 'tf-lone-rtl')).toBeLessThan(
1203-
firstIndexOfTestIdInTree(rtlTree, 'lone-start-acc')
1279+
expect(firstIndexOfTestIdInTree(rtlTree, 'lone-start-acc')).toBeLessThan(
1280+
firstIndexOfTestIdInTree(rtlTree, 'tf-lone-rtl')
12041281
);
12051282
});
12061283

‎src/components/__tests__/__snapshots__/TextInput.test.tsx.snap‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
127127
"position": "absolute",
128128
},
129129
{
130-
"left": 52,
130+
"start": 52,
131131
},
132132
{
133133
"top": 16,
@@ -197,6 +197,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
197197
"marginStart": 12,
198198
"width": 24,
199199
},
200+
false,
200201
{
201202
"opacity": 0.38,
202203
},
@@ -364,6 +365,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
364365
"marginEnd": 12,
365366
"width": 24,
366367
},
368+
false,
367369
{
368370
"opacity": 0.38,
369371
},
@@ -584,7 +586,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
584586
"position": "absolute",
585587
},
586588
{
587-
"left": 52,
589+
"start": 52,
588590
},
589591
{
590592
"top": 16,
@@ -653,6 +655,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
653655
"width": 24,
654656
},
655657
false,
658+
false,
656659
null,
657660
]
658661
}
@@ -813,6 +816,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
813816
"width": 24,
814817
},
815818
false,
819+
false,
816820
null,
817821
]
818822
}
@@ -1000,7 +1004,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
10001004
"position": "absolute",
10011005
},
10021006
{
1003-
"left": 52,
1007+
"start": 52,
10041008
},
10051009
{
10061010
"top": 16,
@@ -1069,6 +1073,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
10691073
"width": 24,
10701074
},
10711075
false,
1076+
false,
10721077
null,
10731078
]
10741079
}
@@ -1229,6 +1234,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
12291234
"width": 24,
12301235
},
12311236
false,
1237+
false,
12321238
null,
12331239
]
12341240
}
@@ -1452,7 +1458,7 @@ exports[`renders filled TextInput with label and value 1`] = `
14521458
"position": "absolute",
14531459
},
14541460
{
1455-
"left": 16,
1461+
"start": 16,
14561462
},
14571463
{
14581464
"top": 16,
@@ -1625,7 +1631,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
16251631
},
16261632
{
16271633
"backgroundColor": "rgba(254, 247, 255, 1)",
1628-
"left": 48,
1634+
"start": 48,
16291635
},
16301636
{
16311637
"top": -12,
@@ -1699,6 +1705,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
16991705
"width": 24,
17001706
},
17011707
false,
1708+
false,
17021709
null,
17031710
]
17041711
}
@@ -1859,6 +1866,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
18591866
"width": 24,
18601867
},
18611868
false,
1869+
false,
18621870
null,
18631871
]
18641872
}
@@ -2021,7 +2029,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
20212029
},
20222030
{
20232031
"backgroundColor": "rgba(254, 247, 255, 1)",
2024-
"left": 48,
2032+
"start": 48,
20252033
},
20262034
{
20272035
"top": -12,
@@ -2095,6 +2103,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
20952103
"width": 24,
20962104
},
20972105
false,
2106+
false,
20982107
null,
20992108
]
21002109
}
@@ -2255,6 +2264,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
22552264
"width": 24,
22562265
},
22572266
false,
2267+
false,
22582268
null,
22592269
]
22602270
}
@@ -2453,7 +2463,7 @@ exports[`renders outlined TextInput with label and value 1`] = `
24532463
},
24542464
{
24552465
"backgroundColor": "rgba(254, 247, 255, 1)",
2456-
"left": 16,
2466+
"start": 16,
24572467
},
24582468
{
24592469
"top": -12,

0 commit comments

Comments
 (0)