0

I have a lightning-record-form in a modal dialog defined as follows:

<div role="dialog"
        tabindex="-1"
        data-id="editDialog"
        class='slds-modal'>
    <div class="slds-modal__container">
        <header class="slds-modal__header">
            <h2 class="slds-modal__title">Edit Lead</h2>
        </header>
        <div class="slds-modal__content
                    slds-var-p-around_medium">
            <lightning-record-form record-id={recordId}
                                    object-api-name="Lead__c"
                                    fields={fields}
                                    columns="2"
                                    mode="edit"
                                    onsuccess={handleSuccess}
                                    oncancel={handleCancel}>
            </lightning-record-form>
        </div>
    </div>
</div>

It works perfectly the first time, showing a modal dialog with the fields ready to edit.

enter image description here

But regardless of whether I save or cancel the dialog, the second time it appears, it shows up in view mode. Users can edit, but they have to click the pencil button. Everything does appear to work after that, however.

enter image description here

The handlers for the modal simply remove the appropriate classes and if a save has occurred, update the record.

refresh() {
    refreshApex(this._refresh);
}

get dialog() {
    return this.template.querySelector('[data-id="editDialog"]');
}

get backdrop() {
    return this.template.querySelector('[data-id="dialogBack"]');
}

closeDialog() {
    this.dialog.classList.remove('slds-fade-in-open');
    this.backdrop.classList.remove('slds-backdrop_open');
}

handleCancel() {
    this.closeDialog();
}

handleSuccess() {
    this.closeDialog();
    this.refresh();
}

How do I get it to show up in edit mode every time?

5
  • You can't do that and I think this is the ideal behavior, if you really want that always in edit mode then you can go with the record-edit-form instead. Commented Apr 15, 2021 at 15:22
  • But this is true even if I bring up the dialog on a different record. So I click "Edit" (which brings up the edit dialog) for one record and it's in edit mode, then I do it for a different record and it's in view mode?
    – Chuck
    Commented Apr 15, 2021 at 15:36
  • Ahh so in that case you need to mount and unmount it when the record is changed. Using the if:true tag, so whenever the record id changes it will completely rerender itself. Commented Apr 15, 2021 at 15:40
  • Perhaps it's relevant that clicking "Edit" means selecting that menu item from a datatable's row actions? Is it possible to "unmount" the dialog during the close phase and "mount" it when I want it open? As you can probably tell, the dialog is shown by editing the class list.
    – Chuck
    Commented Apr 15, 2021 at 15:56
  • Yes, let me update that in the answer. Commented Apr 15, 2021 at 16:03

1 Answer 1

1

When you change the record, the record-form component is not reloaded, just the context is changed. But here you need to reload the component completely so you can get the default behavior same as first time load.

Also instead of manipulating the classList you need to use the if:true attribute. If you use this approach, the framework will automatically mount/unmount the component based on the value.

This is how you do it.

JS code.

showDialog;

openDialog(){
    this.showDialog = true;
}

closeDialog(){
    this.showDialog = false;
}

HTML Code

<div role="dialog" if:true={showDialog}
        tabindex="-1"
        data-id="editDialog"
        class='slds-modal'>
    <div class="slds-modal__container">

    <div class="slds-modal__container">
        <header class="slds-modal__header">
            <h2 class="slds-modal__title">Edit Lead</h2>
        </header>
        <div class="slds-modal__content
                    slds-var-p-around_medium">
            <lightning-record-form record-id={recordId}
                                    object-api-name="Lead__c"
                                    fields={fields}
                                    columns="2"
                                    mode="edit"
                                    onsuccess={handleSuccess}
                                    oncancel={handleCancel}>
            </lightning-record-form>
        </div>
    </div>

</div>

Now you can call the openDialog() and closeDialog() from wherever you want. This will solve the problem of the edit mode as well.

Also, you can use the showDialog to display and hide the backdrop in a similar way.

2
  • Thanks very much. Followup question. You placed the if:true within a div rather than a template tag. The documentation at developer.salesforce.com/docs/component-library/documentation/… says, "add the if:true|false directive to a nested <template> tag that encloses the conditional content." Is using the div instead undocumented behavior that might change even though it currently works?
    – Chuck
    Commented Apr 15, 2021 at 18:29
  • @Chuck Not only div but you can use this with any HTML or Lwc components. It will not change, it will continue to work. Commented Apr 16, 2021 at 5:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .