diff --git a/src/components/TextInput/hooks.ts b/src/components/TextInput/hooks.ts index 8096c2d4e4..0f70ae6a88 100644 --- a/src/components/TextInput/hooks.ts +++ b/src/components/TextInput/hooks.ts @@ -510,9 +510,14 @@ export const useTextInput = (props: TextInputProps): TextInputHookReturn => { const renderLeadingAccessory = props.startAccessory; const renderTrailingAccessory = props.endAccessory; - // A space keeps the iOS multiline placeholder updating: - // https://github.com/facebook/react-native/issues/31573 - const placeholder = isPlaceholderShown ? props.placeholder : ' '; + // The hidden placeholder is empty: a blank is read as the field's text + // (TalkBack says "Space, edit box" instead of the label, iOS reports it as + // the value). Only iOS multiline fields keep a space, which keeps their + // placeholder updating: https://github.com/facebook/react-native/issues/31573 + const hiddenPlaceholder = Platform.OS === 'ios' && props.multiline ? ' ' : ''; + const placeholder = isPlaceholderShown + ? props.placeholder + : hiddenPlaceholder; return { input, diff --git a/src/components/__tests__/TextInput.test.tsx b/src/components/__tests__/TextInput.test.tsx index 61ba6bbe27..2408bcf774 100644 --- a/src/components/__tests__/TextInput.test.tsx +++ b/src/components/__tests__/TextInput.test.tsx @@ -1119,9 +1119,9 @@ it('does not expose the placeholder string when the TextInput is not focused', a /> ); - /* Sentinel space avoids iOS multiline UITextView not updating placeholder from nil (react-native#31573). */ + /* Empty, not a blank: a blank is read as the field's text. */ // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('tf-input').props.placeholder).toBe(' '); + expect(screen.getByTestId('tf-input').props.placeholder).toBe(''); }); it('shows placeholder when unfocused and no label is given', async () => { @@ -1155,7 +1155,7 @@ it('shows placeholder when the TextInput is focused, once the label floats away' // The label still rests over the input when the field takes focus. // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. - expect(screen.getByTestId('tf-input').props.placeholder).toBe(' '); + expect(screen.getByTestId('tf-input').props.placeholder).toBe(''); await act(() => jest.advanceTimersByTimeAsync(1000)); @@ -1177,6 +1177,7 @@ it('shows placeholder on multiline TextInput when focused, once the label floats /> ); + // iOS multiline fields keep a blank (react-native#31573). // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. expect(screen.getByTestId('tf-multiline').props.placeholder).toBe(' '); @@ -1193,6 +1194,40 @@ it('shows placeholder on multiline TextInput when focused, once the label floats ); }); +// TalkBack reads an empty field's text, even ' ', in place of its label. +it.each([false, true])( + 'keeps the hidden placeholder empty on Android (multiline: %s)', + async (multiline) => { + const platformOS = jest.replaceProperty(Platform, 'OS', 'android'); + + try { + await render( + {}} + placeholder="e.g. user@example.com" + multiline={multiline} + testID="tf-input" + /> + ); + + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + expect(screen.getByTestId('tf-input').props.placeholder).toBe(''); + + await fireEvent(screen.getByTestId('tf-input'), 'focus'); + await act(() => jest.advanceTimersByTimeAsync(1000)); + + // eslint-disable-next-line no-restricted-syntax -- TODO: replace TestInstance props access with a user-visible assertion. + expect(screen.getByTestId('tf-input').props.placeholder).toBe( + 'e.g. user@example.com' + ); + } finally { + platformOS.restore(); + } + } +); + it('shows placeholder at once when a focused TextInput with a floated label is cleared', async () => { const { rerender } = await render( { diff --git a/src/components/__tests__/TextInputAccessibility.test.tsx b/src/components/__tests__/TextInputAccessibility.test.tsx index 7cbdef5ea1..cb4d25fcab 100644 --- a/src/components/__tests__/TextInputAccessibility.test.tsx +++ b/src/components/__tests__/TextInputAccessibility.test.tsx @@ -279,7 +279,7 @@ it('describes an empty labelled field with its placeholder on web until the inpu renderInput.mock.lastCall?.[0]['aria-describedby']?.split(' ') ?? []; const ids = getDescriptionIds(); - expect(input).toHaveProp('placeholder', ' '); + expect(input).toHaveProp('placeholder', ''); expect(ids).toHaveLength(2); expect(screen.queryByText('name@example.com')).toBeNull(); expect(