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

Document new options and update method in shipping address element #339

Merged
merged 6 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/types/src/invalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
StripeCardElement,
StripeIbanElement,
StripePaymentElement,
StripeShippingAddressElement,
} from '../../../types';

declare const stripe: Stripe;
declare const elements: StripeElements;
declare const cardElement: StripeCardElement;
declare const ibanElement: StripeIbanElement;
declare const paymentElement: StripePaymentElement;
declare const shippingAddressElement: StripeShippingAddressElement;

elements.update({
// @ts-expect-error: `clientSecret` is not updatable
Expand Down Expand Up @@ -41,6 +43,13 @@ paymentElement.on('change', (e) => {
}
});

shippingAddressElement.update({
// @ts-expect-error: Argument of type '{ defaultValues: { allowedCountries: string[]; }; }' is not assignable to parameter of type 'StripeShippingAddressElementUpdateOptions'.
andywang-stripe marked this conversation as resolved.
Show resolved Hide resolved
defaultValues: {
name: 'foo bar',
},
});

stripe
.confirmPayment({elements, confirmParams: {return_url: ''}})
.then((res) => {
Expand Down
17 changes: 17 additions & 0 deletions tests/types/src/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,14 @@ const shippingAddressElement = elements.create('shippingAddress', {
},
},
disableAutocomplete: true,
fields: {
phone: 'always',
},
validation: {
phone: {
required: 'always',
},
},
});

shippingAddressElement
Expand All @@ -525,6 +533,15 @@ shippingAddressElement
}) => {}
);

shippingAddressElement.update({
fields: {
phone: 'never',
},
validation: {
phone: undefined,
andywang-stripe marked this conversation as resolved.
Show resolved Hide resolved
},
});

const retrievedShippingAddressElement: StripeShippingAddressElement | null = elements.getElement(
'shippingAddress'
);
Expand Down
44 changes: 44 additions & 0 deletions types/stripe-js/elements/shipping-address.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ export type StripeShippingAddressElement = StripeElementBase & {
eventType: 'loaderstart',
handler?: (event: {elementType: 'shippingAddress'}) => any
): StripeShippingAddressElement;

/**
* Updates the options the `ShippingAddressElement` was initialized with.
* Updates are merged into the existing configuration.
*/
update(
options: StripeShippingAddressElementUpdateOptions
andywang-stripe marked this conversation as resolved.
Show resolved Hide resolved
): StripeShippingAddressElement;
};

export type StripeShippingAddressElementUpdateOptions = {
/**
* Control which additional fields to display in the shippingAddress Element.
*/
fields?: {
phone?: 'always' | 'never' | 'auto';
};

/**
* Specify validation rules for the above additional fields.
*/
validation?: {
phone?: {
required: 'always' | 'never' | 'auto';
};
};
};

export interface StripeShippingAddressElementOptions {
Expand All @@ -148,12 +174,29 @@ export interface StripeShippingAddressElementOptions {
postal_code?: string | null;
country: string;
};
phone?: string | null;
};

/**
* Whether or not ShippingAddressElement provides autocomplete suggestions
*/
disableAutocomplete?: boolean;

/**
* Control which additional fields to display in the shippingAddress Element.
*/
fields?: {
phone?: 'always' | 'never' | 'auto';
};

/**
* Specify validation rules for the above additional fields.
*/
validation?: {
phone?: {
required: 'always' | 'never' | 'auto';
};
};
}

export interface StripeShippingAddressElementChangeEvent
Expand Down Expand Up @@ -191,5 +234,6 @@ export interface StripeShippingAddressElementChangeEvent
postal_code: string;
country: string;
};
phone?: string;
};
}