From 4251ee36b1530e166304df8392771e502b104277 Mon Sep 17 00:00:00 2001 From: michelleb-stripe <77996191+michelleb-stripe@users.noreply.github.com> Date: Wed, 21 Jul 2021 03:16:26 -0700 Subject: [PATCH] Simple Form Spec can specify if add optional label. (#4024) --- paymentsheet/api/paymentsheet.api | 9 ++++--- paymentsheet/res/values/strings.xml | 1 + .../paymentsheet/elements/TextField.kt | 14 +++++++++- .../elements/TextFieldController.kt | 1 + .../forms/TransformSpecToElement.kt | 3 ++- .../paymentsheet/specifications/Spec.kt | 3 ++- .../forms/TransformSpecToElementTest.kt | 27 +++++++++++++++++++ 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/paymentsheet/api/paymentsheet.api b/paymentsheet/api/paymentsheet.api index 701b8bdee82..503978864db 100644 --- a/paymentsheet/api/paymentsheet.api +++ b/paymentsheet/api/paymentsheet.api @@ -202,18 +202,21 @@ public final class com/stripe/android/paymentsheet/specifications/SectionFieldSp public final class com/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText : com/stripe/android/paymentsheet/specifications/SectionFieldSpec { public static final field $stable I - public synthetic fun (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIZLkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec; public final fun component2 ()I public final fun component3-IUNYP9k ()I public final fun component4-PjHm6EE ()I - public final fun copy-MwhgLSg (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;III)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText; - public static synthetic fun copy-MwhgLSg$default (Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText;Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIILjava/lang/Object;)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText; + public final fun component5 ()Z + public final fun copy-25FCGzQ (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIZ)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText; + public static synthetic fun copy-25FCGzQ$default (Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText;Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIZILjava/lang/Object;)Lcom/stripe/android/paymentsheet/specifications/SectionFieldSpec$SimpleText; public fun equals (Ljava/lang/Object;)Z public final fun getCapitalization-IUNYP9k ()I public fun getIdentifier ()Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec; public final fun getKeyboardType-PjHm6EE ()I public final fun getLabel ()I + public final fun getShowOptionalLabel ()Z public fun hashCode ()I public fun toString ()Ljava/lang/String; } diff --git a/paymentsheet/res/values/strings.xml b/paymentsheet/res/values/strings.xml index 35f6b7d3900..580b2a73559 100644 --- a/paymentsheet/res/values/strings.xml +++ b/paymentsheet/res/values/strings.xml @@ -34,4 +34,5 @@ SEPA Debit Sofort + %s (optional) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextField.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextField.kt index 176d57ea2ca..b20c5081ace 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextField.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextField.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.ImeAction import androidx.lifecycle.asLiveData +import com.stripe.android.paymentsheet.R /** This is a helpful method for setting the next action based on the nextFocus Requester **/ internal fun imeAction(nextFocusRequester: FocusRequester?): ImeAction = nextFocusRequester?.let { @@ -100,7 +101,18 @@ internal fun TextField( value = value, onValueChange = { textFieldController.onValueChange(it) }, isError = shouldShowError, - label = { Text(text = stringResource(textFieldController.label)) }, + label = { + Text( + text = if (textFieldController.showOptionalLabel) { + stringResource( + R.string.stripe_paymentsheet_form_label_optional, + stringResource(textFieldController.label) + ) + } else { + stringResource(textFieldController.label) + } + ) + }, modifier = modifier .fillMaxWidth() .focusOrder(myFocus) { nextFocus?.requestFocus() } diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextFieldController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextFieldController.kt index c6980dc25fe..8a89bef25df 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextFieldController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/elements/TextFieldController.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.map */ internal class TextFieldController constructor( private val textFieldConfig: TextFieldConfig, + val showOptionalLabel: Boolean = false ) : InputController { val capitalization: KeyboardCapitalization = textFieldConfig.capitalization val keyboardType: KeyboardType = textFieldConfig.keyboard diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/forms/TransformSpecToElement.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/forms/TransformSpecToElement.kt index 9d3a0a10a11..626e6919329 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/forms/TransformSpecToElement.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/forms/TransformSpecToElement.kt @@ -73,7 +73,8 @@ private fun SectionFieldSpec.SimpleText.transform( label = this.label, capitalization = this.capitalization, keyboard = this.keyboardType - ) + ), + showOptionalLabel = this.showOptionalLabel ), focusRequesterCount.getAndIncrement() ) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/specifications/Spec.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/specifications/Spec.kt index 84a9d2768e8..c543b15d9e9 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/specifications/Spec.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/specifications/Spec.kt @@ -95,7 +95,8 @@ sealed class SectionFieldSpec(open val identifier: IdentifierSpec) { override val identifier: IdentifierSpec, @StringRes val label: Int, val capitalization: KeyboardCapitalization, - val keyboardType: KeyboardType + val keyboardType: KeyboardType, + val showOptionalLabel: Boolean = false ) : SectionFieldSpec(identifier) internal companion object { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/forms/TransformSpecToElementTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/forms/TransformSpecToElementTest.kt index f5eaf1fc81d..2b8db704aab 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/forms/TransformSpecToElementTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/forms/TransformSpecToElementTest.kt @@ -122,6 +122,33 @@ class TransformSpecToElementTest { assertThat(nameElement.controller.keyboardType).isEqualTo(KeyboardType.Text) } + @Test + fun `Add a simple text section spec sets up the text element correctly`() { + val formElement = listOf( + FormItemSpec.SectionSpec( + IdentifierSpec("simple_section"), + SectionFieldSpec.SimpleText( + IdentifierSpec("simple"), + R.string.address_label_name, + showOptionalLabel = true, + keyboardType = KeyboardType.Text, + capitalization = KeyboardCapitalization.Words + ) + ) + ).transform( + "Example, Inc.", + FocusRequesterCount() + ) + + val nameElement = (formElement.first() as SectionElement).fields[0] + as SectionFieldElement.SimpleText + + // Verify the correct config is setup for the controller + assertThat(nameElement.controller.label).isEqualTo(R.string.address_label_name) + assertThat(nameElement.identifier.value).isEqualTo("simple") + assertThat(nameElement.controller.showOptionalLabel).isTrue() + } + @Test fun `Add a email section spec sets up the email element correctly`() { val formElement = listOf(emailSection).transform(