@@ -2,9 +2,12 @@ package com.texthip.thip.ui.common.forms
22
33import androidx.compose.foundation.clickable
44import androidx.compose.foundation.layout.Box
5+ import androidx.compose.foundation.layout.Column
56import androidx.compose.foundation.layout.Spacer
7+ import androidx.compose.foundation.layout.fillMaxSize
68import androidx.compose.foundation.layout.fillMaxWidth
79import androidx.compose.foundation.layout.height
10+ import androidx.compose.foundation.layout.padding
811import androidx.compose.foundation.layout.size
912import androidx.compose.foundation.shape.RoundedCornerShape
1013import androidx.compose.foundation.text.KeyboardOptions
@@ -16,6 +19,7 @@ import androidx.compose.runtime.Composable
1619import androidx.compose.runtime.getValue
1720import androidx.compose.runtime.mutableStateOf
1821import androidx.compose.runtime.remember
22+ import androidx.compose.runtime.saveable.rememberSaveable
1923import androidx.compose.runtime.setValue
2024import androidx.compose.ui.Alignment
2125import androidx.compose.ui.Modifier
@@ -37,93 +41,139 @@ fun WarningTextField(
3741 hint : String ,
3842 warningMessage : String = "경고 메시지를 입력해주세요.",
3943 showWarning : Boolean = false,
44+ showLimit : Boolean = true,
4045 maxLength : Int = Int .MAX_VALUE ,
46+ showIcon : Boolean = false,
47+ containerColor : Color = colors.Black ,
4148 isNumberOnly : Boolean = false,
4249 keyboardType : KeyboardType = KeyboardType .Text
4350) {
4451 val myStyle = typography.menu_r400_s14_h24.copy(lineHeight = 14 .sp)
45- OutlinedTextField (
46- value = value,
47- onValueChange = { input ->
48- var filtered = input
49- if (isNumberOnly) filtered = filtered.filter { it.isDigit() }
50- if (filtered.length > maxLength) filtered = filtered.take(maxLength)
51- onValueChange(filtered)
52- },
53- placeholder = {
54- Text (
55- text = hint,
56- color = colors.Grey02 ,
57- style = myStyle
52+
53+ Column {
54+ Box (
55+ modifier = modifier
56+ .height(48 .dp)
57+ ) {
58+ OutlinedTextField (
59+ value = value,
60+ onValueChange = { input ->
61+ var filtered = input
62+ if (isNumberOnly) filtered = filtered.filter { it.isDigit() }
63+ if (filtered.length > maxLength) filtered = filtered.take(maxLength)
64+ onValueChange(filtered)
65+ },
66+ placeholder = {
67+ Text (
68+ text = hint,
69+ color = colors.Grey02 ,
70+ style = myStyle
71+ )
72+ },
73+ textStyle = myStyle,
74+ modifier = Modifier .fillMaxSize(),
75+ shape = RoundedCornerShape (12 .dp),
76+ colors = TextFieldDefaults .colors(
77+ focusedTextColor = colors.White ,
78+ focusedIndicatorColor = if (showWarning) colors.Red else Color .Transparent ,
79+ unfocusedIndicatorColor = if (showWarning) colors.Red else Color .Transparent ,
80+ focusedContainerColor = containerColor,
81+ unfocusedContainerColor = containerColor,
82+ cursorColor = colors.NeonGreen
83+ ),
84+ trailingIcon = {
85+ if (showIcon) {
86+ if (value.isNotEmpty()) {
87+ Icon (
88+ painter = painterResource(id = R .drawable.ic_x_circle_white),
89+ contentDescription = " Clear text" ,
90+ modifier = Modifier .clickable { onValueChange(" " )},
91+ tint = Color .Unspecified
92+ )
93+ } else {
94+ Icon (
95+ painter = painterResource(id = R .drawable.ic_x_circle),
96+ contentDescription = " Clear text"
97+ )
98+ }
99+ }
100+ },
101+ singleLine = true ,
102+ keyboardOptions = KeyboardOptions (keyboardType = keyboardType)
103+
58104 )
59- },
60- textStyle = myStyle,
61- modifier = modifier
62- .fillMaxWidth()
63- .height(48 .dp),
64- shape = RoundedCornerShape (12 .dp),
65- colors = TextFieldDefaults .colors(
66- focusedTextColor = colors.White ,
67- focusedIndicatorColor = if (showWarning) colors.Red else Color .Transparent ,
68- unfocusedIndicatorColor = if (showWarning) colors.Red else Color .Transparent ,
69- focusedContainerColor = colors.DarkGrey50 ,
70- unfocusedContainerColor = colors.DarkGrey50 ,
71- cursorColor = colors.NeonGreen
72- ),
73- trailingIcon = {
74- if (value.isNotEmpty()) {
75- Icon (
76- painter = painterResource(id = R .drawable.ic_x_circle_white),
77- contentDescription = " Clear text" ,
78- modifier = Modifier .clickable { onValueChange(" " ) },
79- tint = Color .Unspecified
80- )
81- } else {
82- Icon (
83- painter = painterResource(id = R .drawable.ic_x_circle),
84- contentDescription = " Clear text"
85- )
105+
106+ if (showLimit && maxLength != Int .MAX_VALUE ) {
107+ Box (
108+ modifier = Modifier
109+ .align(Alignment .CenterEnd )
110+ .padding(end = 14 .dp)
111+ ) {
112+ Text (
113+ text = " ${value.length} /$maxLength " ,
114+ color = colors.White ,
115+ style = typography.info_r400_s12_h24
116+ )
117+ }
86118 }
87- },
88- singleLine = true ,
89- keyboardOptions = KeyboardOptions (keyboardType = keyboardType)
90- )
91- if (showWarning) {
92- Spacer (modifier = Modifier .height(4 .dp))
93- Text (
94- text = warningMessage,
95- color = colors.Red ,
96- style = typography.info_r400_s12.copy(lineHeight = 12 .sp)
97- )
119+ }
120+ if (showWarning) {
121+ Spacer (modifier = Modifier .height(4 .dp))
122+ Text (
123+ text = warningMessage,
124+ color = colors.Red ,
125+ style = typography.info_r400_s12.copy(lineHeight = 12 .sp)
126+ )
127+ }
98128 }
99129}
100130
101-
102131@Composable
103132@Preview(showBackground = true , backgroundColor = 0xFF000000 , widthDp = 360 , heightDp = 200 )
104133fun WarningTextFieldPreviewEmpty () {
105- var password by remember { mutableStateOf(" " ) }
134+ var text by rememberSaveable { mutableStateOf(" " ) }
106135
107136 Box (
108137 modifier = Modifier .size(width = 360 .dp, height = 200 .dp),
109138 contentAlignment = Alignment .Center
110139 ) {
111140 WarningTextField (
112- value = password ,
113- onValueChange = { password = it },
114- hint = " 4자리 숫자로 입장 비밀번호를 설정 " ,
115- showWarning = password.isNotEmpty() && password.length < 4 ,
116- warningMessage = " 4자리 숫자를 입력해주세요. " ,
117- maxLength = 4 ,
118- isNumberOnly = true ,
119- keyboardType = KeyboardType . NumberPassword
141+ value = text ,
142+ onValueChange = { text = it },
143+ hint = " 인풋 텍스트 " ,
144+ showWarning = true ,
145+ showIcon = false ,
146+ showLimit = true ,
147+ maxLength = 10 ,
148+ warningMessage = " 경고 메시지를 입력해주세요. "
120149 )
121150 }
122151}
123152
124153@Composable
125154@Preview(showBackground = true , backgroundColor = 0xFF000000 , widthDp = 360 , heightDp = 200 )
126155fun WarningTextFieldPreviewNormal () {
156+ var text by rememberSaveable { mutableStateOf(" " ) }
157+
158+ Box (
159+ modifier = Modifier .size(width = 360 .dp, height = 200 .dp),
160+ contentAlignment = Alignment .Center
161+ ) {
162+ WarningTextField (
163+
164+ value = text,
165+ onValueChange = { text = it },
166+ hint = " 인풋 텍스트" ,
167+ showWarning = false ,
168+ showIcon = true ,
169+ showLimit = false
170+ )
171+ }
172+ }
173+
174+ @Composable
175+ @Preview(showBackground = true , backgroundColor = 0xFF000000 , widthDp = 360 , heightDp = 200 )
176+ fun WarningTextFieldPreviewNormal_numberonly () {
127177 var password by remember { mutableStateOf(" " ) }
128178
129179 Box (
@@ -141,4 +191,4 @@ fun WarningTextFieldPreviewNormal() {
141191 keyboardType = KeyboardType .NumberPassword
142192 )
143193 }
144- }
194+ }
0 commit comments