Skip to content

SCSS and Legend Panel #54

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 15 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 13 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
37 changes: 37 additions & 0 deletions src/DefaultEditor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
CogMenu,
ColorPicker,
DataSelector,
Dropdown,
Flaglist,
Fold,
Info,
Numeric,
Panel,
PanelMenuWrapper,
Expand Down Expand Up @@ -202,6 +204,7 @@ class DefaultEditor extends Component {
attr="legend.font.size"
postfix="px"
/>
<ColorPicker label={_('Color')} attr="legend.font.color" />
</Section>
<Section name={_('Legend Box')}>
<Numeric
Expand All @@ -210,8 +213,42 @@ class DefaultEditor extends Component {
attr="legend.borderwidth"
postfix="px"
/>
<ColorPicker
label={_('Border Color')}
attr="legend.bordercolor"
/>
<ColorPicker
label={_('Background Color')}
attr="legend.bgcolor"
/>
</Section>
<Section name={_('Positioning')}>
<CogMenu>
<Section>
<Info>
{_(
'The positioning inputs are relative to the ' +
'anchor points on the text box'
)}
</Info>
<Radio
attr="legend.xanchor"
options={[
{label: _('Left'), value: 'left'},
{label: _('Center'), value: 'center'},
{label: _('Right'), value: 'right'},
]}
/>
<Radio
attr="legend.orientation"
options={[
{label: _('Top'), value: 'top'},
{label: _('Middle'), value: 'middle'},
{label: _('Bottom'), value: 'bottom'},
]}
/>
</Section>
</CogMenu>
<Numeric
label={_('X Position')}
step={0.01}
Expand Down
8 changes: 3 additions & 5 deletions src/components/containers/Fold.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default class Fold extends Component {
const {canDelete, name} = this.props;
const doDelete = canDelete && typeof deleteContainer === 'function';
return (
<div className={bem('accordion-panel', 'top', ['active'])}>
<div className={bem('fold', 'top', ['active'])}>
{this.props.name}
{doDelete ? (
<a
className={bem('accordion-panel', 'delete')}
className={bem('fold', 'delete')}
href="#"
onClick={deleteContainer}
>
Expand All @@ -28,9 +28,7 @@ export default class Fold extends Component {
return (
<div>
{this.props.hideHeader ? null : this.renderHeader()}
<div className={bem('accordion-panel', 'panel', modifiers)}>
{this.props.children}
</div>
<div className={bem('fold', modifiers)}>{this.props.children}</div>
</div>
);
}
Expand Down
42 changes: 31 additions & 11 deletions src/components/containers/Section.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SubPanel from './SubPanel';
import React, {Component, cloneElement} from 'react';
import PropTypes from 'prop-types';
import {bem} from '../../lib';
import {icon} from '../../lib';
import unpackPlotProps from '../../lib/unpackPlotProps';

function childIsVisible(child) {
Expand All @@ -11,42 +12,61 @@ class Section extends Component {
constructor(props, context) {
super(props, context);

this.children = this.processChildren(context);
this.children = null;
this.subPanel = null;

this.processAndSetChildren(context);
}

componentWillReceiveProps(nextProps, nextContext) {
this.children = this.processChildren(nextContext);
this.processAndSetChildren(nextContext);
}

processChildren(context) {
processAndSetChildren(context) {
let children = this.props.children;
if (!Array.isArray(children)) {
children = [children];
}
children = children.filter(c => Boolean(c));

const attrChildren = [];
let subPanel = null;

for (let i = 0; i < children.length; i++) {
let child = children[i];
if (!child) {
continue;
}
if (child.type === SubPanel) {
// Process the first subPanel. Ignore the rest.
if (subPanel) {
continue;
}
subPanel = child;
continue;
}

let isAttr = !!child.props.attr;
let plotProps = isAttr
? unpackPlotProps(child.props, context, child.constructor)
: {};
: {isVisible: true};
let childProps = Object.assign({plotProps}, child.props);
childProps.key = i;

children[i] = cloneElement(child, childProps, child.children);
attrChildren.push(cloneElement(child, childProps));
}

return children;
this.children = attrChildren.length ? attrChildren : null;
this.subPanel = subPanel;
}

render() {
const hasVisibleChildren = this.children.some(childIsVisible);

return hasVisibleChildren ? (
<div className={bem('section')}>
<div className={bem('section', 'heading')}>{this.props.name}</div>
<div className="section">
<div className="section__heading">
{this.props.name}
{this.subPanel}
</div>
{this.children}
</div>
) : null;
Expand Down
40 changes: 40 additions & 0 deletions src/components/containers/SubPanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';

export default class SubPanel extends Component {
constructor() {
super();
this.state = {isVisible: false};

this.toggleVisibility = this.toggleVisibility.bind(this);
}

toggleVisibility() {
this.setState({isVisible: !this.state.isVisible});
}

render() {
const toggleClass = `subpanel__toggle ${this.props.toggleIconClass}`;
return (
<span>
<span>
<i className={toggleClass} onClick={this.toggleVisibility} />
</span>
{this.state.isVisible ? (
<div className="subpanel">
<div className="subpanel__cover" onClick={this.toggleVisibility} />
<div>{this.props.children}</div>
</div>
) : null}
</span>
);
}
}

SubPanel.propTypes = {
toggleIconClass: PropTypes.string.isRequired,
};

SubPanel.defaultProps = {
toggleIconClass: 'plotlyjs_editor__icon-cog',
};
20 changes: 19 additions & 1 deletion src/components/containers/__tests__/Section-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Section from '../Section';
import {Flaglist, Numeric} from '../../fields';
import {Flaglist, Info, Numeric} from '../../fields';
import {TestEditor, fixtures, plotly} from '../../../lib/test-utils';
import {connectTraceToPlot} from '../../../lib';
import {mount} from 'enzyme';
Expand Down Expand Up @@ -47,6 +47,24 @@ describe('Section', () => {
).toBe(false);
});

fit('is visible if it contains any non attr children', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this behavior? Seems kinda like you wouldn't want <Info> to be enough to invoke display, or maybe sometimes you do but not always?

⚠️ committed fit ⚠️ (you probably fixed this later... but at least in plotly.js we have a syntax test checking for fit et al in all test files)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I did catch that one. And yep we have the same test in the workspace. I'll make an issue for that right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#57

Hmmm yeh I could see skipping <Info> actually. But not <MenuPanel>? Unless you interrogate the <Section>s inside <MenuPanel> ... but that would take some serious gymnastics...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, that's your caveat above. It might be worth thinking a bit about whether there really are any cases where only the <MenuPanel> exists to make a <Section> visible. If that's really "advanced settings" we might be able to argue that if none of the basics for that section exist, there's never a case where the advanced would be needed?

I'm not sure if any similar argument works for <Panel>. Would be worthwhile looking for a concrete example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True I'll add this to an issue and deal with it once I have some breathing room

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#58

const wrapper = mount(
<TestEditor
plotly={plotly}
onUpdate={jest.fn()}
{...fixtures.scatter({deref: true})}
>
<Section name="test-section">
<Info>INFO</Info>
</Section>
</TestEditor>
).find('[name="test-section"]');

expect(wrapper.children().length).toBe(1);
expect(wrapper.find(Info).exists()).toBe(true);
expect(wrapper.find(Info).text()).toBe('INFO');
});

it('is not visible if it contains no visible children', () => {
// pull and hole are not scatter attrs. Section should not show.
const wrapper = mount(
Expand Down
3 changes: 2 additions & 1 deletion src/components/containers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SubPanel from './SubPanel';
import Fold from './Fold';
import Panel from './Panel';
import Section from './Section';
import TraceAccordion from './TraceAccordion';

export {Fold, Panel, Section, TraceAccordion};
export {SubPanel, Fold, Panel, Section, TraceAccordion};
13 changes: 13 additions & 0 deletions src/components/fields/Info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Field from './Field';
import PropTypes from 'prop-types';
import React, {Component} from 'react';

export default class Info extends Component {
render() {
return <Field {...this.props}>{this.props.children}</Field>;
}
}

Info.propTypes = {
...Field.propTypes,
};
2 changes: 2 additions & 0 deletions src/components/fields/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ColorPicker from './Color';
import Dropdown from './Dropdown';
import Flaglist from './Flaglist';
import Info from './Info';
import Radio from './Radio';
import DataSelector from './DataSelector';
import Numeric from './Numeric';
Expand All @@ -10,6 +11,7 @@ export {
ColorPicker,
Dropdown,
Flaglist,
Info,
Radio,
DataSelector,
Numeric,
Expand Down
5 changes: 4 additions & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import {
ColorPicker,
Dropdown,
Flaglist,
Info,
Radio,
DataSelector,
Numeric,
TraceSelector,
} from './fields';

import {Fold, Panel, Section, TraceAccordion} from './containers';
import {SubPanel, Fold, Panel, Section, TraceAccordion} from './containers';
import PanelMenuWrapper from './PanelMenuWrapper';

export {
SubPanel,
ColorPicker,
DataSelector,
Dropdown,
Flaglist,
Fold,
Info,
Numeric,
Panel,
PanelMenuWrapper,
Expand Down
Loading