2015-06-09 13:29:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
|
|
|
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2015-06-09 13:29:22 +02:00
|
|
|
let InputTextAreaToggable = React.createClass({
|
2015-06-09 16:10:38 +02:00
|
|
|
propTypes: {
|
2015-09-21 12:05:42 +02:00
|
|
|
disabled: React.PropTypes.bool,
|
2015-06-09 16:10:38 +02:00
|
|
|
rows: React.PropTypes.number.isRequired,
|
|
|
|
required: React.PropTypes.string,
|
|
|
|
defaultValue: React.PropTypes.string
|
|
|
|
},
|
|
|
|
|
2015-06-09 13:29:22 +02:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
2015-09-18 14:50:08 +02:00
|
|
|
value: null
|
2015-06-09 13:29:22 +02:00
|
|
|
};
|
|
|
|
},
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2015-09-18 14:50:08 +02:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2015-09-21 10:57:10 +02:00
|
|
|
// if the components state value was changed during an update, we want to refresh it
|
|
|
|
// in this component as well as in the parent Property
|
2015-09-21 15:08:24 +02:00
|
|
|
if(!this.state.value && this.state.value !== prevState.value) {
|
2015-09-18 14:50:08 +02:00
|
|
|
this.handleChange({
|
|
|
|
target: {
|
|
|
|
value: this.state.value
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-21 10:57:10 +02:00
|
|
|
// Otherwise, if state wasn't defined beforehand and defaultValue is defined from the outside
|
|
|
|
// we set it as the component's state and update Property by calling handleChange
|
2015-09-18 14:50:08 +02:00
|
|
|
if(!this.state.value && this.props.defaultValue) {
|
2015-09-21 15:08:24 +02:00
|
|
|
this.setState({
|
|
|
|
value: this.props.defaultValue
|
2015-09-18 14:50:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-09 13:29:22 +02:00
|
|
|
handleChange(event) {
|
2015-06-22 23:32:41 +02:00
|
|
|
this.setState({value: event.target.value});
|
|
|
|
this.props.onChange(event);
|
2015-06-09 13:29:22 +02:00
|
|
|
},
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2015-06-09 13:29:22 +02:00
|
|
|
render() {
|
|
|
|
let className = 'form-control ascribe-textarea';
|
|
|
|
let textarea = null;
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2015-09-21 12:05:42 +02:00
|
|
|
if(!this.props.disabled) {
|
2015-06-09 13:29:22 +02:00
|
|
|
className = className + ' ascribe-textarea-editable';
|
|
|
|
textarea = (
|
|
|
|
<TextareaAutosize
|
|
|
|
className={className}
|
|
|
|
value={this.state.value}
|
|
|
|
rows={this.props.rows}
|
2015-07-14 11:42:09 +02:00
|
|
|
maxRows={10}
|
2015-06-09 13:29:22 +02:00
|
|
|
required={this.props.required}
|
|
|
|
onChange={this.handleChange}
|
2015-06-22 23:32:41 +02:00
|
|
|
onBlur={this.props.onBlur}
|
|
|
|
placeholder={this.props.placeholder} />
|
2015-06-09 13:29:22 +02:00
|
|
|
);
|
2015-09-10 11:35:39 +02:00
|
|
|
} else {
|
2015-06-09 13:29:22 +02:00
|
|
|
textarea = <pre className="ascribe-pre">{this.state.value}</pre>;
|
|
|
|
}
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2015-06-22 23:32:41 +02:00
|
|
|
return textarea;
|
2015-06-09 13:29:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-22 23:32:41 +02:00
|
|
|
|
2015-06-09 13:29:22 +02:00
|
|
|
export default InputTextAreaToggable;
|