Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 3 additions & 7 deletions src/components/TextInput/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => {

const isControlled = props.value !== undefined;
const isRTL = direction === 'rtl';
const hasAccessory = isRTL ? !!props.endAccessory : !!props.startAccessory;
const hasAccessory = !!props.startAccessory;

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

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

const renderLeadingAccessory = flags.isRTL
? props.endAccessory
: props.startAccessory;
const renderTrailingAccessory = flags.isRTL
? props.startAccessory
: props.endAccessory;
const renderLeadingAccessory = props.startAccessory;
const renderTrailingAccessory = props.endAccessory;

// https://github.com/facebook/react-native/issues/31573
const placeholder =
Expand Down
8 changes: 8 additions & 0 deletions src/components/TextInput/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ export const styles = StyleSheet.create({
alignItems: 'center',
marginStart: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
},
leadingAccessoryRTL: {
marginStart: 0,
marginEnd: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
},
trailingAccessoryRTL: {
marginEnd: 0,
marginStart: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL,
},
disabled: {
opacity: tokens.md.sys.state.opacity.disabled,
},
Expand Down
14 changes: 12 additions & 2 deletions src/components/TextInput/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const getSharedTextInputStyleData = (
styles.supportingText,
{
color: supportingTextColor,
textAlign: isRTL && Platform.OS === 'web' ? 'right' : 'left',
writingDirection: isRTL ? 'rtl' : 'ltr',
},
isDisabled && styles.disabled,
Expand All @@ -256,6 +257,7 @@ export const getSharedTextInputStyleData = (
styles.counter,
{
color: supportingTextColor,
textAlign: isRTL && Platform.OS === 'web' ? 'left' : 'right',
writingDirection: isRTL ? 'rtl' : 'ltr',
},
isDisabled && styles.disabled,
Expand Down Expand Up @@ -283,11 +285,13 @@ export const getSharedTextInputStyleData = (

const leadingAccessoryStyles: StyleProp<ViewStyle> = [
styles.leadingAccessory,
isRTL && Platform.OS === 'web' && styles.leadingAccessoryRTL,
isDisabled && styles.disabled,
];

const trailingAccessoryStyles: StyleProp<ViewStyle> = [
styles.trailingAccessory,
isRTL && Platform.OS === 'web' && styles.trailingAccessoryRTL,
isDisabled && styles.disabled,
];

Expand Down Expand Up @@ -353,12 +357,15 @@ export const getFilledTextInputData = (
* Variant-specific styles
*/

const labelInlineProp =
shared.isRTL && Platform.OS === 'web' ? 'end' : 'start';

const animatedLabelWrapperStyles: StyleProp<
AnimatedStyle<StyleProp<ViewStyle>>
> = [
filledStyles.labelWrapper,
{
left: hasAccessory
[labelInlineProp]: hasAccessory
? FILLED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
},
Expand Down Expand Up @@ -518,12 +525,15 @@ export const getOutlinedTextInputData = (
isDisabled && { opacity: OUTLINED_DISABLED_OUTLINE_OPACITY },
];

const labelInlineProp =
shared.isRTL && Platform.OS === 'web' ? 'end' : 'start';

const animatedLabelWrapperStyles: StyleProp<
AnimatedStyle<StyleProp<ViewStyle>>
> = [
outlinedStyles.labelWrapper,
{
left: hasAccessory
[labelInlineProp]: hasAccessory
? OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
backgroundColor: labelBackgroundColor,
Expand Down
91 changes: 84 additions & 7 deletions src/components/__tests__/TextInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ it('applies RTL text alignment and writing direction to the TextInput (outlined)
});
});

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

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

expect(screen.getByText('Hint', includeHiddenElements)).toHaveStyle({
textAlign: 'left',
writingDirection: 'rtl',
});
});

it('aligns the counter to the end side in RTL on native', async () => {
I18nManager.isRTL = true;

await render(
<TextInput
label="Email"
value="ab"
maxLength={10}
counter
onChangeText={() => {}}
/>
);

expect(screen.getByText('2/10', includeHiddenElements)).toHaveStyle({
textAlign: 'right',
writingDirection: 'rtl',
});
});

it('places EndAccessory before StartAccessory in the tree when RTL', async () => {
it('places the label on the right side when RTL', async () => {
I18nManager.isRTL = true;

await render(<TextInput label="Email" value="" onChangeText={() => {}} />);

const labelStyle = StyleSheet.flatten(
// eslint-disable-next-line no-restricted-syntax -- label wrapper has no accessible handle.
screen.getByText('Email', includeHiddenElements).parent?.props.style
);
expect(labelStyle).toHaveProperty('start');
});

it('places the helper on the right side when RTL', async () => {
I18nManager.isRTL = true;

await render(
<TextInput
label="Email"
value=""
supportingText="Helper"
onChangeText={() => {}}
/>
);

expect(screen.getByText('Helper', includeHiddenElements)).toHaveStyle({
textAlign: 'left',
writingDirection: 'rtl',
});
});

it('places the accessories on the right side when RTL', async () => {
I18nManager.isRTL = true;

await render(
<TextInput
label="Email"
value=""
onChangeText={() => {}}
startAccessory={({ style }) => (
<View testID="rtl-leading" style={style} />
)}
endAccessory={({ style }) => <View testID="rtl-trailing" style={style} />}
/>
);

const leadingStyle = StyleSheet.flatten(
// eslint-disable-next-line no-restricted-syntax -- reading resolved style.
screen.getByTestId('rtl-leading').props.style
);
const trailingStyle = StyleSheet.flatten(
// eslint-disable-next-line no-restricted-syntax -- reading resolved style.
screen.getByTestId('rtl-trailing').props.style
);
expect(leadingStyle).toHaveProperty('marginStart');
expect(trailingStyle).toHaveProperty('marginEnd');
});

it('keeps StartAccessory before EndAccessory in the tree when RTL (the row mirrors)', async () => {
I18nManager.isRTL = true;

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

const tree = toJSON();
expect(firstIndexOfTestIdInTree(tree, 'rtl-acc-from-end-prop')).toBeLessThan(
expect(
firstIndexOfTestIdInTree(tree, 'rtl-acc-from-start-prop')
);
).toBeLessThan(firstIndexOfTestIdInTree(tree, 'rtl-acc-from-end-prop'));
});

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

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

const rtlTree = toJsonRtl();
expect(firstIndexOfTestIdInTree(rtlTree, 'tf-lone-rtl')).toBeLessThan(
firstIndexOfTestIdInTree(rtlTree, 'lone-start-acc')
expect(firstIndexOfTestIdInTree(rtlTree, 'lone-start-acc')).toBeLessThan(
firstIndexOfTestIdInTree(rtlTree, 'tf-lone-rtl')
);
});

Expand Down
24 changes: 17 additions & 7 deletions src/components/__tests__/__snapshots__/TextInput.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
"position": "absolute",
},
{
"left": 52,
"start": 52,
},
{
"top": 16,
Expand Down Expand Up @@ -197,6 +197,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
"marginStart": 12,
"width": 24,
},
false,
{
"opacity": 0.38,
},
Expand Down Expand Up @@ -364,6 +365,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once
"marginEnd": 12,
"width": 24,
},
false,
{
"opacity": 0.38,
},
Expand Down Expand Up @@ -584,7 +586,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
"position": "absolute",
},
{
"left": 52,
"start": 52,
},
{
"top": 16,
Expand Down Expand Up @@ -653,6 +655,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -813,6 +816,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -1000,7 +1004,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
"position": "absolute",
},
{
"left": 52,
"start": 52,
},
{
"top": 16,
Expand Down Expand Up @@ -1069,6 +1073,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -1229,6 +1234,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -1452,7 +1458,7 @@ exports[`renders filled TextInput with label and value 1`] = `
"position": "absolute",
},
{
"left": 16,
"start": 16,
},
{
"top": 16,
Expand Down Expand Up @@ -1625,7 +1631,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
},
{
"backgroundColor": "rgba(254, 247, 255, 1)",
"left": 48,
"start": 48,
},
{
"top": -12,
Expand Down Expand Up @@ -1699,6 +1705,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -1859,6 +1866,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = `
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -2021,7 +2029,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
},
{
"backgroundColor": "rgba(254, 247, 255, 1)",
"left": 48,
"start": 48,
},
{
"top": -12,
Expand Down Expand Up @@ -2095,6 +2103,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -2255,6 +2264,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i
"width": 24,
},
false,
false,
null,
]
}
Expand Down Expand Up @@ -2453,7 +2463,7 @@ exports[`renders outlined TextInput with label and value 1`] = `
},
{
"backgroundColor": "rgba(254, 247, 255, 1)",
"left": 16,
"start": 16,
},
{
"top": -12,
Expand Down
Loading