Skip to content

Commit

Permalink
feature: expose click event
Browse files Browse the repository at this point in the history
  • Loading branch information
martinerko committed Feb 17, 2022
1 parent 1a4033c commit c4e21a8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/StripeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export class StripeBase extends LitElement {
}

/**
* Adds `ready`, `focus`, and `blur` listeners to the Stripe Element
* Adds `ready`, `focus`, `blur` and `click` listeners to the Stripe Element
*/
private initElementListeners(): void {
if (!this.element) return;
Expand All @@ -382,6 +382,8 @@ export class StripeBase extends LitElement {
this.element.on('focus', this.onFocus);
// @ts-expect-error: should still work
this.element.on('blur', this.onBlur);
// @ts-expect-error: should still work
this.element.on('click', this.onClick);
}

/**
Expand Down Expand Up @@ -453,6 +455,20 @@ export class StripeBase extends LitElement {
this.fire('ready', event);
}

/**
* @param {StripeFocusEvent} event
* @private
*/
@bound private async onClick(event: Stripe.StripePaymentRequestButtonElementClickEvent): Promise<void> {
// copy original props so we don't loose preventDefault and still forward event
// https://stripe.com/docs/js/element/events/on_click#element_on_click-handler
const specialEvent = Object.entries(event).reduce((e, [k,v]) => {
e[k] = v;
return e;
}, new Event('click'));
this.dispatchEvent(specialEvent);
}

/**
* POSTs the payment info represenation to the endpoint at `/action`
*/
Expand Down

0 comments on commit c4e21a8

Please sign in to comment.