-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tag): Add missing test, examples, configurator for tag component (
#785)
- Loading branch information
1 parent
a96ec77
commit 5bec64f
Showing
25 changed files
with
624 additions
and
46 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
218 changes: 218 additions & 0 deletions
218
.../src/main/kotlin/com/adevinta/spark/catalog/configurator/samples/tags/TagsConfigurator.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,218 @@ | ||
/* | ||
* Copyright (c) 2023 Adevinta | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.adevinta.spark.catalog.configurator.samples.tags | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
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.font.FontWeight | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.adevinta.spark.SparkTheme | ||
import com.adevinta.spark.catalog.model.Configurator | ||
import com.adevinta.spark.catalog.themes.SegmentedButton | ||
import com.adevinta.spark.catalog.util.SampleSourceUrl | ||
import com.adevinta.spark.components.icons.FilledTonalIconToggleButton | ||
import com.adevinta.spark.components.icons.Icon | ||
import com.adevinta.spark.components.menu.DropdownMenuItem | ||
import com.adevinta.spark.components.surface.Surface | ||
import com.adevinta.spark.components.tags.TagFilled | ||
import com.adevinta.spark.components.tags.TagIntent | ||
import com.adevinta.spark.components.tags.TagOutlined | ||
import com.adevinta.spark.components.tags.TagTinted | ||
import com.adevinta.spark.components.text.Text | ||
import com.adevinta.spark.components.textfields.SelectTextField | ||
import com.adevinta.spark.components.textfields.TextField | ||
import com.adevinta.spark.icons.LikeFill | ||
import com.adevinta.spark.icons.SparkIcon | ||
import com.adevinta.spark.icons.SparkIcons | ||
|
||
public val TagsConfigurator: Configurator = Configurator( | ||
name = "Tag", | ||
description = "Tag configuration", | ||
sourceUrl = "$SampleSourceUrl/TagSamples.kt", | ||
) { | ||
TagSample() | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
@Preview( | ||
showBackground = true, | ||
) | ||
@Composable | ||
private fun TagSample() { | ||
val scrollState = rememberScrollState() | ||
Column( | ||
verticalArrangement = Arrangement.spacedBy(16.dp), | ||
modifier = Modifier.verticalScroll(scrollState), | ||
) { | ||
var icon: SparkIcon? by remember { mutableStateOf(null) } | ||
var style by remember { mutableStateOf(TagStyle.Filled) } | ||
var intent by remember { mutableStateOf(TagIntent.Main) } | ||
var buttonText by remember { mutableStateOf("Filled Tag") } | ||
|
||
ConfigedTag( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
style = style, | ||
tagText = buttonText, | ||
intent = intent, | ||
icon = icon, | ||
) | ||
|
||
Row( | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.Absolute.spacedBy(8.dp), | ||
) { | ||
Text( | ||
text = "With Icon", | ||
modifier = Modifier | ||
.weight(1f) | ||
.padding(bottom = 8.dp), | ||
style = SparkTheme.typography.body2.copy(fontWeight = FontWeight.Bold), | ||
) | ||
FilledTonalIconToggleButton( | ||
checked = icon != null, | ||
onCheckedChange = { | ||
icon = if (it) SparkIcons.LikeFill else null | ||
}, | ||
) { | ||
Icon( | ||
sparkIcon = SparkIcons.LikeFill, | ||
contentDescription = null, | ||
) | ||
} | ||
} | ||
|
||
Column { | ||
Text( | ||
text = "Style", | ||
modifier = Modifier.padding(bottom = 8.dp), | ||
style = SparkTheme.typography.body2.copy(fontWeight = FontWeight.Bold), | ||
) | ||
val tagStyles = TagStyle.entries | ||
val tagStylesLabel = tagStyles.map { it.name } | ||
SegmentedButton( | ||
options = tagStylesLabel, | ||
selectedOption = style.name, | ||
onOptionSelect = { style = TagStyle.valueOf(it) }, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.height(48.dp), | ||
) | ||
} | ||
|
||
val intents = TagIntent.entries | ||
var expanded by remember { mutableStateOf(false) } | ||
SelectTextField( | ||
modifier = Modifier.fillMaxWidth(), | ||
value = intent.name, | ||
onValueChange = {}, | ||
readOnly = true, | ||
label = "Intent", | ||
expanded = expanded, | ||
onExpandedChange = { | ||
expanded = !expanded | ||
}, | ||
onDismissRequest = { | ||
expanded = false | ||
}, | ||
dropdownContent = { | ||
intents.forEach { | ||
DropdownMenuItem( | ||
text = { Text(it.name) }, | ||
onClick = { | ||
intent = it | ||
expanded = false | ||
}, | ||
) | ||
} | ||
}, | ||
) | ||
|
||
TextField( | ||
modifier = Modifier.fillMaxWidth(), | ||
value = buttonText, | ||
onValueChange = { | ||
buttonText = it | ||
}, | ||
label = "Tag text", | ||
placeholder = "Vérifier les Disponibilité", | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ConfigedTag( | ||
modifier: Modifier = Modifier, | ||
style: TagStyle, | ||
tagText: String, | ||
intent: TagIntent, | ||
icon: SparkIcon?, | ||
) { | ||
Surface( | ||
modifier = modifier, | ||
color = SparkTheme.colors.backgroundVariant, | ||
) { | ||
Box( | ||
modifier = Modifier.padding(8.dp), | ||
) { | ||
when (style) { | ||
TagStyle.Filled -> TagFilled( | ||
text = tagText, | ||
intent = intent, | ||
leadingIcon = icon, | ||
) | ||
|
||
TagStyle.Outlined -> TagOutlined( | ||
text = tagText, | ||
intent = intent, | ||
leadingIcon = icon, | ||
) | ||
|
||
TagStyle.Tinted -> TagTinted( | ||
text = tagText, | ||
intent = intent, | ||
leadingIcon = icon, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private enum class TagStyle { | ||
Filled, | ||
Outlined, | ||
Tinted, | ||
} |
141 changes: 141 additions & 0 deletions
141
catalog/src/main/kotlin/com/adevinta/spark/catalog/examples/samples/tags/TagsExamples.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,141 @@ | ||
/* | ||
* Copyright (c) 2023 Adevinta | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.adevinta.spark.catalog.examples.samples.tags | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.ExperimentalLayoutApi | ||
import androidx.compose.foundation.layout.FlowRow | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.unit.dp | ||
import com.adevinta.spark.catalog.model.Example | ||
import com.adevinta.spark.catalog.util.SampleSourceUrl | ||
import com.adevinta.spark.components.tags.TagFilled | ||
import com.adevinta.spark.components.tags.TagIntent | ||
import com.adevinta.spark.components.tags.TagOutlined | ||
import com.adevinta.spark.components.tags.TagTinted | ||
import com.adevinta.spark.icons.Booster | ||
import com.adevinta.spark.icons.SparkIcon | ||
import com.adevinta.spark.icons.SparkIcons | ||
|
||
private const val TagsExampleDescription = "Tags examples" | ||
private const val TagsExampleSourceUrl = "$SampleSourceUrl/TagSamples.kt" | ||
|
||
@OptIn(ExperimentalLayoutApi::class) | ||
public val TagsExamples: List<Example> = listOf( | ||
Example( | ||
name = "Filled Tag", | ||
description = TagsExampleDescription, | ||
sourceUrl = TagsExampleSourceUrl, | ||
) { | ||
TagSample( | ||
tag = { text, intent, leadingIcon -> | ||
TagFilled( | ||
text = text, | ||
intent = intent, | ||
leadingIcon = leadingIcon, | ||
) | ||
}, | ||
) | ||
}, | ||
Example( | ||
name = "Tinted Tag", | ||
description = TagsExampleDescription, | ||
sourceUrl = TagsExampleSourceUrl, | ||
) { | ||
TagSample( | ||
tag = { text, intent, leadingIcon -> | ||
TagTinted( | ||
text = text, | ||
intent = intent, | ||
leadingIcon = leadingIcon, | ||
) | ||
}, | ||
) | ||
}, | ||
Example( | ||
name = "Outlined Tag", | ||
description = TagsExampleDescription, | ||
sourceUrl = TagsExampleSourceUrl, | ||
) { | ||
TagSample( | ||
tag = { text, intent, leadingIcon -> | ||
TagOutlined( | ||
text = text, | ||
intent = intent, | ||
leadingIcon = leadingIcon, | ||
) | ||
}, | ||
) | ||
}, | ||
Example( | ||
name = "Tag layouts", | ||
description = "Showcase how to layout tags sao that they don't clip on parent width but can go to a new " + | ||
"line if they don't fit", | ||
sourceUrl = TagsExampleSourceUrl, | ||
) { | ||
FlowRow( | ||
horizontalArrangement = Arrangement.spacedBy(8.dp), | ||
verticalArrangement = Arrangement.spacedBy(8.dp), | ||
maxItemsInEachRow = 4, | ||
) { | ||
TagFilled(text = "Tag 1", intent = TagIntent.Main) | ||
TagFilled(text = "Tag longer 2", intent = TagIntent.Accent) | ||
TagFilled(text = "Tag a bit longer 3", intent = TagIntent.Info) | ||
TagTinted(text = "Tag way more longer 4", intent = TagIntent.Main) | ||
TagTinted(text = "Tag small 5", intent = TagIntent.Main) | ||
TagOutlined(text = "Tag 6", intent = TagIntent.Main) | ||
} | ||
}, | ||
) | ||
|
||
@Composable | ||
private fun TagSample( | ||
tag: @Composable ( | ||
text: String, | ||
intent: TagIntent, | ||
leadingIcon: SparkIcon?, | ||
) -> Unit, | ||
) { | ||
Column( | ||
verticalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
val icon = SparkIcons.Booster | ||
val tagText = "available" | ||
|
||
tag( | ||
/* text = */ tagText, | ||
/* intent = */TagIntent.Main, | ||
/* leadingIcon = */ null, | ||
) | ||
tag( | ||
/* text = */ tagText, | ||
/* intent = */TagIntent.Main, | ||
/* leadingIcon = */ icon, | ||
) | ||
tag( | ||
/* text = */ "", | ||
/* intent = */TagIntent.Main, | ||
/* leadingIcon = */ icon, | ||
) | ||
} | ||
} |
Oops, something went wrong.