@salesforcedevs/dw-components 1.3.397-canary.4 → 1.3.397-canary.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dw-components",
3
- "version": "1.3.397-canary.4",
3
+ "version": "1.3.397-canary.5",
4
4
  "description": "Lightning web components for https://developer.salesforce.com",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -1,6 +1,6 @@
1
1
  <template>
2
2
  <div class="container">
3
- <slot onslotchange={onSlotChange}></slot>
3
+ <div class="g-recaptcha"></div>
4
4
  <div
5
5
  if:true={errorMessage}
6
6
  aria-live="assertive"
@@ -1,25 +1,33 @@
1
1
  import { api, LightningElement } from "lwc";
2
2
 
3
- declare module globalThis {
4
- let grecaptcha: {
5
- render(element: HTMLElement | null, recaptchaConfig: any): void;
6
- reset(): void;
7
- };
8
- }
3
+ // NOTE: This component assumes it is the only Recaptcha widget on a page.
4
+ type RecaptchaApi = {
5
+ render(element: HTMLElement | null, recaptchaConfig: any): void;
6
+ reset(): void;
7
+ };
9
8
 
10
9
  export default class Recaptcha extends LightningElement {
11
- private _recaptchaToken = "";
12
10
  private errorMessage = "";
13
- private slottedRecaptchaDiv: HTMLDivElement | null = null;
11
+ private recaptchaDiv!: HTMLDivElement;
12
+ private recaptchaApi?: RecaptchaApi;
13
+ private recaptchaToken = "";
14
14
 
15
15
  @api missingErrorMessage = "Complete the captcha";
16
16
  @api recaptchaSiteKey = "";
17
17
 
18
18
  @api // read only
19
19
  get value() {
20
- return this._recaptchaToken;
20
+ return this.recaptchaToken;
21
21
  }
22
22
 
23
+ @api
24
+ recaptchaScriptOnLoad = (recaptchaApi: RecaptchaApi) => {
25
+ this.recaptchaApi = recaptchaApi;
26
+ if (this.recaptchaDiv) {
27
+ this.renderRecaptcha();
28
+ }
29
+ };
30
+
23
31
  @api
24
32
  reset = () => this.resetRecaptcha();
25
33
 
@@ -36,38 +44,42 @@ export default class Recaptcha extends LightningElement {
36
44
 
37
45
  @api
38
46
  checkValidity = () => {
39
- return Boolean(
40
- (
41
- this.slottedRecaptchaDiv?.querySelector(
42
- "#g-recaptcha-response"
43
- ) as HTMLTextAreaElement
44
- )?.value
45
- );
47
+ return Boolean(this.recaptchaToken);
46
48
  };
47
49
 
48
- private onSlotChange(e: Event) {
49
- this.slottedRecaptchaDiv = e.target as HTMLDivElement;
50
+ renderedCallback() {
51
+ if (!this.recaptchaDiv) {
52
+ this.recaptchaDiv = this.template.querySelector(
53
+ ".g-recaptcha"
54
+ ) as HTMLDivElement;
55
+ if (this.recaptchaApi) {
56
+ this.renderRecaptcha();
57
+ }
58
+ }
59
+ }
60
+
61
+ private renderRecaptcha = () => {
50
62
  const smallMatchMedia = window.matchMedia(
51
63
  "screen and (max-width: 768px)"
52
64
  );
53
65
 
54
66
  // render recaptcha, referencing this component's callbacks for handling events
55
- globalThis.grecaptcha?.render(this.slottedRecaptchaDiv, {
67
+ this.recaptchaApi?.render(this.recaptchaDiv, {
56
68
  sitekey: this.recaptchaSiteKey,
57
69
  callback: this.recaptchaCallback,
58
70
  "expired-callback": this.recaptchaExpiredCallback,
59
71
  "error-callback": this.recaptchaErrorCallback,
60
72
  size: smallMatchMedia.matches ? "compact" : "normal"
61
73
  });
62
- }
74
+ };
63
75
 
64
76
  private recaptchaCallback = (token: string) => {
65
- this._recaptchaToken = token;
77
+ this.recaptchaToken = token;
66
78
  this.errorMessage = "";
67
79
  };
68
80
 
69
81
  private recaptchaExpiredCallback = () => {
70
- this._recaptchaToken = ""; // note: recaptcha element itself handles showing an "expired" message
82
+ this.recaptchaToken = ""; // note: recaptcha element itself handles showing an "expired" message
71
83
  };
72
84
 
73
85
  private recaptchaErrorCallback = () => {
@@ -76,7 +88,7 @@ export default class Recaptcha extends LightningElement {
76
88
  };
77
89
 
78
90
  private resetRecaptcha = () => {
79
- this._recaptchaToken = "";
80
- globalThis.grecaptcha?.reset();
91
+ this.recaptchaToken = "";
92
+ this.recaptchaApi?.reset();
81
93
  };
82
94
  }