Skip to content

make values focusable (and copyable) when not editable #200

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

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -152,8 +152,8 @@ internal fun BaseTextField(
text: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
readOnly: Boolean,
isEditable: Boolean,
readOnly: Boolean = !isEditable,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
readOnly: Boolean = !isEditable,

It might be better to remove this parameter and use isEditable directly like below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Switch specifies readOnly as true, but isEditable may be true or false.

label: String,
placeholder: String,
singleLine: Boolean,
Expand Down Expand Up @@ -189,7 +189,6 @@ internal fun BaseTextField(
.focusable(isEditable, interactionSource)
.semantics { contentDescription = "outlined text field" },
readOnly = readOnly,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
readOnly = readOnly,
readOnly = !isEditable,

enabled = isEditable,
label = {
Text(
text = label,
Expand Down Expand Up @@ -247,7 +246,6 @@ private fun BaseTextFieldPreview() {
BaseTextField(
text = "",
onValueChange = {},
readOnly = false,
isEditable = true,
label = "Title",
placeholder = "Enter Value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ internal fun ComboBoxField(
interactionSource.interactions.collect {
if (it is PressInteraction.Release) {
wasFocused = true
onDialogRequest()
if (isEditable) {
onDialogRequest()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ private fun RadioButtonField(
label
},
style = MaterialTheme.typography.bodyMedium,
color = colors.labelColor(enabled = editable)
color = colors.labelColor
)
Column(
modifier = Modifier
.selectableGroup()
.border(
width = 1.dp,
color = colors.containerBorderColor(enabled = editable),
color = colors.containerBorderColor,
shape = RoundedCornerShape(5.dp)
)
) {
CompositionLocalProvider(
LocalContentColor provides colors.textColor(enabled = editable)
LocalContentColor provides colors.textColor
) {
options.forEach { (_, name) ->
RadioButtonRow(
Expand All @@ -130,7 +130,7 @@ private fun RadioButtonField(
Text(
text = description,
style = MaterialTheme.typography.bodySmall,
color = colors.supportingTextColor(enabled = editable)
color = colors.supportingTextColor
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,94 +22,30 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

internal object RadioButtonFieldDefaults {

private const val textDisabledAlpha = 0.38f
private const val containerDisabledAlpha = 0.12f

@Composable
fun colors(): RadioButtonFieldColors = RadioButtonFieldColors(
defaultLabelColor = MaterialTheme.colorScheme.onSurfaceVariant,
disabledLabelColor = MaterialTheme.colorScheme.onSurface.copy(
alpha = textDisabledAlpha
),
defaultSupportingTextColor = MaterialTheme.colorScheme.onSurfaceVariant,
disabledSupportingTextColor = MaterialTheme.colorScheme.onSurfaceVariant.copy(
alpha = textDisabledAlpha
),
labelColor = MaterialTheme.colorScheme.onSurfaceVariant,
supportingTextColor = MaterialTheme.colorScheme.onSurfaceVariant,
errorColor = MaterialTheme.colorScheme.error,
defaultContainerBorderColor = MaterialTheme.colorScheme.outline,
disabledContainerBorderColor = MaterialTheme.colorScheme.outline.copy(
alpha = containerDisabledAlpha
),
defaultTextColor = LocalContentColor.current,
disabledTextColor = LocalContentColor.current.copy(alpha = textDisabledAlpha)
containerBorderColor = MaterialTheme.colorScheme.outline,
textColor = LocalContentColor.current
)
}

/**
* Color properties of a radio button field.
*
* @property labelColor The color used for the label of this radio button field.
* @property supportingTextColor The color used for the supporting text of this radio button field.
* @property errorColor The color used for the supporting text of this radio button field when the value is considered
* invalid.
* @property containerBorderColor The color used for the container border of this radio button field.
* @property textColor The color used for the text of this radio button field options.
*/
internal data class RadioButtonFieldColors(
val defaultLabelColor: Color,
val disabledLabelColor: Color,
val defaultSupportingTextColor: Color,
val disabledSupportingTextColor: Color,
val labelColor: Color,
val supportingTextColor: Color,
val errorColor: Color,
val defaultContainerBorderColor: Color,
val disabledContainerBorderColor: Color,
val defaultTextColor: Color,
val disabledTextColor: Color
) {
/**
* Represents the color used for the label of this radio button field.
*
* @param enabled whether the field is enabled
*/
@Composable
fun labelColor(enabled: Boolean): Color {
return if (enabled) {
defaultLabelColor
} else {
disabledLabelColor
}
}

/**
* Represents the color used for the supporting text of this radio button field.
*
* @param enabled whether the field is enabled
*/
@Composable
fun supportingTextColor(enabled: Boolean): Color {
return if (enabled) {
defaultSupportingTextColor
} else {
disabledSupportingTextColor
}
}

/**
* Represents the color used for the container border of this radio button field.
*
* @param enabled whether the field is enabled
*/
@Composable
fun containerBorderColor(enabled: Boolean): Color {
return if (enabled) {
defaultContainerBorderColor
} else {
disabledContainerBorderColor
}
}

/**
* Represents the color used for the text of this radio button field options.
*
* @param enabled whether the field is enabled
*/
@Composable
fun textColor(enabled: Boolean): Color {
return if (enabled) {
defaultTextColor
} else {
disabledTextColor
}
}
}
val containerBorderColor: Color,
val textColor: Color
)
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ internal fun SwitchField(state: SwitchFieldState, modifier: Modifier = Modifier)

LaunchedEffect(codeName) {
interactionSource.interactions.collect {
if (it is PressInteraction.Release) {
val newValue = (
if (checkedState)
state.offValue.name
else
state.onValue.name
)
state.onValueChanged(newValue)
if (isEditable) {
if (it is PressInteraction.Release) {
val newValue = (
if (checkedState)
state.offValue.name
else
state.onValue.name
)
state.onValueChanged(newValue)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.arcgismaps.toolkit.featureforms.components.datetime
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.CalendarMonth
import androidx.compose.material.icons.rounded.EditCalendar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
Expand Down Expand Up @@ -75,7 +76,7 @@ internal fun DateTimeField(
placeholder = state.placeholder.ifEmpty { stringResource(id = R.string.no_value) },
singleLine = true,
interactionSource = interactionSource,
trailingIcon = Icons.Rounded.EditCalendar,
trailingIcon = if (isEditable) Icons.Rounded.EditCalendar else Icons.Rounded.CalendarMonth,
supportingText = {
// if the field was focused and is required, validate the current value
if (wasFocused && isRequired && instant == null) {
Expand All @@ -98,7 +99,8 @@ internal fun DateTimeField(
wasFocused = true
// request to show the date picker dialog only when the touch is released
// the dialog is responsible for updating the value on the state
onDialogRequest()
if (isEditable)
onDialogRequest()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ internal fun FormTextField(
state.onValueChanged(it)
},
modifier = modifier.fillMaxWidth(),
readOnly = false,
isEditable = isEditable,
label = label,
placeholder = state.placeholder,
Expand All @@ -76,7 +75,7 @@ internal fun FormTextField(
color = textColor
)
}
if (isFocused) {
if (isFocused && isEditable) {
Spacer(modifier = Modifier.weight(1f))
Text(
text = contentLength,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ internal class FormTextFieldState(
errorMessages[errorToDisplay] ?: throw IllegalStateException("validation error must have a message")
} else {
description.ifEmpty {
if (_isFocused.value) helperText else ""
if (_isFocused.value && isEditable.value) {
helperText
} else {
""
}
}
}
_hasError.value = errors.isNotEmpty()
Expand Down Expand Up @@ -328,7 +332,7 @@ internal class FormTextFieldState(
value: String,
validationErrors: List<ValidationErrorState>
): ValidationErrorState =
if (validationErrors.isEmpty()) {
if (validationErrors.isEmpty() || !isEditable.value) {
NoError
} else if (isFocused.value) {
if (value.isEmpty()) {
Expand Down