Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard navigation in edit origin address #13385

Open
wants to merge 1 commit into
base: issue/12439-connecting-address-with-ui
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.woocommerce.android.ui.orders.wooshippinglabels.address
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -12,6 +13,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Icon
Expand All @@ -24,10 +26,15 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -91,11 +98,29 @@ fun WooShippingEditAddressScreen(
.verticalScroll(rememberScrollState())
.padding(padding)
) {
val companyFocusRequester = remember { FocusRequester() }
val addressFocusRequester = remember { FocusRequester() }
val cityFocusRequester = remember { FocusRequester() }
val postalCodeFocusRequester = remember { FocusRequester() }
val emailFocusRequester = remember { FocusRequester() }
val phoneFocusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current

RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_name)} *",
text = editableAddress.name.value,
error = editableAddress.name.error,
onTextChange = onNameChange,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = {
if (isCompanyExpanded) {
companyFocusRequester.requestFocus()
} else {
addressFocusRequester.requestFocus()
}
}
),
)

CollapsedField(
Expand All @@ -108,7 +133,14 @@ fun WooShippingEditAddressScreen(
label = stringResource(id = R.string.woo_shipping_label_company),
text = editableAddress.company.value,
onTextChange = onCompanyChange,
modifier = Modifier.padding(top = 8.dp)
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = {
addressFocusRequester.requestFocus()
}
),
modifier = Modifier.padding(top = 8.dp),
focusRequester = companyFocusRequester
)
}

Expand All @@ -123,13 +155,27 @@ fun WooShippingEditAddressScreen(
text = editableAddress.address.value,
error = editableAddress.address.error,
onTextChange = onAddressChange,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = {
cityFocusRequester.requestFocus()
}
),
focusRequester = addressFocusRequester,
modifier = Modifier.padding(top = 8.dp)
)
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_city)} *",
text = editableAddress.city.value,
error = editableAddress.city.error,
onTextChange = onCityChange,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = {
postalCodeFocusRequester.requestFocus()
}
),
focusRequester = cityFocusRequester,
modifier = Modifier.padding(top = 8.dp)
)

Expand All @@ -147,7 +193,16 @@ fun WooShippingEditAddressScreen(
label = "${stringResource(id = R.string.woo_shipping_label_post_code)} *",
text = editableAddress.postalCode.value,
error = editableAddress.postalCode.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = {
emailFocusRequester.requestFocus()
}
),
focusRequester = postalCodeFocusRequester,
onTextChange = onPostalCodeChange,
modifier = Modifier
.padding(top = 8.dp)
Expand All @@ -159,15 +214,34 @@ fun WooShippingEditAddressScreen(
label = "${stringResource(id = R.string.woo_shipping_label_email)} *",
text = editableAddress.email.value,
error = editableAddress.email.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next
),
onTextChange = onEmailChange,
keyboardActions = KeyboardActions(
onNext = {
phoneFocusRequester.requestFocus()
}
),
focusRequester = emailFocusRequester,
modifier = Modifier.padding(top = 32.dp)
)
RoundedBorderTextFieldWithLabel(
label = "${stringResource(id = R.string.woo_shipping_label_phone)} *",
text = editableAddress.phone.value,
error = editableAddress.phone.error,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Phone,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
phoneFocusRequester.freeFocus()
keyboardController?.hide()
}
),
focusRequester = phoneFocusRequester,
onTextChange = onPhoneChange,
modifier = Modifier.padding(top = 8.dp)
)
Expand All @@ -184,6 +258,8 @@ internal fun RoundedBorderTextFieldWithLabel(
hint: String = "",
error: String? = null,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
focusRequester: FocusRequester? = null,
endSection: @Composable () -> Unit = {}
) {
val hasError = error.isNotNullOrEmpty()
Expand All @@ -192,6 +268,15 @@ internal fun RoundedBorderTextFieldWithLabel(
} else {
colorResource(R.color.divider_color)
}

val textFieldModifier = if (focusRequester != null) {
Modifier
.focusable()
.focusRequester(focusRequester)
} else {
Modifier
}

Column(modifier = modifier) {
Text(
text = label,
Expand All @@ -208,12 +293,12 @@ internal fun RoundedBorderTextFieldWithLabel(
) {
Box(modifier = Modifier.weight(1f)) {
BasicTextField(
modifier = Modifier.fillMaxWidth(),
modifier = textFieldModifier.fillMaxWidth(),
value = text,
onValueChange = onTextChange,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
textStyle = MaterialTheme.typography.body2.copy(color = MaterialTheme.colors.onSurface),

)
if (text.isEmpty()) {
Text(
Expand Down
Loading