-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable submit button after click to stop multiple submissions (#636)
* submit button fix * add javascript to disable and style on click * remove log line * added tests * remove .only from test * refactored * changed readme * change readme * revert refactor * remove button loading from feeback * pr comments and changes to examples * removed test functionality covered by another test
- Loading branch information
1 parent
d1a3abf
commit 3fbea5e
Showing
11 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import domready from 'js/domready'; | ||
|
||
async function submitButton() { | ||
const buttons = [...document.querySelectorAll('.js-button')]; | ||
|
||
if (buttons.length) { | ||
const SubmitButton = (await import('./button')).default; | ||
buttons.forEach(button => { | ||
new SubmitButton(button); | ||
}); | ||
} | ||
} | ||
|
||
domready(submitButton); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default class SubmitButton { | ||
constructor(component) { | ||
this.button = component; | ||
this.button.addEventListener('click', this.loadingButton.bind(this)); | ||
} | ||
|
||
loadingButton() { | ||
this.button.classList.add('is-loading'); | ||
this.button.setAttribute('disabled', true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{% from "components/button/_macro.njk" import onsButton %} | ||
{{ | ||
onsButton({ | ||
"type": 'button', | ||
"text": 'Start', | ||
"classes": 'btn--loader is-loading' | ||
"type": 'submit', | ||
"text": 'Submit' | ||
}) | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{% from "components/button/_macro.njk" import onsButton %} | ||
{{ | ||
onsButton({ | ||
"type": 'button', | ||
"text": 'Submit' | ||
"text": 'Save and continue' | ||
}) | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { awaitPolyfills } from 'js/polyfills/await-polyfills'; | ||
import template from 'components/button/_test-template.njk'; | ||
import SubmitButton from 'components/button/button'; | ||
|
||
const params = { | ||
id: 'button', | ||
type: 'submit', | ||
text: 'Submit', | ||
}; | ||
|
||
describe('Function: Submit Button ', function() { | ||
let wrapper, buttonElement; | ||
|
||
before(() => awaitPolyfills); | ||
|
||
beforeEach(() => { | ||
const html = template.render({ params }); | ||
|
||
wrapper = document.createElement('div'); | ||
wrapper.innerHTML = html; | ||
document.body.appendChild(wrapper); | ||
|
||
buttonElement = document.getElementById(params.id); | ||
}); | ||
|
||
afterEach(() => { | ||
if (wrapper) { | ||
wrapper.remove(); | ||
} | ||
}); | ||
|
||
describe('Before the button is initialised', () => { | ||
it('Button should have relevant classes', () => { | ||
expect(buttonElement.classList.contains('btn--loader')).to.be.true; | ||
expect(buttonElement.classList.contains('js-button')).to.be.true; | ||
}); | ||
|
||
it('Button should be type submit', () => { | ||
expect(buttonElement.getAttribute('type')).to.equal('submit'); | ||
}); | ||
}); | ||
|
||
describe('Once the button is initialised', () => { | ||
beforeEach(() => { | ||
new SubmitButton(buttonElement); | ||
}); | ||
|
||
it('Button disabled attribute should not be set', () => { | ||
expect(buttonElement.getAttribute('disabled')).to.not.exist; | ||
}); | ||
|
||
describe('and the button is clicked', () => { | ||
beforeEach(() => { | ||
buttonElement.click(); | ||
}); | ||
|
||
it('Button should have loading style applied', () => { | ||
expect(buttonElement.classList.contains('is-loading')).to.be.true; | ||
}); | ||
|
||
it('Button should be disabled', () => { | ||
expect(buttonElement.getAttribute('disabled')).to.equal('true'); | ||
}); | ||
}); | ||
}); | ||
}); |