Skip to content

Commit

Permalink
Divide DecorationBox code and fix placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
jja08111 committed Jan 26, 2024
1 parent 3c4449a commit 31f33b0
Showing 1 changed file with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,32 +191,13 @@ fun CamstudyTextField(
visualTransformation = visualTransformation,
cursorBrush = SolidColor(colorScheme.primary),
decorationBox = { innerTextField ->
Box(
modifier = Modifier
.heightIn(min = 48.dp)
.padding(horizontal = 16.dp)
) {
val verticalAlignmentModifier = Modifier.align(Alignment.CenterStart)

if (value.isEmpty() && placeholder != null) {
CamstudyText(
modifier = verticalAlignmentModifier,
text = placeholder,
style = textStyle
)
}
Row(
modifier = verticalAlignmentModifier,
verticalAlignment = Alignment.CenterVertically
) {
if (prefix != null) {
ProvideTextStyle(value = CamstudyTextFieldTextStyle) {
prefix()
}
}
innerTextField()
}
}
DecorationBox(
value = value,
textStyle = textStyle,
placeholder = placeholder,
prefix = prefix,
innerTextField = innerTextField
)
}
)
supportingContent?.invoke()
Expand All @@ -230,6 +211,43 @@ fun CamstudyTextField(
}
}

@Composable
private fun DecorationBox(
value: String,
textStyle: TextStyle,
placeholder: String?,
prefix: (@Composable () -> Unit)?,
innerTextField: @Composable () -> Unit,
) {
Box(
modifier = Modifier
.heightIn(min = 48.dp)
.padding(horizontal = 16.dp)
) {
val verticalAlignmentModifier = Modifier.align(Alignment.CenterStart)

Row(
modifier = verticalAlignmentModifier,
verticalAlignment = Alignment.CenterVertically
) {
if (prefix != null) {
ProvideTextStyle(value = CamstudyTextFieldTextStyle) {
prefix()
}
}
if (value.isEmpty() && placeholder != null) {
CamstudyText(
modifier = verticalAlignmentModifier,
text = placeholder,
style = textStyle
)
} else {
innerTextField()
}
}
}
}

@Preview(showBackground = true)
@Composable
fun CamstudyTextFieldPreview() {
Expand Down

0 comments on commit 31f33b0

Please sign in to comment.