1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/ascribe_forms/input_textarea_toggable.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-06-09 13:29:22 +02:00
'use strict';
import React from 'react';
import TextareaAutosize from 'react-textarea-autosize';
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({
propTypes: {
autoFocus: React.PropTypes.bool,
convertLinks: React.PropTypes.bool,
defaultValue: React.PropTypes.string,
disabled: React.PropTypes.bool,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
placeholder: React.PropTypes.string,
required: React.PropTypes.bool,
rows: React.PropTypes.number.isRequired
},
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() {
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
if (this.state.value === null && this.props.defaultValue) {
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() {
const { convertLinks, disabled, onBlur, placeholder, required, rows } = this.props;
const { value } = this.state;
2015-09-10 11:35:39 +02:00
if (!disabled) {
return (
2015-06-09 13:29:22 +02:00
<TextareaAutosize
ref='textarea'
className='form-control ascribe-textarea ascribe-textarea-editable'
value={value}
rows={rows}
2015-07-14 11:42:09 +02:00
maxRows={10}
required={required}
2015-06-09 13:29:22 +02:00
onChange={this.handleChange}
onBlur={onBlur}
placeholder={placeholder} />
2015-06-09 13:29:22 +02:00
);
2015-09-10 11:35:39 +02:00
} else {
// 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
export default InputTextAreaToggable;