Skip to content

Commit 43bd3d6

Browse files
committed
add type attribute to <input> element e.g: <input id="toggle-all" type="checkbox"> for #44
1 parent 511c868 commit 43bd3d6

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Diff for: examples/todo-list/elmish.js

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ function add_attributes(attrlist, node) {
6262
case 'placeholder':
6363
node.placeholder = a[1]; // add placeholder to <input> element
6464
break;
65+
case 'type':
66+
node.setAttribute('type', a[1]); // e.g: <input id="toggle-all" type="checkbox">
67+
break;
6568
default:
6669
break;
6770
}

Diff for: test/elmish.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ test('elmish.add_attributes set "for" attribute <label> element', function (t) {
126126
t.end();
127127
});
128128

129+
test('elmish.add_attributes type="checkbox" on <input> element', function (t) {
130+
const root = document.getElementById(id);
131+
let input = document.createElement('input');
132+
input = elmish.add_attributes(["type=checkbox", "id=toggle-all"], input);
133+
root.appendChild(input);
134+
const type_atrr = document.getElementById('toggle-all').getAttribute("type");
135+
t.equal(type_atrr, "checkbox", '<input id="toggle-all" type="checkbox">');
136+
t.end();
137+
});
138+
139+
140+
141+
129142

130143
test('test default branch of elmish.add_attributes (no effect)', function (t) {
131144
const root = document.getElementById(id);

Diff for: todo-list.md

+25
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,31 @@ test.only('elmish.add_attributes set "for" attribute <label> element', function
580580
Write the "case" in to make this test _pass_ in `elmish.js`.
581581

582582

583+
#### `<input>` attribute `type`
584+
585+
In order to use a Checkbox in our Todo List UI,
586+
we need to set the `type=checkbox` on the `<input>` element.
587+
588+
Add the following test to the `test/elmish.test.js` file: <br />
589+
590+
```js
591+
test('elmish.add_attributes type="checkbox" on <input> element', function (t) {
592+
const root = document.getElementById(id);
593+
let input = document.createElement('input');
594+
input = elmish.add_attributes(["type=checkbox", "id=toggle-all"], input);
595+
root.appendChild(input);
596+
const type_atrr = document.getElementById('toggle-all').getAttribute("type");
597+
t.equal(type_atrr, "checkbox", '<input id="toggle-all" type="checkbox">');
598+
t.end();
599+
});
600+
```
601+
602+
Write the "case" in to make this test _pass_ in `elmish.js`.
603+
604+
`<input>` attribute `type`:
605+
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes
606+
607+
583608

584609

585610

0 commit comments

Comments
 (0)