Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue of text input losing calculated value when not focused #64

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -80,6 +81,7 @@ object DydxTradeInputSizeView : DydxComponent {
}

val showingUsdc = remember { mutableStateOf(state.showingUsdc) }
val focusManager = LocalFocusManager.current

InputFieldScarfold(modifier) {
Column {
Expand Down Expand Up @@ -111,7 +113,10 @@ object DydxTradeInputSizeView : DydxComponent {

PlatformAccessoryButton(
modifier = Modifier,
action = { showingUsdc.value = !showingUsdc.value },
action = {
focusManager.clearFocus()
showingUsdc.value = !showingUsdc.value
},
) {
Column(
modifier = Modifier.padding(ThemeShapes.InputPaddingValues),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.TextFieldColors
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -60,7 +59,10 @@ fun PlatformTextInput(
}
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()
val currentValue = remember { mutableStateOf<String?>(null) }
val currentValue = remember { mutableStateOf<String?>(value) } // value during editing
if (!isFocused) {
currentValue.value = value
}
val displayValue = if (isFocused) currentValue.value ?: "" else value ?: ""

BasicTextField(
Expand Down Expand Up @@ -98,69 +100,6 @@ fun PlatformTextInput(
)
},
)

// TextField(
// modifier = Modifier,
// value = displayValue,
// onValueChange = {
// currentValue.value = it
// onValueChange(it)
// },
// // label = label,
// placeholder = {
// Text(
// text = placeHolder ?: "",
// style = TextStyle.dydxDefault
// .themeFont(fontSize = ThemeFont.FontSize.medium)
// .themeColor(ThemeColor.SemanticColor.text_tertiary)
// )
// },
// interactionSource = interactionSource,
// singleLine = true,
// colors = inputFieldColors,
// textStyle = TextStyle.dydxDefault
// .themeFont(fontSize = ThemeFont.FontSize.medium)
// )
}
}
}

@Composable
fun PlatformTextInput(
labelText: String? = null,
value: String? = null,
placeHolder: String? = null,
onValueChange: (String) -> Unit = {},
) {
PlatformTextInput(
label = if (labelText?.isNotEmpty() == true) { {
Text(
text = labelText ?: "",
style = TextStyle.dydxDefault
.themeFont(fontSize = ThemeFont.FontSize.mini)
.themeColor(ThemeColor.SemanticColor.text_tertiary),
)
} } else {
null
},
value = value,
placeHolder = placeHolder,
onValueChange = onValueChange,
)
}

private val inputFieldColors: TextFieldColors
@Composable
get() = TextFieldDefaults.textFieldColors(
textColor = ThemeColor.SemanticColor.text_primary.color,
disabledTextColor = ThemeColor.SemanticColor.text_tertiary.color,
backgroundColor = ThemeColor.SemanticColor.transparent.color,
focusedIndicatorColor = ThemeColor.SemanticColor.transparent.color,
unfocusedIndicatorColor = ThemeColor.SemanticColor.transparent.color,
errorIndicatorColor = ThemeColor.SemanticColor.transparent.color,
cursorColor = ThemeColor.SemanticColor.text_primary.color,
errorCursorColor = ThemeColor.SemanticColor.color_yellow.color,
errorLabelColor = ThemeColor.SemanticColor.color_yellow.color,
errorLeadingIconColor = ThemeColor.SemanticColor.color_yellow.color,
errorTrailingIconColor = ThemeColor.SemanticColor.color_yellow.color,
)
Loading