-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#415] Propose only one style of text fields in the library according…
… the provided theme configuration
- Loading branch information
1 parent
612bca0
commit f2931a1
Showing
4 changed files
with
228 additions
and
140 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
lib/src/main/java/com/orange/ods/compose/component/textfield/OdsFilledTextField.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
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 | ||
import androidx.compose.material.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
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 | ||
|
||
@Composable | ||
internal fun OdsFilledTextField( | ||
value: String, | ||
onValueChange: (String) -> Unit, | ||
modifier: Modifier = Modifier, | ||
enabled: Boolean = true, | ||
readOnly: Boolean = false, | ||
label: String? = null, | ||
placeholder: String? = null, | ||
leadingIcon: Painter? = null, | ||
leadingIconContentDescription: String? = null, | ||
onLeadingIconClick: (() -> Unit)? = null, | ||
trailingIcon: Painter? = null, | ||
trailingIconContentDescription: String? = null, | ||
onTrailingIconClick: (() -> Unit)? = null, | ||
trailingText: String? = null, | ||
isError: Boolean = false, | ||
errorMessage: String? = null, | ||
visualTransformation: VisualTransformation = VisualTransformation.None, | ||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, | ||
keyboardActions: KeyboardActions = KeyboardActions(), | ||
singleLine: Boolean = false, | ||
maxLines: Int = Int.MAX_VALUE, | ||
characterCounter: (@Composable () -> Unit)? = null | ||
) { | ||
Column(modifier = modifier) { | ||
TextField( | ||
modifier = Modifier.fillMaxWidth(), | ||
value = if (singleLine) value.filter { it != '\n' } else value, | ||
onValueChange = { newValue -> | ||
if (!singleLine || !newValue.contains('\n')) { | ||
onValueChange(newValue) | ||
} | ||
}, | ||
enabled = enabled, | ||
readOnly = readOnly, | ||
textStyle = OdsTheme.typography.subtitle1, | ||
label = label?.let { { Text(label) } }, | ||
placeholder = placeholder?.let { { Text(text = placeholder, style = OdsTheme.typography.subtitle1) } }, | ||
leadingIcon = leadingIcon?.let { | ||
{ | ||
OdsTextFieldIcon( | ||
painter = leadingIcon, | ||
contentDescription = leadingIconContentDescription, | ||
onClick = if (enabled) onLeadingIconClick else null, | ||
) | ||
} | ||
}, | ||
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 | ||
}, | ||
isError = isError, | ||
visualTransformation = visualTransformation, | ||
keyboardOptions = keyboardOptions, | ||
keyboardActions = keyboardActions, | ||
singleLine = singleLine, | ||
maxLines = maxLines, | ||
colors = OdsTextFieldDefaults.textFieldColors() | ||
) | ||
|
||
OdsTextFieldBottomRow(isError = isError, errorMessage = errorMessage, characterCounter = characterCounter) | ||
} | ||
} | ||
|
||
@UiModePreviews.Default | ||
@Composable | ||
private fun PreviewOdsTextField(@PreviewParameter(OdsTextFieldPreviewParameterProvider::class) parameter: OdsTextFieldPreviewParameter) = Preview { | ||
var value by remember { mutableStateOf("Input text") } | ||
OdsFilledTextField( | ||
modifier = Modifier.fillMaxWidth(), | ||
value = value, | ||
onValueChange = { value = it }, | ||
placeholder = "Placeholder", | ||
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info), | ||
trailingIcon = painterResource(id = android.R.drawable.ic_input_add), | ||
isError = parameter.hasErrorMessage, | ||
errorMessage = getPreviewErrorMessage(parameter.hasErrorMessage, parameter.isVeryLongErrorMessage), | ||
characterCounter = if (parameter.hasCounter) { | ||
{ | ||
OdsTextFieldCharacterCounter(value.length, 30) | ||
} | ||
} else null | ||
) | ||
} | ||
|
||
private fun getPreviewErrorMessage(hasErrorMessage: Boolean, isVeryLongErrorMessage: Boolean) = when { | ||
hasErrorMessage && !isVeryLongErrorMessage -> "Error message" | ||
hasErrorMessage && isVeryLongErrorMessage -> "Very very very very very very very very very long error message" | ||
else -> null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.