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 would I pass an object to a component inside a story? #73

Open
nathangross opened this issue Mar 23, 2023 · 4 comments
Open

How would I pass an object to a component inside a story? #73

nathangross opened this issue Mar 23, 2023 · 4 comments

Comments

@nathangross
Copy link

For instance, I have a typical blade component called ticket.blade.php. Inside that component I render things like ticket->title and $ticket->price. In typical Laravel fashion, I render the component by passing a whole ticket object (eloquent model) to the component like <x-card.ticket :ticket="$ticket" />

How would I do similar inside a story file? How can I pass a single $ticket object to the component inside a story?

In the button example, there are single variables like $label, etc. but I can't figure out how to create a story with an object rather than just individual vars.

Hopefully that question makes sense :) Thanks!

@nathangross
Copy link
Author

Here is how I currently have it set up and things seem to be working ok.

    'args' => [
        'type' => 'Single',
        'title' => 'Season Tickets',
        'description' => 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quod.',
        'flag' => 'Our Most Popular',
        'price' => '$14.99',
        'imgSrc' => 'https://source.unsplash.com/XHTBZpRBoi0',
    ],
])

@php
    $ticket = compact('type', 'title', 'flag', 'price', 'imgSrc', 'description');
    $ticket = (object) $ticket;
@endphp

<x-card.ticket :ticket="$ticket" />

I'm curious if there is a better, supported way of handling this type of component

@mrtimbrook
Copy link
Collaborator

Hey! Sorry for the delay. The args data is all converted to json by blast so your approach is probably the best way to pass an object to the component at this time. I can look into a better way to handle it in the future.

@nathangross
Copy link
Author

No worries—I appreciate the response!

Passing individual vars is great for smaller, atomic components. But being able to create and pass an object would be great.

Just thinking out loud here but maybe something like:

@storybook([
    'args' => [
        'ticket' => [
            'gameIsHome' => true,
            'date' => 'date',
            'title' => 'Title',
            'sponsor' => 'Sponsor',
            'imgSrc' => 'https://source.unsplash.com/XHTBZpRBoi0',
        ],
        'someOtherVars' => 'someOtherVars',
    ],
    'argTypes' => [
        'ticket' => [
            'isObject' => true,
        ],
    ],
])

@mikebronner
Copy link

Here is how I currently have it set up and things seem to be working ok.

    'args' => [
        'type' => 'Single',
        'title' => 'Season Tickets',
        'description' => 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quod.',
        'flag' => 'Our Most Popular',
        'price' => '$14.99',
        'imgSrc' => 'https://source.unsplash.com/XHTBZpRBoi0',
    ],
])

@php
    $ticket = compact('type', 'title', 'flag', 'price', 'imgSrc', 'description');
    $ticket = (object) $ticket;
@endphp

<x-card.ticket :ticket="$ticket" />

I'm curious if there is a better, supported way of handling this type of component

When I try this, I get undefined variable when passing in the variable to the component, as the logic in the component depends on this being specified, and is a required attribute, not nullable. How can I work around that?

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

No branches or pull requests

3 participants