Skip to content

Commit

Permalink
#17 - Implement isPristine(path) and isDirty(path)
Browse files Browse the repository at this point in the history
  • Loading branch information
hnordt committed Sep 20, 2016
1 parent 5bf1d81 commit 32b520f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Form.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component, PropTypes } from 'react'
import isEqual from 'lodash/isEqual'
import get from 'lodash/get'
import cloneDeep from 'lodash/cloneDeep'
import set from 'lodash/set'
Expand All @@ -13,16 +12,24 @@ export default class Form extends Component {

static defaultProps = { initialValues: {} }

state = { values: this.props.initialValues }
state = { values: this.props.initialValues, touches: [] }

isPristine = () => isEqual(this.state.values, this.props.initialValues)
isPristine = path => {
if (path) {
return !this.state.touches.find(touch => touch === path)
}
return !this.state.touches.length
}

isDirty = () => !this.isPristine()
isDirty = path => !this.isPristine(path)

getValue = path => get(this.state.values, path, '')

setValue = (path, value) => {
this.setState({ values: set(cloneDeep(this.state.values), path, value) })
this.setState({
values: set(cloneDeep(this.state.values), path, value),
touches: this.isPristine(path) ? [...this.state.touches, path] : this.state.touches
})
}

handleSubmit = event => {
Expand All @@ -32,7 +39,7 @@ export default class Form extends Component {
}
}

reset = () => this.setState({ values: this.props.initialValues })
reset = () => this.setState({ values: this.props.initialValues, touches: [] })

render() {
// eslint-disable-next-line
Expand Down

0 comments on commit 32b520f

Please sign in to comment.