@@ -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 */
0 commit comments