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

feature: expose click event #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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