Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ import { TextInput, type TextInputProps } from 'react-native-paper';
- **`left` / `right`** → **`startAccessory` / `endAccessory`**
- **`TextInput.Affix`** → **`prefix` / `suffix`**, or **`TextInput.Icon`**, or **`startAccessory` / `endAccessory`**

`startAccessory` and `endAccessory` follow the writing direction. In RTL, `startAccessory` sits on the right on every platform, while v5's `left` stayed on the left on web.

```tsx
// Before (v5)
<TextInput
Expand Down
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
22 changes: 14 additions & 8 deletions src/components/TextInput/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,17 @@ export const getFilledTextInputData = (
* Variant-specific styles
*/

const filledLabelOffset = hasAccessory
? FILLED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY;

const animatedLabelWrapperStyles: StyleProp<
AnimatedStyle<StyleProp<ViewStyle>>
> = [
filledStyles.labelWrapper,
{
left: hasAccessory
? FILLED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
},
Platform.OS === 'web' && shared.isRTL
? { right: filledLabelOffset }
: { start: filledLabelOffset },
animatedLabelWrapperStyle,
];

Expand Down Expand Up @@ -518,14 +520,18 @@ export const getOutlinedTextInputData = (
isDisabled && { opacity: OUTLINED_DISABLED_OUTLINE_OPACITY },
];

const outlinedLabelOffset = hasAccessory
? OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY;

const animatedLabelWrapperStyles: StyleProp<
AnimatedStyle<StyleProp<ViewStyle>>
> = [
outlinedStyles.labelWrapper,
{
left: hasAccessory
? OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY
: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
...(Platform.OS === 'web' && shared.isRTL
? { right: outlinedLabelOffset }
: { start: outlinedLabelOffset }),
backgroundColor: labelBackgroundColor,
},
animatedLabelWrapperStyle,
Expand Down
132 changes: 126 additions & 6 deletions src/components/__tests__/TextInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import {
I18nManager,
Platform,
StyleSheet,
TextInput as NativeTextInput,
View,
Expand All @@ -11,6 +12,8 @@ import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
jest,
Expand All @@ -21,6 +24,11 @@ import { act, fireEvent, render, screen, userEvent } from '../../test-utils';
import { LightTheme } from '../../theme/schemes';
import { tokens } from '../../theme/tokens';
import TextInput from '../TextInput';
import {
FILLED_LABEL_START_OFFSET_WITH_ACCESSORY,
LABEL_START_OFFSET_WITHOUT_ACCESSORY,
OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY,
} from '../TextInput/constants';
import type {
TextInputRenderProps,
TextInputHandles,
Expand Down Expand Up @@ -987,7 +995,7 @@ it('applies RTL writing direction to supporting text', async () => {
});
});

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

function StartAccessory() {
Expand All @@ -1010,9 +1018,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 +1172,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 before the input in both LTR and RTL (tree order)', async () => {
function LoneStartAccessory() {
return <View testID="lone-start-acc" />;
}
Expand Down Expand Up @@ -1199,9 +1207,121 @@ 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')
);
});

it('shows the error icon in RTL when only a StartAccessory is set', async () => {
I18nManager.isRTL = true;

function StartAccessory() {
return <View testID="rtl-error-start-acc" />;
}

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

expect(
screen.getByText('alert-circle', includeHiddenElements)
).toBeOnTheScreen();
});

it('keeps the label at the field edge in RTL when only an EndAccessory is set', async () => {
I18nManager.isRTL = true;

function EndAccessory() {
return <View testID="rtl-lone-end-acc" />;
}

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

expect(screen.getByText('Email', includeHiddenElements).parent).toHaveStyle({
start: LABEL_START_OFFSET_WITHOUT_ACCESSORY,
});
});

// React Native Web resolves `start` from a `dir` prop on its own Views, not
// from `<html dir>`, so on web the label takes its side from Paper's direction.
describe('on web in RTL', () => {
let platformOS: { restore(): void } | undefined;

beforeEach(() => {
platformOS = jest.replaceProperty(Platform, 'OS', 'web');
I18nManager.isRTL = true;
});

afterEach(() => {
platformOS?.restore();
});

it.each([
{
variant: 'filled' as const,
offset: FILLED_LABEL_START_OFFSET_WITH_ACCESSORY,
},
{
variant: 'outlined' as const,
offset: OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY,
},
])(
'places the $variant label next to the StartAccessory on the right',
async ({ variant, offset }) => {
function StartAccessory() {
return <View testID="web-rtl-start-acc" />;
}

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

const labelWrapper = screen.getByText(
'Email',
includeHiddenElements
).parent;
expect(labelWrapper).toHaveStyle({ right: offset });
expect(labelWrapper).not.toHaveStyle({ start: offset });
}
);

it('keeps the label on the right, clear of a lone EndAccessory', async () => {
function EndAccessory() {
return <View testID="web-rtl-end-acc" />;
}

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

expect(screen.getByText('Email', includeHiddenElements).parent).toHaveStyle(
{ right: LABEL_START_OFFSET_WITHOUT_ACCESSORY }
);
});
});

it('shows prefix and suffix when the field is floating and hides them after value is cleared while blurred', async () => {
Expand Down
14 changes: 7 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 @@ -584,7 +584,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = `
"position": "absolute",
},
{
"left": 52,
"start": 52,
},
{
"top": 16,
Expand Down Expand Up @@ -1000,7 +1000,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is
"position": "absolute",
},
{
"left": 52,
"start": 52,
},
{
"top": 16,
Expand Down Expand Up @@ -1452,7 +1452,7 @@ exports[`renders filled TextInput with label and value 1`] = `
"position": "absolute",
},
{
"left": 16,
"start": 16,
},
{
"top": 16,
Expand Down Expand Up @@ -1625,7 +1625,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 @@ -2021,7 +2021,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 @@ -2453,7 +2453,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