Skip to content

Commit

Permalink
Use static class fields for all defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jun 20, 2023
1 parent 465294b commit 2547f89
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@ import { mergeConfigs, extractConfigByNamespace } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { I18n } from '../../i18n.mjs'

/**
* Accordion translation defaults
*
* @see {@link AccordionConfig.i18n}
* @constant
* @default
* @type {AccordionTranslations}
*/
const ACCORDION_TRANSLATIONS = {
hideAllSections: 'Hide all sections',
hideSection: 'Hide',
hideSectionAriaLabel: 'Hide this section',
showAllSections: 'Show all sections',
showSection: 'Show',
showSectionAriaLabel: 'Show this section'
}

/**
* Accordion component
*
Expand All @@ -44,18 +27,12 @@ export class Accordion {
/** @private */
this.$module = $module

/** @type {AccordionConfig} */
const defaultConfig = {
i18n: ACCORDION_TRANSLATIONS,
rememberExpanded: true
}

/**
* @private
* @type {AccordionConfig}
*/
this.config = mergeConfigs(
defaultConfig,
Accordion.defaults,
config || {},
normaliseDataset($module.dataset)
)
Expand Down Expand Up @@ -564,6 +541,26 @@ export class Accordion {
$punctuationEl.innerHTML = ', '
return $punctuationEl
}

/**
* Accordion default config
*
* @see {@link AccordionConfig}
* @constant
* @default
* @type {AccordionConfig}
*/
static defaults = Object.freeze({
i18n: {
hideAllSections: 'Hide all sections',
hideSection: 'Hide',
hideSectionAriaLabel: 'Hide this section',
showAllSections: 'Show all sections',
showSection: 'Show',
showSectionAriaLabel: 'Show this section'
},
rememberExpanded: true
})
}

const helper = {
Expand All @@ -589,16 +586,17 @@ const helper = {
/**
* Accordion config
*
* @see {@link Accordion.defaults}
* @typedef {object} AccordionConfig
* @property {AccordionTranslations} [i18n=ACCORDION_TRANSLATIONS] - Accordion translations
* @property {AccordionTranslations} [i18n=Accordion.defaults.i18n] - Accordion translations
* @property {boolean} [rememberExpanded] - Whether the expanded and collapsed
* state of each section is remembered and restored when navigating.
*/

/**
* Accordion translations
*
* @see {@link ACCORDION_TRANSLATIONS}
* @see {@link Accordion.defaults.i18n}
* @typedef {object} AccordionTranslations
*
* Messages used by the component for the labels of its buttons. This includes
Expand Down
19 changes: 13 additions & 6 deletions packages/govuk-frontend/src/govuk/components/button/button.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ export class Button {
/** @private */
this.debounceFormSubmitTimer = null

/** @type {ButtonConfig} */
const defaultConfig = {
preventDoubleClick: false
}

/**
* @private
* @type {ButtonConfig}
*/
this.config = mergeConfigs(
defaultConfig,
Button.defaults,
config || {},
normaliseDataset($module.dataset)
)
Expand Down Expand Up @@ -106,6 +101,18 @@ export class Button {
this.debounceFormSubmitTimer = null
}, DEBOUNCE_TIMEOUT_IN_SECONDS * 1000)
}

/**
* Button default config
*
* @see {@link ButtonConfig}
* @constant
* @default
* @type {ButtonConfig}
*/
static defaults = Object.freeze({
preventDoubleClick: false
})
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,6 @@ import { extractConfigByNamespace, mergeConfigs } from '../../common/index.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { I18n } from '../../i18n.mjs'

/**
* Character count translation defaults
*
* @see {@link CharacterCountConfig.i18n}
* @constant
* @default
* @type {CharacterCountTranslations}
*/
const CHARACTER_COUNT_TRANSLATIONS = {
// Characters
charactersUnderLimit: {
one: 'You have %{count} character remaining',
other: 'You have %{count} characters remaining'
},
charactersAtLimit: 'You have 0 characters remaining',
charactersOverLimit: {
one: 'You have %{count} character too many',
other: 'You have %{count} characters too many'
},
// Words
wordsUnderLimit: {
one: 'You have %{count} word remaining',
other: 'You have %{count} words remaining'
},
wordsAtLimit: 'You have 0 words remaining',
wordsOverLimit: {
one: 'You have %{count} word too many',
other: 'You have %{count} words too many'
},
textareaDescription: {
other: ''
}
}

/**
* Character count component
*
Expand Down Expand Up @@ -67,12 +33,6 @@ export class CharacterCount {
return this
}

/** @type {CharacterCountConfig} */
const defaultConfig = {
threshold: 0,
i18n: CHARACTER_COUNT_TRANSLATIONS
}

