From a7b9a08a86e7911b76d11ecbc84120a574fe2b8f Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Tue, 17 Apr 2018 19:34:55 +0300 Subject: [PATCH] PayoutDetailsForm: clear address stuff onUnmount --- .../FieldTextInput/FieldTextInput.js | 30 +++++++++++-------- .../PayoutDetailsForm/PayoutDetailsForm.js | 7 +++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/components/FieldTextInput/FieldTextInput.js b/src/components/FieldTextInput/FieldTextInput.js index bef47687..f0b4f6f6 100644 --- a/src/components/FieldTextInput/FieldTextInput.js +++ b/src/components/FieldTextInput/FieldTextInput.js @@ -3,7 +3,7 @@ */ import React, { Component } from 'react'; -import PropTypes from 'prop-types'; +import { func, object, shape, string } from 'prop-types'; import { Field } from 'react-final-form'; import classNames from 'classnames'; import { ValidationError, ExpandingTextarea } from '../../components'; @@ -13,24 +13,19 @@ import css from './FieldTextInput.css'; const CONTENT_MAX_LENGTH = 5000; class FieldTextInputComponent extends Component { - componentWillUnmount() { - if (this.props.clearOnUnmount) { - this.props.input.onChange(''); - } - } render() { /* eslint-disable no-unused-vars */ const { rootClassName, className, inputRootClass, - clearOnUnmount, customErrorText, id, label, type, input, meta, + onUnmount, ...rest } = this.props; /* eslint-enable no-unused-vars */ @@ -76,20 +71,18 @@ FieldTextInputComponent.defaultProps = { rootClassName: null, className: null, inputRootClass: null, - clearOnUnmount: false, + onUnmount: null, customErrorText: null, id: null, label: null, }; -const { string, bool, shape, func, object } = PropTypes; - FieldTextInputComponent.propTypes = { rootClassName: string, className: string, inputRootClass: string, - clearOnUnmount: bool, + onUnmount: func, // Error message that can be manually passed to input field, // overrides default validation message @@ -110,8 +103,19 @@ FieldTextInputComponent.propTypes = { meta: object.isRequired, }; -const FieldTextInput = props => { - return ; +class FieldTextInput extends Component { + componentWillUnmount() { + // Unmounting happens too late if it is done inside Field component + // (Then Form has already registered its (new) fields and + // changing the value without corresponding field is prohibited in Final Form + if (this.props.onUnmount) { + this.props.onUnmount(); + } + } + + render() { + return ; + } }; export default FieldTextInput; diff --git a/src/containers/PayoutDetailsForm/PayoutDetailsForm.js b/src/containers/PayoutDetailsForm/PayoutDetailsForm.js index f2c2559a..0f8da2fa 100644 --- a/src/containers/PayoutDetailsForm/PayoutDetailsForm.js +++ b/src/containers/PayoutDetailsForm/PayoutDetailsForm.js @@ -48,6 +48,7 @@ const PayoutDetailsFormComponent = props => ( const { className, createStripeAccountError, + change, disabled, handleSubmit, inProgress, @@ -248,7 +249,7 @@ const PayoutDetailsFormComponent = props => ( label={streetAddressLabel} placeholder={streetAddressPlaceholder} validate={streetAddressRequired} - clearOnUnmount + onUnmount={() => change('streetAddress', undefined)} />
( label={postalCodeLabel} placeholder={postalCodePlaceholder} validate={postalCodeRequired} - clearOnUnmount + onUnmount={() => change('postalCode', undefined)} /> ( label={cityLabel} placeholder={cityPlaceholder} validate={cityRequired} - clearOnUnmount + onUnmount={() => change('city', undefined)} />