From 84c9dd376212699f43ace5e79025538b5b9c25de Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 18 Jul 2023 22:04:23 +0530 Subject: [PATCH] feat(common): add a simple SearchBar component Material 3's SearchBar has way too much "interaction" for my liking --- .../dev/msfjarvis/claw/common/ui/SearchBar.kt | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 common/src/main/kotlin/dev/msfjarvis/claw/common/ui/SearchBar.kt diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/ui/SearchBar.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/ui/SearchBar.kt new file mode 100644 index 00000000..5b7e4ff4 --- /dev/null +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/ui/SearchBar.kt @@ -0,0 +1,71 @@ +/* + * Copyright © 2023 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.common.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.focusable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.KeyboardActions +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextField +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.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import dev.msfjarvis.claw.common.theme.LobstersTheme + +@Composable +fun SearchBar( + value: String, + onValueChange: (String) -> Unit, + onSearch: (String) -> Unit, + modifier: Modifier = Modifier, +) { + TextField( + value = value, + onValueChange = onValueChange, + shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp), + textStyle = MaterialTheme.typography.bodyLarge, + placeholder = { Text(text = "Search") }, + keyboardActions = KeyboardActions(onSearch = { onSearch(value) }), + keyboardOptions = + KeyboardOptions( + keyboardType = KeyboardType.Text, + imeAction = ImeAction.Search, + ), + singleLine = true, + modifier = modifier.focusable(), + ) +} + +@Preview +@Composable +fun SearchBarPreview() { + LobstersTheme { + Box(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.background).padding(8.dp)) { + var value by remember { mutableStateOf("") } + SearchBar( + value = value, + onValueChange = { value = it }, + onSearch = {}, + modifier = Modifier.align(Alignment.TopCenter) + ) + } + } +}