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

How to define a function in mdx? #10214

Closed
dragooncjw opened this issue Mar 26, 2020 · 9 comments
Closed

How to define a function in mdx? #10214

dragooncjw opened this issue Mar 26, 2020 · 9 comments

Comments

@dragooncjw
Copy link

First, I want to ask if it has a export function to delete this
image
I'm using mdx to avoid this. However, I have no idea to use functions.
For example, I have code here:

<Story>
{{
  template: '<button @click="clickHandler">button</button>'
}}
</Story>

where should I define clickHandler?
I'v tried to import js file but in template it can not read import js files.

@dragooncjw
Copy link
Author

By the way, if I use like below,

<Story>
  <Button onClick="clickHandler"></Button>
</Story>

It will show error 'Cannot add property _Ctor, object is not extensible'.

@shilman
Copy link
Member

shilman commented Mar 27, 2020

Which framework is that @click example?

@dragooncjw
Copy link
Author

The framework I used is Vue. I've tried JSX by using onClick. It didn't work.
But I have found a solution on this url by mix stories.js and stories.mdx: https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/recipes.md#mixed-csf--mdx-stories

However, I still want to know the reason cause the error 'error 'Cannot add property _Ctor, object is not extensible'.

@shilman
Copy link
Member

shilman commented Mar 27, 2020

In Vue, how about this:

<Story>
{{
  template: '<button @click="clickHandler">button</button>',
  methods: {
    clickHandler: e => {
      e.preventDefault();
      console.log('hello');
    },
  }
}}
</Story>

Or this:

export const clickHandler = e => {
  e.preventDefault();
  console.log('hello');
}

<Story>
{{
  template: '<button @click="clickHandler">button</button>',
  methods: { clickHandler }
}}
</Story>

@dragooncjw
Copy link
Author

dragooncjw commented Mar 27, 2020

Yes, it works! Thanks!!
But I found that it cannot use life cycle functions.
I'v write like this:

<Story>
{{
  template: '<button @click="clickHandler">button</button>',
  methods: { clickHandler },
  mounted () {
    console.log('mounted')
  }
}}
</Story>

But 'mounted' did not console.

@dragooncjw
Copy link
Author

I found that if I use parameters: { notes: markdownNotes }
in Story in Preview, the second time I visit Doc tag, It will disappear. Which is the same in other way. Is it a bug?

@stale
Copy link

stale bot commented Apr 17, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Apr 17, 2020
@stale
Copy link

stale bot commented May 17, 2020

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

@stale stale bot closed this as completed May 17, 2020
@shilman
Copy link
Member

shilman commented May 18, 2020

Late answer to your original question, but here are the docs for remixing docsPage:

https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/docspage.md#remixing-docspage-using-doc-blocks

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

2 participants