Skip to content

Max length prop of TextInput is ignored #1239

@pierrezimmermannbam

Description

@pierrezimmermannbam

Describe the bug

It is possible to change the value of a TextInput that has a maxLength prop. The following test fails for instance :

test('it should not allow to write a text longer than maxlength in a text input', () => {
  const TestComponent = () => {
    const [value, setValue] = useState('');

    return (
      <RNTextInput placeholder="placeholder" maxLength={1} onChangeText={setValue} value={value} />
    );
  };

  render(<TestComponent />);

  fireEvent.changeText(screen.getByPlaceholderText('placeholder'), 'text');

  screen.debug();
  expect(screen.queryByDisplayValue('text')).toBeFalsy();

});

Expected behavior

I'd expect that only the substring of length equal to maxlength is set as value.

Regarding the implementation I'm not sure though if this should be done by fireEvent because it just calls props and we'd have to check what the event is and have special checks in that case so maybe it's something that should be done once we have an implementation of userEvent.type. So actually I'm saying this is not really a bug but rather a use case that userEvent.type should support

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions