Skip to content

Commit 9e7d20d

Browse files
aulneauVeraZab
authored andcommitted
update modal provider to accept props
1 parent 885d7ab commit 9e7d20d

File tree

6 files changed

+50
-49
lines changed

6 files changed

+50
-49
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ lib
1616
!src/lib
1717

1818
accessTokens.js
19-
2019
yarn.lock
2120
package-lock.json

src/components/containers/Modal.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,27 @@ const ModalHeader = ({title, handleClose}) => (
1919
class Modal extends Component {
2020
constructor(props) {
2121
super(props);
22-
this.state = {
23-
isAnimatingOut: false,
24-
};
2522
}
2623

27-
handleClose() {
28-
this.setState({isAnimatingOut: true});
29-
const {closeModal} = this.context;
30-
const animationDuration = 600;
31-
setTimeout(() => {
32-
this.setState({isAnimatingOut: false});
33-
closeModal();
34-
}, animationDuration);
35-
}
3624
render() {
3725
const {children, title} = this.props;
3826
let classes = 'modal';
39-
if (this.state.isAnimatingOut) {
27+
if (this.context.isAnimatingOut) {
4028
classes += ' modal--animate-out';
4129
}
4230
return (
4331
<div className={classes}>
4432
<div className="modal__card">
45-
<ModalHeader title={title} handleClose={() => this.handleClose()} />
33+
<ModalHeader
34+
title={title}
35+
handleClose={() => this.context.handleClose()}
36+
/>
4637
{children}
4738
</div>
48-
<div className="modal__backdrop" onClick={() => this.handleClose()} />
39+
<div
40+
className="modal__backdrop"
41+
onClick={() => this.context.handleClose()}
42+
/>
4943
</div>
5044
);
5145
}
@@ -70,7 +64,8 @@ Modal.propTypes = {
7064
};
7165

7266
Modal.contextTypes = {
73-
closeModal: PropTypes.func,
67+
handleClose: PropTypes.func,
68+
isAnimatingOut: PropTypes.bool,
7469
};
7570

7671
export default Modal;

src/components/containers/ModalProvider.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ class ModalProvider extends React.Component {
66
super(props);
77
this.state = {
88
component: null,
9+
componentProps: {},
910
open: false,
11+
isAnimatingOut: false,
1012
};
1113
}
14+
1215
componentDidUpdate() {
1316
const body = document.body;
1417
const {open} = this.state;
@@ -24,16 +27,16 @@ class ModalProvider extends React.Component {
2427
}
2528
}
2629

27-
openModal(component) {
30+
openModal(component, props) {
2831
if (!component) {
2932
throw Error('You need to provide a component for the modal to open!');
3033
}
31-
3234
const {open} = this.state;
3335

3436
if (!open) {
3537
this.setState({
3638
component: component,
39+
componentProps: props,
3740
open: true,
3841
});
3942
}
@@ -48,20 +51,32 @@ class ModalProvider extends React.Component {
4851
});
4952
}
5053
}
54+
handleClose() {
55+
this.setState({isAnimatingOut: true});
56+
const animationDuration = 600;
57+
setTimeout(() => {
58+
this.setState({isAnimatingOut: false});
59+
this.closeModal();
60+
}, animationDuration);
61+
}
5162

5263
getChildContext() {
5364
return {
54-
openModal: c => this.openModal(c),
65+
openModal: (c, p) => this.openModal(c, p),
5566
closeModal: () => this.closeModal(),
67+
handleClose: () => this.handleClose(),
68+
isAnimatingOut: this.state.isAnimatingOut,
5669
};
5770
}
5871

5972
render() {
60-
const {component: Component} = this.state;
73+
const {component: Component, componentProps, isAnimatingOut} = this.state;
6174
return (
6275
<Fragment>
6376
{this.props.children}
64-
{this.state.open ? Component : null}
77+
{this.state.open ? (
78+
<Component isAnimatingOut={isAnimatingOut} {...componentProps} />
79+
) : null}
6580
</Fragment>
6681
);
6782
}
@@ -73,6 +88,8 @@ ModalProvider.propTypes = {
7388
ModalProvider.childContextTypes = {
7489
openModal: PropTypes.func,
7590
closeModal: PropTypes.func,
91+
handleClose: PropTypes.func,
92+
isAnimatingOut: PropTypes.bool,
7693
};
7794

7895
export default ModalProvider;

src/components/fields/TraceSelector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TraceSelector extends Component {
140140
}
141141

142142
updatePlot(value) {
143+
143144
const {updateContainer} = this.props;
144145
if (updateContainer) {
145146
updateContainer(traceTypeToPlotlyInitFigure(value));
@@ -160,7 +161,7 @@ class TraceSelector extends Component {
160161
<div
161162
className="trace-type-select-dropdown__wrapper"
162163
onClick={() =>
163-
this.context.openModal(<TraceTypeSelector {...props} />)
164+
this.context.openModal(TraceTypeSelector, props)
164165
}
165166
>
166167
<UnconnectedDropdown {...props} />

src/components/widgets/TraceTypeSelector.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {Component} from 'react';
22
import PropTypes from 'prop-types';
33
import {SearchIcon, ThumnailViewIcon, GraphIcon} from 'plotly-icons';
44
import Modal, {ModalContent} from 'components/containers/Modal';
5+
import {traceTypeToPlotlyInitFigure} from 'lib';
56

67
// to be removed when icons are converted to svg
78
function slugify(text) {
@@ -15,7 +16,7 @@ function slugify(text) {
1516
.replace(/-+$/, ''); // Trim - from end of text
1617
}
1718

18-
const Item = ({item, active, columnLength, columnIndex}) => {
19+
const Item = ({item, active, columnLength, columnIndex, handleClick}) => {
1920
const isEven = value => value % 2 === 0;
2021
const middle = Math.floor(columnLength / 2);
2122

@@ -41,7 +42,10 @@ const Item = ({item, active, columnLength, columnIndex}) => {
4142
}
4243
const {label, type} = item;
4344
return (
44-
<div className={`trace-item${active ? ' trace-item--active' : ''}`}>
45+
<div
46+
className={`trace-item${active ? ' trace-item--active' : ''}`}
47+
onClick={() => handleClick()}
48+
>
4549
<div className="trace-item__actions">
4650
<a
4751
className="trace-item__actions__item"
@@ -79,6 +83,10 @@ const Item = ({item, active, columnLength, columnIndex}) => {
7983
};
8084

8185
class TraceTypeSelector extends Component {
86+
selectAndClose(value) {
87+
this.props.updateContainer(traceTypeToPlotlyInitFigure(value));
88+
this.context.handleClose();
89+
}
8290
renderCategories() {
8391
const {fullValue} = this.props;
8492
const {traces: TRACE_TYPES, categories} = this.context.traceSelectorConfig;
@@ -99,6 +107,7 @@ class TraceTypeSelector extends Component {
99107
key={key}
100108
active={fullValue === key}
101109
item={value.meta}
110+
handleClick={() => this.selectAndClose(value.meta.type)}
102111
/>
103112
))}
104113
</div>
@@ -117,9 +126,12 @@ class TraceTypeSelector extends Component {
117126
);
118127
}
119128
}
120-
129+
TraceTypeSelector.propTypes = {
130+
updateContainer: PropTypes.func,
131+
};
121132
TraceTypeSelector.contextTypes = {
122133
traceSelectorConfig: PropTypes.object,
134+
handleClose: PropTypes.func,
123135
};
124136
Item.propTypes = {
125137
item: PropTypes.object,

yarn.lock

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,10 +1673,6 @@ center-align@^0.1.1:
16731673
align-text "^0.1.3"
16741674
lazy-cache "^1.0.3"
16751675

1676-
chain-function@^1.0.0:
1677-
version "1.0.0"
1678-
resolved "https://registry.yarnpkg.com/chain-function/-/chain-function-1.0.0.tgz#0d4ab37e7e18ead0bdc47b920764118ce58733dc"
1679-
16801676
chalk@^1.1.1, chalk@^1.1.3:
16811677
version "1.1.3"
16821678
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@@ -2611,10 +2607,6 @@ [email protected]:
26112607
version "1.6.7"
26122608
resolved "https://registry.yarnpkg.com/dom-align/-/dom-align-1.6.7.tgz#6858138efb6b77405ce99146d0be5e4f7282813f"
26132609

2614-
dom-helpers@^3.2.0:
2615-
version "3.3.1"
2616-
resolved "https://registry.yarnpkg.com/dom-helpers/-/dom-helpers-3.3.1.tgz#fc1a4e15ffdf60ddde03a480a9c0fece821dd4a6"
2617-
26182610
dom-serializer@0, dom-serializer@~0.1.0:
26192611
version "0.1.0"
26202612
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82"
@@ -7943,21 +7935,6 @@ react-test-renderer@^16.0.0-0, react-test-renderer@^16.2.0:
79437935
object-assign "^4.1.1"
79447936
prop-types "^15.6.0"
79457937

7946-
react-transition-group@^2.2.1:
7947-
version "2.2.1"
7948-
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-2.2.1.tgz#e9fb677b79e6455fd391b03823afe84849df4a10"
7949-
dependencies:
7950-
chain-function "^1.0.0"
7951-
classnames "^2.2.5"
7952-
dom-helpers "^3.2.0"
7953-
loose-envify "^1.3.1"
7954-
prop-types "^15.5.8"
7955-
warning "^3.0.0"
7956-
7957-
react-tunnels@^1.0.2:
7958-
version "1.0.2"
7959-
resolved "https://registry.yarnpkg.com/react-tunnels/-/react-tunnels-1.0.2.tgz#cd03aaaeb47305e38d471303e5569dd2427a01b9"
7960-
79617938
react@^16.2.0:
79627939
version "16.2.0"
79637940
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"

0 commit comments

Comments
 (0)