-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a child -> parent -> child constraint example
all the communications...
- Loading branch information
Showing
5 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react'; | ||
import { Record } from 'immutable'; | ||
import { Union, Maybe } from 'results'; | ||
import { component, Update } from '../../spindle'; | ||
|
||
|
||
const Model = Record({ | ||
value: 0, | ||
min: -Infinity, | ||
max: Infinity, | ||
}); | ||
|
||
|
||
const Msg = Union({ | ||
Increment: null, | ||
Decrement: null, | ||
}); | ||
|
||
|
||
// this is just a helper, it's not a special funciton | ||
const constrain = model => { | ||
const { value, min, max } = model.toObject(); | ||
return model.set('value', Math.max(min, Math.min(max, value))); | ||
} | ||
|
||
|
||
const handleProps = ({ min = -Infinity, max = Infinity }, model) => | ||
Update({ model: constrain(model.merge({ min, max })) }); | ||
|
||
|
||
const update = (msg, model) => Msg.match(msg, { | ||
Increment: () => { | ||
const newModel = constrain(model.update('value', v => v + 1)); | ||
return Update({ | ||
model: newModel, | ||
emit: newModel.get('value'), | ||
}); | ||
}, | ||
|
||
Decrement: () => { | ||
const newModel = constrain(model.update('value', v => v - 1)); | ||
return Update({ | ||
model: newModel, | ||
emit: newModel.get('value'), | ||
}); | ||
}, | ||
}); | ||
|
||
|
||
const view = (model, BoundMsg) => ( | ||
<p> | ||
<button | ||
disabled={model.get('value') <= model.get('min')} | ||
onClick={BoundMsg.Decrement}> | ||
- | ||
</button> | ||
{model.get('value')} | ||
<button | ||
disabled={model.get('value') >= model.get('max')} | ||
onClick={BoundMsg.Increment}> | ||
+ | ||
</button> | ||
</p> | ||
); | ||
|
||
|
||
export default component('Counter', | ||
{ Model, Msg, handleProps, update, view }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { Record } from 'immutable'; | ||
import { Union } from 'results'; | ||
import { component, Update } from '../../spindle'; | ||
import Counter from './Counter'; | ||
|
||
|
||
const Model = Record({ | ||
min: 0, | ||
max: 0, | ||
}); | ||
|
||
|
||
const Msg = Union({ | ||
SetMin: null, | ||
SetMax: null, | ||
}); | ||
|
||
|
||
const update = (msg, model) => Msg.match(msg, { | ||
SetMin: v => | ||
Update({ model: model.set('min', v) }), | ||
|
||
SetMax: v => | ||
Update({ model: model.set('max', v) }), | ||
}); | ||
|
||
|
||
const view = (model, BoundMsg) => ( | ||
<div> | ||
<p>min</p> | ||
<Counter | ||
onEmit={BoundMsg.SetMin} | ||
max={model.get('max')} /> | ||
|
||
<p>val</p> | ||
<Counter | ||
min={model.get('min')} | ||
max={model.get('max')} /> | ||
|
||
<p>max</p> | ||
<Counter | ||
onEmit={BoundMsg.SetMax} | ||
min={model.get('min')} /> | ||
</div> | ||
); | ||
|
||
|
||
export default component('Parent', | ||
{ Model, Msg, update, view }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Parent from './Parent'; | ||
|
||
|
||
ReactDOM.render(<Parent />, document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters