NumericInputField.kt (2396B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2023 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.compose 18 19 import androidx.compose.foundation.layout.Row 20 import androidx.compose.material.icons.Icons 21 import androidx.compose.material.icons.filled.Add 22 import androidx.compose.material.icons.filled.Remove 23 import androidx.compose.material3.Icon 24 import androidx.compose.material3.IconButton 25 import androidx.compose.material3.OutlinedTextField 26 import androidx.compose.runtime.Composable 27 import androidx.compose.ui.Modifier 28 29 @Composable 30 fun NumericInputField( 31 modifier: Modifier = Modifier, 32 value: Long, 33 onValueChange: (Long) -> Unit, 34 readOnly: Boolean = true, 35 label: @Composable () -> Unit, 36 minValue: Long? = 0L, 37 maxValue: Long? = null, 38 ) { 39 OutlinedTextField( 40 modifier = modifier, 41 value = value.toString(), 42 singleLine = true, 43 readOnly = readOnly, 44 onValueChange = { 45 val dd = it.toLongOrNull() ?: 0 46 onValueChange(dd) 47 }, 48 trailingIcon = { 49 Row { 50 IconButton( 51 content = { Icon(Icons.Default.Remove, "add1") }, 52 onClick = { 53 if (minValue != null && value - 1 >= minValue) { 54 onValueChange(value - 1) 55 } 56 }, 57 ) 58 IconButton( 59 content = { Icon(Icons.Default.Add, "add1") }, 60 onClick = { 61 if (maxValue != null && value + 1 <= maxValue) { 62 onValueChange(value + 1) 63 } 64 }, 65 ) 66 } 67 }, 68 label = label, 69 ) 70 }