Skip to content

Commit

Permalink
Simple Form Spec can specify if add optional label. (stripe#4024)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelleb-stripe authored Jul 21, 2021
1 parent a2c8900 commit 4251ee3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
9 changes: 6 additions & 3 deletions paymentsheet/api/paymentsheet.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Lcom/stripe/android/paymentsheet/specifications/IdentifierSpec;IIIZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (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;
}
Expand Down
1 change: 1 addition & 0 deletions paymentsheet/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<string name="stripe_paymentsheet_payment_method_sepa_debit">SEPA Debit</string>
<!-- Title for Sofort payment method -->
<string name="stripe_paymentsheet_payment_method_sofort" translatable="false">Sofort</string>
<string name="stripe_paymentsheet_form_label_optional" tools:ignore="MissingTranslation">%s (optional)</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ private fun SectionFieldSpec.SimpleText.transform(
label = this.label,
capitalization = this.capitalization,
keyboard = this.keyboardType
)
),
showOptionalLabel = this.showOptionalLabel
),
focusRequesterCount.getAndIncrement()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4251ee3

Please sign in to comment.