2015-06-09 13:29:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
|
|
|
|
2016-01-07 18:29:55 +01:00
|
|
|
import { anchorize } from '../../utils/dom_utils';
|
|
|
|
|
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-11-10 18:24:46 +01:00
|
|
|
autoFocus: React.PropTypes.bool,
|
2016-01-07 18:29:55 +01:00
|
|
|
convertLinks: React.PropTypes.bool,
|
2015-10-28 19:19:14 +01:00
|
|
|
defaultValue: React.PropTypes.string,
|
2016-01-07 18:29:55 +01:00
|
|
|
disabled: React.PropTypes.bool,
|
2015-10-28 19:19:14 +01:00
|
|
|
onBlur: React.PropTypes.func,
|
2016-01-07 18:29:55 +01:00
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
placeholder: React.PropTypes.string,
|
|
|
|
required: React.PropTypes.bool,
|
|
|
|
rows: React.PropTypes.number.isRequired
|
2015-06-09 16:10:38 +02:00
|
|
|
},
|
|
|
|
|
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-23 14:12:33 +02:00
|
|
|
componentDidMount() {
|
2015-11-10 18:24:46 +01:00
|
|
|
if (this.props.autoFocus) {
|
|
|
|
this.refs.textarea.focus();
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:12:33 +02:00
|
|
|
this.setState({
|
|
|
|
value: this.props.defaultValue
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-22 17:13:06 +02:00
|
|
|
componentDidUpdate() {
|
|
|
|
// If the initial value of state.value is null, we want to set props.defaultValue
|
|
|
|
// as a value. In all other cases TextareaAutosize.onChange is updating.handleChange already
|
2016-01-07 18:29:55 +01:00
|
|
|
if (this.state.value === null && 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() {
|
2016-01-07 18:29:55 +01:00
|
|
|
const { convertLinks, disabled, onBlur, placeholder, required, rows } = this.props;
|
|
|
|
const { value } = this.state;
|
2015-09-10 11:35:39 +02:00
|
|
|
|
2016-01-07 18:29:55 +01:00
|
|
|
if (!disabled) {
|
|
|
|
return (
|
2015-06-09 13:29:22 +02:00
|
|
|
<TextareaAutosize
|
2015-11-10 18:24:46 +01:00
|
|
|
ref='textarea'
|
2016-01-07 18:29:55 +01:00
|
|
|
className='form-control ascribe-textarea ascribe-textarea-editable'
|
|
|
|
value={value}
|
|
|
|
rows={rows}
|
2015-07-14 11:42:09 +02:00
|
|
|
maxRows={10}
|
2016-01-07 18:29:55 +01:00
|
|
|
required={required}
|
2015-06-09 13:29:22 +02:00
|
|
|
onChange={this.handleChange}
|
2016-01-07 18:29:55 +01:00
|
|
|
onBlur={onBlur}
|
|
|
|
placeholder={placeholder} />
|
2015-06-09 13:29:22 +02:00
|
|
|
);
|
2015-09-10 11:35:39 +02:00
|
|
|
} else {
|
2016-01-07 18:29:55 +01:00
|
|
|
// Can only convert links when not editable, as textarea does not support anchors
|
|
|
|
return <pre className="ascribe-pre">{convertLinks ? anchorize(value) : value}</pre>;
|
2015-06-09 13:29:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-22 23:32:41 +02:00
|
|
|
|
2015-10-28 19:19:14 +01:00
|
|
|
export default InputTextAreaToggable;
|