Skip to content
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

Update createAll types so they don't rely on component's defaults #5496

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions packages/govuk-frontend/src/govuk/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ function initAll(config) {
*
* Any component errors will be caught and logged to the console.
*
* @template {CompatibleClass} T
* @param {T} Component - class of the component to create
* @param {T["defaults"]} [config] - Config supplied to component
* @param {OnErrorCallback<T> | Element | Document | CreateAllOptions<T> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
* @returns {Array<InstanceType<T>>} - array of instantiated components
* @template {CompatibleClass} ComponentClass
* @param {ComponentClass} Component - class of the component to create
* @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component
* @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
* @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components
*/
function createAll(Component, config, createAllOptions) {
let /** @type {Element | Document} */ $scope = document
Expand Down Expand Up @@ -162,7 +162,7 @@ export { initAll, createAll }
**/

/**
* @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass
* @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass
*/

/* eslint-enable jsdoc/valid-types */
Expand Down Expand Up @@ -202,23 +202,28 @@ export { initAll, createAll }
*/

/**
* @template {CompatibleClass} T
* @template {CompatibleClass} ComponentClass
* @typedef {ConstructorParameters<ComponentClass>[1]} ComponentConfig
*/

/**
* @template {CompatibleClass} ComponentClass
* @typedef {object} ErrorContext
* @property {Element} [element] - Element used for component module initialisation
* @property {T} [component] - Class of component
* @property {T["defaults"]} config - Config supplied to component
* @property {ComponentClass} [component] - Class of component
* @property {ComponentConfig<ComponentClass>} config - Config supplied to component
*/

/**
* @template {CompatibleClass} T
* @template {CompatibleClass} ComponentClass
* @callback OnErrorCallback
* @param {unknown} error - Thrown error
* @param {ErrorContext<T>} context - Object containing the element, component class and configuration
* @param {ErrorContext<ComponentClass>} context - Object containing the element, component class and configuration
*/

/**
* @template {CompatibleClass} T
* @template {CompatibleClass} ComponentClass
* @typedef {object} CreateAllOptions
* @property {Element | Document} [scope] - scope of the document to search within
* @property {OnErrorCallback<T>} [onError] - callback function if error throw by component on init
* @property {OnErrorCallback<ComponentClass>} [onError] - callback function if error throw by component on init
*/