Skip to content

Commit 9e1379e

Browse files
committed
Rebased, retained existing behavior to be back compat
1 parent b405b8b commit 9e1379e

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/index.js

Lines changed: 9 additions & 4 deletions
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
}
@@ -46,10 +53,8 @@ function toVdom(element, nodeName) {
4653
a = element.attributes,
4754
cn = element.childNodes;
4855
for (i = a.length; i--; ) {
49-
var propName = a[i].name.replace(/-(\w)/, function(_, c) {
50-
return c ? c.toUpperCase() : ''
51-
});
52-
props[propName] = a[i].value;
56+
props[a[i].name] = a[i].value;
57+
props[toCamelCase(a[i].name)] = a[i].value;
5358
}
5459
for (i = cn.length; i--; ) children[i] = toVdom(cn[i]);
5560
return h(nodeName || element.nodeName.toLowerCase(), props, children);

src/index.test.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,62 @@ import assert from 'assert';
22
import { h } from 'preact';
33
import registerElement from './index';
44

5+
function Clock ({ time }) {
6+
return <span>{time}</span>;
7+
}
8+
59
registerElement(Clock, 'x-clock', ['time', 'custom-date']);
610

711
it('renders ok, updates on attr change', () => {
812
const root = document.createElement('div');
913
const el = document.createElement('x-clock');
1014
el.setAttribute('time', '10:28:57 PM');
11-
el.setAttribute('custom-date', '11/11/2011');
1215

1316
root.appendChild(el);
1417
document.body.appendChild(root);
1518

1619
assert.equal(
1720
root.innerHTML,
18-
'<x-clock time="10:28:57 PM" custom-date="11/11/2011"><span>10:28:57 PM, 11/11/2011</span></x-clock>'
21+
'<x-clock time="10:28:57 PM"><span>10:28:57 PM</span></x-clock>'
1922
);
2023

2124
el.setAttribute('time', '11:01:10 AM');
22-
el.setAttribute('custom-date', '01/01/2001');
2325

2426
assert.equal(
2527
root.innerHTML,
26-
'<x-clock time="11:01:10 AM" custom-date="01/01/2001"><span>11:01:10 AM, 01/01/2001</span></x-clock>'
28+
'<x-clock time="11:01:10 AM"><span>11:01:10 AM</span></x-clock>'
29+
);
30+
31+
document.body.removeChild(root);
32+
});
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>`
2761
);
2862

2963
document.body.removeChild(root);

0 commit comments

Comments
 (0)