1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-30 21:52:08 +02:00
onion/js/components/ascribe_forms/input_textarea_toggable.js
Brett Sun 84e8e4612f Autofocus message field in consignment form and use custom labels
For now, we’ll let the artist specify their suggested price in the
consignment form’s message field.
2015-11-10 18:24:46 +01:00

79 lines
2.1 KiB
JavaScript

'use strict';
import React from 'react';
import TextareaAutosize from 'react-textarea-autosize';
let InputTextAreaToggable = React.createClass({
propTypes: {
autoFocus: React.PropTypes.bool,
disabled: React.PropTypes.bool,
rows: React.PropTypes.number.isRequired,
required: React.PropTypes.bool,
defaultValue: React.PropTypes.string,
placeholder: React.PropTypes.string,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func
},
getInitialState() {
return {
value: null
};
},
componentDidMount() {
if (this.props.autoFocus) {
this.refs.textarea.focus();
}
this.setState({
value: this.props.defaultValue
});
},
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
});
}
},
handleChange(event) {
this.setState({value: event.target.value});
this.props.onChange(event);
},
render() {
let className = 'form-control ascribe-textarea';
let textarea = null;
if(!this.props.disabled) {
className = className + ' ascribe-textarea-editable';
textarea = (
<TextareaAutosize
ref='textarea'
className={className}
value={this.state.value}
rows={this.props.rows}
maxRows={10}
required={this.props.required}
onChange={this.handleChange}
onBlur={this.props.onBlur}
placeholder={this.props.placeholder} />
);
} else {
textarea = <pre className="ascribe-pre">{this.state.value}</pre>;
}
return textarea;
}
});
export default InputTextAreaToggable;