Skip to content

Commit f516258

Browse files
committed
add spread operator props
1 parent d351508 commit f516258

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [5.0.0] - 2023-08-
5+
- **Breaking changes**
6+
- Removed property `autoCreateChildren` use instead `suspendcontent` prop in markup
7+
- **Added** spread operator props like `<my-component ...${myObject}/>`
8+
- **Improved** performance
9+
410
## [4.0.3] - 2022-09-22
511
- **Fixed** component data attributes do not work
612

src/directives/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function extractDirectivesFromProps(cmp) {
2929
return cmp._directiveProps;
3030
}
3131
function isDirective(aName) {
32+
//console.log(aName)
3233
//return REGEX.IS_DIRECTIVE.test(name);
3334
return aName[0] === 'd' && (aName[1] === '-' || aName[1] === ':');
3435
}

src/vdom/h.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ function generateItemKey(values) {
5151
}
5252
function fillCompiled(obj, values, parent, _this) {
5353
let keys = Object.keys(obj);
54+
if (obj.__spreadprops) {
55+
for (let o in values[0]) {
56+
obj[o] = values[0][o]
57+
}
58+
59+
delete obj.__spreadprops;
60+
}
5461
for (let i = 0; i < keys.length; i++) {
5562
//for (let k in obj) {
5663
if (obj[keys[i]] && typeof obj[keys[i]] === 'object') {

src/vdom/parser.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dashToCamel from "../utils/dashToCamel.js";
22
import { REGEX, ATTR, TAG, PROPS_ATTRIBUTES } from "../constants.js";
3-
import directive from "../directives/index.js";
3+
//import directive from "../directives/index.js";
44
import { isDirective } from "../directives/helpers.js";
55
import { tplCache } from "./stores.js";
66
const regExcludeSpecial = new RegExp(`<\/?(${TAG.TEXT_NODE_PLACE}|${TAG.ITERATE_NODE_PLACE})?>$`);
@@ -83,6 +83,7 @@ function compile(tpl) {
8383
let props;
8484
//console.log(tpl)
8585
while (match = REGEX.HTML_MARKUP.exec(tpl)) {
86+
//console.log(match)
8687
if (lastTextPos > -1) {
8788
if ( /*lastTextPos > -1 && */lastTextPos + match[0].length < REGEX.HTML_MARKUP.lastIndex) {
8889
// remove new line space
@@ -135,6 +136,10 @@ function compile(tpl) {
135136
if (regExcludeSpecial.test(match[0])) {
136137
continue;
137138
}
139+
//support to spread operator
140+
if(match[3] && match[3].startsWith('...')) {
141+
match[3] = match[3].replace('...','__spreadprops="') + '"';
142+
}
138143
// transform slot to dz-slot
139144
if (match[2] === 'slot')
140145
match[2] = TAG.SLOT;
@@ -222,7 +227,7 @@ function serializeProps($node) {
222227
}
223228
return props;
224229
}
225-
function propsFixer(nName, aName, aValue, props, $node) {
230+
function propsFixer(nName, aName, aValue, props/*, $node*/) {
226231
if (typeof aValue === 'string' && REGEX.IS_STRING_QUOTED.test(aValue))
227232
aValue = aValue.replace(REGEX.REPLACE_QUOT, '&quot;');
228233
//let isDirective = REGEX.IS_DIRECTIVE.test(aName);

test/spread.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Doz from "../src/index.js";
2+
//import be from "bejs";
3+
4+
describe('spread', function () {
5+
6+
beforeEach(function () {
7+
document.body.innerHTML = '';
8+
Doz.collection.removeAll();
9+
});
10+
11+
describe('create app', function () {
12+
13+
it('should be ok', function (done) {
14+
15+
document.body.innerHTML = `<div id="app"></div>`;
16+
17+
Doz.component('salutation-card', {
18+
props: {
19+
show: false
20+
},
21+
template(h) {
22+
return h`
23+
<div title="hello">Hello</div>
24+
`
25+
},
26+
onMount() {
27+
if (this.props.a && this.props.b) {
28+
done()
29+
} else {
30+
throw new Error('props not found')
31+
}
32+
}
33+
});
34+
new Doz({
35+
root: '#app',
36+
template(h){
37+
let someProps = {a: 1, b: 2};
38+
return h`
39+
<salutation-card ...${someProps}/>
40+
`}
41+
});
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)