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

219 lines
6.3 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-07-07 18:07:12 +02:00
handleChange: React.PropTypes.func,
2015-07-10 15:56:54 +02:00
ignoreFocus: React.PropTypes.bool,
className: React.PropTypes.string,
onClick: React.PropTypes.func,
onChange: React.PropTypes.func,
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element
2015-07-10 16:04:57 +02:00
]),
style: React.PropTypes.object
2015-06-18 19:03:03 +02:00
},
getDefaultProps() {
return {
2015-06-22 23:32:41 +02:00
editable: true,
2015-07-10 15:56:54 +02:00
hidden: false,
className: ''
2015-06-18 19:03:03 +02:00
};
},
getInitialState() {
return {
2015-07-28 12:03:45 +02:00
// Please don't confuse initialValue with react's defaultValue.
// initialValue is set by us to ensure that a user can reset a specific
// property (after editing) to its initial value
2015-06-18 19:03:03 +02:00
initialValue: null,
value: null,
isFocused: false,
errors: null
};
},
2015-07-10 15:56:54 +02:00
2015-07-14 20:35:16 +02:00
componentWillReceiveProps() {
2015-07-28 15:33:47 +02:00
let childInput = this.refs.input;
2015-07-14 20:35:16 +02:00
// In order to set this.state.value from another component
// the state of value should only be set if its not undefined and
// actually references something
2015-07-28 15:33:47 +02:00
if(typeof childInput.getDOMNode().value !== 'undefined') {
2015-07-14 20:35:16 +02:00
this.setState({
2015-07-28 15:33:47 +02:00
value: childInput.getDOMNode().value
});
// When implementing custom input components, their value isn't exposed like the one
// from native HTML elements.
// To enable developers to create input elements, they can expose a property called value
// in their state that will be picked up by property.js
} else if(childInput.state && typeof childInput.state.value !== 'undefined') {
this.setState({
value: childInput.state.value
2015-07-14 20:35:16 +02:00
});
}
2015-08-05 09:52:17 +02:00
if(!this.state.initialValue && childInput.props.defaultValue) {
this.setState({
2015-08-05 09:52:17 +02:00
initialValue: childInput.props.defaultValue
});
}
2015-06-18 19:03:03 +02:00
},
2015-07-10 15:56:54 +02:00
2015-08-05 09:52:17 +02:00
reset() {
// maybe do reset by reload instead of front end state?
2015-06-18 19:03:03 +02:00
this.setState({value: this.state.initialValue});
2015-08-05 09:52:17 +02:00
// resets the value of a custom react component input
this.refs.input.state.value = this.state.initialValue;
// resets the value of a plain HTML5 input
this.refs.input.getDOMNode().value = this.state.initialValue;
2015-06-22 23:32:41 +02:00
2015-06-18 19:03:03 +02:00
},
handleChange(event) {
2015-07-14 20:35:16 +02:00
2015-06-18 19:03:03 +02:00
this.props.handleChange(event);
2015-06-30 10:42:58 +02:00
if ('onChange' in this.props) {
this.props.onChange(event);
}
2015-07-14 20:35:16 +02:00
2015-07-15 14:48:51 +02:00
this.setState({value: event.target.value});
2015-06-18 19:03:03 +02:00
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
handleFocus() {
2015-07-10 15:56:54 +02:00
// if ignoreFocus (bool) is defined, then just ignore focusing on
// the property and input
2015-07-07 18:07:12 +02:00
if(this.props.ignoreFocus) {
return;
}
2015-07-10 15:56:54 +02:00
// if onClick is defined from the outside,
// just call it
if(this.props.onClick) {
this.props.onClick();
}
2015-06-18 19:03:03 +02:00
this.refs.input.getDOMNode().focus();
this.setState({
isFocused: true
});
},
2015-07-10 15:56:54 +02:00
handleBlur(event) {
2015-06-20 16:43:18 +02:00
this.setState({
isFocused: false
});
if(this.props.onBlur) {
this.props.onBlur(event);
}
2015-06-20 16:43:18 +02:00
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
handleSuccess(){
this.setState({
isFocused: false,
errors: null,
// also update initialValue in case of the user updating and canceling its actions again
initialValue: this.refs.input.getDOMNode().value
2015-06-18 19:03:03 +02:00
});
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
setErrors(errors){
this.setState({
errors: errors.map((error) => {
return <span className="pull-right" key={error}>{error}</span>;
})
});
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
clearErrors(){
this.setState({errors: null});
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
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 '';
}
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
renderChildren() {
return ReactAddons.Children.map(this.props.children, (child) => {
return ReactAddons.addons.cloneWithProps(child, {
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'
});
});
},
2015-07-10 15:56:54 +02:00
2015-06-18 19:03:03 +02:00
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-07-07 18:07:12 +02:00
onClick={this.handleFocus}
2015-07-10 16:04:57 +02:00
onFocus={this.handleFocus}
style={this.props.style}>
2015-06-20 16:43:18 +02:00
<OverlayTrigger
delay={500}
placement="top"
overlay={tooltip}>
2015-07-10 15:56:54 +02:00
<div className={'ascribe-settings-property ' + this.props.className}>
2015-06-20 16:43:18 +02:00
{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;