Skip to content

Commit 83320f4

Browse files
authored
Fix bug where OnFocus was only getting called once (#3)
* Fix bug where OnFocus was only getting called once * Migrated from cWU to cDU
1 parent 3e382c8 commit 83320f4

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

src/ExternallyChanged.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ExternallyChangedState extends React.Component<Props, State> {
3030
}
3131
}
3232

33-
componentWillReceiveProps(nextProps: Props) {
34-
const { input: { value }, meta: { active } } = nextProps
33+
componentDidUpdate() {
34+
const { input: { value }, meta: { active } } = this.props
3535
const { previous } = this.state
3636
if (value !== previous) {
3737
this.setState({ previous: value, externallyChanged: !active })

src/ExternallyChanged.test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ describe('ExternallyChanged', () => {
3636
expect(spy.mock.calls[0][0]).toBe(false)
3737
focus('foo')
3838
expect(spy).toHaveBeenCalledTimes(2)
39-
expect(spy.mock.calls[1][0]).toBe(false)
4039
change('foo', 'bar')
41-
expect(spy).toHaveBeenCalledTimes(3)
42-
expect(spy.mock.calls[2][0]).toBe(false)
40+
expect(spy).toHaveBeenCalledTimes(4)
41+
expect(spy.mock.calls[3][0]).toBe(false)
4342
})
4443

4544
it('should change to true when value changes while not active', () => {
@@ -57,8 +56,8 @@ describe('ExternallyChanged', () => {
5756
expect(spy).toHaveBeenCalledTimes(1)
5857
expect(spy.mock.calls[0][0]).toBe(false)
5958
change('foo', 'bar')
60-
expect(spy).toHaveBeenCalledTimes(2)
61-
expect(spy.mock.calls[1][0]).toBe(true)
59+
expect(spy).toHaveBeenCalledTimes(3)
60+
expect(spy.mock.calls[2][0]).toBe(true)
6261
})
6362

6463
it('should go back to false when field modified while active', () => {
@@ -78,13 +77,13 @@ describe('ExternallyChanged', () => {
7877
expect(spy).toHaveBeenCalledTimes(1)
7978
expect(spy.mock.calls[0][0]).toBe(false)
8079
change('foo', 'bar')
81-
expect(spy).toHaveBeenCalledTimes(2)
82-
expect(spy.mock.calls[1][0]).toBe(true)
83-
focus('foo')
8480
expect(spy).toHaveBeenCalledTimes(3)
8581
expect(spy.mock.calls[2][0]).toBe(true)
86-
change('foo', 'baz')
82+
focus('foo')
8783
expect(spy).toHaveBeenCalledTimes(4)
88-
expect(spy.mock.calls[3][0]).toBe(false)
84+
expect(spy.mock.calls[3][0]).toBe(true)
85+
change('foo', 'baz')
86+
expect(spy).toHaveBeenCalledTimes(6)
87+
expect(spy.mock.calls[5][0]).toBe(false)
8988
})
9089
})

src/OnBlur.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class OnBlurState extends React.Component<Props, State> {
2525
}
2626
}
2727

28-
componentWillReceiveProps(nextProps: Props) {
29-
const { children, meta: { active } } = nextProps
28+
componentDidUpdate() {
29+
const { children, meta: { active } } = this.props
3030
const { previous } = this.state
3131
if (previous && !active) {
3232
children()

src/OnChange.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class OnChangeState extends React.Component<Props, State> {
2525
}
2626
}
2727

28-
componentWillReceiveProps(nextProps: Props) {
29-
const { children, input: { value } } = nextProps
28+
componentDidUpdate() {
29+
const { children, input: { value } } = this.props
3030
const { previous } = this.state
3131
if (value !== previous) {
3232
this.setState({ previous: value })

src/OnFocus.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class OnFocusState extends React.Component<Props, State> {
2525
}
2626
}
2727

28-
componentWillReceiveProps(nextProps: Props) {
29-
const { children, meta: { active } } = nextProps
28+
componentDidUpdate() {
29+
const { children, meta: { active } } = this.props
3030
const { previous } = this.state
3131
if (active && !previous) {
3232
this.setState({ previous: active })
3333
children()
34+
} else if (!active && previous) {
35+
this.setState({ previous: active })
3436
}
3537
}
3638

src/OnFocus.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,30 @@ describe('OnFocus', () => {
5555
blur('foo')
5656
expect(spy).toHaveBeenCalledTimes(1)
5757
})
58+
59+
it('should call listener on subsequent focuses', () => {
60+
const spy = jest.fn()
61+
let blur
62+
let focus
63+
TestUtils.renderIntoDocument(
64+
<Form onSubmit={onSubmitMock}>
65+
{props => {
66+
blur = props.blur
67+
focus = props.focus
68+
return <OnFocus name="foo">{spy}</OnFocus>
69+
}}
70+
</Form>
71+
)
72+
expect(spy).not.toHaveBeenCalled()
73+
focus('foo')
74+
expect(spy).toHaveBeenCalled()
75+
expect(spy).toHaveBeenCalledTimes(1)
76+
expect(spy).toHaveBeenCalledWith()
77+
78+
blur('foo')
79+
expect(spy).toHaveBeenCalledTimes(1)
80+
81+
focus('foo')
82+
expect(spy).toHaveBeenCalledTimes(2)
83+
})
5884
})

0 commit comments

Comments
 (0)