clapton 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ce2ec441c0e14b8a234cdb853bc55619bc4dc082f6b9a2fb790afef8348eaf0
4
- data.tar.gz: 0c26377b8f1a160a87ce90ee12de22c11a59b49caffc4256765c4c7e79172897
3
+ metadata.gz: 01e0976bdfdb23e447884dec93ee2f25ceb2d5e0312ca4a54aeed3ba603a69c2
4
+ data.tar.gz: e950680f7a0928e186eb14ae04d1479e741e55670ec16ee97a6cc4b799edeb0e
5
5
  SHA512:
6
- metadata.gz: 36664ec11be13ad14254e1fcd0b71181f670f5e6dabcb1a9a2aa58cd9ca06d8edb76a0d63783c5da9c0db56a7dbda8d302438a9db5e3b6b2c57d9b4b633a0bf1
7
- data.tar.gz: f6ab3acf8c0dc683ad42088013cd1e6394892a658a86c176e86a0f92eda88aeca8d9d0db3042c37d7e5d8fa2d8296097bc39a7fa5c8bbc0e15cb352e70c493ff
6
+ metadata.gz: d3ed3bd257263eb9875acb301c798d18bed14700cbed7352b8ac55187e320f8a49a555a6fbbd90e3db6980c9b4572ed62371a7d3f16af7b60a858a1ab02102de
7
+ data.tar.gz: d89a048e4c2df53e1fb35c01063c6f59e3eef7f3737907b8523c8c919ad2eb84fc7cce63f138455ecf6d29650df12a387d64e7e348fedfffdc47760cf6b14393
data/README.md CHANGED
@@ -137,6 +137,12 @@ mount Clapton::Engine => "/clapton"
137
137
 
138
138
  ![TODO APP DEMO](./docs/todo-app-demo.gif)
139
139
 
140
+ ### Component rendering
141
+
142
+ ```html
143
+ <%= clapton_component_tag([:TaskListComponent, { tasks: @tasks.map { |task| { id: task.id, title: task.title, due: task.due, done: task.done } }]) %>
144
+ ```
145
+
140
146
  ### Generate Component and State
141
147
 
142
148
  ```bash
@@ -17,5 +17,15 @@ module Clapton
17
17
  end
18
18
  tag.div(id: "clapton", data: { clapton: datas })
19
19
  end
20
+
21
+ def clapton_component_tag(component)
22
+ state_class = component[0].to_s.gsub("Component", "State")
23
+ if Object.const_defined?(state_class)
24
+ data = { component: component[0].to_s, state: Object.const_get(state_class).new(component[1]).to_h }
25
+ else
26
+ data = { component: component[0].to_s, state: {} }
27
+ end
28
+ tag.div(class: "clapton-component", data: { clapton: data })
29
+ end
20
30
  end
21
31
  end
@@ -1393,15 +1393,19 @@ const initializeInputs$1 = () => {
1393
1393
 
1394
1394
  const initializeComponents = () => {
1395
1395
  const components = document.querySelector("#clapton")?.getAttribute("data-clapton") || "[]";
1396
- JSON.parse(components).forEach(createAndAppendComponent);
1396
+ JSON.parse(components).forEach((component) => createAndAppendComponent(component, document.querySelector("#clapton")));
1397
+ document.querySelectorAll(".clapton-component").forEach((element) => {
1398
+ const component = JSON.parse(element.getAttribute("data-clapton") || "{}");
1399
+ createAndAppendComponent(component, element);
1400
+ });
1397
1401
  };
1398
- const createAndAppendComponent = (component) => {
1402
+ const createAndAppendComponent = (component, element) => {
1399
1403
  const componentDom = document.createElement('div');
1400
1404
  const instance = new window[component.component](component.state);
1401
1405
  componentDom.innerHTML = instance.render;
1402
1406
  const firstChild = componentDom.firstChild;
1403
1407
  if (firstChild) {
1404
- document.querySelector("#clapton")?.appendChild(firstChild);
1408
+ element.appendChild(firstChild);
1405
1409
  }
1406
1410
  };
1407
1411
  document.addEventListener("DOMContentLoaded", () => {
@@ -17,16 +17,20 @@ interface ComponentInstance {
17
17
 
18
18
  const initializeComponents = () => {
19
19
  const components = document.querySelector("#clapton")?.getAttribute("data-clapton") || "[]";
20
- JSON.parse(components).forEach(createAndAppendComponent);
20
+ JSON.parse(components).forEach((component: ComponentDefinition) => createAndAppendComponent(component, document.querySelector("#clapton")!));
21
+ document.querySelectorAll(".clapton-component").forEach((element) => {
22
+ const component = JSON.parse(element.getAttribute("data-clapton") || "{}");
23
+ createAndAppendComponent(component, element as HTMLElement);
24
+ });
21
25
  };
22
26
 
23
- const createAndAppendComponent = (component: ComponentDefinition) => {
27
+ const createAndAppendComponent = (component: ComponentDefinition, element: HTMLElement) => {
24
28
  const componentDom = document.createElement('div');
25
29
  const instance = new (window[component.component as any] as any)(component.state);
26
30
  componentDom.innerHTML = instance.render;
27
31
  const firstChild = componentDom.firstChild as HTMLElement;
28
32
  if (firstChild) {
29
- document.querySelector("#clapton")?.appendChild(firstChild);
33
+ element.appendChild(firstChild);
30
34
  }
31
35
  };
32
36
 
@@ -1,3 +1,3 @@
1
1
  module Clapton
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clapton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moeki Kawakami