Skip to content

Feature/remove context typees #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@
"fs": "^0.0.1-security",
"gl": "^4.0.4",
"glob": "^7.1.2",
"jest": "^23.5.0",
"jest": "^23.6.0",
"jest-cli": "^23.5.0",
"mkdirp": "^0.5.1",
"node-sass": "^4.7.2",
"postcss": "^6.0.23",
"postcss-combine-duplicated-selectors": "^6.0.2",
"postcss-custom-properties": "^6.3.1",
"postcss-remove-root": "^0.0.2",
"prettier": "1.14.2",
"react": "^16.0.0",
"prettier": "^1.14.2",
"react": "^16.6.0",
"react-ace": "^6.1.4",
"react-dom": "^16.0.0",
"react-hot-loader": "^4.0.0-beta.21",
"react-dom": "^16.6.0",
"react-hot-loader": "^4.3.12",
"react-inspector": "^2.2.2",
"react-percy": "^0.3.0",
"react-test-renderer": "^16.2.0",
"rimraf": "2.6.2",
"sass-loader": "^7.1.0",
Expand All @@ -77,8 +78,8 @@
"webpack-dev-server": "^2.11.1"
},
"peerDependencies": {
"react": ">15",
"react-dom": ">15"
"react": ">16.6.0",
"react-dom": ">16.6.0"
},
"homepage": "https://plotly.github.io/react-chart-editor/",
"jest": {
Expand Down
8 changes: 2 additions & 6 deletions src/DefaultEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {traceHasColorbar} from './default_panels/StyleColorbarsPanel';
import Logo from './components/widgets/Logo';
import {TRANSFORMABLE_TRACES} from './lib/constants';
import {EditorControlsContext} from './context';

class DefaultEditor extends Component {
constructor(props, context) {
Expand Down Expand Up @@ -98,11 +99,6 @@ DefaultEditor.propTypes = {
menuPanelOrder: PropTypes.array,
};

DefaultEditor.contextTypes = {
localize: PropTypes.func,
fullData: PropTypes.array,
fullLayout: PropTypes.object,
layout: PropTypes.object,
};
DefaultEditor.contextType = EditorControlsContext;

export default DefaultEditor;
60 changes: 47 additions & 13 deletions src/EditorControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import nestedProperty from 'plotly.js/src/lib/nested_property';
import {categoryLayout, traceTypes} from 'lib/traceTypes';
import {ModalProvider} from 'components/containers';
import {DEFAULT_FONTS} from 'lib/constants';
import {EditorControlsContext} from './context';

class EditorControls extends Component {
constructor(props, context) {
Expand Down Expand Up @@ -62,6 +63,37 @@ class EditorControls extends Component {
};
}

provideValue() {
const gd = this.props.graphDiv || {};
return {
advancedTraceTypeSelector: this.props.advancedTraceTypeSelector,
config: gd._context,
srcConverters: this.props.srcConverters,
data: gd.data,
dataSources: this.props.dataSources,
dataSourceOptions: this.props.dataSourceOptions,
dataSourceValueRenderer: this.props.dataSourceValueRenderer,
dataSourceOptionRenderer: this.props.dataSourceOptionRenderer,
dictionaries: this.props.dictionaries || {},
localize: this.localize,
frames: gd._transitionData ? gd._transitionData._frames : [],
fullData: gd._fullData,
fullLayout: gd._fullLayout,
graphDiv: gd,
layout: gd.layout,
locale: this.props.locale,
onUpdate: this.handleUpdate.bind(this),
plotSchema: this.plotSchema,
plotly: this.props.plotly,
traceTypesConfig: this.props.traceTypesConfig,
showFieldTooltips: this.props.showFieldTooltips,
glByDefault: this.props.glByDefault,
mapBoxAccess: this.props.mapBoxAccess,
fontOptions: this.props.fontOptions,
chartHelp: this.props.chartHelp,
};
}

handleUpdate({type, payload}) {
const {graphDiv} = this.props;

Expand Down Expand Up @@ -295,19 +327,21 @@ class EditorControls extends Component {

render() {
return (
<div
className={
bem('editor_controls') +
' plotly-editor--theme-provider' +
`${this.props.className ? ` ${this.props.className}` : ''}`
}
>
<ModalProvider>
{this.props.graphDiv &&
this.props.graphDiv._fullLayout &&
(this.props.children ? this.props.children : <DefaultEditor />)}
</ModalProvider>
</div>
<EditorControlsContext.Provider value={this.provideValue()}>
<div
className={
bem('editor_controls') +
' plotly-editor--theme-provider' +
`${this.props.className ? ` ${this.props.className}` : ''}`
}
>
<ModalProvider>
{this.props.graphDiv &&
this.props.graphDiv._fullLayout &&
(this.props.children ? this.props.children : <DefaultEditor />)}
</ModalProvider>
</div>
</EditorControlsContext.Provider>
);
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/components/PanelMenuWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {cloneElement, Component} from 'react';
import SidebarGroup from './sidebar/SidebarGroup';
import {bem} from 'lib';
import sortMenu from 'lib/sortMenu';
import {PanelMenuWrapperContext} from '../context';

class PanelsWithSidebar extends Component {
constructor(props) {
Expand Down Expand Up @@ -30,6 +31,12 @@ class PanelsWithSidebar extends Component {
};
}

provideValue() {
return {
setPanel: this.setPanel,
};
}

renderSection(section, i) {
if (section.type && (section.type.plotly_editor_traits || {}).sidebar_element) {
return cloneElement(section, {key: i});
Expand Down Expand Up @@ -89,18 +96,20 @@ class PanelsWithSidebar extends Component {
const menuOpts = this.computeMenuOptions(this.props);

return (
<div className={bem('editor_controls', 'wrapper')}>
<div className={bem('sidebar')}>{menuOpts.map(this.renderSection)}</div>
{React.Children.map(
this.props.children,
(child, i) =>
child === null ||
this.state.group !== child.props.group ||
this.state.panel !== child.props.name
? null
: cloneElement(child, {key: i})
)}
</div>
<PanelMenuWrapperContext.Provider value={this.provideValue()}>
<div className={bem('editor_controls', 'wrapper')}>
<div className={bem('sidebar')}>{menuOpts.map(this.renderSection)}</div>
{React.Children.map(
this.props.children,
(child, i) =>
child === null ||
this.state.group !== child.props.group ||
this.state.panel !== child.props.name
? null
: cloneElement(child, {key: i})
)}
</div>
</PanelMenuWrapperContext.Provider>
);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/components/containers/AnnotationAccordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {PanelMessage} from './PanelEmpty';
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {connectAnnotationToLayout} from 'lib';
import {EditorControlsContext} from '../../context';

const AnnotationFold = connectAnnotationToLayout(PlotlyFold);

Expand Down Expand Up @@ -61,10 +62,7 @@ class AnnotationAccordion extends Component {
}
}

AnnotationAccordion.contextTypes = {
layout: PropTypes.object,
localize: PropTypes.func,
};
AnnotationAccordion.contextType = EditorControlsContext;

AnnotationAccordion.propTypes = {
children: PropTypes.node,
Expand Down
6 changes: 2 additions & 4 deletions src/components/containers/ImageAccordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {connectImageToLayout} from 'lib';
import {PanelMessage} from './PanelEmpty';
import {EditorControlsContext} from '../../context';

const ImageFold = connectImageToLayout(PlotlyFold);

Expand Down Expand Up @@ -66,10 +67,7 @@ class ImageAccordion extends Component {
}
}

ImageAccordion.contextTypes = {
layout: PropTypes.object,
localize: PropTypes.func,
};
ImageAccordion.contextType = EditorControlsContext;

ImageAccordion.propTypes = {
children: PropTypes.node,
Expand Down
6 changes: 2 additions & 4 deletions src/components/containers/Modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {CloseIcon} from 'plotly-icons';
import {ModalProviderContext} from '../../context';

const ModalHeader = ({title, handleClose}) => (
<div className="modal__header">
Expand Down Expand Up @@ -68,10 +69,7 @@ Modal.propTypes = {
title: PropTypes.node,
};

Modal.contextTypes = {
handleClose: PropTypes.func,
isAnimatingOut: PropTypes.bool,
};
Modal.contextType = ModalProviderContext;

export default Modal;

Expand Down
22 changes: 18 additions & 4 deletions src/components/containers/ModalProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import {ModalProviderContext} from '../../context';

class ModalProvider extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -70,13 +71,26 @@ class ModalProvider extends React.Component {
};
}

provideValue() {
return {
openModal: (c, p) => this.openModal(c, p),
closeModal: () => this.closeModal(),
handleClose: () => this.handleClose(),
isAnimatingOut: this.state.isAnimatingOut,
};
}

render() {
const {component: Component, componentProps, isAnimatingOut} = this.state;
return (
<Fragment>
{this.props.children}
{this.state.open ? <Component isAnimatingOut={isAnimatingOut} {...componentProps} /> : null}
</Fragment>
<ModalProviderContext.Provider value={this.provideValue()}>
<Fragment>
{this.props.children}
{this.state.open ? (
<Component isAnimatingOut={isAnimatingOut} {...componentProps} />
) : null}
</Fragment>
</ModalProviderContext.Provider>
);
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/components/containers/PlotlyFold.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {Component} from 'react';
import classnames from 'classnames';
import {CloseIcon, AngleDownIcon} from 'plotly-icons';
import {unpackPlotProps, containerConnectedContextTypes, striptags} from 'lib';
import {PlotlyFoldContext} from '../../context';

export class Fold extends Component {
constructor() {
Expand All @@ -17,9 +18,15 @@ export class Fold extends Component {
};
}

provideValue() {
return {
foldInfo: this.props.foldInfo ? this.props.foldInfo : null,
};
}

render() {
if (!this.foldVisible && !this.props.messageIfEmpty) {
return null;
return <PlotlyFoldContext.Provider value={this.getChildContext()} />;
}
const {deleteContainer} = this.context;
const {
Expand Down Expand Up @@ -97,10 +104,12 @@ export class Fold extends Component {
const classes = className ? ' ' + className : '';

return (
<div className={`fold${classes}`}>
{foldHeader}
{foldContent}
</div>
<PlotlyFoldContext.Provider value={this.provideValue()}>
<div className={`fold${classes}`}>
{foldHeader}
{foldContent}
</div>
</PlotlyFoldContext.Provider>
);
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/components/containers/PlotlyPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {Component, cloneElement} from 'react';
import update from 'immutability-helper';
import {bem} from 'lib';
import {EmbedIconIcon} from 'plotly-icons';
import {PlotlyPanelContext} from '../../context';

class PanelErrorImpl extends Component {
render() {
Expand Down Expand Up @@ -41,6 +42,12 @@ export class Panel extends Component {
};
}

provideValue() {
return {
deleteContainer: this.props.deleteAction ? this.props.deleteAction : null,
};
}

componentDidCatch() {
this.setState({hasError: true});
}
Expand Down Expand Up @@ -88,7 +95,11 @@ export class Panel extends Component {
const {individualFoldStates, hasError} = this.state;

if (hasError) {
return <PanelError />;
return (
<PlotlyPanelContext.Provider value={this.provideValue()}>
<PanelError />
</PlotlyPanelContext.Provider>
);
}

const newChildren = React.Children.map(this.props.children, (child, index) => {
Expand All @@ -103,15 +114,17 @@ export class Panel extends Component {
});

return (
<div className={`panel${this.props.noPadding ? ' panel--no-padding' : ''}`}>
<PanelHeader
addAction={this.props.addAction}
allowCollapse={this.props.showExpandCollapse && individualFoldStates.length > 1}
toggleFolds={this.toggleFolds}
hasOpen={individualFoldStates.some(s => s === false)}
/>
<div className={bem('panel', 'content')}>{newChildren}</div>
</div>
<PlotlyPanelContext.Provider value={this.provideValue()}>
<div className={`panel${this.props.noPadding ? ' panel--no-padding' : ''}`}>
<PanelHeader
addAction={this.props.addAction}
allowCollapse={this.props.showExpandCollapse && individualFoldStates.length > 1}
toggleFolds={this.toggleFolds}
hasOpen={individualFoldStates.some(s => s === false)}
/>
<div className={bem('panel', 'content')}>{newChildren}</div>
</div>
</PlotlyPanelContext.Provider>
);
}
}
Expand Down
Loading