From 9f12b30d1c7c262ee3c1d13e4f9ae071e0ec9fbd Mon Sep 17 00:00:00 2001 From: Robert Kuykendall Date: Thu, 9 Nov 2017 23:40:06 -0500 Subject: [PATCH] Fix eslint --- src/Wrapper.js | 48 +++++++++++++------------- src/index.js | 92 +++++++++++++++++++++++++------------------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/Wrapper.js b/src/Wrapper.js index e9d95be9..cc0c9475 100644 --- a/src/Wrapper.js +++ b/src/Wrapper.js @@ -127,6 +127,30 @@ export default (Component) => { return this.state.value; } + setValidations(validations, required) { + // Add validations to the store itself as the props object can not be modified + this.validations = convertValidationsToObject(validations) || {}; + this.requiredValidations = required === true ? { isDefaultRequiredValue: true } : + convertValidationsToObject(required); + } + + // By default, we validate after the value has been set. + // A user can override this and pass a second parameter of `false` to skip validation. + setValue(value, validate = true) { + if (!validate) { + this.setState({ + value, + }); + } else { + this.setState({ + value, + isPristine: false, + }, () => { + this.context.formsy.validate(this); + }); + } + } + hasValue() { return this.state.value !== ''; } @@ -165,30 +189,6 @@ export default (Component) => { }); } - setValidations(validations, required) { - // Add validations to the store itself as the props object can not be modified - this.validations = convertValidationsToObject(validations) || {}; - this.requiredValidations = required === true ? { isDefaultRequiredValue: true } : - convertValidationsToObject(required); - } - - // By default, we validate after the value has been set. - // A user can override this and pass a second parameter of `false` to skip validation. - setValue(value, validate = true) { - if (!validate) { - this.setState({ - value, - }); - } else { - this.setState({ - value, - isPristine: false, - }, () => { - this.context.formsy.validate(this); - }); - } - } - showError() { return !this.showRequired() && !this.isValid(); } diff --git a/src/index.js b/src/index.js index df219008..567835cb 100644 --- a/src/index.js +++ b/src/index.js @@ -63,28 +63,6 @@ class Formsy extends React.Component { } } - // Method put on each input component to register - // itself to the form - attachToForm(component) { - if (this.inputs.indexOf(component) === -1) { - this.inputs.push(component); - } - - this.validate(component); - } - - // Method put on each input component to unregister - // itself from the form - detachFromForm(component) { - const componentPos = this.inputs.indexOf(component); - - if (componentPos !== -1) { - this.inputs = this.inputs.slice(0, componentPos).concat(this.inputs.slice(componentPos + 1)); - } - - this.validateForm(); - } - getCurrentValues() { return this.inputs.reduce((data, component) => { const name = component.props.name; @@ -108,9 +86,30 @@ class Formsy extends React.Component { }, {}); } - // Checks if the values have changed from their initial value - isChanged() { - return !utils.isSame(this.getPristineValues(), this.getCurrentValues()); + setFormPristine(isPristine) { + this.setState({ + formSubmitted: !isPristine, + }); + + // Iterate through each component and set it as pristine + // or "dirty". + this.inputs.forEach((component) => { + component.setState({ + formSubmitted: !isPristine, + isPristine, + }); + }); + } + + setInputValidationErrors(errors) { + this.inputs.forEach((component) => { + const name = component.props.name; + const args = [{ + isValid: !(name in errors), + validationError: typeof errors[name] === 'string' ? [errors[name]] : errors[name], + }]; + component.setState(...args); + }); } isFormDisabled() { @@ -210,30 +209,31 @@ class Formsy extends React.Component { }; } - setInputValidationErrors(errors) { - this.inputs.forEach((component) => { - const name = component.props.name; - const args = [{ - isValid: !(name in errors), - validationError: typeof errors[name] === 'string' ? [errors[name]] : errors[name], - }]; - component.setState(...args); - }); + // Method put on each input component to register + // itself to the form + attachToForm(component) { + if (this.inputs.indexOf(component) === -1) { + this.inputs.push(component); + } + + this.validate(component); } - setFormPristine(isPristine) { - this.setState({ - formSubmitted: !isPristine, - }); + // Method put on each input component to unregister + // itself from the form + detachFromForm(component) { + const componentPos = this.inputs.indexOf(component); - // Iterate through each component and set it as pristine - // or "dirty". - this.inputs.forEach((component) => { - component.setState({ - formSubmitted: !isPristine, - isPristine, - }); - }); + if (componentPos !== -1) { + this.inputs = this.inputs.slice(0, componentPos).concat(this.inputs.slice(componentPos + 1)); + } + + this.validateForm(); + } + + // Checks if the values have changed from their initial value + isChanged() { + return !utils.isSame(this.getPristineValues(), this.getCurrentValues()); } // Update model, submit to url prop and send the model