Skip to content

Commit

Permalink
added: support for slot fallback riot/riot#3014
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Aug 23, 2024
1 parent ed9fc4f commit 957e1c5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/generators/template/bindings/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
import {
createBindingAttributes,
createSelectorProperties,
createTemplateProperty,
getName,
getNodeAttributes,
} from '../utils.js'
import { builders } from '../../../utils/build-types.js'
import { findAttribute } from '../find.js'
import { simplePropertyNode } from '../../../utils/custom-ast-nodes.js'
import { createNestedBindings } from '../builder.js'

/**
* Transform a RiotParser.Node.Tag of type slot into a slot binding
Expand Down Expand Up @@ -61,6 +63,14 @@ export default function createSlotBinding(
),
),
simplePropertyNode(BINDING_NAME_KEY, builders.literal(slotName)),
createTemplateProperty(
createNestedBindings(
sourceNode,
sourceFile,
sourceCode,
selectorAttribute,
),
),
...createSelectorProperties(selectorAttribute),
])
}
5 changes: 4 additions & 1 deletion src/generators/template/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ function createTagWithBindings(sourceNode, sourceFile, sourceCode) {
]
case isSlotNode(cloneNode):
// slot tag
return [tagOpeningHTML, [slotBinding(cloneNode, bindingsSelector)]]
return [
tagOpeningHTML,
[slotBinding(cloneNode, bindingsSelector, sourceFile, sourceCode)],
]
default:
// this node has expressions bound to it
return [
Expand Down
8 changes: 4 additions & 4 deletions src/generators/template/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ export function hasExpressions(node) {
}

/**
* True if the node is a directive having its own template
* True if the node is a directive having its own template or it's a slot node
* @param {RiotParser.Node} node - riot parser node
* @returns {boolean} true only for the IF EACH and TAG bindings
* @returns {boolean} true only for the IF EACH and TAG bindings or it's a slot node
*/
export function hasItsOwnTemplate(node) {
return [findEachAttribute, findIfAttribute, isCustomNode].some((test) =>
test(node),
return [findEachAttribute, findIfAttribute, isCustomNode, isSlotNode].some(
(test) => test(node),
)
}

Expand Down
1 change: 0 additions & 1 deletion src/generators/template/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const BINDING_SLOTS_KEY = 'slots'
export const BINDING_EXPRESSIONS_KEY = 'expressions'
export const BINDING_IS_BOOLEAN_ATTRIBUTE = 'isBoolean'
export const BINDING_CHILD_NODE_INDEX_KEY = 'childNodeIndex'
export const BINDING_SLOT_FALLBACK_KEY = 'fallback'
// slots keys
export const BINDING_BINDINGS_KEY = 'bindings'
export const BINDING_ID_KEY = 'id'
Expand Down
5 changes: 2 additions & 3 deletions test/generators/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
BINDING_IS_BOOLEAN_ATTRIBUTE,
BINDING_NAME_KEY,
BINDING_SELECTOR_KEY,
BINDING_SLOT_FALLBACK_KEY,
BINDING_TEMPLATE_KEY,
BINDING_TYPE_KEY,
NAME_ATTRIBUTE,
Expand Down Expand Up @@ -1295,7 +1294,7 @@ describe('Generators - Template', () => {
expect(output[NAME_ATTRIBUTE]).to.be.equal('default')
})

it.only('Slot fallback html', () => {
it('Slot fallback html', () => {
const source =
'<div><slot><ul><li each={item in items}>{item}</li></ul></slot></div>'
const { template } = parse(source)
Expand All @@ -1307,7 +1306,7 @@ describe('Generators - Template', () => {
const output = evaluateOutput(bindings[0])

// the fallback template is defined
expect(output[BINDING_SLOT_FALLBACK_KEY]).to.be.ok()
expect(output[BINDING_TEMPLATE_KEY]).to.be.ok
// the each binding should be part of the fallback template
expect(bindings).to.have.length(1)
})
Expand Down

0 comments on commit 957e1c5

Please sign in to comment.