2019-10-17 10:53:11 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import styled, { css } from "styled-components";
|
|
|
|
import { tablet } from "../../utils/device";
|
|
|
|
import Label from "../label";
|
|
|
|
import IconButton from "../icon-button";
|
|
|
|
import Tooltip from "../tooltip";
|
|
|
|
import { handleAnyClick } from "../../utils/event";
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-09-04 09:17:55 +00:00
|
|
|
const horizontalCss = css`
|
2019-08-27 12:43:30 +00:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
2019-09-04 09:17:55 +00:00
|
|
|
align-items: start;
|
2019-08-27 12:43:30 +00:00
|
|
|
margin: 0 0 16px 0;
|
|
|
|
|
|
|
|
.field-label {
|
|
|
|
line-height: 32px;
|
|
|
|
margin: 0;
|
2019-10-17 10:53:11 +00:00
|
|
|
position: relative;
|
2019-08-27 12:43:30 +00:00
|
|
|
}
|
2019-09-10 09:49:51 +00:00
|
|
|
.field-body {
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
2019-10-10 12:08:39 +00:00
|
|
|
.icon-button {
|
2019-10-17 10:53:11 +00:00
|
|
|
position: relative;
|
2019-10-10 12:08:39 +00:00
|
|
|
line-height: 24px;
|
2019-10-17 10:53:11 +00:00
|
|
|
margin: 2px 0 0 4px;
|
2019-10-10 12:08:39 +00:00
|
|
|
}
|
2019-10-17 10:53:11 +00:00
|
|
|
`;
|
2019-09-04 09:17:55 +00:00
|
|
|
const verticalCss = css`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: start;
|
|
|
|
margin: 0 0 16px 0;
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-09-04 09:17:55 +00:00
|
|
|
.field-label {
|
|
|
|
line-height: unset;
|
|
|
|
margin: 0 0 4px 0;
|
2019-09-10 09:49:51 +00:00
|
|
|
}
|
|
|
|
.field-body {
|
|
|
|
width: 100%;
|
2019-08-27 12:43:30 +00:00
|
|
|
}
|
2019-10-11 06:17:27 +00:00
|
|
|
.icon-button {
|
2019-10-17 10:53:11 +00:00
|
|
|
position: relative;
|
2019-10-11 06:17:27 +00:00
|
|
|
line-height: unset;
|
2019-10-17 10:53:11 +00:00
|
|
|
margin: -4px 0 0 4px;
|
2019-10-11 06:17:27 +00:00
|
|
|
}
|
2019-10-17 10:53:11 +00:00
|
|
|
`;
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-09-04 09:17:55 +00:00
|
|
|
const Container = styled.div`
|
2019-10-17 10:53:11 +00:00
|
|
|
.field-label-icon {
|
|
|
|
width: 110px;
|
|
|
|
min-width: 110px;
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
${props => (props.vertical ? verticalCss : horizontalCss)}
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-09-04 10:29:32 +00:00
|
|
|
@media ${tablet} {
|
2019-09-04 09:17:55 +00:00
|
|
|
${verticalCss}
|
2019-08-27 12:43:30 +00:00
|
|
|
}
|
|
|
|
`;
|
2019-10-17 10:53:11 +00:00
|
|
|
class FieldContainer extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-10-17 10:53:11 +00:00
|
|
|
this.state = { isOpen: false };
|
|
|
|
this.ref = React.createRef();
|
2019-10-17 12:49:01 +00:00
|
|
|
this.refTooltip = React.createRef();
|
|
|
|
//console.log(`FieldContainer constructor(${this.helperId})`, props, this.ref);
|
2019-10-17 10:53:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 13:15:43 +00:00
|
|
|
afterShow = () => {
|
2019-10-17 12:49:01 +00:00
|
|
|
//console.log(`afterShow ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
|
|
|
this.setState({ isOpen: true }, () => {
|
|
|
|
handleAnyClick(true, this.handleClick);
|
|
|
|
});
|
2019-10-17 10:53:11 +00:00
|
|
|
};
|
|
|
|
|
2019-10-17 13:15:43 +00:00
|
|
|
afterHide = () => {
|
2019-10-17 12:49:01 +00:00
|
|
|
//console.log(`afterHide ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
2019-10-17 10:53:11 +00:00
|
|
|
if (this.state.isOpen) {
|
2019-10-17 12:49:01 +00:00
|
|
|
this.setState({ isOpen: false }, () => {
|
|
|
|
handleAnyClick(false, this.handleClick);
|
|
|
|
});
|
2019-10-17 10:53:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
handleClick = e => {
|
2019-10-17 12:49:01 +00:00
|
|
|
//console.log(`handleClick ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
|
|
|
|
|
|
|
if (!this.ref.current.contains(e.target)) {
|
|
|
|
//console.log(`hideTooltip() tooltipId=${this.props.tooltipId}`, this.refTooltip.current);
|
|
|
|
this.refTooltip.current.hideTooltip();
|
2019-10-17 10:53:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-17 13:15:43 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
handleAnyClick(false, this.handleClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-17 10:53:11 +00:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
isVertical,
|
|
|
|
className,
|
|
|
|
isRequired,
|
|
|
|
hasError,
|
|
|
|
labelText,
|
|
|
|
children,
|
|
|
|
tooltipContent,
|
|
|
|
tooltipOffsetRight,
|
|
|
|
tooltipMaxWidth,
|
|
|
|
tooltipId
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container vertical={isVertical} className={className}>
|
|
|
|
<div className="field-label-icon">
|
|
|
|
<Label
|
|
|
|
isRequired={isRequired}
|
|
|
|
error={hasError}
|
|
|
|
text={labelText}
|
|
|
|
truncate={true}
|
|
|
|
className="field-label"
|
|
|
|
/>
|
|
|
|
{tooltipContent && (
|
|
|
|
<div ref={this.ref}>
|
|
|
|
<IconButton
|
|
|
|
data-tip=""
|
|
|
|
data-for={tooltipId}
|
|
|
|
data-event="click focus"
|
|
|
|
className="icon-button"
|
|
|
|
isClickable={true}
|
|
|
|
iconName="QuestionIcon"
|
|
|
|
size={13}
|
|
|
|
/>
|
|
|
|
<Tooltip
|
|
|
|
id={tooltipId}
|
2019-10-17 12:49:01 +00:00
|
|
|
reference={this.refTooltip}
|
2019-10-17 10:53:11 +00:00
|
|
|
effect="solid"
|
|
|
|
place="top"
|
|
|
|
offsetRight={tooltipOffsetRight}
|
|
|
|
maxWidth={tooltipMaxWidth}
|
|
|
|
afterShow={this.afterShow}
|
|
|
|
afterHide={this.afterHide}
|
|
|
|
globalEventOff={true}
|
|
|
|
>
|
|
|
|
{tooltipContent}
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="field-body">{children}</div>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 12:43:30 +00:00
|
|
|
|
2019-10-17 10:53:11 +00:00
|
|
|
FieldContainer.displayName = "FieldContainer";
|
2019-09-10 09:49:51 +00:00
|
|
|
|
|
|
|
FieldContainer.propTypes = {
|
|
|
|
isVertical: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
isRequired: PropTypes.bool,
|
|
|
|
hasError: PropTypes.bool,
|
|
|
|
labelText: PropTypes.string,
|
2019-10-10 12:08:39 +00:00
|
|
|
icon: PropTypes.string,
|
2019-09-10 09:49:51 +00:00
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
|
|
|
PropTypes.node
|
2019-10-10 12:08:39 +00:00
|
|
|
]),
|
2019-10-17 10:53:11 +00:00
|
|
|
tooltipContent: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
|
|
|
tooltipOffsetRight: PropTypes.number,
|
|
|
|
tooltipMaxWidth: PropTypes.number,
|
|
|
|
tooltipId: PropTypes.string
|
2019-09-10 09:49:51 +00:00
|
|
|
};
|
|
|
|
|
2019-10-17 10:53:11 +00:00
|
|
|
export default FieldContainer;
|