|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.fhir.datacapture.views.compose |
| 18 | + |
| 19 | +import androidx.compose.material3.DropdownMenuItem |
| 20 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 21 | +import androidx.compose.material3.ExposedDropdownMenuBox |
| 22 | +import androidx.compose.material3.ExposedDropdownMenuDefaults |
| 23 | +import androidx.compose.material3.Icon |
| 24 | +import androidx.compose.material3.MaterialTheme |
| 25 | +import androidx.compose.material3.MenuAnchorType |
| 26 | +import androidx.compose.material3.OutlinedTextField |
| 27 | +import androidx.compose.material3.Text |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.runtime.LaunchedEffect |
| 30 | +import androidx.compose.runtime.derivedStateOf |
| 31 | +import androidx.compose.runtime.getValue |
| 32 | +import androidx.compose.runtime.mutableStateOf |
| 33 | +import androidx.compose.runtime.remember |
| 34 | +import androidx.compose.runtime.setValue |
| 35 | +import androidx.compose.ui.Modifier |
| 36 | +import androidx.compose.ui.graphics.asImageBitmap |
| 37 | +import androidx.core.graphics.drawable.toBitmap |
| 38 | +import com.google.android.fhir.datacapture.views.factories.DropDownAnswerOption |
| 39 | + |
| 40 | +@OptIn(ExperimentalMaterial3Api::class) |
| 41 | +@Composable |
| 42 | +internal fun ExposedDropDownMenuBoxItem( |
| 43 | + modifier: Modifier, |
| 44 | + enabled: Boolean, |
| 45 | + selectedOption: DropDownAnswerOption? = null, |
| 46 | + options: List<DropDownAnswerOption>, |
| 47 | + onDropDownAnswerOptionSelected: (DropDownAnswerOption?) -> Unit, |
| 48 | +) { |
| 49 | + var expanded by remember { mutableStateOf(false) } |
| 50 | + var selectedDropDownAnswerOption by |
| 51 | + remember(selectedOption, options) { mutableStateOf(selectedOption) } |
| 52 | + val selectedOptionDisplay by remember { |
| 53 | + derivedStateOf { selectedDropDownAnswerOption?.answerOptionString ?: "" } |
| 54 | + } |
| 55 | + |
| 56 | + LaunchedEffect(selectedDropDownAnswerOption) { |
| 57 | + onDropDownAnswerOptionSelected(selectedDropDownAnswerOption) |
| 58 | + } |
| 59 | + |
| 60 | + ExposedDropdownMenuBox( |
| 61 | + modifier = modifier, |
| 62 | + expanded = expanded, |
| 63 | + onExpandedChange = { expanded = it }, |
| 64 | + ) { |
| 65 | + OutlinedTextField( |
| 66 | + value = selectedOptionDisplay, |
| 67 | + onValueChange = {}, |
| 68 | + modifier = Modifier.menuAnchor(MenuAnchorType.PrimaryNotEditable, enabled), |
| 69 | + readOnly = true, |
| 70 | + enabled = enabled, |
| 71 | + minLines = 1, |
| 72 | + label = {}, |
| 73 | + supportingText = {}, |
| 74 | + trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, |
| 75 | + ) |
| 76 | + ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) { |
| 77 | + options.forEach { option -> |
| 78 | + DropdownMenuItem( |
| 79 | + text = { |
| 80 | + Text(option.answerOptionAnnotatedString(), style = MaterialTheme.typography.bodyLarge) |
| 81 | + }, |
| 82 | + leadingIcon = { |
| 83 | + option.answerOptionImage?.let { |
| 84 | + Icon( |
| 85 | + it.toBitmap().asImageBitmap(), |
| 86 | + contentDescription = option.answerOptionString, |
| 87 | + ) |
| 88 | + } |
| 89 | + }, |
| 90 | + enabled = enabled, |
| 91 | + onClick = { |
| 92 | + selectedDropDownAnswerOption = option |
| 93 | + expanded = false |
| 94 | + }, |
| 95 | + contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding, |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments