Skip to content

Commit 1a41db3

Browse files
authored
refactor!: move vaadin-crud new item button to light DOM (#4959)
1 parent f9b9bf4 commit 1a41db3

6 files changed

Lines changed: 79 additions & 73 deletions

File tree

‎packages/crud/src/vaadin-crud.d.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ export type CrudEventMap<T> = CrudCustomEventMap<T> & HTMLElementEventMap;
160160
* `save-button` | To replace the "Save" button.
161161
* `cancel-button`| To replace the "Cancel" button.
162162
* `delete-button`| To replace the "Delete" button.
163-
* `toolbar` | To replace the toolbar content. Add an element with the attribute `new-button` for the new item action.
163+
* `toolbar` | To provide the toolbar content (by default, it's empty).
164+
* `new-button` | To replace the "New item" button.
164165
*
165166
* #### Example:
166167
*
@@ -181,10 +182,8 @@ export type CrudEventMap<T> = CrudCustomEventMap<T> & HTMLElementEventMap;
181182
* <vaadin-text-field label="Surname" path="surname"></vaadin-text-field>
182183
* </vaadin-form-layout>
183184
*
184-
* <div slot="toolbar">
185-
* Total singers: [[size]]
186-
* <button new-button>New singer</button>
187-
* </div>
185+
* <div slot="toolbar">Total singers: 2</div>
186+
* <button slot="new-button">New singer</button>
188187
*
189188
* <button slot="save-button">Save changes</button>
190189
* <button slot="cancel-button">Discard changes</button>

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class ButtonSlotController extends SlotController {
7777
* `save-button` | To replace the "Save" button.
7878
* `cancel-button`| To replace the "Cancel" button.
7979
* `delete-button`| To replace the "Delete" button.
80-
* `toolbar` | To replace the toolbar content. Add an element with the attribute `new-button` for the new item action.
80+
* `toolbar` | To provide the toolbar content (by default, it's empty).
81+
* `new-button` | To replace the "New item" button.
8182
*
8283
* #### Example:
8384
*
@@ -98,10 +99,8 @@ class ButtonSlotController extends SlotController {
9899
* <vaadin-text-field label="Surname" path="surname"></vaadin-text-field>
99100
* </vaadin-form-layout>
100101
*
101-
* <div slot="toolbar">
102-
* Total singers: [[size]]
103-
* <button new-button>New singer</button>
104-
* </div>
102+
* <div slot="toolbar">Total singers: 2</div>
103+
* <button slot="new-button">New singer</button>
105104
*
106105
* <button slot="save-button">Save changes</button>
107106
* <button slot="cancel-button">Discard changes</button>
@@ -255,10 +254,9 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
255254
<div id="main">
256255
<slot name="grid"></slot>
257256
258-
<div id="toolbar" part="toolbar" on-click="__new">
259-
<slot name="toolbar">
260-
<vaadin-button new-button="" id="new" theme="primary">[[i18n.newItem]]</vaadin-button>
261-
</slot>
257+
<div id="toolbar" part="toolbar">
258+
<slot name="toolbar"></slot>
259+
<slot name="new-button"></slot>
262260
</div>
263261
</div>
264262
@@ -374,6 +372,14 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
374372
type: Object,
375373
},
376374

375+
/**
376+
* A reference to the "New item" button
377+
* @private
378+
*/
379+
_newButton: {
380+
type: Object,
381+
},
382+
377383
/**
378384
* An array containing the items which will be stamped to the column template instances.
379385
* @type {Array<unknown> | undefined}
@@ -618,6 +624,7 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
618624
'__saveButtonPropsChanged(_saveButton, i18n.saveItem, __isDirty)',
619625
'__cancelButtonPropsChanged(_cancelButton, i18n.cancel)',
620626
'__deleteButtonPropsChanged(_deleteButton, i18n.deleteItem, __isNew)',
627+
'__newButtonPropsChanged(_newButton, i18n.newItem)',
621628
];
622629
}
623630

@@ -637,6 +644,7 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
637644
this.__cancel = this.__cancel.bind(this);
638645
this.__delete = this.__delete.bind(this);
639646
this.__save = this.__save.bind(this);
647+
this.__new = this.__new.bind(this);
640648
this.__onFormChange = this.__onFormChange.bind(this);
641649
this.__onGridEdit = this.__onGridEdit.bind(this);
642650
this.__onGridSizeChanged = this.__onGridSizeChanged.bind(this);
@@ -666,6 +674,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
666674

667675
this.addController(new SlotController(this, 'form', 'vaadin-crud-form'));
668676

677+
this.addController(new ButtonSlotController(this, 'new', 'primary'));
678+
669679
// NOTE: order in which buttons are added should match the order of slots in template
670680
this.addController(new ButtonSlotController(this, 'save', 'primary'));
671681
this.addController(new ButtonSlotController(this, 'cancel', 'tertiary'));
@@ -998,6 +1008,17 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
9981008
}
9991009
}
10001010

1011+
/**
1012+
* @param {HTMLElement | undefined} newButton
1013+
* @param {string} i18nNewItem
1014+
* @private
1015+
*/
1016+
__newButtonPropsChanged(newButton, i18nNewItem) {
1017+
if (newButton) {
1018+
newButton.textContent = i18nNewItem;
1019+
}
1020+
}
1021+
10011022
/**
10021023
* @param {string} type
10031024
* @param {HTMLElement} newButton
@@ -1178,11 +1199,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
11781199
}
11791200

11801201
/** @private */
1181-
__new(event) {
1182-
// This allows listening to parent element and fire only when clicking on default or custom new-button.
1183-
if (event.composedPath().some((e) => e.nodeType === 1 && e.hasAttribute('new-button'))) {
1184-
this.__confirmBeforeChangingEditedItem(null, true);
1185-
}
1202+
__new() {
1203+
this.__confirmBeforeChangingEditedItem(null, true);
11861204
}
11871205

11881206
/** @private */

‎packages/crud/test/crud-buttons.test.js‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('crud buttons', () => {
7777
});
7878

7979
it('should save a new item', () => {
80-
crud.$.new.click();
80+
crud._newButton.click();
8181
crud._form._fields[0].value = 'baz';
8282
change(crud._form);
8383
saveButton.click();
@@ -169,7 +169,7 @@ describe('crud buttons', () => {
169169

170170
it('should not ask for confirmation on cancel when not modified - click out', () => {
171171
edit(crud.items[0]);
172-
crud.$.new.click();
172+
crud._newButton.click();
173173
expect(confirmCancelDialog.opened).not.to.be.ok;
174174
});
175175

@@ -189,7 +189,7 @@ describe('crud buttons', () => {
189189
it('should ask for confirmation on cancel when modified - click out', () => {
190190
edit(crud.items[0]);
191191
change(crud._form);
192-
crud.$.new.click();
192+
crud._newButton.click();
193193
expect(confirmCancelDialog.opened).to.be.true;
194194
});
195195

@@ -239,13 +239,7 @@ describe('crud buttons', () => {
239239
crud.addEventListener('cancel', cancelSpyListener);
240240

241241
crud._grid.activeItem = crud.items[0];
242-
crud.$.new.dispatchEvent(
243-
new CustomEvent('click', {
244-
bubbles: true,
245-
cancelable: true,
246-
composed: true,
247-
}),
248-
);
242+
crud._newButton.click();
249243

250244
await aTimeout(0);
251245
expect(cancelSpyListener.calledOnce).to.be.ok;
@@ -323,7 +317,7 @@ describe('crud buttons', () => {
323317
});
324318

325319
it('should configure dirty and new flags on new', () => {
326-
crud.$.new.click();
320+
crud._newButton.click();
327321
expect(crud.__isDirty).not.to.be.true;
328322
expect(crud.__isNew).to.be.true;
329323
});
@@ -348,7 +342,7 @@ describe('crud buttons', () => {
348342
});
349343

350344
it('should hide delete button on new', async () => {
351-
crud.$.new.click();
345+
crud._newButton.click();
352346
await nextRender(crud.$.dialog.$.overlay);
353347
expect(deleteButton.hasAttribute('hidden')).to.be.true;
354348
});
@@ -402,7 +396,7 @@ describe('crud buttons', () => {
402396
expect(crud.editorOpened).to.be.true;
403397

404398
change(crud._form);
405-
crud.$.new.click();
399+
crud._newButton.click();
406400
expect(confirmCancelDialog.opened).to.be.true;
407401
});
408402

@@ -411,7 +405,7 @@ describe('crud buttons', () => {
411405
expect(crud.editorOpened).to.be.true;
412406

413407
change(crud._form);
414-
crud.$.new.click();
408+
crud._newButton.click();
415409
await oneEvent(confirmCancelOverlay, 'vaadin-overlay-open');
416410

417411
confirmCancelOverlay.querySelector('[slot^="confirm"]').click();
@@ -440,19 +434,19 @@ describe('crud buttons', () => {
440434
describe('new', () => {
441435
it('should fire the new event', (done) => {
442436
listenOnce(crud, 'new', () => done());
443-
crud.$.new.click();
437+
crud._newButton.click();
444438
});
445439

446440
it('on new should set the item and open dialog if not default prevented', () => {
447441
expect(crud.editedItem).not.to.be.ok;
448-
crud.$.new.click();
442+
crud._newButton.click();
449443
expect(crud.editedItem).to.be.ok;
450444
expect(crud.editorOpened).to.be.true;
451445
});
452446

453447
it('on new should not set the item but open dialog if default prevented', () => {
454448
listenOnce(crud, 'new', (e) => e.preventDefault());
455-
crud.$.new.click();
449+
crud._newButton.click();
456450
expect(crud.editedItem).not.to.be.ok;
457451
expect(crud.editorOpened).to.be.true;
458452
});
@@ -645,7 +639,7 @@ describe('crud buttons', () => {
645639
});
646640

647641
it('should hide delete button on new', async () => {
648-
crud.$.new.click();
642+
crud._newButton.click();
649643
await nextRender(crud.$.dialog.$.overlay);
650644
expect(deleteButton.hasAttribute('hidden')).to.be.true;
651645
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('crud editor', () => {
1717
});
1818

1919
it('should have new item title', () => {
20-
crud.$.new.click();
20+
crud._newButton.click();
2121
expect(header.textContent).to.be.equal('New item');
2222
});
2323

@@ -29,7 +29,7 @@ describe('crud editor', () => {
2929
it('should change to new item title', () => {
3030
crud.editedItem = crud.items[0];
3131
expect(header.textContent).to.be.equal('Edit item');
32-
crud.$.new.click();
32+
crud._newButton.click();
3333
expect(header.textContent).to.be.equal('New item');
3434
});
3535
});

0 commit comments

Comments
 (0)