-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
896e8c6
commit 8c43cfb
Showing
12 changed files
with
381 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/cn/idesign/cui/testclient/form/FormActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cn.idesign.cui.testclient.form | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TopAppBar | ||
import androidx.compose.ui.Modifier | ||
import cn.idesign.cui.testclient.ui.theme.CUITestTheme | ||
|
||
class FormActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
CUITestTheme { | ||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { | ||
Text( | ||
text = "Form示例", | ||
color = MaterialTheme.colors.onPrimary | ||
) | ||
}, | ||
backgroundColor = MaterialTheme.colors.primary, | ||
) | ||
}, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
FormTest() | ||
} | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/cn/idesign/cui/testclient/form/FormTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cn.idesign.cui.testclient.form | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.ContentAlpha | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import cn.idesign.cui.form.Form | ||
import cn.idesign.cui.form.FormField | ||
import cn.idesign.cui.form.FormInput | ||
import cn.idesign.cui.form.FormItemType | ||
import cn.idesign.cui.form.composable | ||
import cn.idesign.cui.form.rememberFormState | ||
|
||
@Composable | ||
fun FormTest() { | ||
val context = LocalContext.current | ||
LazyColumn(Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) { | ||
item { | ||
Text( | ||
text = "基本用法", | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 5.dp), | ||
color = MaterialTheme.colors.onSurface.copy(ContentAlpha.high), | ||
style = MaterialTheme.typography.subtitle1.copy(fontWeight = FontWeight.Medium) | ||
) | ||
val state = rememberFormState() | ||
Form( | ||
state = state, | ||
){ | ||
composable( | ||
label = "姓名", | ||
name = "username", | ||
) { | ||
FormInput(state = it) | ||
} | ||
composable( | ||
label = "手机号", | ||
name = "mobile", | ||
) { | ||
FormInput(state = it) | ||
} | ||
|
||
composable(type = FormItemType.Submit) { | ||
Button(onClick = {state.submit() }) { | ||
Text(text = "提交") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package cn.idesign.cui.form | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.text.BasicTextField | ||
import androidx.compose.foundation.text.KeyboardActions | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material.ContentAlpha | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.input.VisualTransformation | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
|
||
@SuppressLint("RememberReturnType") | ||
@Composable | ||
fun Form( | ||
state: FormState = rememberFormState(), | ||
onFinish: ((Map<String, Any>) -> Unit)? = null, | ||
builder: FormBuilder.() -> Unit, | ||
) { | ||
|
||
Column() { | ||
FormBuilder().apply(builder).formItems.forEach { item -> | ||
val itemState = item.state ?: rememberFormItemState() | ||
remember(itemState.value) { | ||
println("---> itemState key:${item.name},value:${itemState.value}") | ||
item.name?.let { | ||
state.value.put(it, itemState.value) | ||
} | ||
} | ||
FormField( | ||
label = item.label, | ||
state = itemState, | ||
showDivider = item.type === FormItemType.Normal | ||
) { | ||
item.content(itemState) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun FormField( | ||
modifier: Modifier = Modifier, | ||
label: String? = null, | ||
labelAlign: TextAlign = TextAlign.Left, | ||
labelWeight: Float = 1f, | ||
wrapperWeight: Float = 3f, | ||
verticalAlignment: Alignment.Vertical = Alignment.CenterVertically, | ||
showDivider: Boolean = true, | ||
state: FormItemState, | ||
content: @Composable() () -> Unit | ||
) { | ||
println("FormField state:$state") | ||
Column { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.background(MaterialTheme.colors.background) | ||
.then(modifier), | ||
verticalAlignment = verticalAlignment, | ||
) { | ||
label?.let { | ||
Text( | ||
text = label, | ||
modifier = Modifier.weight(labelWeight), | ||
textAlign = labelAlign | ||
) | ||
} | ||
Box(modifier = Modifier.weight(wrapperWeight)) { | ||
content.invoke() | ||
} | ||
} | ||
if (showDivider) { | ||
Divider() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun FormInput( | ||
state: FormItemState, | ||
placeholder: String = "请输入搜索信息", | ||
color: Color = MaterialTheme.colors.onSurface.copy(ContentAlpha.disabled), | ||
) { | ||
BasicTextField( | ||
value = state.value, | ||
onValueChange = { | ||
state.value = it | ||
}, | ||
singleLine = true, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.height(48.dp), | ||
textStyle = MaterialTheme.typography.body2, | ||
visualTransformation = VisualTransformation.None, | ||
keyboardOptions = KeyboardOptions.Default, | ||
keyboardActions = KeyboardActions.Default, | ||
|
||
decorationBox = { innerTextField -> | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize(), | ||
contentAlignment = Alignment.CenterStart | ||
) { | ||
if (state.value.isEmpty()) { | ||
Text( | ||
modifier = Modifier | ||
.fillMaxWidth(), | ||
text = placeholder, | ||
color = color, | ||
) | ||
} | ||
innerTextField() | ||
} | ||
} | ||
) | ||
} | ||
|
||
@Preview("form") | ||
@Composable | ||
fun FormPreview() { | ||
Form() { | ||
composable( | ||
label = "姓名", | ||
name = "username", | ||
) { | ||
FormInput(state = it) | ||
} | ||
composable( | ||
label = "手机号", | ||
name = "mobile", | ||
) { | ||
FormInput(state = it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.idesign.cui.form | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
public open class FormBuilder { | ||
private val items = mutableListOf<FormItem>() | ||
|
||
public val formItems | ||
get() = items | ||
|
||
public fun addItem(item: FormItem) { | ||
items += item | ||
} | ||
} | ||
|
||
fun FormBuilder.composable( | ||
label: String?= null, | ||
name: String?= null, | ||
type: FormItemType? = FormItemType.Normal, | ||
state: FormItemState? = null, | ||
content: @Composable (FormItemState) -> Unit | ||
) { | ||
addItem(FormItem(label, name,type, state, content)) | ||
} | ||
|
||
class FormItem( | ||
val label: String? = null, | ||
val name: String? = null, | ||
val type: FormItemType? = FormItemType.Normal, | ||
val state: FormItemState?, | ||
internal val content: @Composable (FormItemState) -> Unit | ||
) | ||
|
||
sealed class FormItemType { | ||
object Normal : FormItemType() | ||
object Submit : FormItemType() | ||
} |
Oops, something went wrong.