I have a modal that allows a user to select some options and then upload a file. I want to close the modal as soon as the upload completes (the user clicks the Done button). So here's my code:
Parent markup
<lightning:overlayLibrary aura:id="overlayLib"/>
...
<lightning:buttonIcon iconName="utility:upload"
variant="border"
alternativeText="Upload Files"
title="Upload Files"
onclick="{!c.handleShowModal }" disabled="{!v.disabled}"/>
Parent controller
handleShowModal: function(component, evt, helper) {
var modalBody;
var modalHeader;
var recordId = component.get("v.myObjId");
$A.createComponent("c:fileUploadModal", {relatedRecordId: recordId},
function(content, status) {
if (status === "SUCCESS") {
modalBody = content;
modalHeader = $A.get("$Label.c.Upload_Modal_Header");
component.find('overlayLib').showCustomModal({
header: modalHeader,
body: modalBody,
showCloseButton: true,
cssClass: "mymodal",
closeCallback: function() {
// Refresh our list of files
helper.retrieveDocs(component, event, helper);
}
})
}
});
}
Modal markup
<lightning:fileUpload name="fileUploader"
multiple="false"
accept="{!v.fileExtensions}"
recordId="{!v.relatedRecordId}"
onuploadfinished="{!c.handleUploadFinished }"/>
Modal controller:
handleUploadFinished: function (component, event) {
var uploadedFiles = event.getParam("files");
console.log(uploadedFiles[0].documentId); // See? it works.
component.find("overlayLib").notifyClose();
}
Obviously the .notifyClose()
isn't working because component.find("overlayLib")
returns undefined
since that component does not have access to the parent component.
I tried document.getElementsByClassName('uiButton--modal-closeBtn')[0].click()
but that doesn't work because of Aura.
So how do I programmatically close the modal after the user acknowledges that their file has been uploaded successfully?