@darwino/darwino-react-bootstrap
Version:
A set of Javascript classes and utilities
59 lines (52 loc) • 1.74 kB
JSX
/*
* (c) Copyright Darwino Inc. 2014-2017.
*/
import React, { createElement, Component } from "react";
import PropTypes from 'prop-types';
import { renderStatic } from "./renderStatic"
/*
* ComputedField
*/
import { FormGroup, ControlLabel } from 'react-bootstrap';
class ComputedField extends Component {
// Context to read from the parent - router
static contextTypes = {
documentForm: PropTypes.object
};
constructor(props, context) {
super(props, context)
}
render() {
const {children} = this.props;
if(children) {
const {label} = this.props
return (
<FormGroup>
{label && <ControlLabel>{label}</ControlLabel>}
<p className="form-control-static">{children}</p>
</FormGroup>
)
} else {
const {label, name, format} = this.props
let value;
if(name) {
if(!this.context.documentForm) {
throw new Error('ComputedField must be inside a component within a DocumentForm to get a field by name');
}
value = this.context.documentForm.getComputedValue(name);
} else {
value = this.props.value;
}
if(format) {
value = format(value,name)
}
return (
<FormGroup>
{label && <ControlLabel>{label}</ControlLabel>}
<p className="form-control-static">{(value!==null && value!==undefined) ? value.toString() : ""}</p>
</FormGroup>
)
}
}
}
export default ComputedField