2015-06-18 19:03:03 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import ReactAddons from 'react/addons';
|
|
|
|
|
2015-11-17 15:46:46 +01:00
|
|
|
import Panel from 'react-bootstrap/lib/Panel';
|
2015-06-20 16:43:18 +02:00
|
|
|
|
2015-09-14 10:49:45 +02:00
|
|
|
import AppConstants from '../../constants/application_constants';
|
|
|
|
|
2015-10-14 16:17:10 +02:00
|
|
|
import { mergeOptions } from '../../utils/general_utils';
|
2015-08-19 17:06:14 +02:00
|
|
|
|
2015-09-14 10:49:45 +02:00
|
|
|
|
2015-11-17 15:46:46 +01:00
|
|
|
const { bool, element, string, oneOfType, func, object, arrayOf } = React.PropTypes;
|
|
|
|
|
|
|
|
const Property = React.createClass({
|
2015-06-18 19:03:03 +02:00
|
|
|
propTypes: {
|
2015-11-17 15:46:46 +01:00
|
|
|
editable: bool,
|
2015-08-20 11:03:00 +02:00
|
|
|
|
|
|
|
// If we want Form to have a different value for disabled as Property has one for
|
|
|
|
// editable, we need to set overrideForm to true, as it will then override Form's
|
|
|
|
// disabled value for individual Properties
|
2015-11-17 15:46:46 +01:00
|
|
|
overrideForm: bool,
|
2015-08-20 11:03:00 +02:00
|
|
|
|
2015-11-17 15:46:46 +01:00
|
|
|
label: string,
|
|
|
|
value: oneOfType([
|
|
|
|
string,
|
|
|
|
element
|
2015-06-18 19:03:03 +02:00
|
|
|
]),
|
2015-11-17 15:46:46 +01:00
|
|
|
footer: element,
|
|
|
|
handleChange: func,
|
|
|
|
ignoreFocus: bool,
|
|
|
|
name: string.isRequired,
|
|
|
|
className: string,
|
|
|
|
|
|
|
|
onClick: func,
|
|
|
|
onChange: func,
|
|
|
|
onBlur: func,
|
|
|
|
|
|
|
|
children: oneOfType([
|
|
|
|
arrayOf(element),
|
|
|
|
element
|
2015-07-10 16:04:57 +02:00
|
|
|
]),
|
2015-11-17 15:46:46 +01:00
|
|
|
style: object,
|
2015-11-18 16:31:37 +01:00
|
|
|
expanded: bool,
|
2015-12-08 20:55:13 +01:00
|
|
|
checkboxLabel: string,
|
|
|
|
autoFocus: bool
|
2015-06-18 19:03:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
2015-06-22 23:32:41 +02:00
|
|
|
editable: true,
|
2015-11-17 15:46:46 +01:00
|
|
|
expanded: true,
|
2015-08-19 16:03:59 +02:00
|
|
|
className: ''
|
2015-06-18 19:03:03 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
2015-11-18 16:31:37 +01:00
|
|
|
const { expanded, ignoreFocus, checkboxLabel } = this.props;
|
|
|
|
|
2015-06-18 19:03:03 +02:00
|
|
|
return {
|
2015-11-18 16:31:37 +01:00
|
|
|
// We're mirroring expanded here as a state
|
|
|
|
// React's docs do NOT consider this an antipattern as long as it's
|
|
|
|
// not a "source of truth"-duplication
|
|
|
|
expanded,
|
|
|
|
|
|
|
|
// When a checkboxLabel is defined in the props, we want to set
|
|
|
|
// `ignoreFocus` to true
|
|
|
|
ignoreFocus: ignoreFocus || checkboxLabel,
|
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,
|
2015-12-08 18:23:02 +01:00
|
|
|
errors: null,
|
|
|
|
hasWarning: false
|
2015-06-18 19:03:03 +02:00
|
|
|
};
|
|
|
|
},
|
2015-07-10 15:56:54 +02:00
|
|
|
|
2015-12-08 20:55:13 +01:00
|
|
|
componentDidMount() {
|
|
|
|
if(this.props.autoFocus) {
|
|
|
|
this.handleFocus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-18 16:31:37 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2015-07-28 15:33:47 +02:00
|
|
|
let childInput = this.refs.input;
|
2015-07-14 20:35:16 +02:00
|
|
|
|
2015-12-08 20:55:13 +01:00
|
|
|
// For expanded there are actually three use cases:
|
2015-11-18 16:31:37 +01:00
|
|
|
//
|
|
|
|
// 1. Control its value from the outside completely (do not define `checkboxLabel`)
|
|
|
|
// 2. Let it be controlled from the inside (default value can be set though via `expanded`)
|
2015-12-08 20:55:13 +01:00
|
|
|
// 3. Let it be controlled from a child by using `setExpanded` (`expanded` must not be
|
|
|
|
// set from the outside as a prop then(!!!))
|
2015-11-18 16:31:37 +01:00
|
|
|
//
|
2015-12-08 20:55:13 +01:00
|
|
|
// This handles case 1. and 3.
|
|
|
|
if(nextProps.expanded !== this.props.expanded && nextProps.expanded !== this.state.expanded && !this.props.checkboxLabel) {
|
2015-11-18 16:31:37 +01:00
|
|
|
this.setState({ expanded: nextProps.expanded });
|
|
|
|
}
|
|
|
|
|
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-09-15 11:50:23 +02:00
|
|
|
if(childInput && typeof childInput.getDOMNode().value !== 'undefined') {
|
2015-07-14 20:35:16 +02:00
|
|
|
this.setState({
|
2015-08-19 16:03:59 +02:00
|
|
|
value: childInput.getDOMNode().value
|
2015-07-28 15:33:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
2015-11-19 16:16:25 +01:00
|
|
|
} else if(childInput && childInput.state && typeof childInput.state.value !== 'undefined') {
|
2015-07-28 15:33:47 +02:00
|
|
|
this.setState({
|
2015-08-19 16:03:59 +02:00
|
|
|
value: childInput.state.value
|
2015-07-14 20:35:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:16:25 +01:00
|
|
|
if(!this.state.initialValue && childInput && childInput.props.defaultValue) {
|
2015-07-21 16:23:50 +02:00
|
|
|
this.setState({
|
2015-08-05 09:52:17 +02:00
|
|
|
initialValue: childInput.props.defaultValue
|
2015-07-21 16:23:50 +02:00
|
|
|
});
|
|
|
|
}
|
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() {
|
2015-09-14 10:49:45 +02:00
|
|
|
let input = this.refs.input;
|
|
|
|
|
2015-07-21 16:23:50 +02:00
|
|
|
// 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
|
|
|
|
2015-09-14 10:49:45 +02:00
|
|
|
if(input.state && input.state.value) {
|
|
|
|
// resets the value of a custom react component input
|
|
|
|
input.state.value = this.state.initialValue;
|
|
|
|
}
|
2015-08-05 09:52:17 +02:00
|
|
|
|
2015-09-14 10:49:45 +02:00
|
|
|
// For some reason, if we set the value of a non HTML element (but a custom input),
|
|
|
|
// after a reset, the value will be be propagated to this component.
|
2015-09-14 11:36:24 +02:00
|
|
|
//
|
2015-09-14 10:49:45 +02:00
|
|
|
// Therefore we have to make sure only to reset the initial value
|
2015-09-14 11:36:24 +02:00
|
|
|
// of HTML inputs (which we determine by checking if there 'type' attribute matches
|
|
|
|
// the ones included in AppConstants.possibleInputTypes).
|
2015-09-14 10:49:45 +02:00
|
|
|
let inputDOMNode = input.getDOMNode();
|
2015-09-14 11:36:24 +02:00
|
|
|
if(inputDOMNode.type && typeof inputDOMNode.type === 'string' &&
|
|
|
|
AppConstants.possibleInputTypes.indexOf(inputDOMNode.type.toLowerCase()) > -1) {
|
2015-09-14 10:49:45 +02:00
|
|
|
inputDOMNode.value = this.state.initialValue;
|
|
|
|
}
|
2015-06-22 23:32:41 +02:00
|
|
|
|
2015-09-10 11:22:42 +02:00
|
|
|
// For some inputs, reseting state.value is not enough to visually reset the
|
|
|
|
// component.
|
|
|
|
//
|
|
|
|
// So if the input actually needs a visual reset, it needs to implement
|
|
|
|
// a dedicated reset method.
|
2015-09-14 11:36:24 +02:00
|
|
|
if(typeof input.reset === 'function') {
|
2015-09-14 10:49:45 +02:00
|
|
|
input.reset();
|
2015-09-10 11:22:42 +02:00
|
|
|
}
|
2015-06-18 19:03:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
handleChange(event) {
|
|
|
|
this.props.handleChange(event);
|
2015-09-14 11:36:24 +02:00
|
|
|
if (typeof this.props.onChange === 'function') {
|
2015-06-30 10:42:58 +02:00
|
|
|
this.props.onChange(event);
|
|
|
|
}
|
2015-07-14 20:35:16 +02:00
|
|
|
|
2015-08-19 16:03:59 +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-11-18 16:31:37 +01:00
|
|
|
if(this.state.ignoreFocus) {
|
2015-07-07 18:07:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-10 15:56:54 +02:00
|
|
|
|
|
|
|
// if onClick is defined from the outside,
|
|
|
|
// just call it
|
2015-09-14 11:36:24 +02:00
|
|
|
if(typeof this.props.onClick === 'function') {
|
2015-07-10 15:56:54 +02:00
|
|
|
this.props.onClick();
|
|
|
|
}
|
2015-10-14 16:17:10 +02:00
|
|
|
// skip the focus of non-input elements
|
|
|
|
let nonInputHTMLElements = ['pre', 'div'];
|
|
|
|
if (this.refs.input &&
|
|
|
|
nonInputHTMLElements.indexOf(this.refs.input.getDOMNode().nodeName.toLowerCase()) > -1 ) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2015-07-15 14:03:23 +02:00
|
|
|
handleBlur(event) {
|
2015-06-20 16:43:18 +02:00
|
|
|
this.setState({
|
|
|
|
isFocused: false
|
|
|
|
});
|
2015-07-15 14:03:23 +02:00
|
|
|
|
2015-09-14 11:36:24 +02:00
|
|
|
if(typeof this.props.onBlur === 'function') {
|
2015-07-15 14:03:23 +02:00
|
|
|
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,
|
2015-07-21 16:23:50 +02:00
|
|
|
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({
|
2015-11-13 09:48:22 +01:00
|
|
|
errors: errors.pop()
|
2015-06-18 19:03:03 +02:00
|
|
|
});
|
|
|
|
},
|
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-12-08 18:23:02 +01:00
|
|
|
setWarning(hasWarning) {
|
|
|
|
this.setState({ hasWarning });
|
|
|
|
},
|
|
|
|
|
2015-06-18 19:03:03 +02:00
|
|
|
getClassName() {
|
2015-12-08 18:23:02 +01:00
|
|
|
if (!this.state.expanded && !this.props.checkboxLabel) {
|
2015-06-22 23:32:41 +02:00
|
|
|
return 'is-hidden';
|
2015-12-08 18:23:02 +01:00
|
|
|
} else if (!this.props.editable) {
|
2015-06-18 19:03:03 +02:00
|
|
|
return 'is-fixed';
|
2015-12-08 18:23:02 +01:00
|
|
|
} else if (this.state.errors) {
|
2015-06-18 19:03:03 +02:00
|
|
|
return 'is-error';
|
2015-12-08 18:23:02 +01:00
|
|
|
} else if (this.state.hasWarning) {
|
|
|
|
return 'is-warning';
|
|
|
|
} else if (this.state.isFocused) {
|
2015-06-18 19:03:03 +02:00
|
|
|
return 'is-focused';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
2015-07-10 15:56:54 +02:00
|
|
|
|
2015-12-08 20:55:13 +01:00
|
|
|
setExpanded(expanded) {
|
|
|
|
this.setState({ expanded });
|
|
|
|
},
|
|
|
|
|
|
|
|
handleCheckboxToggle() {
|
2016-02-05 10:38:59 +01:00
|
|
|
const expanded = !this.state.expanded;
|
|
|
|
|
|
|
|
this.setExpanded(expanded);
|
|
|
|
|
|
|
|
// Reset the value to be the initial value when the checkbox is unticked since the
|
|
|
|
// user doesn't want to specify their own value.
|
|
|
|
if (!expanded) {
|
|
|
|
this.setState({
|
|
|
|
value: this.state.initialValue
|
|
|
|
});
|
|
|
|
}
|
2015-12-08 20:55:13 +01:00
|
|
|
},
|
|
|
|
|
2015-08-19 17:06:14 +02:00
|
|
|
renderChildren(style) {
|
2015-11-19 16:16:25 +01:00
|
|
|
// Input's props should only be cloned and propagated down the tree,
|
|
|
|
// if the component is actually being shown (!== 'expanded === false')
|
|
|
|
if((this.state.expanded && this.props.checkboxLabel) || !this.props.checkboxLabel) {
|
|
|
|
return ReactAddons.Children.map(this.props.children, (child) => {
|
2015-11-23 17:59:20 +01:00
|
|
|
// Since refs will be overriden by this functions return statement,
|
|
|
|
// we still want to be able to define refs for nested `Form` or `Property`
|
|
|
|
// children, which is why we're upfront simply invoking the callback-ref-
|
|
|
|
// function before overriding it.
|
|
|
|
if(typeof child.ref === 'function' && this.refs.input) {
|
|
|
|
child.ref(this.refs.input);
|
|
|
|
}
|
|
|
|
|
|
|
|
return React.cloneElement(child, {
|
2015-11-19 16:16:25 +01:00
|
|
|
style,
|
|
|
|
onChange: this.handleChange,
|
|
|
|
onFocus: this.handleFocus,
|
|
|
|
onBlur: this.handleBlur,
|
2015-12-08 18:23:02 +01:00
|
|
|
setWarning: this.setWarning,
|
2015-11-19 16:16:25 +01:00
|
|
|
disabled: !this.props.editable,
|
|
|
|
ref: 'input',
|
2015-12-08 20:55:13 +01:00
|
|
|
name: this.props.name,
|
|
|
|
setExpanded: this.setExpanded
|
2015-11-19 16:16:25 +01:00
|
|
|
});
|
2015-06-18 19:03:03 +02:00
|
|
|
});
|
2015-11-19 16:16:25 +01:00
|
|
|
}
|
2015-06-18 19:03:03 +02:00
|
|
|
},
|
2015-07-10 15:56:54 +02:00
|
|
|
|
2015-11-17 10:38:10 +01:00
|
|
|
getLabelAndErrors() {
|
|
|
|
if(this.props.label || this.state.errors) {
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
<span className="pull-left">{this.props.label}</span>
|
|
|
|
<span className="pull-right">{this.state.errors}</span>
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-18 16:31:37 +01:00
|
|
|
getCheckbox() {
|
2016-02-05 14:21:31 +01:00
|
|
|
const { checkboxLabel, name } = this.props;
|
2015-11-18 16:31:37 +01:00
|
|
|
|
2016-02-05 14:21:31 +01:00
|
|
|
if (checkboxLabel) {
|
2015-11-18 16:31:37 +01:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="ascribe-property-collapsible-toggle"
|
|
|
|
onClick={this.handleCheckboxToggle}>
|
|
|
|
<input
|
2016-02-05 14:21:31 +01:00
|
|
|
name={`${name}-checkbox`}
|
2015-11-18 16:31:37 +01:00
|
|
|
checked={this.state.expanded}
|
2016-02-05 14:21:31 +01:00
|
|
|
onChange={this.handleCheckboxToggle}
|
|
|
|
type="checkbox" />
|
2015-11-18 16:31:37 +01:00
|
|
|
<span className="checkbox">{' ' + checkboxLabel}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-18 19:03:03 +02:00
|
|
|
render() {
|
2015-09-10 14:00:59 +02:00
|
|
|
let footer = null;
|
2015-08-19 17:06:14 +02:00
|
|
|
|
|
|
|
if(this.props.footer){
|
2015-06-30 10:42:58 +02:00
|
|
|
footer = (
|
|
|
|
<div className="ascribe-property-footer">
|
|
|
|
{this.props.footer}
|
2015-12-08 20:55:13 +01:00
|
|
|
</div>
|
|
|
|
);
|
2015-06-30 10:42:58 +02:00
|
|
|
}
|
2015-08-19 17:06:14 +02:00
|
|
|
|
2015-06-18 19:03:03 +02:00
|
|
|
return (
|
|
|
|
<div
|
2015-10-12 15:25:21 +02:00
|
|
|
className={'ascribe-property-wrapper ' + this.getClassName()}
|
2015-07-07 18:07:12 +02:00
|
|
|
onClick={this.handleFocus}
|
2015-12-08 20:55:13 +01:00
|
|
|
style={this.props.style}>
|
2015-11-18 16:31:37 +01:00
|
|
|
{this.getCheckbox()}
|
2015-11-17 15:46:46 +01:00
|
|
|
<Panel
|
|
|
|
collapsible
|
2015-11-18 16:31:37 +01:00
|
|
|
expanded={this.state.expanded}
|
2015-11-17 15:46:46 +01:00
|
|
|
className="bs-custom-panel">
|
2015-10-12 15:25:21 +02:00
|
|
|
<div className={'ascribe-property ' + this.props.className}>
|
2015-11-17 10:38:10 +01:00
|
|
|
{this.getLabelAndErrors()}
|
2015-12-08 20:55:13 +01:00
|
|
|
{this.renderChildren(this.props.style)}
|
2015-06-30 10:42:58 +02:00
|
|
|
{footer}
|
2015-06-20 16:43:18 +02:00
|
|
|
</div>
|
2015-11-17 15:46:46 +01:00
|
|
|
</Panel>
|
2015-06-18 19:03:03 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-14 15:30:10 +02:00
|
|
|
export default Property;
|