-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add createAll
function to initialise individual components
#4975
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
334702e
Split initAll into its own file
36degrees e3c9133
Split component instantiation into separate function
36degrees 4f86edd
Add tests for createAll function
36degrees 08edc0f
Return component objects from `createAll`
romaricpascal aa1aaa6
Refactor `createAll` tests
romaricpascal 7432e7f
Export `createAll` function in `all.mjs`
romaricpascal f4667fd
Add CHANGELOG entry
romaricpascal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,124 +1,19 @@ | ||
/* eslint-disable no-new */ | ||
|
||
import { version } from './common/govuk-frontend-version.mjs' | ||
import { isSupported } from './common/index.mjs' | ||
import { Accordion } from './components/accordion/accordion.mjs' | ||
import { Button } from './components/button/button.mjs' | ||
import { CharacterCount } from './components/character-count/character-count.mjs' | ||
import { Checkboxes } from './components/checkboxes/checkboxes.mjs' | ||
import { ErrorSummary } from './components/error-summary/error-summary.mjs' | ||
import { ExitThisPage } from './components/exit-this-page/exit-this-page.mjs' | ||
import { Header } from './components/header/header.mjs' | ||
import { NotificationBanner } from './components/notification-banner/notification-banner.mjs' | ||
import { PasswordInput } from './components/password-input/password-input.mjs' | ||
import { Radios } from './components/radios/radios.mjs' | ||
import { SkipLink } from './components/skip-link/skip-link.mjs' | ||
import { Tabs } from './components/tabs/tabs.mjs' | ||
import { SupportError } from './errors/index.mjs' | ||
|
||
/** | ||
* Initialise all components | ||
* | ||
* Use the `data-module` attributes to find, instantiate and init all of the | ||
* components provided as part of GOV.UK Frontend. | ||
* | ||
* @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope) | ||
*/ | ||
function initAll(config) { | ||
config = typeof config !== 'undefined' ? config : {} | ||
|
||
// Skip initialisation when GOV.UK Frontend is not supported | ||
if (!isSupported()) { | ||
console.log(new SupportError()) | ||
return | ||
} | ||
|
||
const components = /** @type {const} */ ([ | ||
[Accordion, config.accordion], | ||
[Button, config.button], | ||
[CharacterCount, config.characterCount], | ||
[Checkboxes], | ||
[ErrorSummary, config.errorSummary], | ||
[ExitThisPage, config.exitThisPage], | ||
[Header], | ||
[NotificationBanner, config.notificationBanner], | ||
[PasswordInput, config.passwordInput], | ||
[Radios], | ||
[SkipLink], | ||
[Tabs] | ||
]) | ||
|
||
// Allow the user to initialise GOV.UK Frontend in only certain sections of the page | ||
// Defaults to the entire document if nothing is set. | ||
const $scope = config.scope ?? document | ||
|
||
components.forEach(([Component, config]) => { | ||
const $elements = $scope.querySelectorAll( | ||
`[data-module="${Component.moduleName}"]` | ||
) | ||
|
||
$elements.forEach(($element) => { | ||
try { | ||
// Only pass config to components that accept it | ||
'defaults' in Component | ||
? new Component($element, config) | ||
: new Component($element) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export { | ||
initAll, | ||
version, | ||
|
||
// Components | ||
Accordion, | ||
Button, | ||
CharacterCount, | ||
Checkboxes, | ||
ErrorSummary, | ||
ExitThisPage, | ||
Header, | ||
NotificationBanner, | ||
PasswordInput, | ||
Radios, | ||
SkipLink, | ||
Tabs | ||
} | ||
|
||
/** | ||
* Config for all components via `initAll()` | ||
* | ||
* @typedef {object} Config | ||
* @property {AccordionConfig} [accordion] - Accordion config | ||
* @property {ButtonConfig} [button] - Button config | ||
* @property {CharacterCountConfig} [characterCount] - Character Count config | ||
* @property {ErrorSummaryConfig} [errorSummary] - Error Summary config | ||
* @property {ExitThisPageConfig} [exitThisPage] - Exit This Page config | ||
* @property {NotificationBannerConfig} [notificationBanner] - Notification Banner config | ||
* @property {PasswordInputConfig} [passwordInput] - Password input config | ||
*/ | ||
|
||
/** | ||
* Config for individual components | ||
* | ||
* @typedef {import('./components/accordion/accordion.mjs').AccordionConfig} AccordionConfig | ||
* @typedef {import('./components/accordion/accordion.mjs').AccordionTranslations} AccordionTranslations | ||
* @typedef {import('./components/button/button.mjs').ButtonConfig} ButtonConfig | ||
* @typedef {import('./components/character-count/character-count.mjs').CharacterCountConfig} CharacterCountConfig | ||
* @typedef {import('./components/character-count/character-count.mjs').CharacterCountTranslations} CharacterCountTranslations | ||
* @typedef {import('./components/error-summary/error-summary.mjs').ErrorSummaryConfig} ErrorSummaryConfig | ||
* @typedef {import('./components/exit-this-page/exit-this-page.mjs').ExitThisPageConfig} ExitThisPageConfig | ||
* @typedef {import('./components/exit-this-page/exit-this-page.mjs').ExitThisPageTranslations} ExitThisPageTranslations | ||
* @typedef {import('./components/notification-banner/notification-banner.mjs').NotificationBannerConfig} NotificationBannerConfig | ||
* @typedef {import('./components/password-input/password-input.mjs').PasswordInputConfig} PasswordInputConfig | ||
*/ | ||
export { version } from './common/govuk-frontend-version.mjs' | ||
export { Accordion } from './components/accordion/accordion.mjs' | ||
export { Button } from './components/button/button.mjs' | ||
export { CharacterCount } from './components/character-count/character-count.mjs' | ||
export { Checkboxes } from './components/checkboxes/checkboxes.mjs' | ||
export { ErrorSummary } from './components/error-summary/error-summary.mjs' | ||
export { ExitThisPage } from './components/exit-this-page/exit-this-page.mjs' | ||
export { Header } from './components/header/header.mjs' | ||
export { NotificationBanner } from './components/notification-banner/notification-banner.mjs' | ||
export { PasswordInput } from './components/password-input/password-input.mjs' | ||
export { Radios } from './components/radios/radios.mjs' | ||
export { SkipLink } from './components/skip-link/skip-link.mjs' | ||
export { Tabs } from './components/tabs/tabs.mjs' | ||
export { initAll, createAll } from './init.mjs' | ||
|
||
/** | ||
* Component config keys, e.g. `accordion` and `characterCount` | ||
* | ||
* @typedef {keyof Config} ConfigKey | ||
* @typedef {import('./init.mjs').Config} Config | ||
* @typedef {import('./init.mjs').ConfigKey} ConfigKey | ||
*/ |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note With the removal of
initAll
fromall.mjs
, all the file does is re-exporting the difference pieces of our library that constitute our public API, so we can switch to theexport { NAMED_IMPORT } from 'module'
syntax rather than have a bunch ofimport
s followed by anexport
.