Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
1,818 additions
and 1,102 deletions.
- +2 −2 README.md
- +3 −0 __tests__/__snapshots__/unstated.js.snap
- +80 −0 __tests__/unstated.js
- +0 −11 example/complex.html
- +3 −5 example/complex.js
- +7 −5 example/index.html
- +0 −11 example/simple.html
- +4 −6 example/simple.js
- +591 −0 flow-typed/npm/jest_v22.x.x.js
- +7 −4 package.json
- +21 −12 src/{index.js → unstated.js}
- +0 −4 test.js
- +1,100 −1,042 yarn.lock
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`basic 1`] = `<div />`; |
@@ -0,0 +1,80 @@ | ||
// @flow | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { Provide, Subscribe, Container } from '../src/unstated'; | ||
|
||
function render(element) { | ||
return renderer.create(element).toJSON(); | ||
} | ||
|
||
class CounterContainer extends Container<{ count: number }> { | ||
state = { count: 0 }; | ||
increment(amount = 1) { | ||
this.setState({ count: this.state.count + amount }); | ||
} | ||
decrement(amount = 1) { | ||
this.setState({ count: this.state.count - amount }); | ||
} | ||
} | ||
|
||
class AmounterContainer extends Container<{ amount: number }> { | ||
state = { amount: 1 }; | ||
setAmount(amount) { | ||
this.setState({ amount }); | ||
} | ||
} | ||
|
||
function Counter() { | ||
return ( | ||
<Subscribe to={[CounterContainer]}> | ||
{counter => ( | ||
<div> | ||
<span>{counter.state.count}</span> | ||
<button onClick={() => counter.decrement()}>-</button> | ||
<button onClick={() => counter.increment()}>+</button> | ||
</div> | ||
)} | ||
</Subscribe> | ||
); | ||
} | ||
|
||
function CounterWithAmount() { | ||
return ( | ||
<Subscribe to={[CounterContainer, AmounterContainer]}> | ||
{(counter, amounter) => ( | ||
<div> | ||
<span>{counter.state.count}</span> | ||
<button onClick={() => counter.decrement(amounter.state.amount)}> | ||
- | ||
</button> | ||
<button onClick={() => counter.increment(amounter.state.amount)}> | ||
+ | ||
</button> | ||
</div> | ||
)} | ||
</Subscribe> | ||
); | ||
} | ||
|
||
function CounterWithAmountApp() { | ||
return ( | ||
<Subscribe to={[AmounterContainer]}> | ||
{amounter => ( | ||
<div> | ||
<Counter /> | ||
<input | ||
type="number" | ||
value={amounter.state.amount} | ||
onChange={event => { | ||
amounter.setAmount(parseInt(event.currentTarget.value, 10)); | ||
}} | ||
/> | ||
</div> | ||
)} | ||
</Subscribe> | ||
); | ||
} | ||
|
||
test('basic', () => { | ||
// still too lazy | ||
}); |
Oops, something went wrong.