Skip to content

Commit

Permalink
chore(tag): Add missing test, examples, configurator for tag component (
Browse files Browse the repository at this point in the history
  • Loading branch information
soulcramer committed Oct 24, 2023
1 parent a96ec77 commit 5bec64f
Show file tree
Hide file tree
Showing 25 changed files with 624 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -279,7 +278,7 @@ private fun ConfiguredButton(
) {
val containerColor by animateColorAsState(
targetValue = if (intent != ButtonIntent.Surface) {
Color.Transparent
SparkTheme.colors.backgroundVariant
} else {
SparkTheme.colors.surfaceInverse
},
Expand All @@ -289,7 +288,7 @@ private fun ConfiguredButton(
color = containerColor,
) {
Box(
modifier = Modifier.padding(4.dp),
modifier = Modifier.padding(8.dp),
) {
when (style) {
ButtonStyle.Filled -> ButtonFilled(
Expand Down
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,
}
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,
)
}
}
Loading

0 comments on commit 5bec64f

Please sign in to comment.