Simplify getting value of slot attribute

pull/8535/head
Nguyen Tran 3 years ago
parent ae02d4720a
commit 8098b6bc7e

@ -7,6 +7,7 @@ import { INode } from './interfaces';
import compiler_errors from '../compiler_errors';
import get_const_tags from './shared/get_const_tags';
import ConstTag from './ConstTag';
import { Attribute as AttributeNode } from '../../interfaces';
export default class SlotTemplate extends Node {
type: 'SlotTemplate';
@ -15,8 +16,8 @@ export default class SlotTemplate extends Node {
lets: Let[] = [];
const_tags: ConstTag[];
slot_attribute: Attribute;
slot_template_name: string = 'default';
is_static: boolean = true;
is_static: boolean;
slot_template_name: string;
constructor(
component: Component,
@ -65,6 +66,16 @@ export default class SlotTemplate extends Node {
}
});
if (!this.slot_template_name) {
this.slot_template_name = 'default';
this.is_static = false;
this.slot_attribute = new Attribute(component, this, scope, {
type: 'Attribute',
name: 'slot',
value: [ { type: 'Text', data: 'default' } ]
} as AttributeNode);
}
this.scope = scope;
([this.const_tags, this.children] = get_const_tags(info.children, component, this, this));
}

@ -175,8 +175,8 @@ export default class InlineComponentWrapper extends Wrapper {
? [
p`$$slots: {
${Array.from(this.slots).map(([slot_template, slot]) => {
const { is_static, slot_template_name, slot_attribute } = slot_template;
const slot_expression = is_static ? { type: 'Literal', value: slot_template_name } : slot_attribute.get_value(block);
const { slot_attribute } = slot_template;
const slot_expression = slot_attribute.get_value(block);
return p`[${slot_expression}]: [${slot.block.name}, ${slot.get_context || null}, ${slot.get_changes || null}]`;
})}
}`,

@ -4,6 +4,7 @@ import remove_whitespace_children from './utils/remove_whitespace_children';
import { get_slot_scope } from './shared/get_slot_scope';
import InlineComponent from '../../nodes/InlineComponent';
import { get_const_tags } from './shared/get_const_tags';
import { get_attribute_value } from './shared/get_attribute_value';
export default function(node: SlotTemplate, renderer: Renderer, options: RenderOptions & {
slot_scopes: Map<any, any>;
@ -29,7 +30,7 @@ export default function(node: SlotTemplate, renderer: Renderer, options: RenderO
throw new Error(`Duplicate slot name "${node.slot_template_name}" in <${parent_inline_component.name}>`);
}
const slot_expression = node.is_static ? { type: 'Literal', value: node.slot_template_name } : node.slot_attribute.get_value(null);
const slot_expression = get_attribute_value(node.slot_attribute);
options.slot_scopes.set(slot_expression, {
input: get_slot_scope(node.lets),
output: slot_fragment_content,

Loading…
Cancel
Save