Skip to content

Commit

Permalink
Debounce submission of the form
Browse files Browse the repository at this point in the history
  • Loading branch information
joegaudet committed Nov 3, 2022
1 parent e582d63 commit d4d5e78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
24 changes: 22 additions & 2 deletions addon/components/form-for.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Component from '@glimmer/component';
import { arg, func } from 'ember-arg-types';
import { bool, string, object, boolean } from 'prop-types';
import { bool, string, object, boolean, number } from 'prop-types';
import { next } from '@ember/runloop';
import { dasherize } from '@ember/string';
import { action, setProperties, notifyPropertyChange } from '@ember/object';
Expand All @@ -9,6 +9,7 @@ import { A } from '@ember/array';
import { inject as service } from '@ember/service';
import { Promise } from 'rsvp';
import { tracked } from '@glimmer/tracking';
import { debounce } from '@ember/runloop';

export default class FormForComponent extends Component {
@service formFor;
Expand Down Expand Up @@ -154,6 +155,19 @@ export default class FormForComponent extends Component {
return this.useBemClassConfig || false;
}


/**
* How long in ms to debounce the form submit, useful for autoSubmit situations
* @property formActionDebounce
* @type number
* @default 200
* @public
*/
@arg(number)
get formActionDebounce() {
return this.formFor.formActionDebounce ?? 0;
}

/**
* A class which will be appended to the form for styling purposes using bem style
* @property _bemClass
Expand Down Expand Up @@ -207,6 +221,7 @@ export default class FormForComponent extends Component {
: 'form-button';
}


// --------------------------------------------------------------------------------
// This section is where the DSL syntax lives

Expand Down Expand Up @@ -798,7 +813,11 @@ export default class FormForComponent extends Component {
* @method doSubmit
* @public
*/
async doSubmit() {
doSubmit() {
return debounce(this, '_doSubmit', this.formActionDebounce);
}

async _doSubmit() {
const lastDoSubmit = this.lastDoSubmit;

const model = this.model;
Expand Down Expand Up @@ -1191,6 +1210,7 @@ export default class FormForComponent extends Component {
updateValues(keyValues) {
return this.updateValuesFn(keyValues);
}

@action testClass(type) {
return `${type}-button`;
}
Expand Down
1 change: 1 addition & 0 deletions addon/services/form-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class FormForService extends Service {
this.customLabelComponent = this.config.customLabelComponent;
this.customInfoTextComponent = this.config.customInfoTextComponent;
this.preventsNavigationByDefault = this.config.preventsNavigationByDefault;
this.formActionDebounce = this.config.formActionDebounce;

this.router.on('routeWillChange', (transition) => {
if (
Expand Down
1 change: 1 addition & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function (/* environment, appConfig */) {

controlsFolder: 'form-controls',
controlPrefix: 'ff-',
formActionDebounce: 200,
},
},
};
Expand Down

0 comments on commit d4d5e78

Please sign in to comment.