Skip to content

Commit

Permalink
babel plugin should transform htmlFor into for
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampo Kivistö committed Mar 25, 2017
1 parent c203e28 commit b5c24b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ function getVNodeProps(t, astProps, isComponent) {

if (!isComponent && (propName === 'className' || propName === 'class')) {
className = getValue(t, astProp.value);
} else if (!isComponent && (propName === 'htmlFor')) {
props.push({
astName: getName(t, 'for'),
astValue: getValue(t, astProp.value),
astSpread: null
})
} else if (propName.substr(0, 11) === 'onComponent' && isComponent) {
if (!ref) {
ref = t.ObjectExpression([]);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"mocha": "^3.2.0"
},
"scripts": {
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha tests.js",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha tests.js",
"test": "./node_modules/.bin/_mocha tests.js",
"lint": "eslint lib",
"lintfix": "eslint lib --fix"
},
Expand Down
6 changes: 6 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('Array', function() {
it('Events should be in props', function () {
expect(transform('<div id="test" onClick={func} class={variable}>1</div>')).to.equal('createVNode(2, "div", variable, "1", {\n "id": "test",\n "onClick": func\n});');
});

it('Should transform input and htmlFor correctly', function () {
var result = transform('<label htmlFor={id}><input id={id} name={name} value={value} onChange={onChange} onInput={onInput} onKeyup={onKeyup} onFocus={onFocus} onClick={onClick} type="number" pattern="[0-9]+([,\.][0-9]+)?" inputMode="numeric" min={minimum}/></label>');
var expected = 'createVNode(2, "label", null, createVNode(512, "input", null, null, {\n "id": id,\n "name": name,\n "value": value,\n "onChange": onChange,\n "onInput": onInput,\n "onKeyup": onKeyup,\n "onFocus": onFocus,\n "onClick": onClick,\n "type": "number",\n "pattern": "[0-9]+([,.][0-9]+)?",\n "inputMode": "numeric",\n "min": minimum\n}), {\n "for": id\n});';
expect(result).to.equal(expected);
});
});

/**
Expand Down

0 comments on commit b5c24b7

Please sign in to comment.