From 69a37094504ee7f442fa917e069631cd822299d3 Mon Sep 17 00:00:00 2001 From: Jay Newstrom Date: Thu, 2 Jan 2025 18:13:03 -0600 Subject: [PATCH] Make drop down selectable on Android TV. (#9849) --- .../uicore/elements/DropdownFieldUI.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/DropdownFieldUI.kt b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/DropdownFieldUI.kt index bfd42ba3af8..0d43232bd79 100644 --- a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/DropdownFieldUI.kt +++ b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/DropdownFieldUI.kt @@ -1,5 +1,6 @@ package com.stripe.android.uicore.elements +import android.content.pm.PackageManager import androidx.annotation.RestrictTo import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -15,6 +16,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.requiredSizeIn import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.wrapContentSize +import androidx.compose.foundation.selection.selectable import androidx.compose.material.DropdownMenu import androidx.compose.material.Icon import androidx.compose.material.MaterialTheme @@ -31,6 +33,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource @@ -120,6 +123,17 @@ fun DropDown( onClickLabel = stringResource(R.string.stripe_change), onClick = { expanded = true }, ) + .let { + if (isAndroidTv()) { + it.selectable( + selected = false, + enabled = shouldEnable, + onClick = { expanded = true }, + ) + } else { + it + } + } .testTag("DropDown:${if (controller.tinyMode) "tiny" else "normal"}") ) { if (controller.tinyMode) { @@ -287,3 +301,8 @@ internal fun DropdownMenuItem( // Size defaults. internal val DropdownMenuItemDefaultMaxWidth = 280.dp internal val DropdownMenuItemDefaultMinHeight = 48.dp + +@Composable +private fun isAndroidTv(): Boolean { + return LocalContext.current.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK) +}