discoveryjs / discovery

A framework for rapid data (JSON) analysis, shareable serverless reports and dashboards
https://discoveryjs.github.io/discovery/
MIT License
331 stars 6 forks source link

Could you please explain what I do wrong with built in tabs view? #89

Closed FeltBoots closed 2 years ago

FeltBoots commented 2 years ago

I'm getting an error with this code snippet I took from tab.usage.js https://github.com/discoveryjs/discovery/blob/02798acce8e43f87a154bd23355a5d4222d440a3/src/views/tab.usage.js

  const appTabs = utils.createElement('div', 'app-tabs-container')
  host.view.render(appTabs, {
    view: 'tabs',
    name: 'tabs',
    tabs: [
      {
        value: 'one',
        text: 'One'
      },
      {
        value: 'two',
        text: 'Two'
      },
      {
        value: 'three',
        text: 'Three'
      }
    ],
    content: {
      view: 'switch',
      content: [
        {
          when: '#.tabs="one"',
          content: 'text:"One"'
        },
        {
          when: '#.tabs="two"',
          content: 'text:"Two"'
        },
        {
          when: '#.tabs="three"',
          content: 'text:"Three"'
        }
      ]
    },
    beforeTabs: 'text:"<Content before tabs>"',
    afterTabs: 'text:"<Content after tabs>"'
  })

I am getting an error: TypeError: Cannot use 'in' operator to search for 'tabs' in undefined. I have no idea how to work with it. Trying follow examples from surrounding repos and docs in readme but it does not work for me.

FeltBoots commented 2 years ago

I used debugger and found the reason. I didn't pass the value to tabs view. It works now.

If you don't pass any value and context is undefined then there is an error with 'in' operator. There might be another check if context is not undefined.

        let initValue =
            'value' in config
                ? config.value
                : name in context
                    ? context[name]
                    : undefined;

I added check to undefined for context value in tabs.js view and it seems to work as expected. If you are agree I send PR with fix.

lahmatiy commented 2 years ago

host.view.render() expected to have 4 arguments: outputElement, config, data and context. In your case you omitted last two arguments and therefore got an error. When a component works with context it expected that context is non-nullish. I'm not sure a fix for tabs is needed. Also, you should avoid using host.view.render() directly since it is intended for use in a component definition only.

FeltBoots commented 2 years ago

Thanks! I realised your point about host.view.render(). Now it works as expected.