Skip to content

Commit

Permalink
fix: [ANDROAPP-5900] set initial selection to value length
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy committed Feb 6, 2024
1 parent be69d70 commit 84aab55
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import org.dhis2.R
Expand Down Expand Up @@ -52,9 +53,11 @@ fun ProvideInputDate(
) {
if (uiModel.showField) {
Spacer(modifier = Modifier.height(16.dp))
val textSelection = TextRange(if (uiModel.eventDate.dateValue != null) uiModel.eventDate.dateValue.length else 0)

var value by remember(uiModel.eventDate.dateValue) {
if (uiModel.eventDate.dateValue != null) {
mutableStateOf(TextFieldValue(formatStoredDateToUI(uiModel.eventDate.dateValue) ?: ""))
mutableStateOf(TextFieldValue(formatStoredDateToUI(uiModel.eventDate.dateValue) ?: "", textSelection))
} else {
mutableStateOf(TextFieldValue())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import org.dhis2.commons.date.DateUtils
import org.dhis2.commons.extensions.toDate
Expand Down Expand Up @@ -37,11 +38,12 @@ fun ProvideInputDate(
ValueType.TIME -> DateTimeActionIconType.TIME to TimeTransformation()
else -> DateTimeActionIconType.DATE to DateTransformation()
}
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(
if (fieldUiModel.value != null) {
TextFieldValue(formatStoredDateToUI(fieldUiModel.value!!, fieldUiModel.valueType))
TextFieldValue(formatStoredDateToUI(fieldUiModel.value!!, fieldUiModel.valueType), textSelection)
} else {
TextFieldValue()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.TextFieldValue
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -448,8 +449,10 @@ private fun ProvideIntegerPositive(
focusManager: FocusManager,
onNextClicked: () -> Unit,
) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputPositiveInteger(
Expand Down Expand Up @@ -487,8 +490,10 @@ private fun ProvideIntegerPositiveOrZero(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputPositiveIntegerOrZero(
Expand Down Expand Up @@ -526,8 +531,10 @@ private fun ProvidePercentage(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputPercentage(
Expand Down Expand Up @@ -565,8 +572,10 @@ private fun ProvideNumber(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputNumber(
Expand Down Expand Up @@ -605,8 +614,9 @@ private fun ProvideIntegerNegative(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)
var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value?.replace("-", "") ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value?.replace("-", "") ?: "", textSelection))
}

InputNegativeInteger(
Expand Down Expand Up @@ -644,8 +654,10 @@ private fun ProvideLongText(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputLongText(
Expand Down Expand Up @@ -684,8 +696,9 @@ private fun ProvideLetter(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)
var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputLetter(
Expand Down Expand Up @@ -723,8 +736,9 @@ private fun ProvideInteger(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)
var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputInteger(
Expand Down Expand Up @@ -762,8 +776,10 @@ private fun ProvideEmail(
focusManager: FocusManager,
onNextClicked: () -> Unit,
) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputEmail(
Expand Down Expand Up @@ -811,8 +827,10 @@ private fun ProvideInputPhoneNumber(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputPhoneNumber(
Expand Down Expand Up @@ -860,8 +878,10 @@ private fun ProvideInputLink(
onNextClicked: () -> Unit,

) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputLink(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import org.dhis2.form.extensions.autocompleteList
import org.dhis2.form.extensions.inputState
Expand Down Expand Up @@ -72,8 +73,9 @@ private fun ProvideQRInput(
focusManager: FocusManager,
onNextClicked: () -> Unit,
) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)
var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputQRCode(
Expand Down Expand Up @@ -170,8 +172,10 @@ private fun ProvideBarcodeInput(
focusManager: FocusManager,
onNextClicked: () -> Unit,
) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}

InputBarCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import org.dhis2.form.extensions.inputState
import org.dhis2.form.extensions.legend
Expand All @@ -22,8 +23,10 @@ fun ProvideUnitIntervalInput(
intentHandler: (FormIntent) -> Unit,
onNextClicked: () -> Unit,
) {
val textSelection = TextRange(if (fieldUiModel.value != null) fieldUiModel.value!!.length else 0)

var value by remember(fieldUiModel.value) {
mutableStateOf(TextFieldValue(fieldUiModel.value ?: ""))
mutableStateOf(TextFieldValue(fieldUiModel.value ?: "", textSelection))
}
InputUnitInterval(
modifier = modifier.fillMaxWidth(),
Expand Down

0 comments on commit 84aab55

Please sign in to comment.