I am trying to add a captcha to a LWC and the only way I have found was the hack to add it to an iframe with a URL of a static resource.
As part of that the iframe HTML is using postMessage
to send the response back. When that happens I am trying to set the value of a @track
decorated property to false so that a button is disabled.
However, it seems that this.disableSubmit
is undefined in the callback. how do I get access to the LWC property in this use case?
TEMPLATE
<template>
<iframe src={navigateTo} name="captchaFrame" onload={captchaLoaded} width="100%" height="100%" style="border-style: none;"></iframe>
<lightning-button onclick={handleClick} type="submit" name="submit" disabled={disableSubmit} variant="brand" label="Submit" ></lightning-button>
</template>
JS
import { LightningElement, track, api } from 'lwc';
import pageUrl from '@salesforce/resourceUrl/reCaptchaForm';
export default class GoogleCapatcha extends LightningElement {
@track navigateTo;
@track disableSubmit = false;
@api captchaSettings;
connectedCallback() {
this.navigateTo = pageUrl;
this.disableSubmit = true;
window.addEventListener("message", this.listenForMessage);
}
listenForMessage(message){
if (message.data==="Unlock"){
console.log(this.disableSubmit); ***This outputs undefined
this.disableSubmit = false; **This does NOT enable the button
}
}
//Used to test that if enabled that clicking will cause it to be disabled. Have to comment out the this.disableSubmit=true in the connectedCallback to test
handleClick(event){
event.preventDefault();
console.log(this.disableSubmit); ***This outputs true
this.disableSubmit = true; **This DOES disable the button
}
}
Post Message Code in iFrame
parent.postMessage("Unlock", "https://xxxxx.force.com");
How do I get access to the disableSubmit
property from the listenForMessage
function