11package com.texthip.thip.ui.common.forms
22
33
4+ import androidx.compose.foundation.border
45import androidx.compose.foundation.clickable
56import androidx.compose.foundation.layout.Box
67import androidx.compose.foundation.layout.Column
7- import androidx.compose.foundation.layout.Spacer
88import androidx.compose.foundation.layout.height
9+ import androidx.compose.foundation.layout.padding
910import androidx.compose.foundation.layout.size
1011import androidx.compose.foundation.shape.RoundedCornerShape
1112import androidx.compose.foundation.text.KeyboardOptions
@@ -41,79 +42,93 @@ import com.texthip.thip.ui.theme.ThipTheme.typography
4142@Composable
4243fun BookPageTextField (
4344 modifier : Modifier = Modifier ,
44- bookPage : Int
45+ bookTotalPage : Int ,
46+ enabled : Boolean = true,
47+ text : String ,
48+ onValueChange : (String ) -> Unit ,
4549) {
46- var text by rememberSaveable { mutableStateOf(" " ) }
4750 var isError by rememberSaveable { mutableStateOf(false ) }
4851 var errorMessageRes by rememberSaveable { mutableStateOf<Int ?>(null ) }
49- var errorMessageParam by rememberSaveable { mutableStateOf(0 ) }
5052
5153 Column {
5254 OutlinedTextField (
5355 value = text,
5456 onValueChange = { newText: String ->
5557 if (newText.isEmpty() || newText.all { it.isDigit() }) {
56- text = newText
58+ onValueChange(newText)
59+
5760 if (newText.isNotEmpty()) {
5861 val pageNum = newText.toInt()
59- isError = pageNum > bookPage
60- if (isError) {
61- errorMessageRes = R .string.error_page_over
62- errorMessageParam = bookPage
62+ isError = pageNum > bookTotalPage
63+ errorMessageRes = if (isError) {
64+ R .string.error_page_over
6365 } else {
64- errorMessageRes = null
66+ null
6567 }
6668 } else {
6769 isError = false
6870 errorMessageRes = null
6971 }
7072 }
7173 },
72- visualTransformation = SuffixTransformation (
73- suffix = " /${bookPage} p" ,
74- suffixColor = colors.Grey02
75- ),
74+ enabled = enabled,
75+ visualTransformation = if (enabled) {
76+ SuffixTransformation (" /${bookTotalPage} p" , colors.Grey02 )
77+ } else {
78+ VisualTransformation .None
79+ },
7680 keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
77- modifier = modifier.size(width = 320 .dp, height = 48 .dp),
81+ modifier = modifier
82+ .size(width = 320 .dp, height = 48 .dp)
83+ .then(
84+ if (isError)
85+ Modifier .border(
86+ width = 1 .dp,
87+ color = colors.Red ,
88+ shape = RoundedCornerShape (12 .dp)
89+ )
90+ else Modifier
91+ ),
7892 textStyle = typography.menu_r400_s14_h24.copy(lineHeight = 12 .sp),
7993 maxLines = 1 ,
8094 shape = RoundedCornerShape (12 .dp),
8195 colors = TextFieldDefaults .colors(
8296 focusedTextColor = colors.White ,
83- focusedIndicatorColor = if (isError) colors.Red else Color .Transparent ,
84- unfocusedIndicatorColor = if (isError) colors.Red else Color .Transparent ,
85- focusedContainerColor = colors.Black ,
86- unfocusedContainerColor = colors.Black ,
87- cursorColor = colors.NeonGreen
97+ unfocusedTextColor = colors.White ,
98+ disabledTextColor = colors.White ,
99+ focusedIndicatorColor = Color .Transparent ,
100+ unfocusedIndicatorColor = Color .Transparent ,
101+ disabledIndicatorColor = Color .Transparent ,
102+ focusedContainerColor = colors.DarkGrey02 ,
103+ unfocusedContainerColor = colors.DarkGrey02 ,
104+ disabledContainerColor = colors.DarkGrey02 ,
105+ cursorColor = colors.NeonGreen ,
88106 ),
89107 trailingIcon = {
90- if (text.isNotEmpty()) {
91- Icon (
92- painter = painterResource(id = R .drawable.ic_x_circle_white) ,
93- contentDescription = " Clear text " ,
94- modifier = Modifier .clickable {
95- text = " "
108+ Icon (
109+ painter = painterResource(id = R .drawable.ic_x_circle_grey),
110+ contentDescription = " Clear text " ,
111+ modifier = Modifier .clickable {
112+ if (text.isNotEmpty()) {
113+ onValueChange( " " )
96114 isError = false
97115 errorMessageRes = null
98- },
99- tint = Color .Unspecified
100- )
101- } else {
102- Icon (
103- painter = painterResource(id = R .drawable.ic_x_circle),
104- contentDescription = " Clear text"
105- )
106- }
116+ }
117+ },
118+ tint = Color .Unspecified
119+ )
107120 }
108121 )
109122
110- if (isError && errorMessageRes != null ) {
111- Spacer (modifier = Modifier .height(4 .dp))
112- Text (
113- text = stringResource(id = errorMessageRes!! , errorMessageParam),
114- color = colors.Red ,
115- style = typography.menu_r400_s14_h24.copy(lineHeight = 12 .sp)
116- )
123+ Box (modifier = Modifier .height(22 .dp)) {
124+ if (isError && errorMessageRes != null ) {
125+ Text (
126+ modifier = Modifier .padding(start = 4 .dp, top = 8 .dp),
127+ text = stringResource(id = errorMessageRes!! ),
128+ color = colors.Red ,
129+ style = typography.menu_r400_s14_h24.copy(lineHeight = 12 .sp)
130+ )
131+ }
117132 }
118133 }
119134}
@@ -135,6 +150,7 @@ class SuffixTransformation(
135150 val offsetMapping = object : OffsetMapping {
136151 override fun originalToTransformed (offset : Int ): Int =
137152 offset.coerceAtMost(original.length)
153+
138154 override fun transformedToOriginal (offset : Int ): Int =
139155 offset.coerceAtMost(original.length)
140156 }
@@ -147,12 +163,18 @@ class SuffixTransformation(
147163@Composable
148164@Preview(showBackground = true , backgroundColor = 0xFF000000 , widthDp = 360 , heightDp = 200 )
149165fun BookPageTextFieldPreviewEmpty () {
166+ var text by rememberSaveable { mutableStateOf(" " ) }
167+
150168 Box (
151169 modifier = Modifier .size(width = 360 .dp, height = 200 .dp),
152170 contentAlignment = Alignment .Center
153171 ) {
154172 BookPageTextField (
155- bookPage = 456
173+ bookTotalPage = 456 ,
174+ text = text,
175+ onValueChange = {
176+ text = it
177+ }
156178 )
157179 }
158180}
0 commit comments