forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SearchBar.kt
378 lines (354 loc) · 17 KB
/
SearchBar.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.microsoft.fluentui.tokenized
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
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.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextDirection
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.icons.ListItemIcons
import com.microsoft.fluentui.icons.SearchBarIcons
import com.microsoft.fluentui.icons.listitemicons.Chevron
import com.microsoft.fluentui.icons.searchbaricons.Arrowback
import com.microsoft.fluentui.icons.searchbaricons.Dismisscircle
import com.microsoft.fluentui.icons.searchbaricons.Microphone
import com.microsoft.fluentui.icons.searchbaricons.Search
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.*
import com.microsoft.fluentui.theme.token.controlTokens.SearchBarInfo
import com.microsoft.fluentui.theme.token.controlTokens.SearchBarTokens
import com.microsoft.fluentui.tokenized.persona.Person
import com.microsoft.fluentui.tokenized.persona.SearchBarPersonaChip
import com.microsoft.fluentui.tokenized.progress.CircularProgressIndicator
import com.microsoft.fluentui.topappbars.R
/**
* API to create a searchbar. This control takes input from user's keyboard and runs it against a lambda
* function provided by user to generate results. It allows user to select a person and display in the form
* of a persona chip.
*
* @param onValueChange Lambda function against which the input text is run.
* @param modifier Optional modifier for Searchbar.
* @param enabled Boolean to enable/disable the CTAs on Searchbar. Default: [false]
* @param style Color Scheme to be applied to Searchbar. Default: [FluentStyle.Neutral]
* @param keyboardOptions Keyboard Configuration Options for Input Text Field. Default: [KeyboardOptions]
* @param keyboardActions Configures keyboards response w.r.t. user interactions. Default: [KeyboardActions]
* @param searchHint String provided as hint on SearchBar. Default: "Search"
* @param focusByDefault Boolean which allows Searchbar to be initially composed in focused state. Default: [false]
* @param loading Boolean to display progress indicator on SearchBar. Default: [false]
* @param selectedPerson Person object which has to be displayed as a Persona Chip. Default: [null]
* @param personaChipOnClick OnClick Behaviour for above persona chip. Default: [null]
* @param microphoneCallback Callback to be provided to microphone icon, available at right side. Default: [null]
* @param navigationIconCallback Callback to be provided to navigation icon, present at left side. Default: [null]
* @param rightAccessoryIcon [FluentIcon] Object which is displayed on the right side of microphone. Default: [null]
* @param searchBarTokens Tokens which help in customizing appearance of search bar. Default: [null]
*/
// AnimatedContent Backspace Key
@OptIn(ExperimentalComposeUiApi::class, ExperimentalAnimationApi::class)
@Composable
fun SearchBar(
onValueChange: (String, Person?) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
style: FluentStyle = FluentStyle.Neutral,
keyboardOptions: KeyboardOptions = KeyboardOptions(),
keyboardActions: KeyboardActions = KeyboardActions(),
searchHint: String = LocalContext.current.resources.getString(R.string.fluentui_search),
focusByDefault: Boolean = false,
loading: Boolean = false,
selectedPerson: Person? = null,
personaChipOnClick: (() -> Unit)? = null,
microphoneCallback: (() -> Unit)? = null,
navigationIconCallback: (() -> Unit)? = null,
rightAccessoryIcon: FluentIcon? = null,
searchBarTokens: SearchBarTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = searchBarTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.SearchBarControlType] as SearchBarTokens
val searchBarInfo = SearchBarInfo(style)
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
var queryText by rememberSaveable { mutableStateOf("") }
var searchHasFocus by rememberSaveable { mutableStateOf(false) }
var personaChipSelected by rememberSaveable { mutableStateOf(false) }
var selectedPerson: Person? = selectedPerson
val scope = rememberCoroutineScope()
Row(
modifier = modifier
.background(token.backgroundBrush(searchBarInfo))
.padding(token.searchBarPadding(searchBarInfo))
) {
Row(
Modifier
.requiredHeightIn(min = token.height(searchBarInfo))
.fillMaxWidth()
.clip(RoundedCornerShape(8.dp))
.background(
token.inputBackgroundBrush(searchBarInfo),
RoundedCornerShape(8.dp)
),
verticalAlignment = Alignment.CenterVertically
) {
//Left Section
AnimatedContent(searchHasFocus) {
var onClick: (() -> Unit)? = null
var icon: ImageVector? = null
var contentDescription: String? = null
var mirrorImage = false
when (it) {
true -> {
onClick = {
queryText = ""
selectedPerson = null
onValueChange(queryText, selectedPerson)
focusManager.clearFocus()
searchHasFocus = false
navigationIconCallback?.invoke()
}
icon = SearchBarIcons.Arrowback
contentDescription =
LocalContext.current.resources.getString(R.string.fluentui_back)
if (LocalLayoutDirection.current == LayoutDirection.Rtl)
mirrorImage = true
}
false -> {
onClick = {
focusRequester.requestFocus()
}
icon = SearchBarIcons.Search
contentDescription =
LocalContext.current.resources.getString(R.string.fluentui_search)
mirrorImage = false
}
}
Icon(
icon,
contentDescription = contentDescription,
modifier = Modifier
.padding(
(44.dp - token.leftIconSize(searchBarInfo)) / 2,
(40.dp - token.leftIconSize(searchBarInfo)) / 2
)
.size(token.leftIconSize(searchBarInfo)),
tint = token.leftIconColor(searchBarInfo),
flipOnRtl = mirrorImage,
enabled = enabled,
onClick = onClick
)
}
//Center Section
Row(
modifier = Modifier
.height(24.dp)
.weight(1F)
.onKeyEvent {
if (it.key == Key.Backspace) {
if (personaChipSelected) {
selectedPerson = null
personaChipSelected = false
onValueChange(queryText, selectedPerson)
} else {
personaChipSelected = true
}
}
false
},
verticalAlignment = Alignment.CenterVertically
) {
LaunchedEffect(selectedPerson) {
queryText = ""
if (personaChipSelected)
personaChipSelected = false
onValueChange(queryText, selectedPerson)
}
if (selectedPerson != null) {
SearchBarPersonaChip(
person = selectedPerson!!,
modifier = Modifier.padding(end = 8.dp),
style = style,
enabled = enabled,
selected = personaChipSelected,
onClick = {
personaChipSelected = !personaChipSelected
personaChipOnClick?.invoke()
},
onCloseClick = {
selectedPerson = null
onValueChange(queryText, selectedPerson)
}
)
}
BasicTextField(
value = queryText,
onValueChange = {
queryText = it
personaChipSelected = false
onValueChange(queryText, selectedPerson)
},
singleLine = true,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
modifier = Modifier
.weight(1F)
.focusRequester(focusRequester)
.onFocusChanged { focusState ->
when {
focusState.isFocused ->
searchHasFocus = true
}
}
.padding(horizontal = 8.dp)
.semantics { contentDescription = searchHint },
textStyle = token.typography(searchBarInfo).merge(
TextStyle(
color = token.textColor(searchBarInfo),
textDirection = TextDirection.ContentOrLtr
)
),
decorationBox = @Composable { innerTextField ->
Box(
Modifier.fillMaxWidth(),
contentAlignment = if (LocalLayoutDirection.current == LayoutDirection.Rtl)
Alignment.CenterEnd
else
Alignment.CenterStart
) {
if (queryText.isEmpty()) {
BasicText(
searchHint,
style = token.typography(searchBarInfo)
.merge(TextStyle(color = token.textColor(searchBarInfo)))
)
}
}
innerTextField()
},
cursorBrush = token.cursorColor(searchBarInfo)
)
}
LaunchedEffect(Unit) {
if (focusByDefault)
focusRequester.requestFocus()
}
//Right Section
AnimatedContent((queryText.isBlank() && selectedPerson == null)) {
when (it) {
true ->
if (microphoneCallback != null) {
Icon(
SearchBarIcons.Microphone,
contentDescription = LocalContext.current.resources.getString(
R.string.fluentui_microphone
),
modifier = Modifier
.padding(
(44.dp - token.rightIconSize(searchBarInfo)) / 2,
(40.dp - token.rightIconSize(searchBarInfo)) / 2
)
.size(
token.rightIconSize(
searchBarInfo
)
),
tint = token.rightIconColor(searchBarInfo),
onClick = microphoneCallback
)
}
false ->
Box(
modifier = Modifier
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(),
enabled = enabled,
onClick = {
queryText = ""
selectedPerson = null
onValueChange(queryText, selectedPerson)
},
role = Role.Button
)
.size(44.dp, 40.dp),
contentAlignment = Alignment.Center
) {
if (loading) {
CircularProgressIndicator(
size = token.circularProgressIndicatorSize(
searchBarInfo
)
)
}
Icon(
SearchBarIcons.Dismisscircle,
contentDescription = LocalContext.current.resources.getString(
R.string.fluentui_clear_text
),
modifier = Modifier
.size(token.rightIconSize(searchBarInfo)),
tint = token.rightIconColor(searchBarInfo)
)
}
}
}
if (rightAccessoryIcon?.isIconAvailable() == true && rightAccessoryIcon.onClick != null) {
Row(
modifier = Modifier
.size(44.dp, 40.dp)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(),
enabled = enabled,
onClick = rightAccessoryIcon.onClick!!,
role = Role.Button
),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Icon(
rightAccessoryIcon.value(),
contentDescription = rightAccessoryIcon.contentDescription,
modifier = Modifier
.size(token.rightIconSize(searchBarInfo)),
tint = token.rightIconColor(searchBarInfo)
)
Icon(
ListItemIcons.Chevron,
contentDescription = LocalContext.current.resources.getString(R.string.fluentui_chevron),
Modifier.rotate(90F),
tint = token.rightIconColor(searchBarInfo)
)
}
}
}
}
}