Skip to content

Use default OK on buttonPositive to prevent showing an alert without any buttons#25033

Closed
Mookiies wants to merge 1 commit into
facebook:masterfrom
Mookiies:fixAlert
Closed

Use default OK on buttonPositive to prevent showing an alert without any buttons#25033
Mookiies wants to merge 1 commit into
facebook:masterfrom
Mookiies:fixAlert

Conversation

@Mookiies
Copy link
Copy Markdown
Contributor

Summary

Solve #25016

Use OK as default text for the affirmative button if no text is specified. When setting an alert on android with button configuration, and no text field specified no button is shown on the alert. This makes it impossible to dismiss. An example of how this can happen is creating a simple button where a onPress callback is used but no text is specified:

Alert.alert(
   'title',
   'message',
   [ { onPress: () => console.log('onPress') } ],
)

Does not change the current behavior of no text button configurations on iOS. On iOS at least one button is always shown, and buttons with no text can be displayed.

Behavior on setting multiple buttons is a little wonky, but this PR does not aim to solve it. I did test these cases and included some examples below.

Changelog

[Android] [Fixed] - Use OK as default text on Android Alert if button configuration specified without text

Test Plan

Tested alerts with varying configurations set

Configuration without text, one button:

Alert.alert(
'title',
'message',
[ {  onPress: () => console.log('first') } ])

Screen Shot 2019-05-24 at 11 05 47 AM

Configuration without text, two button:

Alert.alert(
'title',
'message',
[
    {  onPress: () => console.log('first') },
    {  onPress: () => console.log('first') },
],
)

Screen Shot 2019-05-24 at 11 42 38 AM

Configuration without text, three buttons

Alert.alert(
'title',
'message',
[
    {  onPress: () => console.log('first') },
    {  onPress: () => console.log('second') },
    {  onPress: () => console.log('third') },
],
)

Screen Shot 2019-05-24 at 11 43 25 AM

Custom text for two of three buttons:

Alert.alert(
'title',
'message',
[
    {  text: 'first', onPress: () => console.log('first') },
    {  text: 'second', onPress: () => console.log('second') },
    {  onPress: () => console.log('third') },
],
)

Screen Shot 2019-05-24 at 12 00 44 PM

Custom text for first button only:

Alert.alert(
'title',
'message',
[
    {  text: 'first', onPress: () => console.log('first') },
    {  onPress: () => console.log('second') },
    {  onPress: () => console.log('third') },
],
)

Screen Shot 2019-05-24 at 12 02 44 PM

No button configuration:

Alert.alert(
'title',
'message',
)

Screen Shot 2019-05-24 at 11 09 02 AM

@facebook-github-bot
Copy link
Copy Markdown
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 24, 2019
@facebook-github-bot
Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link
Copy Markdown
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

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

Sounds good! Thank you :)

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @MalcolmScruggs in fa97b23.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 24, 2019
@Mookiies Mookiies deleted the fixAlert branch May 24, 2019 22:45
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this pull request Mar 10, 2020
…any buttons (facebook#25033)

Summary:
Solve facebook#25016

Use `OK` as default text for the affirmative button if no text is specified. When setting an alert on android with button configuration, and no `text` field specified no button is shown on the alert. This makes it impossible to dismiss. An example of how this can happen is creating a simple button where a `onPress` callback is used but no text is specified:

```
Alert.alert(
   'title',
   'message',
   [ { onPress: () => console.log('onPress') } ],
)
```

Does not change the current behavior of no text button configurations on iOS. On iOS at least one button is always shown, and buttons with no text can be displayed.

Behavior on setting multiple buttons is a little wonky, but this PR does not aim to solve it. I did test these cases and included some examples below.

## Changelog

[Android] [Fixed] - Use OK as default text on Android Alert if button configuration specified without text
Pull Request resolved: facebook#25033

Differential Revision: D15502780

Pulled By: cpojer

fbshipit-source-id: 505a9940f4588f4c10e25b67bfed8b8a1e610c69
@cinder92
Copy link
Copy Markdown

this shows an OK button automatically, but if i add a text with Ok, it shows twice

{
                  text: 'Ok',
                  onPress: () => {
                  },
                },

this shows 2 ok buttons ok the alert for android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API: Alert Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Component: Button Merged This PR has been merged. Platform: Android Android applications.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants