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

@storybook/vue@rc : addon-knobs creates and destroys story on knob update #4516

Closed
y-nk opened this issue Oct 23, 2018 · 11 comments
Closed

@storybook/vue@rc : addon-knobs creates and destroys story on knob update #4516

y-nk opened this issue Oct 23, 2018 · 11 comments

Comments

@y-nk
Copy link
Contributor

y-nk commented Oct 23, 2018

Step to reproduce :

  1. Following babel-preset-vue missing as peer dependency #4505 installation steps
  2. add npm i --save-dev @storybook/addon-knobs@rc
  3. add import '@storybook/addon-knobs/register'; to .storybook/addons.js
  4. add a basic story :
import { storiesOf } from '@storybook/vue';
import { withKnobs, text, button } from '@storybook/addon-knobs';

storiesOf('Knobs demo', module)
  .addDecorator(withKnobs)
  .add('foobar', () => ({
    template: '<span @click="i++">My name is {{ name }} and i am {{ i }} years old.</span>',

    data() { return {
      name: text('name', 'John Doe'),
      i: 40,
    }; },

    beforeCreate() {
      button('age me', () => this.i++)
    },

    created() { console.log('created') },
    destroyed() { console.log('destroyed') },
  }));

Expected result :

I can see the "created" log when i launch the story only

Actual result :

I can see the "created" and "destroyed" log everytime i modify the text value from the knob.

Additional comments :

/~https://github.com/y-nk/storybook-fresh-install

@igor-dv
Copy link
Member

igor-dv commented Oct 23, 2018

#4098 is also related

@y-nk
Copy link
Contributor Author

y-nk commented Oct 23, 2018

@igor-dv if that could help, i provided a very dirty patch in the firsts posts of #3777 .

@igor-dv
Copy link
Member

igor-dv commented Oct 23, 2018

In a v4 we do not have a separation in knobs to vue/angular/react/whatever. So we need a more generic solution.

@y-nk y-nk closed this as completed Oct 24, 2018
@y-nk y-nk reopened this Oct 24, 2018
@y-nk
Copy link
Contributor Author

y-nk commented Nov 10, 2018

@igor-dv i'd be happy to help if i could get any pointers to a direction, but i feel it's a deep structural change that should probably come from the core team, right ?

@igor-dv
Copy link
Member

igor-dv commented Nov 11, 2018

I think so =/ There is some discussion about changing knobs anyway.

@y-nk
Copy link
Contributor Author

y-nk commented Nov 12, 2018

@igor-dv would you have any idea about a time to get a chance to see an alpha ? as for now, vue-play isn't supporting knobs and unmaintained for years, there's no options for Vue users, and i suspect it'll be the same for other frameworks.

@igor-dv
Copy link
Member

igor-dv commented Nov 12, 2018

Can you please explain the problem behind the issue ? I understand that the component is recreated each time you change the knob, but sounds like it is still working. Regarding the alpha, there is no alpha yet, but discussion in Discord

@y-nk
Copy link
Contributor Author

y-nk commented Nov 12, 2018

@igor-dv if the component is created and destroyed everytime, then all the variables initialized in the internal state of the component are going back to default and so is the same with all the children of this component.

@y-nk
Copy link
Contributor Author

y-nk commented Nov 12, 2018

@igor-dv actually, my code and gif can a bit misleading.

For the code, I'm using the knob in the data() node, but i should be using a property instead. it looked like working properly because the value is set by storybook after the knob changes and because of Vue accessors, it actually doesn't change so much ; but what you don't see, is that data.i is actually reset to 40 every time (because of this bug), and then then button knob seems useless.

Consider more console.log from the same story :

import { storiesOf } from '@storybook/vue';
import { withKnobs, text, button } from '@storybook/addon-knobs';

storiesOf('Knobs demo', module)
  .addDecorator(withKnobs)
  .add('foobar', () => ({
    template: '<span @click="i++">My name is {{ name }} and i am {{ i }} years old.</span>',

+    props: {
+      name: {
+        type: String,
+        default: text('name', 'John Doe'),
+      },
+    },
+
    data() { return {
-      name: text('name', 'John Doe'),
      i: 40,
    }; },

+    watch: {
+      i(val) {
+        console.log('i has changed to', val)
+      },
+    },
+
    beforeCreate() {
-      button('age me', () => this.i++)
+      button('age me', () => {
+        console.log('button knob has been triggered.')
+        this.i++
+      })
    },

    created() { console.log('created') },
    destroyed() { console.log('destroyed') },
  }));

and you will have :

button fail

In this gif you can see (in order) :

  1. Trying to set up a property still resets the story component
  2. clicking the button knob resets the story componets (and so we don't see i increment)
  3. but the story itself works since i can click on the span and increment i properly
  4. but then, changing a prop kills/respawn the story component, resulting in i going back to default

@shilman
Copy link
Member

shilman commented Nov 21, 2018

Released in 4.0.8. Can we close?

@igor-dv
Copy link
Member

igor-dv commented Nov 21, 2018

Yep 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants