Prevents error when item prop is not set
Allows setting custom value and defaultValue
This commit is contained in:
@@ -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}
|
||||
|
Reference in New Issue
Block a user