Skip to content

Commit 78227f9

Browse files
committed
feat(effects): provide linkState and toggleState
The issue with this implementation is that these effects shadow each others, maybe they should simply be exported and explicitly added by users…
1 parent 49f7006 commit 78227f9

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/factory.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ const makeSpy = (keys, accessor) => {
3232
return [create(null, descriptors), spy]
3333
}
3434

35+
const defaultEffects = {
36+
linkState: (_, { target }) => state => ({
37+
...state,
38+
[target.name]: target.nodeName.toLowerCase() === 'input' &&
39+
target.type.toLowerCase() === 'checkbox'
40+
? target.checked
41+
: target.value,
42+
}),
43+
toggleState: (_, { target: { name } }) => state => ({ ...state, [name]: !state[name] }),
44+
}
45+
3546
module.exports = ({ Component, createElement, PropTypes }) => {
3647
const contextTypes_ = {
3748
[TAG]:
@@ -143,6 +154,7 @@ module.exports = ({ Component, createElement, PropTypes }) => {
143154
let state
144155
if (initialState !== undefined) {
145156
state = initialState(props)
157+
effects = { ...defaultEffects, ...effects }
146158

147159
keys(state).forEach(k => {
148160
if (!(k in stateDescriptors)) {

0 commit comments

Comments
 (0)