@realglebivanov/reactive 1.0.3 → 1.0.4

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.
Files changed (2) hide show
  1. package/README.md +26 -20
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -12,7 +12,9 @@ import {
12
12
  mapObservable,
13
13
  router,
14
14
  tags,
15
- dedupObservable
15
+ dedupObservable,
16
+ tag,
17
+ once
16
18
  } from "@realglebivanov/reactive";
17
19
 
18
20
  import { ReactiveArray } from "@realglebivanov/reactive";
@@ -43,7 +45,7 @@ const counter = () => component({
43
45
  imageSource$: mapObservable(
44
46
  (hard) => hard ? "KashaHard.gif" : "Kasha.png",
45
47
  dedupObservable(hard$)),
46
- hexCounter$: mapObservable((x) => x.toString(2), count$)
48
+ hexCounter$: mapObservable((x) => x.toString(16), count$)
47
49
  }),
48
50
  render: function ({ count$, hard$, veryHard$, imageSource$, hexCounter$ }) {
49
51
  const onClick = () => {
@@ -65,22 +67,27 @@ const counter = () => component({
65
67
  });
66
68
 
67
69
  const shoppingForm = () => component({
68
- render: () => div(
69
- div(span('Name: '), input('text').att('id', 'itemName')),
70
- div(span('Price: '), input('text').att('id', 'itemPrice')),
71
- button(span('Add')).clk(() => {
72
- const itemName = document.getElementById('itemName') as HTMLInputElement;
73
- const itemPrice = document.getElementById('itemPrice') as HTMLInputElement;
74
-
75
- if (itemName.value == "" || itemPrice.value == "") return;
76
-
77
- shoppingItems.push({
78
- name: itemName.value,
79
- price$: observable(itemPrice.value)
80
- });
81
-
82
- itemName.value = "";
83
- itemPrice.value = "";
70
+ observables: () => ({
71
+ name$: observable(''),
72
+ price$: observable(''),
73
+ }),
74
+ derivedObservables: ({ name$, price$ }) => ({
75
+ formInvalid$:
76
+ mapObservable((name, price) => !!!name || !!!price, name$, price$)
77
+ }),
78
+ render: ({ name$, price$, formInvalid$ }) => div(
79
+ cond({
80
+ if$: formInvalid$,
81
+ otherwise: () => div(tag('h4', template`Pending item: ${name$} : ${price$}`))
82
+ }),
83
+ div(span('Name: '), input('text').att('id', 'itemName').model$(name$)),
84
+ div(span('Price: '), input('text').att('id', 'itemPrice').model$(price$)),
85
+ button(span('Add')).prop$('disabled', formInvalid$).clk(() => {
86
+ once((name, price) => {
87
+ shoppingItems.push({ name, price$: observable(price) });
88
+ name$.update((_) => "");
89
+ price$.update((_) => "");
90
+ }, name$, price$);
84
91
  })
85
92
  )
86
93
  });
@@ -101,7 +108,7 @@ const shoppingList = () => component({
101
108
 
102
109
  const exampleRouter = router({
103
110
  "/": div(
104
- h1("Grecha.js"),
111
+ h1("Reactive"),
105
112
  div(a("Foo").att("href", "#/foo")),
106
113
  div(a("Bar").att("href", "#/bar")),
107
114
  counter(),
@@ -136,7 +143,6 @@ function numberToHexColor(number: number) {
136
143
 
137
144
  exampleRouter.mount(document.getElementById('entry')!);
138
145
  exampleRouter.activate();
139
-
140
146
  ```
141
147
  ## Restrictions & Edge Cases
142
148
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realglebivanov/reactive",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "A simple and permissive observable-driven UI toolkit",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",