Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textfield, OutlinedTextField 구현 #29

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.yourssu.handy.demo

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.yourssu.handy.compose.HandyTheme
import com.yourssu.handy.compose.icons.HandyIcons
import com.yourssu.handy.compose.icons.filled.Cancel
import com.yourssu.handy.compose.textfield.OutlinedTextField

@Preview
@Composable
fun OutlinedTextFieldPreview() {
HandyTheme {
Column(
modifier = Modifier
.wrapContentSize()
.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(20.dp)
) {
val (text, onValueChange) = remember { mutableStateOf("") }
OutlinedTextField(
value = text,
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
onValueChange = onValueChange,
)

OutlinedTextField(
value = text,
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
isError = true,
onValueChange = onValueChange,
)

OutlinedTextField(
value = text,
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
enabled = false,
onValueChange = onValueChange,
)

OutlinedTextField(
value = text,
placeholder = "placeholder",
enabled = false,
onValueChange = onValueChange,
)
}
}
}
57 changes: 57 additions & 0 deletions app/src/main/kotlin/com/yourssu/handy/demo/TextFieldPreview.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.yourssu.handy.demo

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.yourssu.handy.compose.HandyTheme
import com.yourssu.handy.compose.icons.HandyIcons
import com.yourssu.handy.compose.icons.filled.Cancel
import com.yourssu.handy.compose.textfield.TextField

@Preview
@Composable
fun TextFieldPreview() {
HandyTheme {
Column(
modifier = Modifier.wrapContentSize().padding(20.dp),
verticalArrangement = Arrangement.spacedBy(20.dp)
) {
val (text, onValueChange) = remember { mutableStateOf("") }
TextField(
value = text,
textLabel = "Label",
helperText = "Helper Text",
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
onValueChange = onValueChange
)

TextField(
value = text,
textLabel = "Label",
helperText = "Helper Text",
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
isError = true,
onValueChange = onValueChange
)

TextField(
value = text,
textLabel = "Label",
helperText = "Helper Text",
placeholder = "placeholder",
trailingIcon = HandyIcons.Filled.Cancel,
enabled = false,
onValueChange = onValueChange
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.yourssu.handy.compose.textfield

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.yourssu.handy.compose.HandyTheme
import com.yourssu.handy.compose.Icon
import com.yourssu.handy.compose.Text
import com.yourssu.handy.compose.foundation.HandyTypography

@Composable
fun OutlinedTextField(
value: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
placeholder: String? = null,
trailingIcon: ImageVector? = null,
enabled: Boolean = true,
isError: Boolean = false,
isSingleLine: Boolean = true,
onClickTrailingIcon: () -> Unit = {}
) {
val interactionSource = remember { MutableInteractionSource() }
val isFocused = interactionSource.collectIsFocusedAsState().value

val (borderColor, cursorColor, textColor, placeholderTextColor) = getTextFieldStyle(
enabled,
isError,
isFocused
)

Row(
modifier = modifier
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
.background(HandyTheme.colors.bgBasicLight)
.border(1.dp, borderColor, RoundedCornerShape(12.dp))
Comment on lines +49 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기두 Radius.M.dp 사용할 수 있지 않을까요..?!

.padding(start = 16.dp, end = 12.dp, top = 12.dp, bottom = 12.dp)
) {
Comment on lines +48 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surface를 사용하면 간단하게 될 것 같아요!

BasicTextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier
.weight(1f)
.fillMaxWidth(),
textStyle = HandyTypography.B1Rg16.toTextStyle().copy(color = textColor),
enabled = enabled,
singleLine = isSingleLine,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
interactionSource = interactionSource,
cursorBrush = SolidColor(cursorColor),
decorationBox = { innerTextField ->
if (value.isEmpty() && placeholder != null) {
Text(
text = placeholder,
style = HandyTypography.B1Rg16,
color = placeholderTextColor
)
}
innerTextField()
}
)

trailingIcon?.let {
Icon(
imageVector = trailingIcon,
Comment on lines +81 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trailingIcon?.let {
Icon(
imageVector = trailingIcon,
trailingIcon?.let {
Icon(
imageVector = it,

it으로 하는 게 통일성 있을 것 같아요

contentDescription = null,
tint = HandyTheme.colors.iconBasicTertiary,
modifier = Modifier
.clickable(
indication = null,
interactionSource = interactionSource,
onClick = onClickTrailingIcon
)
.padding(start = 12.dp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

padding보다는 Spacer가 명시적이어서 나을 것 같긴 한데 어떻게 생각하시나요?

)
}
}
}

data class TextFieldStyle(
val borderColor: Color,
val cursorColor: Color,
val textColor: Color,
val placeholderTextColor: Color
)

@Composable
fun getTextFieldStyle(
enabled: Boolean,
isError: Boolean,
isFocused: Boolean
): TextFieldStyle {
Comment on lines +105 to +110
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextFieldStyle의 접근 지정자를 public으로 두신 이유가 있을까요? internal이나 private이어도 될 것 같아서요.

이 함수는 외부에서 사용될 함수가 아니라면 internal이나 private로 해주시면 좋을 것 같습니다.

return TextFieldStyle(
borderColor = when {
!enabled -> HandyTheme.colors.bgBasicLight
isError -> HandyTheme.colors.lineStatusNegative
isFocused -> HandyTheme.colors.lineStatusPositive
else -> HandyTheme.colors.bgBasicLight
},
cursorColor = when {
isError && isFocused -> HandyTheme.colors.lineStatusNegative
isFocused -> HandyTheme.colors.lineStatusPositive
else -> HandyTheme.colors.textBasicPrimary
},
textColor = if (!enabled) HandyTheme.colors.textBasicDisabled else HandyTheme.colors.textBasicPrimary,
placeholderTextColor = if (!enabled) HandyTheme.colors.textBasicDisabled else HandyTheme.colors.textBasicTertiary
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 TextArea는 별도의 PR로 올려주시고 여기서는 제외하는 게 좋을 것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.yourssu.handy.compose.textfield

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.yourssu.handy.compose.HandyTheme
import com.yourssu.handy.compose.Text
import com.yourssu.handy.compose.foundation.HandyTypography

@Composable
fun TextArea(
value: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
placeholder: String? = null,
helperText: String? = null,
maxCharacters: Int = Int.MAX_VALUE,
enabled: Boolean = true,
isError: Boolean = false,
) {
val textHelperColor = when {
isError -> HandyTheme.colors.lineStatusNegative
else -> HandyTheme.colors.textBasicTertiary
}

Column(
modifier = modifier
) {
OutlinedTextField(
value = value,
onValueChange = {
onValueChange(it)
},
placeholder = placeholder,
enabled = enabled,
isError = isError,
isSingleLine = false,
modifier = Modifier
)

helperText?.let {
Text(
text = it,
style = HandyTypography.B5Rg12,
color = textHelperColor,
modifier = Modifier.padding(top = 4.dp)
)
}
}
}

@Preview
@Composable
fun TextAreaPreview() {
HandyTheme {
Column(
modifier = Modifier.wrapContentSize().padding(20.dp),
verticalArrangement = Arrangement.spacedBy(20.dp)
) {
val (text, onValueChange) = remember {
androidx.compose.runtime.mutableStateOf(
"inputText inputText asdaasdas inputText inputText asdaasdas inputText inputText asdaasdas"
)
}

TextArea(
value = text,
onValueChange = onValueChange,
placeholder = "Placeholder",
helperText = "Helper Text",
maxCharacters = 20,
enabled = true,
)
}
}
}
Loading