I have a modal component where I am passing in two-button handler methods which are callbacks.
These two buttons are hooked up to a getter with the intent that I can override them, which I am having trouble doing.
Code Sample:
export default class CreateCaseApp extends LightningElement {
// Default button handlers
_saveHandler = {
cb: this._modalSaveHandler,
label: "Create Case",
variant: "brand",
};
_cancelHandler = {
cb: this._modalCancelHandler,
label: "Cancel",
variant: "netrual",
class: "btn-left",
};
/**
* Getter: Save handler callback and button props
*/
get saveHandler() {
return this._saveHandler;
}
/**
* Getter: Cancel handler callback and button props
*/
get cancelHandler() {
return this._cancelHandler;
}
/**
* Save event send to modal
* @param {*} event
*/
_modalSaveHandler = (event) => {
console.log("Save Clicked");
};
/**
* Handler event sent to modal
* @param {*} event
*/
_modalCancelHandler = (event) => {
console.log("Cancel Clicked");
};
// Questionable code below
/**
* Handler event sent to modal for override
* @param {*} event
*/
_modalOverrideHandler = (event) => {
console.log("Override Clicked");
};
onDispatchedEventFromChild(event) {
if (event.detail == "nodata") {
// Override save handler button
this._saveHandler = {
cb: this._modalOverrideHandler,
label: "Okay",
variant: "brand"
};
}
}
}
In the above code, the initial saveHandler()
and cancelHandler()
are passing the fat arrow methods correctly via the cb
property in the object. I can click on these buttons in the child component and they execute the method from this parent.
In the bottom section of code, I have an event listener onDispatchedEventFromChild()
that receives data from a child component. When the logic in this method is met, I am trying to override the _saveHandler
button by setting it with this._saveHandler={...}
.
This updates the button details such as label and variant, but when I click on it, the new referenced callback isn't being fired.
Is this possible to do, or am I approaching this incorrectly? I tried using @track
on the two variables, but that also didn't work.
Edit:
Parent Component HTML
<template>
<c-modal
modal-header={modalHeader}
modal-save-handler={saveHandler}
>
<div
slot="modalContent"
class="modalContent slds-modal__content slds-p-around_medium"
>
<!-- Wrapper -->
<c-create-case-wrapper
contact-id={recordId}
onmodalheaderupdate={handleModalHeaderEvent}
onmodalhandlerupdate={handleModalHandlerEvent}
></c-create-case-wrapper>
<!-- Wrapper -->
</div>
</c-modal>
<!-- Main Modal Launch Button -->
<lightning-button
variant="brand"
label="Create Enterprise Case"
title="Create Enterprise Case"
onclick={handleClick}
class="slds-m-left_x-small"
></lightning-button>
<!-- Main Modal Launch Button -->
</template>
The parent is providing the callback for the button click within the modal component. When a button is clicked, the parent executes that method (in the parent).
In this example, the <c-create-case-wrapper>
is also loading child components based on logic. When certain logic is met within those components, I need to change the button callback and label. This is where a child would dispatch an event to the parent so that I know I need to update the modal button.
I assumed that since the button was being passed to the modal reactively through the getter that I could just update that object/callback to override its defaults. This works for the label and variant properties just fine, its passing another/different callback that is not working.
modal_wrapper.html
component is my parent in this case, which passes the save handler as a property through<c-modal>
.onDispatchedEventFromChild
dispatched from another component while the modal is still active? There are a few ways to accomplish your requirement, but I'm curious why you want to hook up the parent component function into the modal componentssave
button? Instead, you can use a standard custom event dispatch from modal component to the parent component. Is that not suitable for your requirement?