Sha256: 124b8f54f94ee0b98b617ed2ccc81fb722e9965f58dd6192629c691e281da093
Contents?: true
Size: 1.97 KB
Versions: 47
Compression:
Stored size: 1.97 KB
Contents
import { mergeConfigs } from '../../common/index.mjs'; import { normaliseDataset } from '../../common/normalise-dataset.mjs'; import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'; const DEBOUNCE_TIMEOUT_IN_SECONDS = 1; /** * JavaScript enhancements for the Button component * * @preserve */ class Button extends GOVUKFrontendComponent { /** * @param {Element | null} $root - HTML element to use for button * @param {ButtonConfig} [config] - Button config */ constructor($root, config = {}) { super($root); this.config = void 0; this.debounceFormSubmitTimer = null; this.config = mergeConfigs(Button.defaults, config, normaliseDataset(Button, this.$root.dataset)); this.$root.addEventListener('keydown', event => this.handleKeyDown(event)); this.$root.addEventListener('click', event => this.debounce(event)); } handleKeyDown(event) { const $target = event.target; if (event.key !== ' ') { return; } if ($target instanceof HTMLElement && $target.getAttribute('role') === 'button') { event.preventDefault(); $target.click(); } } debounce(event) { if (!this.config.preventDoubleClick) { return; } if (this.debounceFormSubmitTimer) { event.preventDefault(); return false; } this.debounceFormSubmitTimer = window.setTimeout(() => { this.debounceFormSubmitTimer = null; }, DEBOUNCE_TIMEOUT_IN_SECONDS * 1000); } } /** * Button config * * @typedef {object} ButtonConfig * @property {boolean} [preventDoubleClick=false] - Prevent accidental double * clicks on submit buttons from submitting forms multiple times. */ /** * @typedef {import('../../common/index.mjs').Schema} Schema */ Button.moduleName = 'govuk-button'; Button.defaults = Object.freeze({ preventDoubleClick: false }); Button.schema = Object.freeze({ properties: { preventDoubleClick: { type: 'boolean' } } }); export { Button }; //# sourceMappingURL=button.mjs.map
Version data entries
47 entries across 47 versions & 2 rubygems