Skip to content

Commit 756f8df

Browse files
fix HexTextField accepting invalid chars
1 parent d71ccdc commit 756f8df

File tree

1 file changed

+17
-60
lines changed
  • colorpicker/src/main/java/com/smarttoolfactory/colorpicker/widget

1 file changed

+17
-60
lines changed

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/widget/HexTextField.kt

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.smarttoolfactory.colorpicker.widget
22

3-
import androidx.compose.foundation.layout.padding
43
import androidx.compose.foundation.layout.widthIn
5-
import androidx.compose.foundation.layout.wrapContentHeight
64
import androidx.compose.foundation.shape.RoundedCornerShape
7-
import androidx.compose.foundation.text.BasicTextField
85
import androidx.compose.material.*
96
import androidx.compose.runtime.Composable
107
import androidx.compose.ui.Modifier
118
import androidx.compose.ui.draw.drawBehind
129
import androidx.compose.ui.geometry.Offset
1310
import androidx.compose.ui.graphics.Color
1411
import androidx.compose.ui.graphics.Shape
15-
import androidx.compose.ui.graphics.SolidColor
1612
import androidx.compose.ui.text.AnnotatedString
1713
import androidx.compose.ui.text.TextStyle
1814
import androidx.compose.ui.text.input.OffsetMapping
@@ -78,68 +74,29 @@ fun HexTextField(
7874
onValueChange = {
7975

8076
if (it.length <= if (useAlpha) 8 else 6) {
81-
if (it.isNotEmpty()) {
82-
val lastChar = it.last()
83-
val isHexChar = hexRegexSingleChar.matches(lastChar.toString())
84-
if (isHexChar) {
85-
onTextChange(it)
86-
}
87-
} else {
88-
onTextChange(it)
89-
}
9077

91-
// Hex String with 6 or 8 chars matches a Color
92-
if (hex.matches(it)) {
93-
onColorChange(hexToColor(it))
78+
println("😍 CurrentString: $it")
79+
var validHex = true
80+
81+
for (index in it.indices) {
82+
validHex = hexRegexSingleChar.matches(it[index].toString())
83+
println("🎃 CHART: ${it[index]}, validHex: $validHex")
84+
if (!validHex) break
9485
}
95-
}
96-
}
97-
)
98-
}
9986

100-
@Composable
101-
private fun BasicHexTextField(
102-
modifier: Modifier = Modifier,
103-
value: String,
104-
textStyle: TextStyle = TextStyle(
105-
fontSize = 20.sp,
106-
),
107-
textColor: Color = Color.Black,
108-
backgroundColor: Color = Color.LightGray,
109-
cursorColor: Color = MaterialTheme.colors.primary,
110-
visualTransformation: VisualTransformation,
111-
hint: String,
112-
shape: Shape = RoundedCornerShape(25),
113-
onValueChange: (String) -> Unit,
114-
) {
87+
println("💬 TEXT: $it, validHex: $validHex")
11588

116-
Surface(
117-
modifier = modifier
118-
.wrapContentHeight()
119-
.widthIn(min = 60.dp),
120-
shape = shape,
121-
color = backgroundColor,
122-
contentColor = textColor
123-
) {
124-
BasicTextField(
125-
textStyle = textStyle,
126-
value = value,
127-
singleLine = true,
128-
visualTransformation = visualTransformation,
129-
cursorBrush = SolidColor(cursorColor),
130-
onValueChange = onValueChange
131-
) {
132-
if (value.isEmpty()) {
133-
Text(
134-
modifier = Modifier.padding(4.dp),
135-
text = hint,
136-
style = textStyle.copy(textStyle.color.copy(.7f))
137-
)
138-
} else {
139-
it()
89+
if (validHex) {
90+
onTextChange(it)
91+
92+
// Hex String with 6 or 8 chars matches a Color
93+
if (hex.matches(it)) {
94+
onColorChange(hexToColor(it))
95+
}
96+
}
14097
}
14198
}
142-
}
99+
)
143100
}
144101

145102
private class HexVisualTransformation(private val useAlpha: Boolean) : VisualTransformation {

0 commit comments

Comments
 (0)