Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve interface #36

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ To use a Clapton component in your view:
# app/components/task_list_component.rb
class TaskListComponent < Clapton::Component
def render
box = c.box
box = c(:box)
@state.tasks.each do |task|
box.add(TaskItemComponent.new(id: task[:id], title: task[:title], due: task[:due], done: task[:done]))
end
btn = c.button
btn.add(c.text("Add Task"))
btn = c(:button)
btn.add(c(:text, "Add Task"))
btn.add_action(:click, :TaskListState, :add_task)
box.add(btn)
end
Expand All @@ -51,15 +51,15 @@ end
# app/components/task_item_component.rb
class TaskItemComponent < Clapton::Component
def render
box = c.box
btn = c.button
btn.add(c.text(@state.done ? "✅" : "🟩"))
box = c(:box)
btn = c(:button)
btn.add(c(:text, @state.done ? "✅" : "🟩"))
btn.add_action(:click, :TaskListState, :toggle_done)

tf = c.input(@state, :title)
tf = c(:input, @state, :title)
tf.add_action(:input, :TaskListState, :update_title)

dt = c.datetime(@state, :due)
dt = c(:datetime, @state, :due)
dt.add_action(:input, :TaskListState, :update_due)

box.add(btn).add(tf).add(dt)
Expand Down Expand Up @@ -171,7 +171,7 @@ The `render` event is a special event that is triggered when the component is re
class TaskListComponent < Clapton::Component
def render
# ...
box = c.box
box = c(:box)
box.add_action(:render, :TaskListState, :add_empty_task, debounce: 500)
end
end
Expand Down Expand Up @@ -256,31 +256,31 @@ text = Clapton::Text.new("Hello")`
### Preset Component Methods

```javascript
c.bq(...props)
c.box(...props)
c.b(...props)
c.button(...props)
c.check(...props)
c.code(...props)
c.datetime(...props)
c.el(...props)
c.embed(...props)
c.em(...props)
c.form(...props)
c.h(...props)
c.img(...props)
c.a(...props)
c.li(...props)
c.ul(...props)
c.ol(...props)
c.p(...props)
c.q(...props)
c.radio(...props)
c.select(...props)
c.span(...props)
c.textarea(...props)
c.input(...props)
c.text(...props)
c(:bq, ...props)
c(:box, ...props)
c(:b, ...props)
c(:button, ...props)
c(:check, ...props)
c(:code, ...props)
c(:datetime, ...props)
c(:el, ...props)
c(:embed, ...props)
c(:em, ...props)
c(:form, ...props)
c(:h, ...props)
c(:img, ...props)
c(:a, ...props)
c(:li, ...props)
c(:ul, ...props)
c(:ol, ...props)
c(:p, ...props)
c(:q, ...props)
c(:radio, ...props)
c(:select, ...props)
c(:span, ...props)
c(:textarea, ...props)
c(:input, ...props)
c(:text, ...props)
```

### Streaming
Expand Down
1 change: 1 addition & 0 deletions app/helpers/clapton/clapton_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def clapton_javascript_tag
"imports": {
"client": "/clapton/client.js",
"components": "/clapton/components.js",
"c": "/clapton/c.js",
#{ all_components.map do
|component| "\"#{File.basename(component, ".rb").camelize}\": \"/clapton/#{File.basename(component, ".rb").camelize}.js\""
end.join(",\n") }
Expand Down
3 changes: 3 additions & 0 deletions lib/clapton/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Engine < ::Rails::Engine
FileUtils.mkdir_p(Rails.root.join("public", "clapton")) unless Rails.root.join("public", "clapton").exist?
File.write(Rails.root.join("public", "clapton", "components.js"), File.read(File.join(__dir__, "javascripts", "dist", "components.js")))
File.write(Rails.root.join("public", "clapton", "client.js"), File.read(File.join(__dir__, "javascripts", "dist", "client.js")))
File.write(Rails.root.join("public", "clapton", "c.js"), File.read(File.join(__dir__, "javascripts", "dist", "c.js")))

compile_components

Expand All @@ -44,6 +45,8 @@ def compile_components
js = ""
js += "import { Clapton } from 'components';"
js += "\n"
js += "import { c } from 'c';"
js += "\n"
code.scan(/(\w+)Component\.new/).each do |match|
js += "import { #{match[0]}Component } from '#{match[0]}Component';"
js += "\n"
Expand Down
Loading
Loading