From 243ed9d61b5ed036b812fa399982ec23f27d4cbb Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 6 Feb 2018 20:29:33 -0500 Subject: [PATCH] Prevents error when item prop is not set Allows setting custom value and defaultValue --- assets/js/src/form/fields/text.jsx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/assets/js/src/form/fields/text.jsx b/assets/js/src/form/fields/text.jsx index 2715c9ffbb..81f5bcd337 100644 --- a/assets/js/src/form/fields/text.jsx +++ b/assets/js/src/form/fields/text.jsx @@ -2,9 +2,19 @@ import React from 'react'; const FormFieldText = React.createClass({ render() { - let value = this.props.item[this.props.field.name]; - if (value === undefined) { - value = this.props.field.defaultValue || ''; + const item = this.props.item || {}; + let value; + let defaultValue; + // value should only be set when onChangeValue is configured + if (this.props.onValueChange instanceof Function) { + value = item[this.props.field.name]; + // set value to defaultValue if available + value = (value === undefined && this.props.field.defaultValue) ? + this.props.field.defaultValue : value; + } + // defaultValue should only be set only when value is not set + if (!value && this.props.field.defaultValue) { + defaultValue = this.props.field.defaultValue; } return ( @@ -24,6 +34,7 @@ const FormFieldText = React.createClass({ name={this.props.field.name} id={`field_${this.props.field.name}`} value={value} + defaultValue={defaultValue} placeholder={this.props.field.placeholder} onChange={this.props.onValueChange} {...this.props.field.validation}