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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃檹 Request: Allow pushing components into array and rendering them in a {#each} block #2810

Closed
rodryquintero opened this issue May 18, 2019 · 7 comments

Comments

@rodryquintero
Copy link

Here is what I would like to do in Svelte.

const components = [
<Edit />,
<Delete />
<Info />
]

{#each components as comp}
<Comp />
{/each}

A use case would be passing a list of button components to a data grid component, like this.

<DataGrid actionButtons={components} />
@mrkishi
Copy link
Member

mrkishi commented May 18, 2019

You can use <svelte:component> for this:

<script>
    export let actions = [];
</script>

{#each actions as component}
    <svelte:component this="{component}">
{/each}
<script>
    import { Edit, Delete, Info } from './components.js';

    const actions = [Edit, Delete, Info];
</script>

<DataGrid {actions}/>

@mrkishi mrkishi closed this as completed May 18, 2019
@rodryquintero
Copy link
Author

Thanks for the quick reply. I am puzzled by this line.

import { Edit, Delete, Info } from './components.js';

I thought that you can only define one Svelte component per file. How do you go about defining several components on a single file?

@mrkishi
Copy link
Member

mrkishi commented May 18, 2019

That was just an example. Normally you'd have one component per .svelte file.

You could have components.js like this, though:

export { default as Edit } from `./Edit.svelte`;
export { default as Delete } from `./Delete.svelte`;
export { default as Info } from `./Info.svelte`;

@rodryquintero
Copy link
Author

Understood. Thanks for all the help.

@rodryquintero
Copy link
Author

Sorry to bother again. Given this solution, how do you go about passing props to these components when building the array of components? In React, it would be done like this:

const components = [
<Edit onClick={handleEdit} />,
<Delete onClick={handleDelete} />,
<Info onClick={handleInfo} />
]

Thanks in advance.

@tivac
Copy link
Contributor

tivac commented May 18, 2019

<script>
import Edit from `./Edit.svelte`;
import Delete from `./Delete.svelte`;
import Info from `./Info.svelte`;

const components = [
    [ Edit, handleEdit ],
    [ Delete, handleDelete ],
    [ Info, handleInfo ],
];
</script>

{#each components as [ component, onclick ]}
    <svelte:component this="{component}" {onclick} />
{/each}

Seems like it'd work though I haven't explicitly tried it. The {onclick} bit is using the shorthand version of onclick="{onclick}".

@rodryquintero
Copy link
Author

Thanks. I appreciate all the help. 馃憤

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