Skip to content
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

fix attempting to focus disabled textinputs #30695

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,15 @@ function InternalTextInput(props: Props): React.Node {
return TextInputState.currentlyFocusedInput() === inputRef.current;
}

function focus(): void {
const {current} = inputRef;
if (props.editable === false || current === null) {
return;
}
// $FlowFixMe - `focus` is missing in `$ReadOnly`
Object.getPrototypeOf(current)?.focus?.call(current);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@yungsters had some feedback:
I think a better fix would be for the platform to return a boolean — true if the focus is successful, and false otherwise.

But for now, this logic should live in TextInputState.focusTextInput.

function focusTextInput(textField: ?ComponentRef) {
  // …

  if (currentlyFocusedInputRef !== textField && textField != null) {
    // EXPLANATION…
    if (textField.props?.editable === false) {
      return;
    }

    // …
  }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lunaleaps @yungsters the PR was updated, thanks for review

Copy link

Choose a reason for hiding this comment

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

thanks

}

function getNativeRef(): ?React.ElementRef<HostComponent<mixed>> {
return inputRef.current;
}
Expand Down Expand Up @@ -1096,6 +1105,7 @@ function InternalTextInput(props: Props): React.Node {
*/
if (ref) {
ref.clear = clear;
ref.focus = focus;
ref.isFocused = isFocused;
ref.getNativeRef = getNativeRef;
ref.setSelection = setSelection;
Expand Down
28 changes: 27 additions & 1 deletion Libraries/Components/TextInput/__tests__/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('TextInput tests', () => {
it('has expected instance functions', () => {
expect(inputRef.current.isFocused).toBeInstanceOf(Function); // Would have prevented S168585
expect(inputRef.current.clear).toBeInstanceOf(Function);
expect(inputRef.current.focus).toBeInstanceOf(jest.fn().constructor);
expect(inputRef.current.focus).toBeInstanceOf(Function);
expect(inputRef.current.blur).toBeInstanceOf(jest.fn().constructor);
expect(inputRef.current.setNativeProps).toBeInstanceOf(
jest.fn().constructor,
Expand Down Expand Up @@ -108,6 +108,32 @@ describe('TextInput tests', () => {
expect(TextInput.State.currentlyFocusedInput()).toBe(null);
});

describe('focus()', () => {
function createTextInput(extraProps) {
const textInputRef = React.createRef(null);
ReactTestRenderer.create(
<TextInput ref={textInputRef} value="value1" {...extraProps} />,
);
const {current} = textInputRef;
const mockComponent = Object.getPrototypeOf(current);
mockComponent.focus = jest.fn();
return {ref: current, mockComponent};
}

it('should call focus() up on the prototype (ReactNativeFiberHostComponent on device / mockComponent.js in tests)', () => {
const {ref, mockComponent} = createTextInput();
ref.focus();

expect(mockComponent.focus).toHaveBeenCalled();
});
it('should not do anything if the TextInput is not editable', () => {
const {ref, mockComponent} = createTextInput({editable: false});
ref.focus();

expect(mockComponent.focus).not.toHaveBeenCalled();
});
});

it('should unfocus when other TextInput is focused', () => {
const textInputRe1 = React.createRef(null);
const textInputRe2 = React.createRef(null);
Expand Down