Skip to content

Commit

Permalink
Merge pull request #1 from privatenumber/wildcard
Browse files Browse the repository at this point in the history
Added wildcard
  • Loading branch information
privatenumber authored May 17, 2019
2 parents 5d53e18 + 2503ef7 commit 188582d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ Pass in an array
<subslot :element="[ChildComponentA, '@ChildComponentB', 'div']" />
```

### To match any element
Use the asterisk to match any element. This is to match only elements and remove any text/white-space.

```html
<subslot element="*" />
```

### Offset the number of returned elements
```html
<subslot
Expand Down
22 changes: 14 additions & 8 deletions src/utils/filter-vnodes.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
const getWhitelist = ({ vm, filter }) => {
let matchAll = false;
const components = [];
const tags = [];
const elements = Array.isArray(filter.element) ? filter.element : [filter.element];

elements.forEach((element) => {
if (typeof element === 'string') {
if (element[0] === '@') {
if (element === '*') {
matchAll = true;
} else if (element[0] === '@') {
const component = vm.$options.components[element.slice(1)];
if (component) {
components.push(component);
}
return;
} else {
tags.push(element);
}
tags.push(element);
return;
} else {
components.push(element);
}

components.push(element);
});

return { components, tags };
return { matchAll, components, tags };
};

export const filterVnodes = ({ vnodes, filter, vm }) => {

if (filter.element) {
const { components, tags } = getWhitelist({ vm, filter });
const { matchAll, components, tags } = getWhitelist({ vm, filter });
vnodes = vnodes.filter((vnode) => {
if (matchAll) {
return vnode.tag;
}

const isComponent = (vnode.componentOptions && vnode.componentOptions.Ctor.extendOptions);
const { tag } = vnode.componentOptions || vnode;

Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/filter-attr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ exports[`Subslot Should support tag 1`] = `
</button>
</div>
`;

exports[`Subslot Should support wildcard 1`] = `
<div
class="card"
>
<span>
Should render
</span>
</div>
`;
34 changes: 34 additions & 0 deletions test/filter-attr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,40 @@ describe('Subslot', () => {
expect(wrapper.element).toMatchSnapshot();
});

test('Should support wildcard', () => {
const Card = {
template: `
<div class="card">
<subslot element="*" />
</div>
`,

components: {
Subslot,
},

data() {
return {
CardHeader,
};
},
};

const usage = {
template: `
<card>
Shouldn't render
<span>Should render</span>
Shouldn't render
</card>
`,
components: { Card },
};

const wrapper = mount(usage);
expect(wrapper.element).toMatchSnapshot();
});

test('Should only be 3 CardHeaders', () => {
const Card = {
template: `
Expand Down

0 comments on commit 188582d

Please sign in to comment.