-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOnChange.js
50 lines (42 loc) · 988 Bytes
/
OnChange.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @flow
import * as React from 'react'
import { Field } from 'react-final-form'
import type { OnChangeProps } from './types'
type Props = {
children: (value: any, previous: any) => void,
input: {
value: any
}
}
type State = {
previous: any
}
class OnChangeState extends React.Component<Props, State> {
props: Props
state: State
constructor(props: Props) {
super(props)
this.state = {
previous: props.input.value
}
}
componentDidUpdate() {
const { children, input: { value } } = this.props
const { previous } = this.state
if (value !== previous) {
this.setState({ previous: value })
children(value, previous)
}
}
render() {
return null
}
}
const OnChange = ({ name, children }: OnChangeProps) =>
React.createElement(Field, {
name,
subscription: { value: true },
allowNull: true,
render: props => React.createElement(OnChangeState, { ...props, children })
})
export default OnChange