-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathExternallyChanged.js
55 lines (47 loc) · 1.19 KB
/
ExternallyChanged.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
51
52
53
54
55
// @flow
import * as React from 'react'
import { Field } from 'react-final-form'
import type { ExternallyChangedProps } from './types'
type Props = {
children: (externallyChanged: boolean) => React.Node,
input: {
value: any
},
meta: {
active?: boolean
}
}
type State = {
previous: any,
externallyChanged: boolean
}
class ExternallyChangedState extends React.Component<Props, State> {
props: Props
state: State
constructor(props: Props) {
super(props)
this.state = {
previous: props.input.value,
externallyChanged: false
}
}
componentDidUpdate() {
const { input: { value }, meta: { active } } = this.props
const { previous } = this.state
if (value !== previous) {
this.setState({ previous: value, externallyChanged: !active })
}
}
render() {
return this.props.children(this.state.externallyChanged)
}
}
const ExternallyChanged = ({ name, children }: ExternallyChangedProps) =>
React.createElement(Field, {
name,
subscription: { active: true, value: true },
allowNull: true,
render: props =>
React.createElement(ExternallyChangedState, { ...props, children })
})
export default ExternallyChanged