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

Relax requirements to receive configuration with createAll #5374

Merged

Conversation

romaricpascal
Copy link
Member

@romaricpascal romaricpascal commented Oct 3, 2024

Remove the need for components to have a defaults static property, as it's an internal convention which is a lot to ask from outside components.

Thoughts

Looks like that check was there to help TypeScript follow when the code was inside initAll. With the introduction of CompatibleClass type and its {new (...args: any[]): any} constructor for the implementation of createAll, we no longer need it as the CompatibleClass loosened the requirements for the components' constructors.

An alternative could have been to check the constructor's length with the length property of Function before passing a config. This would put a hard constraints on constructors not to use rest parameters (constructor(...args) { }), as those are not counted.

If a call to createAll receives a config and a component that does not expect a second argument, no harm done. The only issue would be if a component would expect something else than a config as a second argument but we'll be documenting the shape of constructors in our documentation (not that it prevents people from doing unexpected things, but I think we should have a certain level of trust with people using our API before getting too defensive. If we see too many issues, we can look at educating more or tightening requirements in the future).

Closes #5373

Copy link

github-actions bot commented Oct 3, 2024

📋 Stats

File sizes

File Size
dist/govuk-frontend-development.min.css 118.62 KiB
dist/govuk-frontend-development.min.js 44.38 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 93.72 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 87.97 KiB
packages/govuk-frontend/dist/govuk/all.mjs 1.18 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 1.15 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.css 118.6 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 44.37 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.55 KiB
packages/govuk-frontend/dist/govuk/init.mjs 6.88 KiB

Modules

File Size (bundled) Size (minified)
all.mjs 83.97 KiB 41.69 KiB
accordion.mjs 24.92 KiB 12.85 KiB
button.mjs 7.42 KiB 3.23 KiB
character-count.mjs 23.83 KiB 10.41 KiB
checkboxes.mjs 7.27 KiB 3.34 KiB
error-summary.mjs 9.33 KiB 3.99 KiB
exit-this-page.mjs 18.54 KiB 9.77 KiB
header.mjs 5.9 KiB 3.11 KiB
notification-banner.mjs 7.71 KiB 3.14 KiB
password-input.mjs 16.57 KiB 7.75 KiB
radios.mjs 6.27 KiB 2.89 KiB
service-navigation.mjs 5.88 KiB 3.15 KiB
skip-link.mjs 5.82 KiB 2.7 KiB
tabs.mjs 11.47 KiB 6.56 KiB

View stats and visualisations on the review app


Action run for b7c90a2

Copy link

github-actions bot commented Oct 3, 2024

JavaScript changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
index 4cb3bbdca..adc7bd010 100644
--- a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
+++ b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
@@ -1175,7 +1175,7 @@ function createAll(Component, t, e) {
     const s = i.querySelectorAll(`[data-module="${Component.moduleName}"]`);
     return isSupported() ? Array.from(s).map((e => {
         try {
-            return "defaults" in Component && void 0 !== t ? new Component(e, t) : new Component(e)
+            return void 0 !== t ? new Component(e, t) : new Component(e)
         } catch (i) {
             return n && i instanceof Error ? n(i, {
                 element: e,

Action run for b7c90a2

Copy link

github-actions bot commented Oct 3, 2024

Other changes to npm package

diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.js b/packages/govuk-frontend/dist/govuk/all.bundle.js
index 92f1e5508..bfe80911f 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.js
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.js
@@ -2557,7 +2557,7 @@
     }
     return Array.from($elements).map($element => {
       try {
-        return 'defaults' in Component && typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
+        return typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
       } catch (error) {
         if (onError && error instanceof Error) {
           onError(error, {
diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.mjs b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
index 4be703384..037a2f90d 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.mjs
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
@@ -2551,7 +2551,7 @@ function createAll(Component, config, createAllOptions) {
   }
   return Array.from($elements).map($element => {
     try {
-      return 'defaults' in Component && typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
+      return typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
     } catch (error) {
       if (onError && error instanceof Error) {
         onError(error, {
diff --git a/packages/govuk-frontend/dist/govuk/init.mjs b/packages/govuk-frontend/dist/govuk/init.mjs
index e77789089..05b0bbfb5 100644
--- a/packages/govuk-frontend/dist/govuk/init.mjs
+++ b/packages/govuk-frontend/dist/govuk/init.mjs
@@ -89,7 +89,7 @@ function createAll(Component, config, createAllOptions) {
   }
   return Array.from($elements).map($element => {
     try {
-      return 'defaults' in Component && typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
+      return typeof config !== 'undefined' ? new Component($element, config) : new Component($element);
     } catch (error) {
       if (onError && error instanceof Error) {
         onError(error, {

Action run for b7c90a2

Remove the need for components to have a `defaults` static property,
as it's really an internal convention which is a lot to ask from outside components.
@romaricpascal romaricpascal force-pushed the simplify-createall-config-requirements branch from c48e17d to b7c90a2 Compare October 3, 2024 17:01
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-5374 October 3, 2024 17:01 Inactive
Copy link
Contributor

@patrickpatrickpatrick patrickpatrickpatrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me!

@romaricpascal romaricpascal merged commit fe9eb2f into public-js-api Oct 4, 2024
48 checks passed
@romaricpascal romaricpascal deleted the simplify-createall-config-requirements branch October 4, 2024 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate simplifying createAll requirements for components receiving configuration
3 participants