Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editors): add onBeforeOpen optional callback to Composite Editor #306

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export interface CompositeEditorOpenDetailOption {
*/
viewColumnLayout?: 1 | 2 | 3 | 'auto';

/** onBeforeOpen callback allows the user to optionally execute something before opening the modal (for example cancel any batch edits, or change/reset some validations in column definitions) */
onBeforeOpen?: () => void;

/**
* onClose callback allows user to add a confirm dialog or any other code before closing the modal window, returning false will cancel the modal closing.
* NOTE: this won't be called when there's no changes done in the form.
Expand All @@ -103,7 +106,7 @@ export interface CompositeEditorOpenDetailOption {
/** current selection of row indexes & data context Ids */
selection: CompositeEditorSelection,

/** optional item data context that is returned only when the modal type is clone/create/edit */
/** optional item data context that is returned, this is only provided when the modal type is (clone, create or edit) */
dataContext?: any
) => Promise<boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,22 @@ describe('CompositeEditorService', () => {
expect(compositeContainerElm2).toBeFalsy();
});

it('should execute "onBeforeOpen" callback before opening the composite modal window', () => {
const mockProduct = { id: 222, address: { zip: 123456 }, product: { name: 'Product ABC', price: 12.55 } };
jest.spyOn(gridStub, 'getDataItem').mockReturnValue(mockProduct);
const mockOnBeforeCallback = jest.fn();

component = new SlickCompositeEditorComponent();
component.init(gridStub, container);
component.openDetails({ headerTitle: 'Details', onBeforeOpen: mockOnBeforeCallback });
const compositeContainerElm = document.querySelector('div.slick-editor-modal.slickgrid_123456') as HTMLSelectElement;

expect(mockOnBeforeCallback).toHaveBeenCalled();
expect(component).toBeTruthy();
expect(component.constructor).toBeDefined();
expect(compositeContainerElm).toBeTruthy();
});

it('should execute "onClose" callback when user confirms the closing of the modal when "onClose" callback is defined', (done) => {
const mockProduct = { id: 222, address: { zip: 123456 }, productName: 'Product ABC', price: 12.55 };
jest.spyOn(gridStub, 'getDataItem').mockReturnValue(mockProduct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ export class SlickCompositeEditorComponent implements ExternalResource {
const gridUid = this.grid.getUID() || '';
let headerTitle = options.headerTitle || '';

// execute callback before creating the modal window (that is in short the first event in the lifecycle)
if (typeof this._options.onBeforeOpen === 'function') {
this._options.onBeforeOpen();
}

if (this.hasRowSelectionEnabled() && this._options.modalType === 'auto-mass' && this.grid.getSelectedRows) {
const selectedRowsIndexes = this.grid.getSelectedRows() || [];
if (selectedRowsIndexes.length > 0) {
Expand Down Expand Up @@ -551,7 +556,7 @@ export class SlickCompositeEditorComponent implements ExternalResource {

/** Show a Validation Summary text (as a <div>) when a validation fails or simply hide it when there's no error */
showValidationSummaryText(isShowing: boolean, errorMsg = '') {
if (isShowing) {
if (isShowing && errorMsg !== '') {
this._modalBodyTopValidationElm.textContent = errorMsg;
this._modalBodyTopValidationElm.style.display = 'block';
this._modalBodyTopValidationElm.scrollIntoView?.();
Expand Down
Binary file not shown.