Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MoustaphaDev committed Jan 10, 2024
1 parent 0ca53b8 commit c5ebcf5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
33 changes: 17 additions & 16 deletions internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,25 +692,28 @@ func handleSlots(p *printer, n *Node, opts RenderOptions, depth int) {
var firstNestedSlotProp string
for c1 := c.FirstChild; c1 != nil; c1 = c1.NextSibling {
for _, a := range c1.Attr {
var slotProp = ""
if a.Key == "slot" {
if firstNestedSlotProp == "" {
if a.Type == QuotedAttribute {
firstNestedSlotProp = fmt.Sprintf(`"%s"`, escapeDoubleQuote(a.Val))
} else if a.Type == ExpressionAttribute {
firstNestedSlotProp = fmt.Sprintf(`[%s]`, a.Val)
hasAnyDynamicSlots = true
} else if a.Type == TemplateLiteralAttribute {
firstNestedSlotProp = fmt.Sprintf(`[%s%s%s]`, BACKTICK, a.Val, BACKTICK)
hasAnyDynamicSlots = true
} else {
panic(`unknown slot attribute type`)
}
if a.Type == QuotedAttribute {
slotProp = fmt.Sprintf(`"%s"`, escapeDoubleQuote(a.Val))
} else if a.Type == ExpressionAttribute {
slotProp = fmt.Sprintf(`[%s]`, a.Val)
hasAnyDynamicSlots = true
} else if a.Type == TemplateLiteralAttribute {
slotProp = fmt.Sprintf(`[%s%s%s]`, BACKTICK, a.Val, BACKTICK)
hasAnyDynamicSlots = true
} else {
panic(`unknown slot attribute type`)
}
}
if firstNestedSlotProp != "" {
nestedSlotsCount++

if firstNestedSlotProp == "" {
firstNestedSlotProp = slotProp
}
}
if firstNestedSlotProp != "" {
nestedSlotsCount++
}

}

Expand All @@ -721,7 +724,6 @@ func handleSlots(p *printer, n *Node, opts RenderOptions, depth int) {
child_loop:
for c1 := c.FirstChild; c1 != nil; c1 = c1.NextSibling {
foundNamedSlot := false
fmt.Println(foundNamedSlot)
for _, a := range c1.Attr {
if a.Key == "slot" {
var nestedSlotProp string
Expand Down Expand Up @@ -752,7 +754,6 @@ func handleSlots(p *printer, n *Node, opts RenderOptions, depth int) {
nestedSlotEntry := &NestedSlotEntry{`"@@NON_ELEMENT_ENTRY"`, []*Node{c1}}
nestedSlotEntries = append(nestedSlotEntries, nestedSlotEntry)
}

}
continue
}
Expand Down
15 changes: 15 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ func TestPrinter(t *testing.T) {
code: "${$$renderComponent($$result,'Component',Component,{},$$mergeSlots(({}),Math.random() > 0.5 ? ({\"a\": () => $$render`${$$maybeRenderHead($$result)}<div>A</div>`}) : ({\"b\": () => $$render`<div>B</div>`})))}",
},
},
{
name: "ternary slot with one implicit default",
source: `<Main>
{useSlot
? <div slot="outside">Inside slot with red background</div>
: <div>Outside slot without background</div>
}
</Main>`,
want: want{
code: `${$$renderComponent($$result,'Main',Main,{},$$mergeSlots(({}),useSlot
? ({"outside": () => $$render` + BACKTICK + `${$$maybeRenderHead($$result)}<div>Inside slot with red background</div>` + BACKTICK + `})
: ({"default": () => $$render` + BACKTICK + `<div>Outside slot without background</div>` + BACKTICK + `})
))}`,
},
},
{
name: "function expression slots",
source: "<Component>\n{() => { switch (value) {\ncase 'a': return <div slot=\"a\">A</div>\ncase 'b': return <div slot=\"b\">B</div>\ncase 'c': return <div slot=\"c\">C</div>\n}\n}}\n</Component>",
Expand Down

0 comments on commit c5ebcf5

Please sign in to comment.