wingsuit-designsystem / wingsuit

Twig for Storybook
GNU General Public License v2.0
90 stars 16 forks source link

Allow named key Lists for patterns #214

Open woldtwerk opened 1 year ago

woldtwerk commented 1 year ago

Hi Christian, currently it is not possible to use pattern lists with named keys.

e.g. for putting patterns inside the page.header, page.footer variables.

page:
  use: "@namespace/layout/page.html.twig"
  namespace: "Layout"
  label: Page
  description: Page
  fields:
    page:
      type: pattern
      label: Page
      description: The Page
      preview:
        header:
          id: 'header'
        footer:
          id: 'footer'

I didn't see any problem for allowing this.

Relevant code is: https://github.com/wingsuit-designsystem/wingsuit/blob/68dfedbfc4fe8ed4288fcbf5fdccbe4d5b2d4942/packages/pattern/src/twigRenderEngine.ts#L95 delta is cast to a number here. this would have to allow strings also

https://github.com/wingsuit-designsystem/wingsuit/blob/68dfedbfc4fe8ed4288fcbf5fdccbe4d5b2d4942/packages/pattern/src/twigRenderEngine.ts#L98
previewRenderedVariables is initialised with an empty array. This would need to be an object if delta is a string.

Add object check in

https://github.com/wingsuit-designsystem/wingsuit/blob/68dfedbfc4fe8ed4288fcbf5fdccbe4d5b2d4942/packages/pattern/src/PatternVariant.ts#L151

else if (field.getType() === 'pattern' && typeof preview === 'object') {
        Object.keys(preview).forEach(i => {
          previewPatterns["".concat(key, "--").concat(i)] = _this2.buildPreviewPattern(preview[i]);
        })

This would allow to print {{ page.header }} in templates, but not {{ page }}. Is that good enough?

If that seems alright to you I'd like to create a pull request.

christianwiedemann commented 1 year ago

Hi, sorry for late answer. Somehow missed your ticket. Technical is it not a problem. But you can't use it in Drupal this. So if you use inside your pattern it would not work in drupal.