Sha256: 494b970184dfde7a6cd0f37dae9c139d6702c445126e7b81fb0702b97af5f273

Contents?: true

Size: 837 Bytes

Versions: 2

Compression:

Stored size: 837 Bytes

Contents

require 'react'

class TodoList < React::Component
  def render
    ul(
      props[:items].map { |item|
        li({key: item[:id]}, [ item[:text] ])
      }
    )
  end
end

class TodoApp < React::Component

  def initial_state
    { items: [], text: '' }
  end

  def onChange(e)
    set_state({text: e.target.value})
  end

  def handleSubmit(e)
    e.preventDefault()
    now = Time.now
    nextItems = state[:items] + [{ text: state[:text], id: now.to_i * 1e6 + now.usec }]
    nextText = ''
    set_state({items: nextItems, text: nextText})
  end

  def render
    div([
      h3('TODO'),
      TodoList(items: state[:items]),
      form({onSubmit: method(:handleSubmit)}, [
        input(onChange: method(:onChange), value: state[:text]),
        button("Add ##{state[:items].count+1}")
      ])
    ])
  end

end

TodoApp.run()

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opal-react-0.0.4 examples/todoapp/application.rb
opal-react-0.0.3 examples/todoapp/application.rb