import { live, LiveController } from "@camertron/live-component"; type TodoItemComponentProps = { editing: boolean; } @live("TodoItemComponent") // this is the Ruby class name export class TodoItemComponent extends LiveController<TodoItemComponentProps> { edit() { this.render((component) => { component.props.editing = true; }); } }
import { live, LiveController } from "@camertron/live-component"; type TodoItemComponentProps = { editing: boolean; } @live("TodoItemComponent") // this is the Ruby class name export class TodoItemComponent extends LiveController<TodoItemComponentProps> { edit() { this.render((component) => { component.props.editing = true; }); } }
{ "ruby_class": "TodoItemComponent", "props": { "__lc_attributes": { "data-id": "d7fd8295-1f62-4b2f-99cb-178d86749e10", "_lc_symkeys": [] }, "editing": true }, "slots": {}, "children": {}, "content": null }
{ "ruby_class": "TodoItemComponent", "props": { "__lc_attributes": { "data-id": "d7fd8295-1f62-4b2f-99cb-178d86749e10", "_lc_symkeys": [] }, "editing": true }, "slots": {}, "children": {}, "content": null }
class TodoItemComponent < ApplicationComponent include LiveComponent::Base def initialize(todo_item:, editing: false, start_time: nil, elapsed_time: nil) @todo_item = todo_item @editing = editing @start_time = start_time if @editing && @start_time.nil? @start_time = Time.now elsif !@editing && @start_time.present? @elapsed_time = Time.now - @start_time @start_time = nil end end private def label_text if @start_time "Editing..." elsif @elapsed_time "User spent #{@elapsed_time} seconds editing" else "Never edited" end end def button_text @start_time ? "Stop editing" : "Edit" end end
class TodoItemComponent < ApplicationComponent include LiveComponent::Base def initialize(todo_item:, editing: false, start_time: nil, elapsed_time: nil) @todo_item = todo_item @editing = editing @start_time = start_time if @editing && @start_time.nil? @start_time = Time.now elsif !@editing && @start_time.present? @elapsed_time = Time.now - @start_time @start_time = nil end end private def label_text if @start_time "Editing..." elsif @elapsed_time "User spent #{@elapsed_time} seconds editing" else "Never edited" end end def button_text @start_time ? "Stop editing" : "Edit" end end
<div><%= label_text %></div> <button data-action="click->timercomponent#on_click"> <%= button_text %> </button>
<div><%= label_text %></div> <button data-action="click->timercomponent#on_click"> <%= button_text %> </button>
import { live, LiveController } from "@camertron/live-component"; type TimerProps = { editing: boolean } @live("TimerComponent") export class TimerComponent extends LiveController<TimerProps> { on_click() { this.render((component) => { component.props.editing = !component.props.editing; }); } }
import { live, LiveController } from "@camertron/live-component"; type TimerProps = { editing: boolean } @live("TimerComponent") export class TimerComponent extends LiveController<TimerProps> { on_click() { this.render((component) => { component.props.editing = !component.props.editing; }); } }