Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #476 from acdlite/fix-withStateHandlers
Browse files Browse the repository at this point in the history
Allow to use withStateHandlers with SyntheticEvents
  • Loading branch information
wuct committed Aug 16, 2017
2 parents 4b37008 + 5f1b612 commit b36f152
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/packages/recompose/__tests__/withStateHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@ import sinon from 'sinon'

import { compose, withStateHandlers } from '../'

test('withStateHandlers should persist events passed as argument', () => {
const component = ({ value, onChange }) =>
<div>
<input type="text" value={value} onChange={onChange} />
<p>
{value}
</p>
</div>

const InputComponent = withStateHandlers(
{ value: '' },
{
onChange: () => e => ({
value: e.target.value,
}),
}
)(component)

const wrapper = mount(<InputComponent />)
const input = wrapper.find('input')
const output = wrapper.find('p')
// having that enzyme simulate does not simulate real situation
// emulate persist
input.simulate('change', {
persist() {
this.target = { value: 'Yay' }
},
})
expect(output.text()).toBe('Yay')

input.simulate('change', { target: { value: 'empty' } })
expect(output.text()).toBe('empty')
})

test('withStateHandlers adds a stateful value and a function for updating it', () => {
const component = sinon.spy(() => null)
component.displayName = 'component'
Expand Down
15 changes: 13 additions & 2 deletions src/packages/recompose/withStateHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ const withStateHandlers = (initialState, stateUpdaters) => BaseComponent => {
? initialState(this.props)
: initialState

stateUpdaters = mapValues(stateUpdaters, handler => (...args) =>
this.setState((state, props) => handler(state, props)(...args))
stateUpdaters = mapValues(
stateUpdaters,
handler => (mayBeEvent, ...args) => {
// Having that functional form of setState can be called async
// we need to persist SyntheticEvent
if (mayBeEvent && typeof mayBeEvent.persist === 'function') {
mayBeEvent.persist()
}

this.setState((state, props) =>
handler(state, props)(mayBeEvent, ...args)
)
}
)

shouldComponentUpdate(nextProps, nextState) {
Expand Down

0 comments on commit b36f152

Please sign in to comment.