-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBox.js
More file actions
153 lines (135 loc) · 4.53 KB
/
Box.js
File metadata and controls
153 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import Collapse from 'react-collapse';
import { Card } from 'react-bootstrap';
import classnames from 'classnames';
import Icon from '../../icons';
import withRouter, { withRouterProps } from '../../../helpers/withRouter.js';
import * as styles from './Box.less';
/**
* component for bounding other components like text paragraphs or tables inside.
* It is in fact a re-styled Panel component from Bootstrap. It can be collapsable
* and can be displayed in different colors and types.
*/
class Box extends Component {
state = {
isOpen: this.props.isOpen !== undefined ? this.props.isOpen : true,
};
componentDidMount() {
if (this.props.id && this.props.location.hash === `#${this.props.id}`) {
window.location.hash = this.props.location.hash;
window.scrollBy({ top: -65, behavior: 'instant' }); // 65 is slightly more than LTE top-bar (which is 57px in height)
}
}
toggleDetails = ev => {
ev.preventDefault();
if (!this.props.collapsable) {
return;
}
if (this.state.isOpen) {
this.hideDetails();
} else {
this.showDetails();
}
};
showDetails = () => this.setState({ isOpen: true });
hideDetails = () => this.setState({ isOpen: false });
removeUrlHash = () => {
const scrollPosition = window.scrollY;
window.location.hash = '';
const { pathname, search } = this.props.location;
this.props.navigate(pathname + search, { replace: true });
window.setTimeout(() => window.scrollTo(0, scrollPosition), 0);
};
render() {
const {
id = null,
title,
flexTitle = false,
description = null,
type = null,
solid = false,
collapsable = false,
noPadding = false,
extraPadding = false,
unlimitedHeight = false,
customIcons = null,
className = '',
children,
footer,
} = this.props;
const { isOpen = true } = this.state;
return (
<Card
id={id}
className={classnames({
'mb-3': true,
'card-outline': !solid && type && type.length > 0,
[`card-${type}`]: type && type.length > 0,
[className]: className.length > 0,
})}>
<Card.Header onClick={this.toggleDetails}>
<Card.Title className={flexTitle ? 'd-flex justify-content-between float-none' : null}>
{title}
<span className="whenTargetted text-warning">
<Icon
icon="highlighter"
gapLeft={2}
timid
onClick={this.removeUrlHash}
tooltipId={`highlighter-${id}`}
tooltipPlacement="bottom"
tooltip={
<FormattedMessage
id="app.box.highlighterExplanation"
defaultMessage="This box is highlighted. Click to restore."
/>
}
/>
</span>
</Card.Title>
{customIcons && <span className={styles.customIcons}>{customIcons}</span>}
{collapsable && !customIcons && (
<div className="card-tools me-1">
<Icon icon={isOpen ? 'minus' : 'plus'} onClick={this.toggleDetails} />
</div>
)}
</Card.Header>
<Collapse isOpened={!collapsable || isOpen}>
<Card.Body
className={classnames({
'p-0': noPadding,
[styles.extraPadding]: !noPadding && extraPadding,
[styles.limited]: !unlimitedHeight,
[styles.unlimited]: unlimitedHeight,
})}>
{description && <div className={styles.description}>{description}</div>}
<div>{children}</div>
</Card.Body>
{footer && <Card.Footer>{footer}</Card.Footer>}
</Collapse>
</Card>
);
}
}
Box.propTypes = {
id: PropTypes.string,
title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,
flexTitle: PropTypes.bool,
description: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
type: PropTypes.string,
isOpen: PropTypes.bool,
collapsable: PropTypes.bool,
unlimitedHeight: PropTypes.bool,
noPadding: PropTypes.bool,
extraPadding: PropTypes.bool,
solid: PropTypes.bool,
footer: PropTypes.element,
children: PropTypes.element,
className: PropTypes.string,
customIcons: PropTypes.any,
navigate: withRouterProps.navigate,
location: withRouterProps.location,
};
export default withRouter(Box);