|
| 1 | +# Custom actionview components |
| 2 | + |
| 3 | +Show [specs](/spec/usage/extend/custom_component_spec.rb). |
| 4 | + |
| 5 | +Custom `actionview components`, both `static` and `dynamic`, are a way to harness the power of various Rails `ActionView` helpers without including them in your `custom components`. |
| 6 | + |
| 7 | +## Static actionview components |
| 8 | + |
| 9 | +This is just a simple exmaple of a `custom static actionview component`. Notice that it inherits from a different class than [generic custom static components](/docs/extend/custom_static_components.md). |
| 10 | + |
| 11 | +Create a `custom static actionview component` in `app/matestack/components/time_ago.rb` |
| 12 | + |
| 13 | +```ruby |
| 14 | +class Components::TimeAgo < Matestack::Ui::StaticActionviewComponent |
| 15 | + |
| 16 | + def prepare |
| 17 | + @from_time = Time.now - 3.days - 14.minutes - 25.seconds |
| 18 | + end |
| 19 | + |
| 20 | + def response |
| 21 | + components { |
| 22 | + div id: 'my-component-1' do |
| 23 | + plain time_ago_in_words(@from_time) |
| 24 | + end |
| 25 | + } |
| 26 | + end |
| 27 | + |
| 28 | +end |
| 29 | +``` |
| 30 | + |
| 31 | +We can add it our `ExamplePage` the way we would add a generic `custom static component` |
| 32 | + |
| 33 | +```ruby |
| 34 | +class Pages::ExamplePage < Matestack::Ui::Page |
| 35 | + |
| 36 | + def response |
| 37 | + components { |
| 38 | + div id: 'div-on-page' do |
| 39 | + custom_timeAgo |
| 40 | + end |
| 41 | + } |
| 42 | + end |
| 43 | + |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +and receive the following output: |
| 48 | + |
| 49 | +```HTML |
| 50 | +<div id="div-on-page"> |
| 51 | + 3 days |
| 52 | +</div> |
| 53 | +``` |
| 54 | + |
| 55 | +## Dynamic actionview components |
| 56 | + |
| 57 | +This is just a simple exmaple of a `custom dynamic actionview component`. Notice that it inherits from a different class than [generic custom dynamic components](/docs/extend/custom_dynamic_components.md). |
| 58 | + |
| 59 | +Create a `custom dynamic actionview component` in `app/matestack/components/time_click.rb` |
| 60 | + |
| 61 | +```ruby |
| 62 | +class Components::TimeClick < Matestack::Ui::DynamicActionviewComponent |
| 63 | + |
| 64 | + def prepare |
| 65 | + @start_time = Time.now |
| 66 | + end |
| 67 | + |
| 68 | + def response |
| 69 | + components { |
| 70 | + div id: 'my-component-1' do |
| 71 | + plain "{{dynamic_value}} #{distance_of_time_in_words(@start_time, Time.now, include_seconds: true)}" |
| 72 | + end |
| 73 | + } |
| 74 | + end |
| 75 | + |
| 76 | +end |
| 77 | +``` |
| 78 | + |
| 79 | +and the corresponding Vue.js component in `app/matestack/components/time_click.js` |
| 80 | + |
| 81 | +```javascript |
| 82 | +MatestackUiCore.Vue.component('custom-timeclick', { |
| 83 | + mixins: [MatestackUiCore.componentMixin], |
| 84 | + data: function data() { |
| 85 | + return { |
| 86 | + dynamic_value: "Now I show: " |
| 87 | + }; |
| 88 | + }, |
| 89 | + mounted(){ |
| 90 | + const self = this |
| 91 | + setTimeout(function () { |
| 92 | + self.dynamic_value = "Later I show: " |
| 93 | + }, 300); |
| 94 | + } |
| 95 | +}); |
| 96 | +``` |
| 97 | + |
| 98 | +We can now add it our `ExamplePage` the way we would add a generic `custom dynamic component` |
| 99 | + |
| 100 | + |
| 101 | +```ruby |
| 102 | +class Pages::ExamplePage < Matestack::Ui::Page |
| 103 | + |
| 104 | + def response |
| 105 | + components { |
| 106 | + div id: 'div-on-page' do |
| 107 | + custom_timeClick |
| 108 | + end |
| 109 | + } |
| 110 | + end |
| 111 | + |
| 112 | +end |
| 113 | +``` |
| 114 | + |
| 115 | +Initially, the page will display `Now I show: less than 5 seconds`, but after 300ms, it gets changed into `Later I show: less than 5 seconds`. Nice! |
0 commit comments