Modern Apps git

← Modern-Apps.git

library:ui: MultiCategoryPicker and mandatory form rows

Commit
0de0bde6d5fbfe0755596cede3d6a74580ced2b8
Author
vayun-mathur <[email protected]>
When
2026-08-29 04:46 UTC (3 months ago)
Parents
61a6394

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.ktindex 42d783c611..eda5c98cc3 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt@@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ColumnScope import androidx.compose.foundation.layout.Row+import com.vayunmathur.library.util.sharedContainer import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height@@ -143,12 +144,28 @@ fun <T> FormDetailGroup(     optionLabel: ((Int) -> String)? = null,     leadingIcon: (@Composable (T) -> Unit)? = null,     addIcon: (@Composable () -> Unit)? = null,+    /**+     * Per-item key pairing this field with the read-only row that shows the same value elsewhere, so+     * the two morph into one another. Return null for an item with no counterpart - a row the user+     * has just added has nothing to morph from, and two rows sharing a key would collide.+     */+    sharedKey: ((T) -> Any?)? = null,+    /**+     * Rows that must always exist, by index. Such a row keeps its place when emptied rather than+     * disappearing, so a contact always offers somewhere to put its primary number or address.+     *+     * Only affects this row's own button. The type it uses stays available to every other row, so a+     * contact can still have a second mobile number.+     */+    isMandatory: (Int) -> Boolean = { false }, ) {     Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(Spacing.sm)) {         items.forEachIndexed { index, item ->+            val rowKey = sharedKey?.invoke(item)+            val rowModifier = if (rowKey == null) Modifier else Modifier.sharedContainer(rowKey)             OutlinedTextField(                 value = value(item),-                onValueChange = { v -> onValueChange(index, v) },+                onValueChange = { v: String -> onValueChange(index, v) },                 label = { Text(label) },                 visualTransformation = visualTransformation,                 leadingIcon = leadingIcon?.let { { it(item) } },@@ -170,14 +187,25 @@ fun <T> FormDetailGroup(                                 )                             }                         }-                        IconButton(onClick = { onRemove(index) }) {-                            IconRemoveCircle()+                        // A mandatory row cannot be taken away, so its button clears the value instead+                        // - and only appears when there is something to clear, since an always-present+                        // button on an empty row invites a tap that would do nothing.+                        if (isMandatory(index)) {+                            if (value(item).isNotEmpty()) {+                                IconButton(onClick = { onValueChange(index, "") }) {+                                    IconRemoveCircle()+                                }+                            }+                        } else {+                            IconButton(onClick = { onRemove(index) }) {+                                IconRemoveCircle()+                            }                         }                     }                 },                 keyboardOptions = KeyboardOptions(keyboardType = keyboardType),                 singleLine = true,-                modifier = Modifier.fillMaxWidth(),+                modifier = rowModifier.fillMaxWidth(),             )             if (isCustom(item) && onLabelChange != null) {                 Spacer(Modifier.height(Spacing.xs))
library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.ktnew file mode 100644index 0000000000..3932f21ee8--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/MultiCategoryPicker.kt@@ -0,0 +1,104 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.interaction.MutableInteractionSource+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.FlowRow+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.selection.toggleable+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.text.input.VisualTransformation+import androidx.compose.ui.unit.dp++/**+ * A field for choosing several values from a fixed set: it looks like a read-only text field, its value+ * is the chips inside it, and tapping it opens a menu rather than the keyboard.+ *+ * Built on [OutlinedTextFieldDefaults.DecorationBox] rather than drawn by hand, so the outline, the+ * label and the way the label lifts out of the border are the real thing and stay correct as the theme+ * changes. The chips take the place of the inner text field; [selected] doubles as the field's value so+ * that the label floats exactly when something is chosen.+ *+ * Tapping anywhere on the field opens the menu, via an overlay that covers it. That is the point of the+ * component: a plain read-only text field still raises the IME when focused, which is wrong for a value+ * that cannot be typed.+ */+@Composable+fun <T> MultiCategoryPicker(+    label: String,+    selected: List<T>,+    available: List<T>,+    itemLabel: (T) -> String,+    onAdd: (T) -> Unit,+    onRemove: (T) -> Unit,+    modifier: Modifier = Modifier,+    /** Per-chip modifier, for morphing a chip into its counterpart on another screen. */+    chipModifier: @Composable (T) -> Modifier = { Modifier },+) {+    var expanded by remember { mutableStateOf(false) }+    val interactionSource = remember { MutableInteractionSource() }++    Box(modifier.fillMaxWidth()) {+        OutlinedTextFieldDefaults.DecorationBox(+            value = selected.joinToString { itemLabel(it) },+            innerTextField = {+                FlowRow(+                    horizontalArrangement = Arrangement.spacedBy(8.dp),+                    verticalArrangement = Arrangement.spacedBy(8.dp),+                    modifier = Modifier.fillMaxWidth(),+                ) {+                    selected.forEach { item ->+                        InputChip(+                            selected = false,+                            onClick = { onRemove(item) },+                            label = { Text(itemLabel(item)) },+                            trailingIcon = { IconRemoveCircle(modifier = Modifier.size(18.dp)) },+                            modifier = chipModifier(item),+                        )+                    }+                }+            },+            enabled = true,+            singleLine = false,+            visualTransformation = VisualTransformation.None,+            interactionSource = interactionSource,+            isError = false,+            label = { Text(label) },+            trailingIcon = { IconArrowDropDown() },+            contentPadding = PaddingValues(horizontal = 16.dp, vertical = 12.dp),+        )++        // Above the chips so the whole field opens the menu, but the chips' own remove taps still win+        // because they are drawn later in the same Box.+        if (available.isNotEmpty()) {+            Box(+                Modifier+                    .matchParentSize()+                    .toggleable(+                        value = expanded,+                        onValueChange = { expanded = it },+                        interactionSource = interactionSource,+                        indication = null,+                    )+            )+        }+        DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {+            available.forEach { item ->+                DropdownMenuItem(+                    text = { Text(itemLabel(item)) },+                    onClick = {+                        onAdd(item)+                        expanded = false+                    },+                )+            }+        }+    }+}