// Read config set using dataset ('data-' values)
const datasetConfig = normaliseDataset($module.dataset)

Expand All @@ -96,7 +56,7 @@ export class CharacterCount {
* @type {CharacterCountConfig}
*/
this.config = mergeConfigs(
defaultConfig,
CharacterCount.defaults,
config || {},
configOverrides,
datasetConfig
Expand Down Expand Up @@ -423,42 +383,82 @@ export class CharacterCount {

return (thresholdValue <= currentLength)
}

/**
* Character count default config
*
* @see {@link CharacterCountConfig}
* @constant
* @default
* @type {CharacterCountConfig}
*/
static defaults = Object.freeze({
threshold: 0,
i18n: {
// Characters
charactersUnderLimit: {
one: 'You have %{count} character remaining',
other: 'You have %{count} characters remaining'
},
charactersAtLimit: 'You have 0 characters remaining',
charactersOverLimit: {
one: 'You have %{count} character too many',
other: 'You have %{count} characters too many'
},
// Words
wordsUnderLimit: {
one: 'You have %{count} word remaining',
other: 'You have %{count} words remaining'
},
wordsAtLimit: 'You have 0 words remaining',
wordsOverLimit: {
one: 'You have %{count} word too many',
other: 'You have %{count} words too many'
},
textareaDescription: {
other: ''
}
}
})
}

/**
* Character count config
*
* @see {@link CharacterCount.defaults}
* @typedef {CharacterCountConfigWithMaxLength | CharacterCountConfigWithMaxWords} CharacterCountConfig
*/

/**
* Character count config (with maximum number of characters)
*
* @see {@link CharacterCount.defaults}
* @typedef {object} CharacterCountConfigWithMaxLength
* @property {number} [maxlength] - The maximum number of characters.
* If maxwords is provided, the maxlength option will be ignored.
* @property {number} [threshold=0] - The percentage value of the limit at
* which point the count message is displayed. If this attribute is set, the
* count message will be hidden by default.
* @property {CharacterCountTranslations} [i18n=CHARACTER_COUNT_TRANSLATIONS] - Character count translations
* @property {CharacterCountTranslations} [i18n=CharacterCount.defaults.i18n] - Character count translations
*/

/**
* Character count config (with maximum number of words)
*
* @see {@link CharacterCount.defaults}
* @typedef {object} CharacterCountConfigWithMaxWords
* @property {number} [maxwords] - The maximum number of words. If maxwords is
* provided, the maxlength option will be ignored.
* @property {number} [threshold=0] - The percentage value of the limit at
* which point the count message is displayed. If this attribute is set, the
* count message will be hidden by default.
* @property {CharacterCountTranslations} [i18n=CHARACTER_COUNT_TRANSLATIONS] - Character count translations
* @property {CharacterCountTranslations} [i18n=CharacterCount.defaults.i18n] - Character count translations
*/

/**
* Character count translations
*
* @see {@link CHARACTER_COUNT_TRANSLATIONS}
* @see {@link CharacterCount.defaults.i18n}
* @typedef {object} CharacterCountTranslations
*
* Messages shown to users as they type. It provides feedback on how many words
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ export class ErrorSummary {
/** @private */
this.$module = $module

/** @type {ErrorSummaryConfig} */
const defaultConfig = {
disableAutoFocus: false
}

/**
* @private
* @type {ErrorSummaryConfig}
*/
this.config = mergeConfigs(
defaultConfig,
ErrorSummary.defaults,
config || {},
normaliseDataset($module.dataset)
)
Expand Down Expand Up @@ -212,6 +207,18 @@ export class ErrorSummary {
return document.querySelector(`label[for='${$input.getAttribute('id')}']`) ||
$input.closest('label')
}

/**
* Error summary default config
*
* @see {@link ErrorSummaryConfig}
* @constant
* @default
* @type {ErrorSummaryConfig}
*/
static defaults = Object.freeze({
disableAutoFocus: false
})
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ export class NotificationBanner {
/** @private */
this.$module = $module

/** @type {NotificationBannerConfig} */
const defaultConfig = {
disableAutoFocus: false
}

/**
* @private
* @type {NotificationBannerConfig}
*/
this.config = mergeConfigs(
defaultConfig,
NotificationBanner.defaults,
config || {},
normaliseDataset($module.dataset)
)
Expand Down Expand Up @@ -79,6 +74,18 @@ export class NotificationBanner {

this.$module.focus()
}

/**
* Notification banner default config
*
* @see {@link NotificationBannerConfig}
* @constant
* @default
* @type {NotificationBannerConfig}
*/
static defaults = Object.freeze({
disableAutoFocus: false
})
}

/**
Expand Down
Loading

0 comments on commit 2547f89

Please sign in to comment.