Skip to content

Commit

Permalink
[#415] Add an OdsTextFieldTrailing sealed class to pass a pre-defined…
Browse files Browse the repository at this point in the history
… trailing element to a text field
  • Loading branch information
paulinea authored and florentmaitre committed Jan 31, 2023
1 parent 7f0acbc commit 49ba76d
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import com.orange.ods.compose.component.textfield.OdsIconTrailing
import com.orange.ods.compose.component.textfield.OdsTextField
import com.orange.ods.compose.component.textfield.OdsTextFieldCharacterCounter
import com.orange.ods.compose.component.textfield.OdsTextTrailing
import com.orange.ods.demo.R
import com.orange.ods.demo.ui.components.textfields.TextFieldCustomizationState.Companion.TextFieldMaxChars
import com.orange.ods.demo.ui.components.utilities.clickOnElement
Expand All @@ -30,32 +32,62 @@ fun TextField(customizationState: TextFieldCustomizationState) {
val context = LocalContext.current
val trailingIconName = stringResource(id = R.string.component_element_trailing)

val modifier = Modifier
.fillMaxWidth()
.padding(top = dimensionResource(id = R.dimen.spacing_s))
val leadingIcon = if (customizationState.hasLeadingIcon) painterResource(id = R.drawable.ic_heart) else null
val enabled = customizationState.isEnabled
val isError = customizationState.isError
val errorMessage = if (customizationState.isError) stringResource(id = R.string.component_text_field_error_message) else null
val value = customizationState.displayedText
val onValueChange: (String) -> Unit = { customizationState.updateText(it) }
val label = stringResource(id = R.string.component_element_label)
val placeholder = stringResource(id = R.string.component_text_field_placeholder)
val singleLine = customizationState.isSingleLine
val keyboardOptions = customizationState.keyboardOptions
val characterCounter: (@Composable () -> Unit)? = if (customizationState.hasCharacterCounter) {
{ TextFieldCharacterCounter(valueLength = customizationState.displayedText.length, enabled = customizationState.isEnabled) }
} else null

Column {
OdsTextField(
modifier = Modifier
.fillMaxWidth()
.padding(top = dimensionResource(id = R.dimen.spacing_s)),
leadingIcon = if (customizationState.hasLeadingIcon) painterResource(id = R.drawable.ic_heart) else null,
enabled = customizationState.isEnabled,
isError = customizationState.isError,
errorMessage = if (customizationState.isError) stringResource(id = R.string.component_text_field_error_message) else null,
value = customizationState.displayedText,
onValueChange = { customizationState.updateText(it) },
label = stringResource(id = R.string.component_element_label),
placeholder = stringResource(id = R.string.component_text_field_placeholder),
trailingIcon = if (customizationState.hasTrailingIcon) painterResource(id = R.drawable.ic_eye) else null,
onTrailingIconClick = if (customizationState.hasTrailingIcon) {
{ clickOnElement(context = context, trailingIconName) }
} else null,
trailingText = if (customizationState.hasTrailingText) "units" else null,
singleLine = customizationState.isSingleLine,
keyboardOptions = customizationState.keyboardOptions,
characterCounter = if (customizationState.hasCharacterCounter) {
{
TextFieldCharacterCounter(valueLength = customizationState.displayedText.length, enabled = customizationState.isEnabled)
}
} else null
)
if (customizationState.hasTrailingText || customizationState.hasTrailingIcon) {
OdsTextField(
modifier = modifier,
leadingIcon = leadingIcon,
enabled = enabled,
isError = isError,
errorMessage = errorMessage,
value = value,
onValueChange = onValueChange,
label = label,
placeholder = placeholder,
trailing = if (customizationState.hasTrailingIcon) {
OdsIconTrailing(
painter = painterResource(id = R.drawable.ic_eye),
onClick = { clickOnElement(context = context, trailingIconName) })
} else {
OdsTextTrailing(text = "units")
},
singleLine = singleLine,
keyboardOptions = keyboardOptions,
characterCounter = characterCounter
)
} else {
OdsTextField(
modifier = modifier,
leadingIcon = leadingIcon,
enabled = enabled,
isError = isError,
errorMessage = errorMessage,
value = value,
onValueChange = onValueChange,
label = label,
placeholder = placeholder,
singleLine = singleLine,
keyboardOptions = keyboardOptions,
characterCounter = characterCounter
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package com.orange.ods.compose.component.textfield

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text
Expand All @@ -24,11 +23,9 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.orange.ods.R
import com.orange.ods.compose.component.utilities.Preview
import com.orange.ods.compose.component.utilities.UiModePreviews
import com.orange.ods.compose.theme.OdsTheme
Expand All @@ -45,10 +42,7 @@ internal fun OdsFilledTextField(
leadingIcon: Painter? = null,
leadingIconContentDescription: String? = null,
onLeadingIconClick: (() -> Unit)? = null,
trailingIcon: Painter? = null,
trailingIconContentDescription: String? = null,
onTrailingIconClick: (() -> Unit)? = null,
trailingText: String? = null,
trailing: @Composable (() -> Unit)? = null,
isError: Boolean = false,
errorMessage: String? = null,
visualTransformation: VisualTransformation = VisualTransformation.None,
Expand Down Expand Up @@ -81,28 +75,7 @@ internal fun OdsFilledTextField(
)
}
},
trailingIcon = when {
trailingIcon != null -> {
{
OdsTextFieldIcon(
painter = trailingIcon,
contentDescription = trailingIconContentDescription,
onClick = if (enabled) onTrailingIconClick else null,
)
}
}
trailingText != null -> {
{
Text(
modifier = Modifier.padding(end = dimensionResource(id = R.dimen.spacing_s)),
text = trailingText,
style = OdsTheme.typography.subtitle1,
color = OdsTextFieldDefaults.trailingTextColor(value.isEmpty(), enabled)
)
}
}
else -> null
},
trailingIcon = trailing,
isError = isError,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
Expand All @@ -126,7 +99,9 @@ private fun PreviewOdsTextField(@PreviewParameter(OdsTextFieldPreviewParameterPr
onValueChange = { value = it },
placeholder = "Placeholder",
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info),
trailingIcon = painterResource(id = android.R.drawable.ic_input_add),
trailing = if (parameter.previewTrailingType != PreviewTrailingType.None) {
{ TrailingPreview(parameter.previewTrailingType, value) }
} else null,
isError = parameter.hasErrorMessage,
errorMessage = getPreviewErrorMessage(parameter.hasErrorMessage, parameter.isVeryLongErrorMessage),
characterCounter = if (parameter.hasCounter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package com.orange.ods.compose.component.textfield

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.OutlinedTextField
Expand All @@ -24,11 +23,9 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.orange.ods.R
import com.orange.ods.compose.component.utilities.Preview
import com.orange.ods.compose.component.utilities.UiModePreviews
import com.orange.ods.compose.theme.OdsTheme
Expand All @@ -45,10 +42,7 @@ internal fun OdsOutlinedTextField(
leadingIcon: Painter? = null,
leadingIconContentDescription: String? = null,
onLeadingIconClick: (() -> Unit)? = null,
trailingIcon: Painter? = null,
trailingIconContentDescription: String? = null,
onTrailingIconClick: (() -> Unit)? = null,
trailingText: String? = null,
trailing: @Composable (() -> Unit)? = null,
isError: Boolean = false,
errorMessage: String? = null,
visualTransformation: VisualTransformation = VisualTransformation.None,
Expand Down Expand Up @@ -81,28 +75,7 @@ internal fun OdsOutlinedTextField(
)
}
},
trailingIcon = when {
trailingIcon != null -> {
{
OdsTextFieldIcon(
painter = trailingIcon,
contentDescription = trailingIconContentDescription,
onClick = if (enabled) onTrailingIconClick else null,
)
}
}
trailingText != null -> {
{
Text(
modifier = Modifier.padding(end = dimensionResource(id = R.dimen.spacing_s)),
text = trailingText,
style = OdsTheme.typography.subtitle1,
color = OdsTextFieldDefaults.trailingTextColor(value.isEmpty(), enabled)
)
}
}
else -> null
},
trailingIcon = trailing,
isError = isError,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
Expand All @@ -126,7 +99,9 @@ private fun PreviewOdsOutlinedTextField(@PreviewParameter(OdsTextFieldPreviewPar
onValueChange = { value = it },
placeholder = "Placeholder",
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info),
trailingIcon = painterResource(id = android.R.drawable.ic_input_add),
trailing = if (parameter.previewTrailingType != PreviewTrailingType.None) {
{ TrailingPreview(parameter.previewTrailingType, value) }
} else null,
isError = parameter.hasErrorMessage,
errorMessage = if (parameter.hasErrorMessage) "Error message" else null
)
Expand Down
Loading

0 comments on commit 49ba76d

Please sign in to comment.