diff --git a/src/components/TextInput/hooks.ts b/src/components/TextInput/hooks.ts index c6109e4ce7..a2d466d43e 100644 --- a/src/components/TextInput/hooks.ts +++ b/src/components/TextInput/hooks.ts @@ -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); @@ -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/styles.ts b/src/components/TextInput/styles.ts index 054728248f..49f07a8924 100644 --- a/src/components/TextInput/styles.ts +++ b/src/components/TextInput/styles.ts @@ -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, }, diff --git a/src/components/TextInput/utils.ts b/src/components/TextInput/utils.ts index 5900c86f8b..142818a620 100644 --- a/src/components/TextInput/utils.ts +++ b/src/components/TextInput/utils.ts @@ -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, @@ -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, @@ -283,11 +285,13 @@ export const getSharedTextInputStyleData = ( const leadingAccessoryStyles: StyleProp = [ styles.leadingAccessory, + isRTL && Platform.OS === 'web' && styles.leadingAccessoryRTL, isDisabled && styles.disabled, ]; const trailingAccessoryStyles: StyleProp = [ styles.trailingAccessory, + isRTL && Platform.OS === 'web' && styles.trailingAccessoryRTL, isDisabled && styles.disabled, ]; @@ -353,12 +357,15 @@ export const getFilledTextInputData = ( * Variant-specific styles */ + const labelInlineProp = + shared.isRTL && Platform.OS === 'web' ? 'end' : 'start'; + const animatedLabelWrapperStyles: StyleProp< AnimatedStyle> > = [ filledStyles.labelWrapper, { - left: hasAccessory + [labelInlineProp]: hasAccessory ? FILLED_LABEL_START_OFFSET_WITH_ACCESSORY : LABEL_START_OFFSET_WITHOUT_ACCESSORY, }, @@ -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> > = [ outlinedStyles.labelWrapper, { - left: hasAccessory + [labelInlineProp]: hasAccessory ? OUTLINED_LABEL_START_OFFSET_WITH_ACCESSORY : LABEL_START_OFFSET_WITHOUT_ACCESSORY, backgroundColor: labelBackgroundColor, diff --git a/src/components/__tests__/TextInput.test.tsx b/src/components/__tests__/TextInput.test.tsx index 20bb647ba1..fdfa055aa8 100644 --- a/src/components/__tests__/TextInput.test.tsx +++ b/src/components/__tests__/TextInput.test.tsx @@ -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( @@ -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( + {}} + /> + ); + + 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( {}} />); + + 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( + {}} + /> + ); + + 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( + {}} + startAccessory={({ style }) => ( + + )} + endAccessory={({ 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() { @@ -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 () => { @@ -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 ; } @@ -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') ); }); diff --git a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap index cdbe2843fe..5eb2e81343 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, @@ -197,6 +197,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "marginStart": 12, "width": 24, }, + false, { "opacity": 0.38, }, @@ -364,6 +365,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "marginEnd": 12, "width": 24, }, + false, { "opacity": 0.38, }, @@ -584,7 +586,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "position": "absolute", }, { - "left": 52, + "start": 52, }, { "top": 16, @@ -653,6 +655,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, ] } @@ -813,6 +816,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, ] } @@ -1000,7 +1004,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "position": "absolute", }, { - "left": 52, + "start": 52, }, { "top": 16, @@ -1069,6 +1073,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "width": 24, }, false, + false, null, ] } @@ -1229,6 +1234,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "width": 24, }, false, + false, null, ] } @@ -1452,7 +1458,7 @@ exports[`renders filled TextInput with label and value 1`] = ` "position": "absolute", }, { - "left": 16, + "start": 16, }, { "top": 16, @@ -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, @@ -1699,6 +1705,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, ] } @@ -1859,6 +1866,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, ] } @@ -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, @@ -2095,6 +2103,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i "width": 24, }, false, + false, null, ] } @@ -2255,6 +2264,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i "width": 24, }, false, + false, null, ] } @@ -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,