Skip to content

Commit 039b3b7

Browse files
Merge pull request #30 from preactjs/hyphenate-camelcased-props
2 parents 8ce7220 + 9e1379e commit 039b3b7

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ function connectedCallback() {
2525
(this.hasAttribute('hydrate') ? hydrate : render)(this._vdom, this._root);
2626
}
2727

28+
function toCamelCase(str) {
29+
return str.replace(/-(\w)/g, function(_, c) {
30+
return c ? c.toUpperCase() : ''
31+
});
32+
}
33+
2834
function attributeChangedCallback(name, oldValue, newValue) {
2935
if (!this._vdom) return;
3036
const props = {};
3137
props[name] = newValue;
38+
props[toCamelCase(name)] = newValue;
3239
this._vdom = cloneElement(this._vdom, props);
3340
render(this._vdom, this._root);
3441
}
@@ -45,7 +52,10 @@ function toVdom(element, nodeName) {
4552
i = 0,
4653
a = element.attributes,
4754
cn = element.childNodes;
48-
for (i = a.length; i--; ) props[a[i].name] = a[i].value;
55+
for (i = a.length; i--; ) {
56+
props[a[i].name] = a[i].value;
57+
props[toCamelCase(a[i].name)] = a[i].value;
58+
}
4959
for (i = cn.length; i--; ) children[i] = toVdom(cn[i]);
5060
return h(nodeName || element.nodeName.toLowerCase(), props, children);
5161
}

src/index.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function Clock ({ time }) {
66
return <span>{time}</span>;
77
}
88

9-
registerElement(Clock, 'x-clock', ['time']);
9+
registerElement(Clock, 'x-clock', ['time', 'custom-date']);
1010

1111
it('renders ok, updates on attr change', () => {
1212
const root = document.createElement('div');
@@ -30,3 +30,35 @@ it('renders ok, updates on attr change', () => {
3030

3131
document.body.removeChild(root);
3232
});
33+
34+
const kebabName = 'custom-date-long-name';
35+
const camelName = 'customDateLongName';
36+
const lowerName = camelName.toLowerCase();
37+
function PropNameTransform (props) {
38+
return <span>{props[kebabName]} {props[lowerName]} {props[camelName]}</span>;
39+
}
40+
registerElement(PropNameTransform , 'x-prop-name-transform', [kebabName, camelName]);
41+
42+
it('handles kebab-case attributes with passthrough', () => {
43+
const root = document.createElement('div');
44+
const el = document.createElement('x-prop-name-transform');
45+
el.setAttribute(kebabName, '11/11/2011');
46+
el.setAttribute(camelName, 'pretended to be camel');
47+
48+
root.appendChild(el);
49+
document.body.appendChild(root);
50+
51+
assert.equal(
52+
root.innerHTML,
53+
`<x-prop-name-transform ${kebabName}="11/11/2011" ${lowerName}="pretended to be camel"><span>11/11/2011 pretended to be camel 11/11/2011</span></x-prop-name-transform>`
54+
);
55+
56+
el.setAttribute(kebabName, '01/01/2001');
57+
58+
assert.equal(
59+
root.innerHTML,
60+
`<x-prop-name-transform ${kebabName}="01/01/2001" ${lowerName}="pretended to be camel"><span>01/01/2001 pretended to be camel 01/01/2001</span></x-prop-name-transform>`
61+
);
62+
63+
document.body.removeChild(root);
64+
});

0 commit comments

Comments
 (0)