Skip to content

Commit e9c5348

Browse files
committed
update bmi example
1 parent 450ba99 commit e9c5348

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

examples/bmi-calc/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/bmi-calc/app.tsx

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import * as React from 'react';
22
import { render } from 'react-dom';
3-
import * as RX from '../../src/xs/rx'
4-
import X from '../../src/x'
5-
import { pure, lift2 } from '../../src/fantasy'
6-
import { Actions } from '../../src/interfaces'
7-
interface Change extends Event {
8-
type: string
9-
target: HTMLInputElement
10-
}
11-
type Intent = Change
3+
import * as RX from '../../lib/xs/rx'
4+
import { X, xinput, lift2, Actions } from '../..'
5+
6+
// Types
7+
interface Intent extends Event { }
8+
129
interface BMIState {
1310
value: number
11+
weight: number
12+
height: number
1413
bmi: string
1514
health: string
1615
}
16+
1717
interface BMIProps<I> extends BMIState {
1818
actions: Actions<I>;
1919
}
20+
21+
// View
2022
const View: React.SFC<BMIProps<Intent>> = props => (
2123
<div>
2224
<label>
@@ -34,24 +36,17 @@ const View: React.SFC<BMIProps<Intent>> = props => (
3436

3537
View.defaultProps = { bmi: '', health: '' }
3638

39+
// Plan
40+
const weightx = xinput<'number', RX.URI, Intent, BMIState>('weight')
3741

38-
const planx = (name: string) => (intent$) => {
39-
return {
40-
update$: intent$.filter(i => i.type == 'change' && i.target.name == name)
41-
.map(i => parseFloat(i.target.value))
42-
.map(value => state => ({ value }))
43-
}
44-
}
45-
const weightx = pure<RX.URI, Intent, BMIState>(planx('weight'))
46-
47-
const heightx = pure<RX.URI, Intent, BMIState>(planx('height'))
42+
const heightx = xinput<'number', RX.URI, Intent, BMIState>('height')
4843

4944
const BMIx = lift2<RX.URI, Intent, BMIState>(
5045
(s1, s2) => {
5146
let bmi = 0
5247
let health = '...'
53-
if (s1.value && s2.value) {
54-
bmi = s1.value / (s2.value * s2.value)
48+
if (s1.weight && s2.height) {
49+
bmi = s1.weight / (s2.height * s2.height)
5550
}
5651
if (bmi < 18.5) health = 'underweight'
5752
else if (bmi < 24.9) health = 'normal'
@@ -66,8 +61,8 @@ const BMI = BMIx.apply(View)
6661
// BMIx is compose from weightx and heightx
6762

6863
// while weightx and heightx can be still use to create another component
69-
const Weight = weightx.apply(props => props.value ? <p>where height is {props.value} kg</p> : null)
70-
const Height = heightx.apply(props => props.value ? <p>and {props.value}m height</p> : null)
64+
const Weight = weightx.apply(props => props.weight ? <p>where weight is {props.weight} kg</p> : null)
65+
const Height = heightx.apply(props => props.height ? <p>and {props.height}m height</p> : null)
7166
render(
7267
<X x={RX}>
7368
<form>

0 commit comments

Comments
 (0)