Skip to content

Commit 7584f39

Browse files
authored
fix: support replacing slotted elements when dialog is open (#3667)
1 parent 6546b67 commit 7584f39

4 files changed

Lines changed: 181 additions & 115 deletions

File tree

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

Lines changed: 61 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import { MediaQueryController } from '@vaadin/component-base/src/media-query-con
1919
import { SlotMixin } from '@vaadin/component-base/src/slot-mixin.js';
2020
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
2121

22-
const HOST_PROPS = {
23-
save: [{ attr: 'disabled', prop: '__isDirty', parseProp: '__isSaveBtnDisabled' }, { prop: 'i18n.saveItem' }],
24-
cancel: [{ prop: 'i18n.cancel' }],
25-
delete: [{ attr: 'hidden', prop: '__isNew', parseProp: (prop) => prop }, { prop: 'i18n.deleteItem' }]
26-
};
27-
2822
/**
2923
* `<vaadin-crud>` is a Web Component for [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations.
3024
*
@@ -611,13 +605,9 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
611605
'__formPropsChanged(_form, _theme, include, exclude)',
612606
'__i18nChanged(i18n, _grid)',
613607
'__editOnClickChanged(editOnClick, _grid)',
614-
'__hostPropsChanged(' +
615-
HOST_PROPS.save.map(({ prop }) => prop).join(',') +
616-
',' +
617-
HOST_PROPS.cancel.map(({ prop }) => prop).join(',') +
618-
',' +
619-
HOST_PROPS.delete.map(({ prop }) => prop).join(',') +
620-
')'
608+
'__saveButtonPropsChanged(_saveButton, i18n.saveItem, __isDirty)',
609+
'__cancelButtonPropsChanged(_cancelButton, i18n.cancel)',
610+
'__deleteButtonPropsChanged(_deleteButton, i18n.deleteItem, __isNew)'
621611
];
622612
}
623613

@@ -689,8 +679,6 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
689679
this._grid = this.$.grid;
690680
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-outside-click', this.__cancel);
691681
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-escape-press', this.__cancel);
692-
// Initialize the default buttons
693-
this.__propagateHostAttributes();
694682

695683
this.addController(
696684
new MediaQueryController(this._fullscreenMediaQuery, (matches) => {
@@ -716,24 +704,9 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
716704
* @param {string} i18nEditItem
717705
* @private
718706
*/
719-
__headerPropsChanged(headerNode, isNew, newItem, editItem) {
707+
__headerPropsChanged(headerNode, isNew, i18nNewItem, i18nEditItem) {
720708
if (headerNode) {
721-
headerNode.textContent = isNew ? newItem : editItem;
722-
}
723-
}
724-
725-
/**
726-
* @param {HTMLElement | undefined} form
727-
* @param {string} theme
728-
* @param {string | string[] | undefined} include
729-
* @param {string | RegExp} exclude
730-
* @private
731-
*/
732-
__formPropsChanged(form, theme, include, exclude) {
733-
if (form) {
734-
form.include = include;
735-
form.exclude = exclude;
736-
form.setAttribute('theme', theme);
709+
headerNode.textContent = isNew ? i18nNewItem : i18nEditItem;
737710
}
738711
}
739712

@@ -882,6 +855,10 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
882855
this._headerNode = node;
883856
}
884857
});
858+
859+
if (this.editorOpened) {
860+
this.__ensureChildren();
861+
}
885862
}
886863

887864
/** @private */
@@ -948,6 +925,21 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
948925
form.addEventListener('input', this.__onFormChange);
949926
}
950927

