Skip to content

Commit 6a715fe

Browse files
committed
add Input Placeholder Attribute instructions, test and code for #44
1 parent ac5eeb3 commit 6a715fe

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function add_attributes(attrlist, node) {
5353
case 'id':
5454
node.id = a[1]; // apply element id
5555
break;
56+
case 'placeholder':
57+
node.placeholder = a[1]; // add placeholder to <input> element
58+
break;
5659
default:
5760
break;
5861
}

Diff for: test/elmish.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ test('elmish.add_attributes applies multiple attribute to node', function (t) {
9292
t.end();
9393
});
9494

95+
test.only('elmish.add_attributes set placeholder on <input> element', function (t) {
96+
const root = document.getElementById(id);
97+
let input = document.createElement('input');
98+
input.id = 'new-todo';
99+
input = elmish.add_attributes(["placeholder=What needs to be done?"], input);
100+
root.appendChild(input);
101+
const placeholder = document.getElementById('new-todo')
102+
.getAttribute("placeholder");
103+
t.equal(placeholder, "What needs to be done?", "paceholder set on <input>");
104+
t.end();
105+
});
106+
107+
95108
test('test default branch of elmish.add_attributes (no effect)', function (t) {
96109
const root = document.getElementById(id);
97110
let div = document.createElement('div');

Diff for: todo-list.md

+61-28
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,18 @@ This is a "copy-paste" of the _generated_ code including the Todo items:
410410
Let's split each one of these elements into it's own `function`
411411
(_with any necessary "helpers"_) in the order they appear.
412412

413-
### `<section>` HTML Element
413+
When building a House we don't think "build house" as our _first_ action.
414+
Instead we think: what are the "foundations" that need to be in place
415+
***before*** we lay the first brick?
414416

415-
The _first_ HTML element we encounter in the TodoMVC app is
416-
`<section>`.
417-
`<section>` represents a standalone section — which doesn't have
418-
a more specific semantic element to represent it —
419-
it's an alternative way to group elements to a `<div>`.
417+
In our Todo List App we need a few "Helper Functions"
418+
before we start building the App.
420419

421-
> info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section <br />
422-
> difference:
423-
https://stackoverflow.com/questions/6939864/what-is-the-difference-between-section-and-div
420+
### HTML / DOM Creation Generic Helper Functions
424421

425-
As with other "grouping" or "container" HTML elements,
426-
our `section` function (_which will create the_ `<section>` _DOM node_)
427-
will be a function with _two_ arguments:
422+
All "grouping" or "container" HTML elements e.g: `<div>` or `<section>`
423+
will be called with ***two arguments***:
424+
e.g: `var sec = section(attributes, childnodes)`
428425
+ `attributes` - a list (Array) of HTML attributes/properties
429426
e.g: `id` or `class`.
430427
+ `childnodes` - a list (Array) of child HTML elements
@@ -433,11 +430,6 @@ will be a function with _two_ arguments:
433430
Each of these function arguments will be "_applied_" to the HTML element.
434431
We therefore need a pair of "helper" functions (_one for each argument_).
435432

436-
Section in Elm: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html
437-
<br />
438-
Demo: https://ellie-app.com/LTtNVQjfWVa1
439-
![ellie-elm-section](https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png)
440-
441433

442434
### `add_attributes`
443435

@@ -475,37 +467,57 @@ test('elmish.add_attributes applies class HTML attribute to a node', function (t
475467

476468
Given the code in the test above,
477469
take a moment to think of how _you_ would write,
478-
the `attributes` function to apply a CSS `class` to an element. <br />
470+
the `add_attributes` function to apply a CSS `class` to an element. <br />
479471
Note: we have _seen_ the code _before_ in the `counter` example.
480472
The difference is this time we want it to be "generic";
481473
we want to apply a CSS `class` to _any_ DOM node.
482474

483475
If you can, make the test _pass_
484-
by writing the `attributes` function.
476+
by writing the `add_attributes` function.
485477
(_don't forget to_ `export` _the function at the end of the file_).
486478

487479
If you get "stuck", checkout:
488480
https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js <br />
489481

490482

491-
492-
493-
> The `attributes` function is "impure" as it "mutates"
494-
the target DOM `node`, however the application of attributes
495-
to DOM node(s) is idempotent;
483+
> **Note**: The `add_attributes` function is "impure" as it "mutates"
484+
the target DOM `node`, this is more of a "fact of life" in JavaScript,
485+
and given that the application of attributes
486+
to DOM node(s) is idempotent we aren't "concerned" with "side effects";
496487
the attribute will only be applied _once_ to the node
497-
regardless of how many times the `attributes` function is called.
488+
regardless of how many times the `add_attributes` function is called.
498489
see: https://en.wikipedia.org/wiki/Idempotence
499490

500491

501-
For reference, the Elm HTML Attributes package is:
492+
For reference, the Elm HTML Attributes function on Elm package is:
502493
http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Attributes
503494

504-
### `append_childnodes`
495+
#### Input Placeholder Attribute
496+
497+
The `<input>` form element (_where we create new Todo List items_)
498+
has a helpful `placeholder` _prompting_ us with a question:
499+
"_What needs to be done?_"
500+
501+
Add the following test to the `test/elmish.test.js` file: <br />
502+
503+
```js
504+
test.only('elmish.add_attributes set placeholder on <input> element', function (t) {
505+
const root = document.getElementById(id);
506+
let input = document.createElement('input');
507+
input.id = 'new-todo';
508+
input = elmish.add_attributes(["placeholder=What needs to be done?"], input);
509+
root.appendChild(input);
510+
const placeholder = document.getElementById('new-todo')
511+
.getAttribute("placeholder");
512+
t.equal(placeholder, "What needs to be done?", "paceholder set on <input>");
513+
t.end();
514+
});
515+
```
505516

506517

518+
### `append_childnodes`
507519

508-
The `append_childnodes` functionality is a one-liner: <br />
520+
The `append_childnodes` _functionality_ is a "_one-liner_": <br />
509521
```js
510522
childnodes.forEach(function (el) { parent.appendChild(el) });
511523
```
@@ -557,6 +569,27 @@ If you get "stuck", checkout:
557569
https://github.com/dwyl/learn-elm-architecture-in-javascript/tree/master/examples/todo-list/elmish.js <br />
558570

559571

572+
### `<section>` HTML Element
573+
574+
The _first_ HTML element we encounter in the TodoMVC app is
575+
`<section>`.
576+
`<section>` represents a standalone section — which doesn't have
577+
a more specific semantic element to represent it —
578+
it's an alternative way to group elements to a `<div>`.
579+
580+
> info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section <br />
581+
> difference:
582+
https://stackoverflow.com/questions/6939864/what-is-the-difference-between-section-and-div
583+
584+
585+
586+
587+
588+
589+
Section in Elm: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html
590+
<br />
591+
Demo: https://ellie-app.com/LTtNVQjfWVa1
592+
![ellie-elm-section](https://user-images.githubusercontent.com/194400/42708957-bbcc1020-86d6-11e8-97bf-f2f3a1c6fdea.png)
560593

561594

562595

0 commit comments

Comments
 (0)