Skip to content

Creating raw snippets that accept parameters #13923

@mihaipoenaru

Description

@mihaipoenaru

Describe the problem

The createRawSnippet function could reduce a lot of duplicated code in my company's project if it could create snippets that accept parameters.

Example scenario, let's say we have a table with some columns, including sex, employed, isContractor, etc. The point being, lots of boolean columns. Obviously I don't want to display the table component having true and false but rather yes and no

Right now I kind of have to do it like this (because the table component accepts snippets if you want to do special column rendering and transformations)

<script>
let data = ......

const renderers = {
     active: employedYN,
     contractor: contractorYN,
     otherCol: null
}

</script>
{#snippet employedYN(row: EmployeeItem)}
	<p class="text-blue">{row.active ? 'Yes' : 'No'}</p>
{/snippet}

{#snippet contractorYN(row: EmployeeItem)}
	<p class="text-blue">{row.contractor ? 'Yes' : 'No'}</p>
{/snippet}

<MyTable {data} {renderers}/>

The true/false issue is not the only one that repeats, this is just the one that is easiest to make an example with

Describe the proposed solution

I would love it if I could create a raw snippet that accepts a parameter, so my code would be like this:

<script>
let data = ......

function createYNSnippetForKey(key: keyof EmployeeItem): Snippet<[EmployeeItem]> {
		return createRawSnippet(() => {
			return {
				render: (row) => `{${row[key]} ? 'Yes' : 'No'}			`
			};
		});
	}

const renderers = {
     active: createYNSnippetForKey('active'),
     contractor: createYNSnippetForKey('contractor'),
     otherCol: null
}

</script>


<MyTable {data} {renderers}/>

### Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions