Skip to content

Commit 51d5f23

Browse files
authored
fix: move CRUD children back to light DOM and keep their order (#9253)
1 parent 0475a0d commit 51d5f23

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

packages/crud/src/vaadin-crud-mixin.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,26 +384,26 @@ export const CrudMixin = (superClass) =>
384384
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-outside-click', this.__cancel);
385385
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-escape-press', this.__cancel);
386386

387+
this._gridController = new GridSlotController(this);
388+
this.addController(this._gridController);
389+
390+
// Init button controllers in `ready()` (not constructor) so that Flow can set `_noDefaultButtons`
391+
this._newButtonController = new ButtonSlotController(this, 'new', 'primary', this._noDefaultButtons);
392+
this.addController(this._newButtonController);
393+
387394
this._headerController = new SlotController(this, 'header', 'h3', {
388395
initializer: (node) => {
389396
this._defaultHeader = node;
390397
},
391398
});
392399
this.addController(this._headerController);
393400

394-
this._gridController = new GridSlotController(this);
395-
this.addController(this._gridController);
396-
397401
this.addController(new FormSlotController(this));
398402

399-
// Init controllers in `ready()` (not constructor) so that Flow can set `_noDefaultButtons`
400-
this._newButtonController = new ButtonSlotController(this, 'new', 'primary', this._noDefaultButtons);
401403
this._saveButtonController = new ButtonSlotController(this, 'save', 'primary', this._noDefaultButtons);
402404
this._cancelButtonController = new ButtonSlotController(this, 'cancel', 'tertiary', this._noDefaultButtons);
403405
this._deleteButtonController = new ButtonSlotController(this, 'delete', 'tertiary error', this._noDefaultButtons);
404406

405-
this.addController(this._newButtonController);
406-
407407
// NOTE: order in which buttons are added should match the order of slots in template
408408
this.addController(this._saveButtonController);
409409
this.addController(this._cancelButtonController);
@@ -484,6 +484,9 @@ export const CrudMixin = (superClass) =>
484484
} else {
485485
this.$.editor.removeAttribute('tabindex');
486486
}
487+
} else if (oldOpened) {
488+
// Teleport form and buttons back to light DOM when closing overlay
489+
this.__moveChildNodes(this);
487490
}
488491

489492
this.__toggleToolbar();
@@ -522,7 +525,10 @@ export const CrudMixin = (superClass) =>
522525
// Teleport header node, form, and the buttons to corresponding slots.
523526
// NOTE: order in which buttons are moved matches the order of slots.
524527
[...nodes, ...buttons].forEach((node) => {
525-
target.appendChild(node);
528+
// Do not move nodes if the editor position has not changed
529+
if (node.parentNode !== target) {
530+
target.appendChild(node);
531+
}
526532
});
527533

528534
// Wait to set label until slotted element has been moved.

packages/crud/test/crud-editor.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ describe('crud editor', () => {
8686
expect(form.parentElement).to.equal(overlay);
8787
});
8888

89+
it(`should move ${type} form to crud after dialog closing with default editorPosition`, async () => {
90+
crud._grid.activeItem = crud.items[0];
91+
await nextRender();
92+
btnCancel.click();
93+
expect(form.parentElement).to.equal(crud);
94+
});
95+
8996
it(`should move ${type} form to crud when editorPosition set to bottom`, async () => {
9097
crud.editorPosition = 'bottom';
9198
crud._grid.activeItem = crud.items[0];

packages/crud/test/dom/__snapshots__/crud.test.snap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ export const snapshots = {};
33

44
snapshots["vaadin-crud host default"] =
55
`<vaadin-crud editor-position="">
6-
<h3 slot="header">
7-
Edit item
8-
</h3>
96
<vaadin-crud-grid
107
slot="grid"
118
style="touch-action: none;"
@@ -135,8 +132,6 @@ snapshots["vaadin-crud host default"] =
135132
</vaadin-crud-edit>
136133
</vaadin-grid-cell-content>
137134
</vaadin-crud-grid>
138-
<vaadin-crud-form slot="form">
139-
</vaadin-crud-form>
140135
<vaadin-button
141136
role="button"
142137
slot="new-button"
@@ -145,6 +140,11 @@ snapshots["vaadin-crud host default"] =
145140
>
146141
New item
147142
</vaadin-button>
143+
<h3 slot="header">
144+
Edit item
145+
</h3>
146+
<vaadin-crud-form slot="form">
147+
</vaadin-crud-form>
148148
<vaadin-button
149149
aria-disabled="true"
150150
disabled=""

0 commit comments

Comments
 (0)