Skip to content

Commit

Permalink
feat,fix: CardForm country input defaults to current region on Androi…
Browse files Browse the repository at this point in the history
…d, add option to override (#974)
  • Loading branch information
charliecruzan-stripe authored Jun 6, 2022
1 parent 1279594 commit 3a93adb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### New features

- Added a `defaultValues` prop to the `CardForm` component. Currently only accepts `countryCode`, and is Android-only. [#974](https://github.com/stripe/stripe-react-native/pull/974)

### Fixes

- Resolve with an Error (of type `Canceled`) if no payment option is selected in the Payment Sheet custom flow (i.e., the `x` button is clicked to close the Payment Sheet). [#975](https://github.com/stripe/stripe-react-native/pull/975)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.facebook.react.uimanager.events.EventDispatcher
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.stripe.android.core.model.CountryCode
import com.stripe.android.databinding.CardMultilineWidgetBinding
import com.stripe.android.databinding.StripeCardFormViewBinding
import com.stripe.android.model.Address
Expand Down Expand Up @@ -48,6 +49,13 @@ class CardFormView(context: ThemedReactContext) : FrameLayout(context) {
cardFormViewBinding.postalCodeContainer.visibility = visibility
}

fun setDefaultValues(defaults: ReadableMap) {
defaults.getString("countryCode")?.let {
cardFormViewBinding.countryLayout.setSelectedCountryCode(CountryCode(it))
cardFormViewBinding.countryLayout.updateUiForCountryEntered(CountryCode(it))
}
}

fun setPlaceHolders(value: ReadableMap) {
val numberPlaceholder = getValOr(value, "number", null)
val expirationPlaceholder = getValOr(value, "expiration", null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class CardFormViewManager : SimpleViewManager<CardFormView>() {
view.setCardStyle(cardStyle)
}

@ReactProp(name = "defaultValues")
fun setDefaultValues(view: CardFormView, defaults: ReadableMap) {
view.setDefaultValues(defaults)
}

override fun createViewInstance(reactContext: ThemedReactContext): CardFormView {
val stripeSdkModule: StripeSdkModule? = reactContext.getNativeModule(StripeSdkModule::class.java)
val view = CardFormView(reactContext)
Expand Down
3 changes: 3 additions & 0 deletions example/src/screens/MultilineWebhookPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export default function MultilineWebhookPaymentScreen() {
console.log(cardDetails);
setComplete(cardDetails.complete);
}}
defaultValues={{
countryCode: 'US',
}}
/>
<View style={styles.row}>
<Switch
Expand Down
6 changes: 6 additions & 0 deletions src/components/CardForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface Props extends AccessibilityProps {

/** Android only */
placeholders?: CardFormView.Placeholders;
/** Android only */
defaultValues?: CardFormView.DefaultValues;
// onBlur?(): void;
// onFocus?(focusedField: CardFormView.FieldNames | null): void;
onFormComplete?(card: CardFormView.Details): void;
Expand Down Expand Up @@ -81,6 +83,7 @@ export const CardForm = forwardRef<CardFormView.Methods, Props>(
// onFocus,
// onBlur,
placeholders,
defaultValues,
...props
},
ref
Expand Down Expand Up @@ -185,6 +188,9 @@ export const CardForm = forwardRef<CardFormView.Methods, Props>(
cvc: placeholders?.cvc,
postalCode: placeholders?.postalCode,
}}
defaultValues={{
...(defaultValues ?? {}),
}}
onFocusChange={onFocusHandler}
// postalCodeEnabled={postalCodeEnabled}
{...props}
Expand Down
7 changes: 7 additions & 0 deletions src/types/components/CardFormView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface Placeholders {
postalCode?: string;
}

export type DefaultValues = {
/** The 2-letter country code for the country selected by default on Android. If this is null, it is set by the device's configured region in the Settings app. */
countryCode?: string;
};

/**
* @ignore
*/
Expand All @@ -54,6 +59,8 @@ export interface NativeProps {
cardStyle?: Styles;
/** Android only */
placeholders?: Placeholders;
/** Android only */
defaultValues?: DefaultValues;
// postalCodeEnabled: boolean;
onFocusChange(
event: NativeSyntheticEvent<{ focusedField: FieldName | null }>
Expand Down

0 comments on commit 3a93adb

Please sign in to comment.