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
11 changes: 8 additions & 3 deletions src/components/TextInput/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 39 additions & 4 deletions src/components/__tests__/TextInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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));

Expand All @@ -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(' ');

Expand All @@ -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(
<TextInput
label="Email"
value=""
onChangeText={() => {}}
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(
<TextInput
Expand Down Expand Up @@ -1268,7 +1303,7 @@ it('does not expose the placeholder string again after the TextInput loses focus
await fireEvent(screen.getByTestId('tf-input'), 'blur');

// 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('keeps a lone StartAccessory before the input in both LTR and RTL (tree order)', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/__tests__/TextInputAccessibility.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading