You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
@@ -304,6 +314,7 @@ It reads like a good novel doesn't it? On a `click` event update the todo, sett
304
314
Meanwhile HyperReact sees the value of `params.todo.checked` changing, and this causes the associated HTML INPUT tag to be re-rendered.
305
315
306
316
We will finish up by adding a delete link at the end of the Todo item:
317
+
307
318
```ruby
308
319
# app/hyperloop/components/todo_item.rb
309
320
classTodoItem < Hyperloop::Component
@@ -316,6 +327,7 @@ class TodoItem < Hyperloop::Component
316
327
end
317
328
end
318
329
```
330
+
319
331
*Note: If a component or tag block returns a string it is automatically wrapped in a SPAN, to insert a string in the middle you have to wrap it a SPAN like we did above.*
320
332
321
333
I hope you are starting to see a pattern here. HyperReact components determine what to display based on the `state` of some
@@ -339,6 +351,7 @@ class Todo < ApplicationRecord
339
351
scope :active, -> () { where(completed:false) }
340
352
end
341
353
```
354
+
342
355
Now we can say `Todo.all`, `Todo.completed`, and `Todo.active`, and get the desired subset of Todos.
343
356
You might want to try it now in the rails console. *Note: you will have to do a `reload!` to load the changes to the Model.*
344
357
@@ -352,6 +365,7 @@ We would like the URL of our App to reflect which of these *filters* is being di
352
365
Having the application display different data (or whole different components) based on the URL is called routing.
353
366
354
367
Lets change `App` to look like this:
368
+
355
369
```ruby
356
370
# app/hyperloop/components/app.rb
357
371
classApp < Hyperloop::Router
@@ -367,6 +381,7 @@ class App < Hyperloop::Router
367
381
end
368
382
```
369
383
and the `Index` component to look like this:
384
+
370
385
```ruby
371
386
# app/hyperloop/components/index.rb
372
387
classIndex < Hyperloop::Router::Component
@@ -409,6 +424,7 @@ This is telling Rails to accept all requests and to process them using the `hype
409
424
### Chapter 7: Helper Methods, Inline Styling, Active Support and Router Nav Links
410
425
411
426
Of course we will want to add navigation to move between these routes. We will put the navigation in the footer:
427
+
412
428
```ruby
413
429
# app/hyperloop/components/footer.rb
414
430
classFooter < Hyperloop::Component
@@ -437,10 +453,12 @@ To make this happen we will *mixin* some router helpers by *including* `HyperRou
437
453
Then we can replace the anchor tag with the Router's `NavLink` component:
@@ -470,6 +488,7 @@ Associated with this history is a (you guessed it I hope) *state*. So when the
470
488
So far we can mark Todos as completed, delete them, and filter them. Now we create an `EditItem` component so we can change the Todo title.
471
489
472
490
Add a new component like this:
491
+
473
492
```ruby
474
493
# app/hyperloop/components/edit_item.rb
475
494
classEditItem < Hyperloop::Component
@@ -489,10 +508,12 @@ Before we use this component let's understand how it works.
489
508
+ When the user types the enter key (key code 13) the todo is saved.
490
509
491
510
Now update the `TodoItem` component replacing
511
+
492
512
```ruby
493
513
SPAN { params.todo.title }
494
514
```
495
515
with
516
+
496
517
```ruby
497
518
EditItem(todo: params.todo)
498
519
```
@@ -510,6 +531,7 @@ To summarize:
510
531
+ User changes focus away (`blur`) from the Todo being edited: editing changes to `false`.
511
532
In order to accomplish this our `EditItem` component is going to communicate via two callbacks - `on_save` and `on_cancel` - with the parent component. We can think of these callbacks as custom events, and indeed as we shall see they will work just like any other event.
512
533
Add the following 5 lines to the `EditItem` component like this:
534
+
513
535
```ruby
514
536
# app/hyperloop/components/edit_item.rb
515
537
classEditItem < Hyperloop::Component
@@ -543,6 +565,7 @@ For example, if we had left off `type: Proc` we would have to say `params.on_sav
543
565
Finally we add the `blur` event handler and simply transform it into our custom `cancel` event.
544
566
545
567
Now we can update our `TodoItem` component to be a little state machine, which will react to three events: `double_click`, `save` and `cancel`.
568
+
546
569
```ruby
547
570
# app/hyperloop/components/todo_item.rb
548
571
classTodoItem < Hyperloop::Component
@@ -586,6 +609,7 @@ Our EditItem component has a good robust interface. It takes a Todo, and lets t
586
609
Because of this we can easily reuse EditItem to create new Todos. Not only does this save us time, but it also insures that the user interface acts consistently.
587
610
588
611
Update the `Header` component to use EditItem like this:
612
+
589
613
```ruby
590
614
# app/hyperloop/components/header.
591
615
classHeader < Hyperloop::Component
@@ -637,6 +661,7 @@ You will have to refresh the page after changing the style sheet.
637
661
Now its a matter of updating the css classes which are passed to components via the `class` parameter.
638
662
639
663
Let's start with the `App` component. With styling it will look like this:
664
+
640
665
```ruby
641
666
# app/hyperloop/components/app.rb
642
667
classApp < Hyperloop::Router
@@ -652,6 +677,7 @@ end
652
677
```
653
678
The `Footer` components needs have a `UL` added to hold the links nicely,
654
679
and we can also use the `NavLinks``active_class` param to highlight the link that is currently active:
680
+
655
681
```ruby
656
682
# app/hyperloop/components/footer.rb
657
683
classFooter < Hyperloop::Component
@@ -672,6 +698,7 @@ class Footer < Hyperloop::Component
672
698
end
673
699
```
674
700
For the Index component just add the `main` and `todo-list` classes.
701
+
675
702
```ruby
676
703
# app/hyperloop/components/index.rb
677
704
classIndex < Hyperloop::Router::Component
@@ -686,6 +713,7 @@ end
686
713
```
687
714
For the EditItem component we want the caller specify the class. To keep things compatible with React.js we need to call the param `className`,
688
715
but we can still send it to EditItem with the usual hyperloop style `class` param.
716
+
689
717
```ruby
690
718
# app/hyperloop/components/edit_item.rb
691
719
classEditItem < Hyperloop::Component
@@ -710,6 +738,7 @@ class EditItem < Hyperloop::Component
710
738
end
711
739
```
712
740
Now we can add classes to the TodoItem's list-item, input, anchor tags, and to the `EditItem` component:
741
+
713
742
```ruby
714
743
# app/hyperloop/components/todo_item.rb
715
744
classTodoItem < Hyperloop::Component
@@ -732,6 +761,7 @@ end
732
761
```
733
762
In the Header we can send a different class to the `EditItem` component. While we are at it
734
763
we will add the `H1 { 'todos' }` hero unit.
764
+
735
765
```ruby
736
766
# app/hyperloop/components/header.rb
737
767
classHeader < Hyperloop::Component
@@ -748,7 +778,8 @@ At this point your Todo App should be properly styled.
748
778
### Chapter 12: Other Features
749
779
750
780
+**Show How Many Items Left In Footer**
751
-
This is just a span that we add before the link tags list in the Footer component:
781
+
This is just a span that we add before the link tags list in the Footer component:
782
+
752
783
```ruby
753
784
...
754
785
render(DIV, class: :footer) do
@@ -760,6 +791,7 @@ This is just a span that we add before the link tags list in the Footer componen
760
791
```
761
792
+**Add 'placeholder' Text To Edit Item**
762
793
EditItem should display a meaningful placeholder hint if the title is blank:
794
+
763
795
```ruby
764
796
...
765
797
INPUT(
@@ -772,6 +804,7 @@ EditItem should display a meaningful placeholder hint if the title is blank:
772
804
```
773
805
+**Don't Show the Footer If There are No Todos**
774
806
In the `App` component add a *guard* so that we won't show the Footer if there are no Todos:
0 commit comments