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 Modal onDismiss callback is called too early #2558

Closed

Conversation

bernhardoj
Copy link
Contributor

@bernhardoj bernhardoj commented Jul 17, 2023

Description

Currently, onDismiss is called right after setting the isRendered state to false which leads to onDismiss being called before the modal is completely dismissed. This makes chaining action after the modal is dismissed fails, for example, focusing an input.

Test plan

  1. Add a text input to the modal example
  2. Add onDismiss props to the Modal which will focus the text input
  3. Open the modal. Verify the modal shows
  4. Close the modal
  5. Verify the modal closes and the text input is focused

Currently, the 5th step fails and this PR is trying to fix it.

Before:

Screen.Recording.2023-07-17.at.17.21.37.mov

After:

Screen.Recording.2023-07-17.at.17.19.50.mov

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 17, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2618014:

Sandbox Source
react-native-web-examples Configuration

@necolas
Copy link
Owner

necolas commented Jul 17, 2023

Is there any way we can add unit test coverage for this?

@bernhardoj
Copy link
Contributor Author

@necolas sorry, I have no clue. If you have any ideas in mind, I can try them.

@necolas
Copy link
Owner

necolas commented Jul 19, 2023

Try to create a unit test that reproduces the test case and fails, then show that it passes with your patch. You didn't share a test case for the issue, so I don't know what it takes to reproduce, but it seems like it's related to animations. A unit test combining animation props with onDismiss might surface the issue

https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/exports/Modal/__tests__/index-test.js

@bernhardoj

This comment was marked as resolved.

@bernhardoj
Copy link
Contributor Author

Ignore my previous comment, I think I finally understand what should I do 😅

@bernhardoj
Copy link
Contributor Author

Here is the test that I come up with:

test('focus text input after modal is dismissed, () => {
  const input = React.createRef();
  const onDismissCallback = jest.fn(() => input.current.focus());
  const { rerender } = render(
    <>
      <TextInput ref={input} />
      <Modal onDismiss={onDismissCallback} visible={true} />
    </>
  );
  expect(onDismissCallback).toBeCalledTimes(0);
  rerender(
    <>
      <TextInput ref={input} />
      <Modal onDismiss={onDismissCallback} visible={false} />
    </>
  );
  expect(onDismissCallback).toBeCalledTimes(1);
  expect(input.current.isFocused()).toBe(true);
});

The test is to make sure focusing text input inside onDismiss callback works (which is what this PR is trying to achieve). It will fail on master and pass on this PR. However, I'm still not sure if this is the correct way because we depend on another component, that is TextInput. Let me know if it's okay!

@bernhardoj
Copy link
Contributor Author

@necolas hi, what do you think about the above unit test?

@bernhardoj
Copy link
Contributor Author

@necolas I've added the unit test that is different from what I posted above. Please kindly check it and let me know if I miss something 🙇

@bernhardoj
Copy link
Contributor Author

@necolas hi, how is the review going?

@necolas
Copy link
Owner

necolas commented Aug 24, 2023

Looks good, thanks

@necolas necolas closed this in d8f1b58 Aug 24, 2023
@bernhardoj
Copy link
Contributor Author

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants