-
Notifications
You must be signed in to change notification settings - Fork 629
/
TextFieldUI.kt
281 lines (266 loc) · 9.6 KB
/
TextFieldUI.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
package com.stripe.android.ui.core.elements
import android.view.KeyEvent
import androidx.annotation.RestrictTo
import androidx.annotation.StringRes
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.editableText
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.stripe.android.ui.core.R
import com.stripe.android.ui.core.paymentsColors
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
* This is focused on converting an [TextFieldController] into what is displayed in a section
* with a single textField.
* - some focus logic
* - observes values that impact how things show on the screen
*/
@Composable
fun TextFieldSection(
textFieldController: TextFieldController,
modifier: Modifier = Modifier,
@StringRes sectionTitle: Int? = null,
imeAction: ImeAction,
enabled: Boolean,
onTextStateChanged: (TextFieldState?) -> Unit = {}
) {
val error by textFieldController.error.collectAsState(null)
val sectionErrorString = error?.let {
it.formatArgs?.let { args ->
stringResource(
it.errorMessage,
*args
)
} ?: stringResource(it.errorMessage)
}
Section(sectionTitle, sectionErrorString) {
TextField(
textFieldController = textFieldController,
enabled = enabled,
imeAction = imeAction,
modifier = modifier,
onTextStateChanged = onTextStateChanged
)
}
}
/**
* This is focused on converting an [TextFieldController] into what is displayed in a textField.
* - some focus logic
* - observes values that impact how things show on the screen
*
* @param enabled Whether to show this TextField as enabled or not. Note that the `enabled`
* attribute of [textFieldController] is also taken into account to decide if the UI should be
* enabled.
*/
@Composable
fun TextField(
textFieldController: TextFieldController,
modifier: Modifier = Modifier,
imeAction: ImeAction,
enabled: Boolean,
onTextStateChanged: (TextFieldState?) -> Unit = {}
) {
val focusManager = LocalFocusManager.current
val value by textFieldController.fieldValue.collectAsState("")
val trailingIcon by textFieldController.trailingIcon.collectAsState(null)
val shouldShowError by textFieldController.visibleError.collectAsState(false)
val loading by textFieldController.loading.collectAsState(false)
val contentDescription by textFieldController.contentDescription.collectAsState("")
var hasFocus by rememberSaveable { mutableStateOf(false) }
val colors = TextFieldColors(shouldShowError)
val fieldState by textFieldController.fieldState.collectAsState(
TextFieldStateConstants.Error.Blank
)
val label by textFieldController.label.collectAsState(null)
var processedIsFull by rememberSaveable { mutableStateOf(false) }
/**
* This is setup so that when a field is full it still allows more characters
* to be entered, it just triggers next focus when the event happens.
*/
@Suppress("UNUSED_VALUE")
processedIsFull = if (fieldState == TextFieldStateConstants.Valid.Full) {
if (!processedIsFull) {
focusManager.moveFocus(FocusDirection.Next)
}
true
} else {
false
}
TextField(
value = value,
onValueChange = {
val newTextState = textFieldController.onValueChange(it)
if (newTextState != null) {
onTextStateChanged(newTextState)
}
},
modifier = modifier
.fillMaxWidth()
.onPreviewKeyEvent { event ->
if (event.type == KeyEventType.KeyDown &&
event.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_DEL &&
value.isEmpty()
) {
focusManager.moveFocus(FocusDirection.Previous)
true
} else {
false
}
}
.onFocusChanged {
if (hasFocus != it.isFocused) {
textFieldController.onFocusChange(it.isFocused)
}
hasFocus = it.isFocused
}
.semantics {
this.contentDescription = contentDescription
this.editableText = AnnotatedString("")
},
enabled = enabled && textFieldController.enabled,
label = {
FormLabel(
text = if (textFieldController.showOptionalLabel) {
stringResource(
R.string.form_label_optional,
label?.let { stringResource(it) } ?: ""
)
} else {
label?.let { stringResource(it) } ?: ""
}
)
},
trailingIcon = trailingIcon?.let {
{
Row {
when (it) {
is TextFieldIcon.Trailing -> {
TrailingIcon(it, loading)
}
is TextFieldIcon.MultiTrailing -> {
Row(modifier = Modifier.padding(10.dp)) {
it.staticIcons.forEach {
TrailingIcon(it, loading)
}
AnimatedIcons(icons = it.animatedIcons, loading = loading)
}
}
}
}
}
},
isError = shouldShowError,
visualTransformation = textFieldController.visualTransformation,
keyboardOptions = KeyboardOptions(
keyboardType = textFieldController.keyboardType,
capitalization = textFieldController.capitalization,
imeAction = imeAction
),
keyboardActions = KeyboardActions(
onNext = {
focusManager.moveFocus(FocusDirection.Next)
},
onDone = {
focusManager.clearFocus(true)
}
),
singleLine = true,
colors = colors,
)
}
@Composable
fun AnimatedIcons(
icons: List<TextFieldIcon.Trailing>,
loading: Boolean
) {
if (icons.isEmpty()) return
val composableScope = rememberCoroutineScope()
val target by produceState(initialValue = icons.first()) {
composableScope.launch {
while (true) {
icons.forEach {
delay(1000)
value = it
}
}
}
}
Crossfade(targetState = target) {
TrailingIcon(it, loading)
}
}
@Composable
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun TextFieldColors(
shouldShowError: Boolean = false
) = TextFieldDefaults.textFieldColors(
textColor = if (shouldShowError) {
MaterialTheme.colors.error
} else {
MaterialTheme.paymentsColors.onComponent
},
unfocusedLabelColor = MaterialTheme.paymentsColors.placeholderText,
focusedLabelColor = MaterialTheme.paymentsColors.placeholderText,
placeholderColor = MaterialTheme.paymentsColors.placeholderText,
backgroundColor = MaterialTheme.paymentsColors.component,
focusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
cursorColor = MaterialTheme.paymentsColors.textCursor
)
@Composable
internal fun TrailingIcon(
trailingIcon: TextFieldIcon.Trailing,
loading: Boolean
) {
if (loading) {
CircularProgressIndicator()
} else if (trailingIcon.isTintable) {
Icon(
painter = painterResource(id = trailingIcon.idRes),
contentDescription = trailingIcon.contentDescription?.let {
stringResource(trailingIcon.contentDescription)
}
)
} else {
Image(
painter = painterResource(id = trailingIcon.idRes),
contentDescription = trailingIcon.contentDescription?.let {
stringResource(trailingIcon.contentDescription)
}
)
}
}