diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 5cade18fe3..492db10c46 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -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) { 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); @@ -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 = diff --git a/src/components/TextInput/utils.ts b/src/components/TextInput/utils.ts index 5900c86f8b..b37e3d8229 100644 --- a/src/components/TextInput/utils.ts +++ b/src/components/TextInput/utils.ts @@ -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> > = [ 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, ]; @@ -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> > = [ 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, diff --git a/src/components/__tests__/TextInput.test.tsx b/src/components/__tests__/TextInput.test.tsx index 20bb647ba1..a8153b136c 100644 --- a/src/components/__tests__/TextInput.test.tsx +++ b/src/components/__tests__/TextInput.test.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import { I18nManager, + Platform, StyleSheet, TextInput as NativeTextInput, View, @@ -11,6 +12,8 @@ import { afterAll, afterEach, beforeAll, + beforeEach, + describe, expect, it, jest, @@ -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, @@ -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() { @@ -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 () => { @@ -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 ; } @@ -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 ; + } + + await render( + {}} + 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 ; + } + + await render( + {}} + 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 ``, 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 ; + } + + await render( + {}} + 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 ; + } + + await render( + {}} + 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 () => { diff --git a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap index cdbe2843fe..a59930040f 100644 --- a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap @@ -127,7 +127,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "position": "absolute", }, { - "left": 52, + "start": 52, }, { "top": 16, @@ -584,7 +584,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "position": "absolute", }, { - "left": 52, + "start": 52, }, { "top": 16, @@ -1000,7 +1000,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "position": "absolute", }, { - "left": 52, + "start": 52, }, { "top": 16, @@ -1452,7 +1452,7 @@ exports[`renders filled TextInput with label and value 1`] = ` "position": "absolute", }, { - "left": 16, + "start": 16, }, { "top": 16, @@ -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, @@ -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, @@ -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,