Skip to content

Commit 34f56bf

Browse files
committed
[refactor]: WarningTextField 컴포넌트 수정(#37)
1 parent 871bd62 commit 34f56bf

1 file changed

Lines changed: 86 additions & 40 deletions

File tree

app/src/main/java/com/texthip/thip/ui/common/forms/WarningTextField.kt

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import androidx.compose.foundation.clickable
44
import androidx.compose.foundation.layout.Box
55
import androidx.compose.foundation.layout.Column
66
import androidx.compose.foundation.layout.Spacer
7+
import androidx.compose.foundation.layout.fillMaxSize
78
import androidx.compose.foundation.layout.height
9+
import androidx.compose.foundation.layout.padding
810
import androidx.compose.foundation.layout.size
911
import androidx.compose.foundation.shape.RoundedCornerShape
1012
import androidx.compose.material3.Icon
@@ -31,52 +33,83 @@ import com.texthip.thip.ui.theme.ThipTheme.typography
3133
fun WarningTextField(
3234
modifier: Modifier = Modifier,
3335
hint: String,
36+
text: String,
37+
onTextChange: (String) -> Unit,
3438
warningMessage: String = "경고 메시지를 입력해주세요.",
35-
showWarning: Boolean
36-
) {
37-
var text by rememberSaveable { mutableStateOf("") }
39+
showWarning: Boolean = false,
40+
showLimit: Boolean = true,
41+
limit: Int = 10,
42+
showIcon: Boolean = false
43+
) {
44+
//var text by rememberSaveable { mutableStateOf("") }
3845
val myStyle = typography.menu_r400_s14_h24.copy(lineHeight = 14.sp)
3946

47+
//val displayText = if (showLimit && text.length > limit) text.substring(0, limit) else text
48+
49+
4050
Column {
41-
OutlinedTextField(
42-
value = text,
43-
onValueChange = { text = it },
44-
placeholder = {
45-
Text(
46-
text = hint,
47-
color = colors.Grey02,
48-
style = myStyle
49-
)
50-
},
51-
textStyle = myStyle,
52-
modifier = modifier.size(width = 320.dp, height = 48.dp),
53-
shape = RoundedCornerShape(12.dp),
54-
colors = TextFieldDefaults.colors(
55-
focusedTextColor = colors.White,
56-
focusedIndicatorColor = if (showWarning) colors.Red else Color.Transparent,
57-
unfocusedIndicatorColor = if (showWarning) colors.Red else Color.Transparent,
58-
focusedContainerColor = colors.Black,
59-
unfocusedContainerColor = colors.Black,
60-
cursorColor = colors.NeonGreen
61-
),
62-
trailingIcon = {
63-
if (text.isNotEmpty()) {
64-
Icon(
65-
painter = painterResource(id = R.drawable.ic_x_circle_white),
66-
contentDescription = "Clear text",
67-
modifier = Modifier.clickable { text = "" },
68-
tint = Color.Unspecified
51+
Box(
52+
modifier = modifier
53+
.height(48.dp)
54+
) {
55+
OutlinedTextField(
56+
value = text,
57+
onValueChange = {
58+
val updated = if (showLimit && it.length > limit) it.substring(0, limit) else it
59+
onTextChange(updated)
60+
},
61+
placeholder = {
62+
Text(
63+
text = hint,
64+
color = colors.Grey02,
65+
style = myStyle
6966
)
70-
} else {
71-
Icon(
72-
painter = painterResource(id = R.drawable.ic_x_circle),
73-
contentDescription = "Clear text"
67+
},
68+
textStyle = myStyle,
69+
modifier = Modifier.fillMaxSize(),
70+
shape = RoundedCornerShape(12.dp),
71+
colors = TextFieldDefaults.colors(
72+
focusedTextColor = colors.White,
73+
focusedIndicatorColor = if (showWarning) colors.Red else Color.Transparent,
74+
unfocusedIndicatorColor = if (showWarning) colors.Red else Color.Transparent,
75+
focusedContainerColor = colors.Black,
76+
unfocusedContainerColor = colors.Black,
77+
cursorColor = colors.NeonGreen
78+
),
79+
trailingIcon = {
80+
if (showIcon) {
81+
if (text.isNotEmpty()) {
82+
Icon(
83+
painter = painterResource(id = R.drawable.ic_x_circle_white),
84+
contentDescription = "Clear text",
85+
modifier = Modifier.clickable { onTextChange("")},
86+
tint = Color.Unspecified
87+
)
88+
} else {
89+
Icon(
90+
painter = painterResource(id = R.drawable.ic_x_circle),
91+
contentDescription = "Clear text"
92+
)
93+
}
94+
}
95+
},
96+
singleLine = true
97+
)
98+
99+
if (showLimit) {
100+
Box(
101+
modifier = Modifier
102+
.align(Alignment.CenterEnd)
103+
.padding(end = 14.dp)
104+
) {
105+
Text(
106+
text = "${text.length}/$limit",
107+
color = colors.White,
108+
style = typography.info_r400_s12_h24
74109
)
75110
}
76-
},
77-
singleLine = true
78-
)
79-
111+
}
112+
}
80113
if (showWarning) {
81114
Spacer(modifier = Modifier.height(4.dp))
82115
Text(
@@ -91,13 +124,20 @@ fun WarningTextField(
91124
@Composable
92125
@Preview(showBackground = true, backgroundColor = 0xFF000000, widthDp = 360, heightDp = 200)
93126
fun WarningTextFieldPreviewEmpty() {
127+
var text by rememberSaveable { mutableStateOf("") }
128+
94129
Box(
95130
modifier = Modifier.size(width = 360.dp, height = 200.dp),
96131
contentAlignment = Alignment.Center
97132
) {
98133
WarningTextField(
134+
text = text,
135+
onTextChange = { text = it },
99136
hint = "인풋 텍스트",
100137
showWarning = true,
138+
showIcon = false,
139+
showLimit = true,
140+
limit = 10,
101141
warningMessage = "경고 메시지를 입력해주세요."
102142
)
103143
}
@@ -106,13 +146,19 @@ fun WarningTextFieldPreviewEmpty() {
106146
@Composable
107147
@Preview(showBackground = true, backgroundColor = 0xFF000000, widthDp = 360, heightDp = 200)
108148
fun WarningTextFieldPreviewNormal() {
149+
var text by rememberSaveable { mutableStateOf("") }
150+
109151
Box(
110152
modifier = Modifier.size(width = 360.dp, height = 200.dp),
111153
contentAlignment = Alignment.Center
112154
) {
113155
WarningTextField(
156+
text = text,
157+
onTextChange = { text = it },
114158
hint = "인풋 텍스트",
115-
showWarning = false
159+
showWarning = false,
160+
showIcon = true,
161+
showLimit = false
116162
)
117163
}
118164
}

0 commit comments

Comments
 (0)