Skip to content

feat: support 'editable' prop on TextInput #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
40 changes: 40 additions & 0 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,46 @@ test('should not fire on disabled Pressable', () => {
expect(handlePress).not.toHaveBeenCalled();
});

test('should not fire on non-editable TextInput', () => {
const placeholder = 'Test placeholder';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';

const { getByPlaceholderText } = render(
<View>
<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
/>
</View>
);

fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
});

test('should not fire on non-editable TextInput with nested Text', () => {
const placeholder = 'Test placeholder';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';

const { getByPlaceholderText } = render(
<View>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add the custom TextInputWrapper here and combine all cases into a single test :)

<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
>
<Text>Test text</Text>
</TextInput>
</View>
);

fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
});

test('should pass event up on disabled TouchableOpacity', () => {
const handleInnerPress = jest.fn();
const handleOuterPress = jest.fn();
Expand Down
13 changes: 11 additions & 2 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import act from './act';
import { ErrorWithStack } from './helpers/errors';

const isTextInputComponent = (element: ReactTestInstance) => {
// eslint-disable-next-line import/no-extraneous-dependencies
const { TextInput } = require('react-native');
return element.type === TextInput;
};

const findEventHandler = (
element: ReactTestInstance,
eventName: string,
Expand All @@ -14,8 +20,11 @@ const findEventHandler = (

const isHostComponent = typeof element.type === 'string';
const hostElement = isHostComponent ? element : nearestHostDescendent;
const isEventEnabled =
hostElement?.props.onStartShouldSetResponder?.() !== false;

const isEventEnabled = isTextInputComponent(element)
? element.props.editable !== false
: hostElement?.props.onStartShouldSetResponder?.() !== false;

if (handler && isEventEnabled) return handler;

// Do not bubble event to the root element
Expand Down