mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Migration RN Alert Dialog to androidx (#44494)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44494 Pull Request resolved: https://github.com/facebook/react-native/pull/44880 Migrates the `AlertFragment` from `android.app.AlertDialog` to `androidx.appcompat.app.AlertDialog`. This backports tons of fixes that have gone into the AlertDialog component over the years, including proper line wrapping of button text, alignment of buttons, etc. ## For consideration - Alert dialog themes may no longer need the `android` namespace, meaning themes can now be specified as `alertDialogTheme` rather than `android:alertDialogTheme`. - This change requires all implementing activities to have a theme that inherits from `Theme.AppCompat`. Creation of any activities which do not have a descendant of this style will result in an `IllegalStateException`: https://www.internalfb.com/intern/signalinfra/exception_owners/?mid=5ee93f6ecd59f3d8ad82a78c213ea016&result_id=16044073705339118.281475102518721.1715097866 ## Changelog: [Android] [Changed] - Migrated `AlertFragment` dialog builder to use `androidx.appcompat` Reviewed By: zeyap Differential Revision: D57019423 fbshipit-source-id: 84d8f69d896d32e72434149c0e31735d358370a9
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ae7d543d31
commit
600d3f6ff1
+1
-1
@@ -8,12 +8,12 @@
|
||||
package com.facebook.react.modules.dialog;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
/** A fragment used to display the dialog. */
|
||||
|
||||
+23
-1
@@ -7,10 +7,11 @@
|
||||
|
||||
package com.facebook.react.modules.dialog
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.DialogInterface
|
||||
import android.os.Looper.getMainLooper
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.facebook.react.R
|
||||
import com.facebook.react.bridge.Callback
|
||||
import com.facebook.react.bridge.JavaOnlyMap
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
@@ -48,6 +49,9 @@ class DialogModuleTest {
|
||||
fun setUp() {
|
||||
activityController = Robolectric.buildActivity(FragmentActivity::class.java)
|
||||
activity = activityController.create().start().resume().get()
|
||||
// We must set the theme to a descendant of AppCompat for the AlertDialog to show without
|
||||
// raising an exception
|
||||
activity.setTheme(APP_COMPAT_THEME)
|
||||
|
||||
val context: ReactApplicationContext = mock(ReactApplicationContext::class.java)
|
||||
whenever(context.hasActiveReactInstance()).thenReturn(true)
|
||||
@@ -62,6 +66,19 @@ class DialogModuleTest {
|
||||
activityController.pause().stop().destroy()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIllegalActivityTheme() {
|
||||
val options = JavaOnlyMap()
|
||||
activity.setTheme(NON_APP_COMPAT_THEME)
|
||||
|
||||
assertThrows(NullPointerException::class.java) {
|
||||
dialogModule.showAlert(options, null, null)
|
||||
shadowOf(getMainLooper()).idle()
|
||||
}
|
||||
|
||||
activity.setTheme(APP_COMPAT_THEME)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllOptions() {
|
||||
val options =
|
||||
@@ -158,4 +175,9 @@ class DialogModuleTest {
|
||||
return activity.supportFragmentManager.findFragmentByTag(DialogModule.FRAGMENT_TAG)
|
||||
as? AlertFragment
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val APP_COMPAT_THEME: Int = R.style.Theme_ReactNative_AppCompat_Light
|
||||
private val NON_APP_COMPAT_THEME: Int = android.R.style.Theme_DeviceDefault_Light
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user