govuk-frontend icon indicating copy to clipboard operation
govuk-frontend copied to clipboard

Allow component JavaScript constructor to take a config object

Open vanitabarrett opened this issue 2 years ago • 1 comments

What

Allow components to take a config object through their constructor, before being initialised. The config object should default to an empty object, and therefore a component must be able to continue working even if the config object is empty (e.g: by setting some defaults to fall back on).

Why

To allow for options to be passed to our component JavaScript in future, e.g: translations

Who needs to work on this

Developers

Who needs to review this

Developers

Done when

  • [ ] It's possible to pass a config object to a component
  • [ ] If that config object is empty, the component JavaScript should still work
  • [ ] Tests to handle different scenarios and that the JS behaves as expected, e.g: config object not passed, config object is empty, component sets defaults

vanitabarrett avatar Jul 15 '22 08:07 vanitabarrett

Would we want to add a config object to every component upfront (so that the interface exists, even if it's not being used yet), or only to the components as-and-when they have a reason to need it?

querkmachine avatar Jul 19 '22 09:07 querkmachine

Does this also include being able to pass config via initAll, or should that be a separate story?

36degrees avatar Aug 24 '22 13:08 36degrees

@36degrees I think I imagined it all being part of this story... I don't think the work would be too big to do in one, but if it turned out that way we could split it up. I'll expand the "Done when" section a bit at least to make it clear it's intended to include both

vanitabarrett avatar Aug 24 '22 13:08 vanitabarrett

It's possible to pass a config object to a component via init() and initAll()

Based on this discussion, I think we've agreed that config will be passed to the constructor rather than the init function, which also matches the example in the proposal.

36degrees avatar Aug 26 '22 12:08 36degrees