Skip to content

Commit

Permalink
#27, #60 call editFinishedCallback when edits are canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonLi8 committed May 22, 2020
1 parent 2d48d7c commit 0868ad3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions js/common/view/KeypadDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ class KeypadDialog extends Dialog {
* @param {Property.<number>} valueProperty - the Property that the user can manipulate through the KeypadDialog
* @param {Range} valueRange - the Range that the user can edit the valueProperty
* @param {string} unitsString - the template string that formats the text on the rangeText.
* @param {function} editFinishedCallback - callback when edit is finished through the "Enter" button, regardless
* of whether or not the edit was valid.
* @param {function} editFinishedCallback - callback when edit is entered or canceled.
*/
beginEdit( valueProperty, valueRange, unitsString, editFinishedCallback ) {
assert && assert( valueProperty instanceof Property && typeof valueProperty.value === 'number', `invalid valueProperty: ${ valueProperty }` );
Expand Down Expand Up @@ -175,11 +174,10 @@ class KeypadDialog extends Dialog {
* This is called when the user presses the 'Enter' button.
*/
submitEdit() {
this.editFinishedCallback();

// If the user didn't enter anything, treat this as a cancel.
if ( this.keypad.stringProperty.value === '' ) {
this.resetAndHide();
this.finishEdit();
return;
}

Expand All @@ -189,7 +187,7 @@ class KeypadDialog extends Dialog {
// If the edit is valid, the valueProperty is set and the edit.
if ( this.valueRange.contains( value ) ) {
this.valueProperty.value = value;
this.resetAndHide();
this.finishEdit();
}
else { this.warnOutOfRange(); }
}
Expand All @@ -205,12 +203,12 @@ class KeypadDialog extends Dialog {
}

/**
* Convenience method to reset the state of the KeypadDialog and to hide the KeypadDialog.
* Convenience method to finish the KeypadDialog.
* @private
*
* This method is invoked when a edit is canceled or when a valid edit is entered.
*/
resetAndHide() {
finishEdit() {
this.hide(); // Hide the KeypadDialog
this.keypad.clear(); // Clear the Keypad

Expand All @@ -219,6 +217,16 @@ class KeypadDialog extends Dialog {
this.valueRange = null;
this.editFinishedCallback = null;
}

/**
* @override
* Hides the dialog. Overridden to also call the editFinishedCallback function when edits are canceled.
* @public
*/
hide() {
this.editFinishedCallback();
super.hide();
}
}

collisionLab.register( 'KeypadDialog', KeypadDialog );
Expand Down

0 comments on commit 0868ad3

Please sign in to comment.