forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchBarPersonaChip.kt
139 lines (136 loc) · 5.84 KB
/
SearchBarPersonaChip.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.microsoft.fluentui.tokenized.persona
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.persona.R
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.PersonaChipSize
import com.microsoft.fluentui.theme.token.controlTokens.SearchBarPersonaChipInfo
import com.microsoft.fluentui.theme.token.controlTokens.SearchBarPersonaChipTokens
/**
* [SearchBarPersonaChip] is a compact representations of entities(most commonly, people)that can be types in, deleted or dragged easily
*
* @param person Person data for the persona chip
* @param modifier Modifier for the persona chip
* @param style Optional persona chip style. See [FluentStyle]
* @param size Option persona chip size. See [PersonaChipSize]
* @param enabled Whether persona chip is enabled or disabled. Enabled by default.
* @param selected Whether persona chip is selected or unselected. Unselected by default.
* @param onClick onClick action for persona chip
* @param onCloseClick onClick action for close button. This action is performed after the chip is selected and on the close icon
* @param interactionSource Optional interactionSource
* @param searchbarPersonaChipTokens Optional tokens for persona chip
*/
@Composable
fun SearchBarPersonaChip(
person: Person,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
size: PersonaChipSize = PersonaChipSize.Medium,
enabled: Boolean = true,
selected: Boolean = false,
onClick: (() -> Unit)? = null,
onCloseClick: (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
searchbarPersonaChipTokens: SearchBarPersonaChipTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = searchbarPersonaChipTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.SearchBarPersonaChipControlType] as SearchBarPersonaChipTokens
val searchBarPersonaChipInfo = SearchBarPersonaChipInfo(
style,
enabled,
size = size
)
val backgroundColor =
token.backgroundBrush(personaChipInfo = searchBarPersonaChipInfo)
.getBrushByState(
enabled = enabled, selected = selected, interactionSource = interactionSource
)
val textColor =
token.textColor(personaChipInfo = searchBarPersonaChipInfo)
.getColorByState(
enabled = enabled, selected = selected, interactionSource = interactionSource
)
val fontStyle =
token.typography(personaChipInfo = searchBarPersonaChipInfo)
val avatarSize =
token.avatarSize(personaChipInfo = searchBarPersonaChipInfo)
val verticalPadding =
token.verticalPadding(personaChipInfo = searchBarPersonaChipInfo)
val horizontalPadding =
token.horizontalPadding(personaChipInfo = searchBarPersonaChipInfo)
val avatarToTextSpacing =
token.avatarToTextSpacing(personaChipInfo = searchBarPersonaChipInfo)
val cornerRadius =
token.cornerRadius(personaChipInfo = searchBarPersonaChipInfo)
Box(
modifier = modifier
.clip(RoundedCornerShape(cornerRadius))
.background(backgroundColor)
.clickable(
enabled = enabled,
onClick = onClick ?: {},
interactionSource = interactionSource,
indication = rememberRipple()
)
)
{
Row(
Modifier
.padding(
horizontal = horizontalPadding,
vertical = verticalPadding
),
horizontalArrangement = Arrangement.spacedBy(avatarToTextSpacing),
verticalAlignment = Alignment.CenterVertically
) {
if (size == PersonaChipSize.Medium) {
if (onCloseClick != null && selected) {
Icon(
Icons.Filled.Close,
modifier = Modifier
.size(16.dp)
.clickable(
enabled = true,
onClick = onCloseClick,
role = Role.Button
),
contentDescription = LocalContext.current.resources.getString(R.string.fluentui_close),
tint = textColor
)
} else {
Avatar(person = person, size = avatarSize)
}
}
BasicText(
modifier = Modifier.padding(bottom = 2.dp),//Vertically center align text
text = person.getLabel(),
style = fontStyle.merge(
TextStyle(
color = textColor
)
)
)
}
}
}