Skip to content

Commit

Permalink
fix(InlineEditor): onSave is called twice. Fixes #83
Browse files Browse the repository at this point in the history
it happens when enter key is pressed and saveOnBlur and saveOnEnter are enabled. onEnter call save method and after, it is also called by onBlur event. This approach should be improved, it could generate bugs.
  • Loading branch information
tonivj5 committed Aug 5, 2017
1 parent f95403b commit 2024c0a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/inline-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export class InlineEditorComponent implements OnInit, AfterContentInit, OnDestro
};

private refreshNGModel: (_: any) => void;
private isEnterKeyPressed = false;

ngOnInit() {
this.config = this.generateSafeConfig();
Expand Down Expand Up @@ -397,13 +398,20 @@ export class InlineEditorComponent implements OnInit, AfterContentInit, OnDestro

this.subscriptions.onBlurSubscription = this.events.internal.onBlur.subscribe(
({ event, state }: InternalEvent) => {
if (this.config.saveOnBlur) {
// TODO (xxxtonixx): Maybe, this approach is not the best,
// because we need to set a class property and it is dangerous.
// We should search for one better.
const isSavedByEnterKey = this.isEnterKeyPressed && this.config.saveOnEnter;

if (this.config.saveOnBlur && !isSavedByEnterKey) {
this.saveAndClose({
event,
state: state.getState(),
});
}

this.isEnterKeyPressed = false;

this.emit(this.onBlur, {
event,
state: state.getState(),
Expand All @@ -427,6 +435,8 @@ export class InlineEditorComponent implements OnInit, AfterContentInit, OnDestro

this.subscriptions.onEnterSubscription = this.events.internal.onEnter.subscribe(
({ event, state }: InternalEvent) => {
this.isEnterKeyPressed = true;

if (this.config.saveOnEnter) {
this.save({
event,
Expand Down

0 comments on commit 2024c0a

Please sign in to comment.