|
| 1 | +package com.yapp.ndgl.core.ui.designsystem |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.heightIn |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 12 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 13 | +import androidx.compose.foundation.text.BasicTextField |
| 14 | +import androidx.compose.foundation.text.KeyboardOptions |
| 15 | +import androidx.compose.material3.Surface |
| 16 | +import androidx.compose.material3.Text |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.LaunchedEffect |
| 19 | +import androidx.compose.runtime.getValue |
| 20 | +import androidx.compose.runtime.mutableStateOf |
| 21 | +import androidx.compose.runtime.remember |
| 22 | +import androidx.compose.runtime.setValue |
| 23 | +import androidx.compose.ui.Alignment |
| 24 | +import androidx.compose.ui.Modifier |
| 25 | +import androidx.compose.ui.draw.clip |
| 26 | +import androidx.compose.ui.focus.FocusRequester |
| 27 | +import androidx.compose.ui.focus.focusRequester |
| 28 | +import androidx.compose.ui.text.TextRange |
| 29 | +import androidx.compose.ui.text.TextStyle |
| 30 | +import androidx.compose.ui.text.input.ImeAction |
| 31 | +import androidx.compose.ui.text.input.KeyboardType |
| 32 | +import androidx.compose.ui.text.input.TextFieldValue |
| 33 | +import androidx.compose.ui.text.style.TextAlign |
| 34 | +import androidx.compose.ui.tooling.preview.Preview |
| 35 | +import androidx.compose.ui.unit.Dp |
| 36 | +import androidx.compose.ui.unit.dp |
| 37 | +import androidx.compose.ui.window.Dialog |
| 38 | +import com.yapp.ndgl.core.ui.theme.NDGLTheme |
| 39 | + |
| 40 | +@Composable |
| 41 | +fun NDGLInputModal( |
| 42 | + modifier: Modifier = Modifier, |
| 43 | + onDismissRequest: () -> Unit, |
| 44 | + title: String, |
| 45 | + value: String, |
| 46 | + onValueChange: (String) -> Unit, |
| 47 | + placeholder: String, |
| 48 | + keyboardOptions: KeyboardOptions, |
| 49 | + positiveButtonText: String, |
| 50 | + onPositiveButtonClick: () -> Unit, |
| 51 | + negativeButtonText: String, |
| 52 | + onNegativeButtonClick: (() -> Unit) = {}, |
| 53 | + minHeight: Dp = 56.dp, |
| 54 | + maxLines: Int = Int.MAX_VALUE, |
| 55 | + textStyle: TextStyle, |
| 56 | + placeholderStyle: TextStyle, |
| 57 | + textAlign: TextAlign = TextAlign.Start, |
| 58 | +) { |
| 59 | + val focusRequester = remember { FocusRequester() } |
| 60 | + var textFieldValue by remember(value) { |
| 61 | + mutableStateOf(TextFieldValue(value, selection = TextRange(value.length))) |
| 62 | + } |
| 63 | + |
| 64 | + LaunchedEffect(Unit) { |
| 65 | + focusRequester.requestFocus() |
| 66 | + } |
| 67 | + |
| 68 | + // value가 변경되면 textFieldValue 업데이트 |
| 69 | + LaunchedEffect(value) { |
| 70 | + if (textFieldValue.text != value) { |
| 71 | + textFieldValue = TextFieldValue( |
| 72 | + text = value, |
| 73 | + selection = TextRange(value.length), |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Dialog(onDismissRequest = onDismissRequest) { |
| 79 | + Surface( |
| 80 | + modifier = modifier.wrapContentHeight(), |
| 81 | + shape = RoundedCornerShape(8.dp), |
| 82 | + color = NDGLTheme.colors.white, |
| 83 | + shadowElevation = 16.dp, |
| 84 | + ) { |
| 85 | + Column( |
| 86 | + modifier = Modifier |
| 87 | + .padding(horizontal = 28.dp) |
| 88 | + .padding(top = 28.dp, bottom = 24.dp), |
| 89 | + verticalArrangement = Arrangement.spacedBy(28.dp), |
| 90 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 91 | + ) { |
| 92 | + Text( |
| 93 | + text = title, |
| 94 | + modifier = Modifier.fillMaxWidth(), |
| 95 | + color = NDGLTheme.colors.black900, |
| 96 | + textAlign = TextAlign.Center, |
| 97 | + style = NDGLTheme.typography.subtitleLgSemiBold, |
| 98 | + ) |
| 99 | + |
| 100 | + BasicTextField( |
| 101 | + value = textFieldValue, |
| 102 | + onValueChange = { newValue -> |
| 103 | + textFieldValue = newValue |
| 104 | + onValueChange(newValue.text) |
| 105 | + }, |
| 106 | + modifier = Modifier |
| 107 | + .fillMaxWidth() |
| 108 | + .heightIn(min = minHeight) |
| 109 | + .clip(RoundedCornerShape(8.dp)) |
| 110 | + .padding(horizontal = 16.dp, vertical = 16.dp) |
| 111 | + .focusRequester(focusRequester), |
| 112 | + textStyle = textStyle.copy( |
| 113 | + color = NDGLTheme.colors.black500, |
| 114 | + textAlign = textAlign, |
| 115 | + ), |
| 116 | + keyboardOptions = keyboardOptions, |
| 117 | + maxLines = maxLines, |
| 118 | + decorationBox = { innerTextField -> |
| 119 | + Box( |
| 120 | + modifier = Modifier.fillMaxWidth(), |
| 121 | + contentAlignment = when (textAlign) { |
| 122 | + TextAlign.Center -> Alignment.Center |
| 123 | + else -> Alignment.TopStart |
| 124 | + }, |
| 125 | + ) { |
| 126 | + if (textFieldValue.text.isEmpty()) { |
| 127 | + Text( |
| 128 | + text = placeholder, |
| 129 | + style = placeholderStyle, |
| 130 | + color = NDGLTheme.colors.black300, |
| 131 | + ) |
| 132 | + } |
| 133 | + innerTextField() |
| 134 | + } |
| 135 | + }, |
| 136 | + ) |
| 137 | + |
| 138 | + Row( |
| 139 | + modifier = Modifier.fillMaxWidth(), |
| 140 | + horizontalArrangement = Arrangement.spacedBy(16.dp), |
| 141 | + ) { |
| 142 | + NDGLCTAButton( |
| 143 | + modifier = Modifier.weight(1f), |
| 144 | + type = NDGLCTAButtonAttr.Type.SECONDARY, |
| 145 | + size = NDGLCTAButtonAttr.Size.MEDIUM, |
| 146 | + status = NDGLCTAButtonAttr.Status.ACTIVE, |
| 147 | + label = negativeButtonText, |
| 148 | + onClick = { |
| 149 | + onNegativeButtonClick() |
| 150 | + onDismissRequest() |
| 151 | + }, |
| 152 | + ) |
| 153 | + NDGLCTAButton( |
| 154 | + modifier = Modifier.weight(1f), |
| 155 | + type = NDGLCTAButtonAttr.Type.PRIMARY, |
| 156 | + size = NDGLCTAButtonAttr.Size.MEDIUM, |
| 157 | + status = if (value.isNotEmpty()) { |
| 158 | + NDGLCTAButtonAttr.Status.ACTIVE |
| 159 | + } else { |
| 160 | + NDGLCTAButtonAttr.Status.DISABLED |
| 161 | + }, |
| 162 | + label = positiveButtonText, |
| 163 | + onClick = { |
| 164 | + if (value.isNotEmpty()) { |
| 165 | + onPositiveButtonClick() |
| 166 | + onDismissRequest() |
| 167 | + } |
| 168 | + }, |
| 169 | + ) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +@Preview(showBackground = true) |
| 177 | +@Composable |
| 178 | +private fun NDGLInputModalCostPreview() { |
| 179 | + var value by remember { mutableStateOf("10000") } |
| 180 | + |
| 181 | + NDGLTheme { |
| 182 | + Box(Modifier.fillMaxSize()) { |
| 183 | + NDGLInputModal( |
| 184 | + onDismissRequest = {}, |
| 185 | + title = "비용 추가", |
| 186 | + value = value, |
| 187 | + onValueChange = { newValue -> |
| 188 | + if (newValue.isEmpty() || newValue.all { it.isDigit() }) { |
| 189 | + value = newValue |
| 190 | + } |
| 191 | + }, |
| 192 | + placeholder = "비용을 추가해 보세요", |
| 193 | + keyboardOptions = KeyboardOptions( |
| 194 | + keyboardType = KeyboardType.Number, |
| 195 | + imeAction = ImeAction.Done, |
| 196 | + ), |
| 197 | + positiveButtonText = "확인", |
| 198 | + onPositiveButtonClick = {}, |
| 199 | + negativeButtonText = "취소", |
| 200 | + textAlign = TextAlign.Center, |
| 201 | + placeholderStyle = NDGLTheme.typography.subtitleLgSemiBold, |
| 202 | + textStyle = NDGLTheme.typography.subtitleLgSemiBold, |
| 203 | + ) |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +@Preview(showBackground = true) |
| 209 | +@Composable |
| 210 | +private fun NDGLInputModalMemoPreview() { |
| 211 | + var value by remember { mutableStateOf("") } |
| 212 | + |
| 213 | + NDGLTheme { |
| 214 | + Box(Modifier.fillMaxSize()) { |
| 215 | + NDGLInputModal( |
| 216 | + onDismissRequest = {}, |
| 217 | + title = "메모 추가", |
| 218 | + value = value, |
| 219 | + onValueChange = { value = it }, |
| 220 | + placeholder = "정보들을 메모해 보세요", |
| 221 | + keyboardOptions = KeyboardOptions( |
| 222 | + keyboardType = KeyboardType.Text, |
| 223 | + imeAction = ImeAction.Default, |
| 224 | + ), |
| 225 | + positiveButtonText = "확인", |
| 226 | + onPositiveButtonClick = {}, |
| 227 | + negativeButtonText = "취소", |
| 228 | + minHeight = 120.dp, |
| 229 | + placeholderStyle = NDGLTheme.typography.bodyLgMedium, |
| 230 | + textStyle = NDGLTheme.typography.bodyLgRegular, |
| 231 | + ) |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments