diff --git a/src/components/TextInput/TextInput.tsx b/src/components/TextInput/TextInput.tsx index 6db3359500..571d673f6b 100644 --- a/src/components/TextInput/TextInput.tsx +++ b/src/components/TextInput/TextInput.tsx @@ -527,7 +527,7 @@ function TextInput({ - {!!supportingText && ( + {supportingText ? ( {supportingText} - )} + ) : hasCounter ? ( + + ) : null} {hasCounter && ( diff --git a/src/components/TextInput/styles.ts b/src/components/TextInput/styles.ts index b45ab98e00..6917c7110b 100644 --- a/src/components/TextInput/styles.ts +++ b/src/components/TextInput/styles.ts @@ -40,6 +40,9 @@ export const styles = StyleSheet.create({ opacity: 0, pointerEvents: 'none', }, + addendumSpacer: { + flex: 1, + }, supportingText: { flex: 1, marginTop: SUPPORTING_TEXT_MARGIN_TOP, @@ -72,6 +75,14 @@ export const styles = StyleSheet.create({ alignItems: 'center', marginStart: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL, }, + leadingAccessoryRTL: { + marginStart: 0, + marginRight: TEXT_INPUT_ACCESSORY_MARGIN_HORIZONTAL, + }, + trailingAccessoryRTL: { + marginEnd: 0, + marginLeft: 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 6d3c2b02f6..00a54f3303 100644 --- a/src/components/TextInput/utils.ts +++ b/src/components/TextInput/utils.ts @@ -243,6 +243,7 @@ export const getSharedTextInputStyleData = ( styles.supportingText, { color: supportingTextColor, + textAlign: isRTL && Platform.OS === 'web' ? 'right' : 'left', writingDirection: isRTL ? 'rtl' : 'ltr', }, isDisabled && styles.disabled, @@ -252,6 +253,7 @@ export const getSharedTextInputStyleData = ( styles.counter, { color: supportingTextColor, + textAlign: isRTL && Platform.OS === 'web' ? 'left' : 'right', writingDirection: isRTL ? 'rtl' : 'ltr', }, isDisabled && styles.disabled, @@ -263,6 +265,7 @@ export const getSharedTextInputStyleData = ( fontSize: INPUT_FONT_SIZE, color: onSurfaceVariant, paddingEnd: PREFIX_END_PADDING, + writingDirection: isRTL ? 'rtl' : 'ltr', }, isDisabled && styles.disabled, ]; @@ -273,17 +276,20 @@ export const getSharedTextInputStyleData = ( fontSize: INPUT_FONT_SIZE, color: onSurfaceVariant, paddingStart: SUFFIX_START_PADDING, + writingDirection: isRTL ? 'rtl' : 'ltr', }, isDisabled && styles.disabled, ]; 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, ]; diff --git a/src/components/__tests__/TextInput.test.tsx b/src/components/__tests__/TextInput.test.tsx index 6684621fb1..813d381398 100644 --- a/src/components/__tests__/TextInput.test.tsx +++ b/src/components/__tests__/TextInput.test.tsx @@ -1035,7 +1035,7 @@ it('applies RTL text alignment and writing direction to the TextInput (outlined) }); }); -it('applies RTL writing direction to supporting text', async () => { +it('uses left text alignment and rtl writing direction for supporting text in RTL on native', async () => { I18nManager.isRTL = true; await render( @@ -1048,6 +1048,7 @@ it('applies RTL writing direction to supporting text', async () => { ); expect(screen.getByText('Hint', includeHiddenElements)).toHaveStyle({ + textAlign: 'left', writingDirection: 'rtl', }); }); @@ -1421,6 +1422,71 @@ describe('on web in RTL', () => { { right: LABEL_START_OFFSET_WITHOUT_ACCESSORY } ); }); + + it('uses physical margins for the accessories', async () => { + await render( + {}} + startAccessory={({ style }) => ( + + )} + endAccessory={({ style }) => ( + + )} + /> + ); + + expect(screen.getByTestId('web-rtl-leading')).toHaveStyle({ + marginStart: 0, + marginRight: 12, + }); + expect(screen.getByTestId('web-rtl-trailing')).toHaveStyle({ + marginEnd: 0, + marginLeft: 12, + }); + }); + + it('aligns supporting text to the right and the counter to the left', async () => { + await render( + {}} + /> + ); + + expect( + screen.queryByTestId('text-input-addendum-spacer', includeHiddenElements) + ).not.toBeOnTheScreen(); + expect(screen.getByText('Helper', includeHiddenElements)).toHaveStyle({ + textAlign: 'right', + }); + expect(screen.getByText('2/10', includeHiddenElements)).toHaveStyle({ + textAlign: 'left', + }); + }); + + it('renders the counter without supporting text', async () => { + await render( + {}} + /> + ); + + expect( + screen.getByTestId('text-input-addendum-spacer', includeHiddenElements) + ).toBeOnTheScreen(); + expect(screen.getByText('2/10', includeHiddenElements)).toBeOnTheScreen(); + }); }); 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 a815b58ef0..c48e07b4e0 100644 --- a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap @@ -194,6 +194,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "marginStart": 12, "width": 24, }, + false, { "opacity": 0.38, }, @@ -376,6 +377,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "marginEnd": 12, "width": 24, }, + false, { "opacity": 0.38, }, @@ -685,6 +687,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -861,6 +864,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -1140,6 +1144,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -1316,6 +1321,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -1799,6 +1805,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -1975,6 +1982,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -2234,6 +2242,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i "width": 24, }, false, + false, null, { "borderRadius": 20, @@ -2410,6 +2419,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i "width": 24, }, false, + false, null, { "borderRadius": 20,