928+
/**
929+
* @param {HTMLElement | undefined} form
930+
* @param {string} theme
931+
* @param {string | string[] | undefined} include
932+
* @param {string | RegExp} exclude
933+
* @private
934+
*/
935+
__formPropsChanged(form, theme, include, exclude) {
936+
if (form) {
937+
form.include = include;
938+
form.exclude = exclude;
939+
form.setAttribute('theme', theme);
940+
}
941+
}
942+
951943
/**
952944
* @param {HTMLElement} saveButton
953945
* @param {HTMLElement | undefined} oldSaveButton
@@ -957,6 +949,19 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
957949
this.__setupSlottedButton(saveButton, oldSaveButton, this.__save);
958950
}
959951

952+
/**
953+
* @param {HTMLElement | undefined} saveButton
954+
* @param {string} i18nLabel
955+
* @param {boolean} isDirty
956+
* @private
957+
*/
958+
__saveButtonPropsChanged(saveButton, i18nLabel, isDirty) {
959+
if (saveButton) {
960+
saveButton.toggleAttribute('disabled', this.__isSaveBtnDisabled(isDirty));
961+
saveButton.textContent = i18nLabel;
962+
}
963+
}
964+
960965
/**
961966
* @param {HTMLElement} deleteButton
962967
* @param {HTMLElement | undefined} oldDeleteButton
@@ -966,6 +971,19 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
966971
this.__setupSlottedButton(deleteButton, oldDeleteButton, this.__delete);
967972
}
968973

974+
/**
975+
* @param {HTMLElement | undefined} deleteButton
976+
* @param {string} i18nLabel
977+
* @param {boolean} isNew
978+
* @private
979+
*/
980+
__deleteButtonPropsChanged(deleteButton, i18nLabel, isNew) {
981+
if (deleteButton) {
982+
deleteButton.textContent = i18nLabel;
983+
deleteButton.toggleAttribute('hidden', isNew);
984+
}
985+
}
986+
969987
/**
970988
* @param {HTMLElement} cancelButton
971989
* @param {HTMLElement | undefined} oldCancelButton
@@ -975,6 +993,17 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
975993
this.__setupSlottedButton(cancelButton, oldCancelButton, this.__cancel);
976994
}
977995

996+
/**
997+
* @param {HTMLElement | undefined} saveButton
998+
* @param {string} i18nLabel
999+
* @private
1000+
*/
1001+
__cancelButtonPropsChanged(cancelButton, i18nLabel) {
1002+
if (cancelButton) {
1003+
cancelButton.textContent = i18nLabel;
1004+
}
1005+
}
1006+
9781007
/**
9791008
* @param {HTMLElement} newButton
9801009
* @param {HTMLElement | undefined | null} oldButton
@@ -989,51 +1018,6 @@ class Crud extends SlotMixin(ControllerMixin(ElementMixin(ThemableMixin(PolymerE
9891018
newButton.addEventListener('click', clickListener);
9901019
}
9911020

992-
/** @private */
993-
__hostPropsChanged() {
994-
this.__propagateHostAttributes();
995-
}
996-
997-
/** @private */
998-
__propagateHostAttributes() {
999-
this.__propagateHostAttributesToButton(this._saveButton, HOST_PROPS.save);
1000-
this.__propagateHostAttributesToButton(this._cancelButton, HOST_PROPS.cancel);
1001-
this.__propagateHostAttributesToButton(this._deleteButton, HOST_PROPS.delete);
1002-
}
1003-
1004-
/** @private */
1005-
__propagateHostAttributesToButton(button, props) {
1006-
// Ensure the slotted button element is present in the DOM.
1007-
// This is needed because the observer runs before `ready`.
1008-
if (button) {
1009-
props.forEach(({ attr, prop, parseProp }) => {
1010-
if (prop.indexOf('i18n') >= 0) {
1011-
button.textContent = this.i18n[prop.split('.')[1]];
1012-
} else {
1013-
if (typeof parseProp === 'string') {
1014-
this._setOrToggleAttribute(attr, this[parseProp](this[prop]), button);
1015-
return;
1016-
}
1017-
1018-
this._setOrToggleAttribute(attr, parseProp(this[prop]), button);
1019-
}
1020-
});
1021-
}
1022-
}
1023-
1024-
/** @private */
1025-
_setOrToggleAttribute(name, value, node) {
1026-
if (!name || !node) {
1027-
return;
1028-
}
1029-
1030-
if (value) {
1031-
node.setAttribute(name, typeof value === 'boolean' ? '' : value);
1032-
} else {
1033-
node.removeAttribute(name);
1034-
}
1035-
}
1036-
10371021
/** @private */
10381022
__dataProviderChanged(dataProvider) {
10391023
if (this._grid) {

0 commit comments

Comments
 (0)