1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 17:45:10 +01:00
onion/js/components/ascribe_forms/property.js

154 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-06-18 19:03:03 +02:00
'use strict';
import React from 'react';
import ReactAddons from 'react/addons';
2015-06-20 16:43:18 +02:00
import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger';
import Tooltip from 'react-bootstrap/lib/Tooltip';
2015-06-18 19:03:03 +02:00
let Property = React.createClass({
propTypes: {
2015-06-22 23:32:41 +02:00
hidden: React.PropTypes.bool,
2015-06-18 19:03:03 +02:00
editable: React.PropTypes.bool,
2015-06-22 23:32:41 +02:00
tooltip: React.PropTypes.element,
2015-06-18 19:03:03 +02:00
label: React.PropTypes.string,
value: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.element
]),
2015-06-30 10:42:58 +02:00
footer: React.PropTypes.element,
2015-06-18 19:03:03 +02:00
handleChange: React.PropTypes.func
},
getDefaultProps() {
return {
2015-06-22 23:32:41 +02:00
editable: true,
2015-06-30 10:42:58 +02:00
hidden: false,
2015-06-18 19:03:03 +02:00
};
},
getInitialState() {
return {
initialValue: null,
value: null,
isFocused: false,
errors: null
};
},
componentWillReceiveProps(){
this.setState({
initialValue: this.refs.input.getDOMNode().defaultValue,
value: this.refs.input.getDOMNode().value
});
},
reset(){
2015-06-22 23:32:41 +02:00
// maybe do reset by reload instead of frontend state?
2015-06-18 19:03:03 +02:00
this.setState({value: this.state.initialValue});
2015-06-22 23:32:41 +02:00
if (this.refs.input.state){
// This is probably not the right way but easy fix
this.refs.input.state.value = this.state.initialValue;
}
else{
this.refs.input.getDOMNode().value = this.state.initialValue;
}
2015-06-18 19:03:03 +02:00
},
handleChange(event) {
this.props.handleChange(event);
2015-06-30 10:42:58 +02:00
if ('onChange' in this.props) {
this.props.onChange(event);
}
2015-06-18 19:03:03 +02:00
this.setState({value: event.target.value});
},
handleFocus() {
this.refs.input.getDOMNode().focus();
this.setState({
isFocused: true
});
},
2015-06-20 16:43:18 +02:00
handleBlur() {
this.setState({
isFocused: false
});
},
2015-06-18 19:03:03 +02:00
handleSuccess(){
this.setState({
isFocused: false,
errors: null
});
},
setErrors(errors){
this.setState({
errors: errors.map((error) => {
return <span className="pull-right" key={error}>{error}</span>;
})
});
},
clearErrors(){
this.setState({errors: null});
},
getClassName() {
2015-06-22 23:32:41 +02:00
if(this.props.hidden){
return 'is-hidden';
}
2015-06-18 19:03:03 +02:00
if(!this.props.editable){
return 'is-fixed';
}
if (this.state.errors){
return 'is-error';
}
if(this.state.isFocused) {
return 'is-focused';
} else {
return '';
}
},
renderChildren() {
return ReactAddons.Children.map(this.props.children, (child) => {
return ReactAddons.addons.cloneWithProps(child, {
value: this.state.value,
onChange: this.handleChange,
2015-06-20 16:43:18 +02:00
onFocus: this.handleFocus,
onBlur: this.handleBlur,
2015-06-18 19:03:03 +02:00
disabled: !this.props.editable,
ref: 'input'
});
});
},
render() {
2015-06-20 16:43:18 +02:00
let tooltip = <span/>;
if (this.props.tooltip){
tooltip = (
<Tooltip>
{this.props.tooltip}
</Tooltip>);
}
2015-06-30 10:42:58 +02:00
let footer = null;
if (this.props.footer){
footer = (
<div className="ascribe-property-footer">
{this.props.footer}
</div>);
}
2015-06-18 19:03:03 +02:00
return (
<div
className={'ascribe-settings-wrapper ' + this.getClassName()}
2015-06-20 16:43:18 +02:00
onClick={this.handleFocus} onfocus={this.handleFocus}>
<OverlayTrigger
delay={500}
placement="top"
overlay={tooltip}>
<div className="ascribe-settings-property">
{this.state.errors}
<span>{ this.props.label}</span>
{this.renderChildren()}
2015-06-30 10:42:58 +02:00
{footer}
2015-06-20 16:43:18 +02:00
</div>
</OverlayTrigger>
2015-06-18 19:03:03 +02:00
</div>
);
}
});
export default Property;