From fa97b2383ceaab339bc2f45b60a7b7cf510ecf0d Mon Sep 17 00:00:00 2001 From: Malcolm Scruggs Date: Fri, 24 May 2019 14:53:46 -0700 Subject: [PATCH] Use default OK on buttonPositive to prevent showing an alert without any buttons (#25033) 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 Pull Request resolved: https://github.com/facebook/react-native/pull/25033 Differential Revision: D15502780 Pulled By: cpojer fbshipit-source-id: 505a9940f4588f4c10e25b67bfed8b8a1e610c69 --- Libraries/Alert/Alert.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Libraries/Alert/Alert.js b/Libraries/Alert/Alert.js index 1e03d1d136d..125a488e0b2 100644 --- a/Libraries/Alert/Alert.js +++ b/Libraries/Alert/Alert.js @@ -73,9 +73,10 @@ class Alert { } // At most three buttons (neutral, negative, positive). Ignore rest. // The text 'OK' should be probably localized. iOS Alert does that in native. + const defaultPositiveText = 'OK'; const validButtons: Buttons = buttons ? buttons.slice(0, 3) - : [{text: 'OK'}]; + : [{text: defaultPositiveText}]; const buttonPositive = validButtons.pop(); const buttonNegative = validButtons.pop(); const buttonNeutral = validButtons.pop(); @@ -87,7 +88,7 @@ class Alert { config.buttonNegative = buttonNegative.text || ''; } if (buttonPositive) { - config.buttonPositive = buttonPositive.text || ''; + config.buttonPositive = buttonPositive.text || defaultPositiveText; } const onAction = (action, buttonKey) => {