Skip to content

Commit

Permalink
fix(editors): all Editors should call commitChanges even when invalid,
Browse files Browse the repository at this point in the history
…fixes #571 (#574)

* fix(editors): fix all Editors custom validators with invalid results

* fix(editors): all Editors should call commitChanges even when invalid
- for the Editor validators to work properly it needs to call the commitChanges() at all time even when potentially invalid or value is unchanged so that the validator can be executed at all time
  • Loading branch information
ghiscoding committed Sep 8, 2020
1 parent e2b138d commit fd052d1
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 59 deletions.
14 changes: 11 additions & 3 deletions src/app/examples/grid-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ export class GridEditorComponent implements OnInit {
},
params: {
formatters: [Formatters.collectionEditor, Formatters.percentCompleteBar],
}
},
// validator: (value, args) => {
// if (value < 50) {
// return { valid: false, msg: 'Please use at least 50%' };
// }
// return { valid: true, msg: '' };
// }
}, {
id: 'start',
name: 'Start',
Expand All @@ -260,7 +266,7 @@ export class GridEditorComponent implements OnInit {
sortable: true,
type: FieldType.date,
editor: {
model: Editors.date
model: Editors.date,
},
}, {
id: 'finish',
Expand Down Expand Up @@ -582,7 +588,9 @@ export class GridEditorComponent implements OnInit {
}

onValidationError(e, args) {
alert(args.validationResults.msg);
if (args.validationResults) {
alert(args.validationResults.msg);
}
}

changeAutoCommit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ describe('FloatEditor', () => {
expect(spy).toHaveBeenCalled();
});

it('should not call anything when the input value is not a valid float number', () => {
it('should call "commitCurrentEdit" even when the input value is not a valid float number', () => {
mockItemData = { id: 1, price: null, isActive: true };
gridOptionMock.autoCommitEdit = true;
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
Expand All @@ -415,7 +415,7 @@ describe('FloatEditor', () => {
editor.setValue('-.');
editor.save();

expect(spy).not.toHaveBeenCalled();
expect(spy).toHaveBeenCalled();
});

it('should call "getEditorLock" and "save" methods when "hasAutoCommitEdit" is enabled and the event "focusout" is triggered', (done) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,13 @@ describe('SelectEditor', () => {
mockItemData = { id: 1, gender: '', isActive: true };
mockColumn.internalColumnEditor.required = true;
gridOptionMock.autoCommitEdit = true;
const commitEditSpy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
const commitChangeSpy = jest.spyOn(editorArguments, 'commitChanges');
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');

editor = new SelectEditor(editorArguments, true);
editor.loadValue(mockItemData);
editor.save();

expect(commitEditSpy).not.toHaveBeenCalled();
expect(commitChangeSpy).not.toHaveBeenCalled();
expect(spy).not.toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ describe('SliderEditor', () => {
expect(spy).toHaveBeenCalled();
});

it('should not call anything when the input value is the same as the default value', () => {
it('should call "commitCurrentEdit" even when the input value is the same as the default value', () => {
mockItemData = { id: 1, price: 0, isActive: true };
gridOptionMock.autoCommitEdit = true;
const spy = jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit');
Expand All @@ -398,7 +398,7 @@ describe('SliderEditor', () => {
editor.loadValue(mockItemData);
editor.save();

expect(spy).not.toHaveBeenCalled();
expect(spy).toHaveBeenCalled();
});

it('should call "getEditorLock" and "save" methods when "hasAutoCommitEdit" is enabled and the event "focusout" is triggered', (done) => {
Expand Down
14 changes: 8 additions & 6 deletions src/app/modules/angular-slickgrid/editors/autoCompleteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,14 @@ export class AutoCompleteEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/modules/angular-slickgrid/editors/checkboxEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ export class CheckboxEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged() && this.hasAutoCommitEdit) {
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/app/modules/angular-slickgrid/editors/dateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,15 @@ export class DateEditor implements Editor {
}

save() {
// autocommit will not focus the next editor
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/app/modules/angular-slickgrid/editors/floatEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ export class FloatEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/app/modules/angular-slickgrid/editors/integerEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ export class IntegerEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/modules/angular-slickgrid/editors/longTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export class LongTextEditor implements Editor {
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
Expand Down
17 changes: 8 additions & 9 deletions src/app/modules/angular-slickgrid/editors/selectEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,15 @@ export class SelectEditor implements Editor {
}

save() {
// autocommit will not focus the next editor
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (!this._destroying && this.hasAutoCommitEdit) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (!this._destroying && this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/app/modules/angular-slickgrid/editors/sliderEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ export class SliderEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.args.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/app/modules/angular-slickgrid/editors/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ export class TextEditor implements Editor {

save() {
const validation = this.validate();
if (validation && validation.valid && this.isValueChanged()) {
if (this.hasAutoCommitEdit) {
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
const isValid = (validation && validation.valid) || false;

if (this.hasAutoCommitEdit && isValid) {
// do not use args.commitChanges() as this sets the focus to the next row.
// also the select list will stay shown when clicking off the grid
this.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface Locale {
/** Text "Remove Sort" shown in Header Menu */
TEXT_REMOVE_SORT: string;

/** Text "Cancel" shown in the Long Text Editor dialog */
/** Text "Save" shown in the Long Text Editor dialog */
TEXT_SAVE: string;

/** Text "Select All" displayed in the Multiple Select Editor/Filter */
Expand Down

0 comments on commit fd052d1

Please sign in to comment.