E53F Spread props in Vue 3 components by magersoft · Pull Request #1419 · vue-styleguidist/vue-styleguidist · GitHub
[go: up one dir, main page]

Skip to content

Conversation

magersoft
Copy link

Hello. I have a problem in the Storybook + Vue 3 + Vite that my properties imported from another file are not showing up in the documentation.
I want to use all the features of the composition API and have property documentation.

I tried to implement such an opportunity in a wonderful vue-docgen-api
It may have turned out horribly, but this may push someone toward a better solution.

I note that parsing the properties of a component only works when importing from another file and using the spread operator.
Also, for the correct display of types, I use a wrapper from here propsFactory.ts

As a result, we get the opportunity to do something like
Properties

// src/composable/loading.ts
import { propsFactory } from '../utils';
import { PropType } from 'vue';

export const makeLoadingProps = propsFactory(
  {
    isLoading: {
      type: Boolean as PropType<boolean>,
      default: false,
      description: 'Loading state'
    }
  },
  'loading'
);

Component

// src/components/Button/Button.vue
import { makeLoadingProps } from '../../composable';

export default defineComponent({
  name: 'CdsButton',
  props: {
    ...makeLoadingProps(),
  }
})

And it now works in Storybook!
Once again, I note that this is not a complete solution, but rather a hack, maybe in the future I will finalize this or maybe someone will help make it better. Thank you!

@changeset-bot
Copy link
changeset-bot bot commented Nov 10, 2022

⚠️ No Changeset found

Latest commit: 1f99cda

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@what-the-diff
Copy link
what-the-diff bot commented Nov 10, 2022
  • Add @babel/traverse to dependencies
  • Fix getTypeFromAnnotation for TSUnionTypes with TSLiteralTypes and UnaryExpressions
  • Added new function getSpreadProperties, which parse spread properties from composable files (vue 3)

Copy link
Member
@elevatebart elevatebart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for an amazing idea. I really like it.
A couple of remarks on the implementation:

  • Always add some kind of test to make explicit the use case you are solving for.
  • You installed @babel/traverse while you use the local walkes, I am wondering why that is
  • There is a brilliant library maintained by @johnsoncodehk called vue-compeonent-meta. It's on par with Volar. It's very promising and could be a great typescript compliant alternative to docgen. It is still a WIP but it would, I think, solve your current issue.
  • You might be able to test this in your own storybook

@@ -0,0 +1,127 @@
import path from 'path'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the sweet sweet set of tests you ran this file on?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I plan to write tests for this later. Thank you!

@magersoft
Copy link
Author

@elevatebart thank you for your answer!
I don't have much time right now, so I use this solution. I'll try to make it better and cover it with tests later. Thanks for mentioning vue-compeonent-meta. I will try this later.

@stale
Copy link
stale bot commented Jun 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale more than 60 days without activity label Jun 18, 2023
@Pitigoi
Copy link
Pitigoi commented Jan 10, 2025

is it posible for this feature to be raken into consideration again?

@stale stale bot removed the stale more than 60 days without activity label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0