Skip to content

Menupanel does not trigger section visibility by default #99

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
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
25 changes: 16 additions & 9 deletions src/components/containers/Section.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Info from '../fields/Info';
import MenuPanel from './MenuPanel';
import React, {Component, cloneElement} from 'react';
import PropTypes from 'prop-types';
import unpackPlotProps from '../../lib/unpackPlotProps';
import {containerConnectedContextTypes} from '../../lib/connectToContainer';

function childIsVisible(child) {
return Boolean((child.props.plotProps || {}).isVisible);
const attrVisible = Boolean((child.props.plotProps || {}).isVisible);
const sectionVisible = Boolean(child.props['data-section-child-visible']);
return attrVisible || sectionVisible;
}

export default class Section extends Component {
Expand Down Expand Up @@ -47,6 +50,7 @@ export default class Section extends Component {

const isAttr = Boolean(child.props.attr);
let plotProps;
let newProps = {};
if (child.plotProps) {
plotProps = child.plotProps;
} else if (isAttr) {
Expand All @@ -58,15 +62,19 @@ export default class Section extends Component {
} else {
plotProps = unpackPlotProps(child.props, context);
}

// assign plotProps as a prop of children. If they are connectedToContainer
// it will see plotProps and skip recomputing them.
newProps = {plotProps, key: i};
} else if (child.type === Info) {
// Info panels do not change section visibility.
newProps = {key: i, 'data-section-child-visible': false};
} else {
plotProps = {isVisible: true};
// custom UI currently forces section visibility.
newProps = {key: i, 'data-section-child-visible': true};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you have a use case in mind for this right now? I'm trying to figure out whether it seems more like <Info> or more like an attr.

Copy link
Member Author

Choose a reason for hiding this comment

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

For example if you want to add a business logic component that isn't tied to the editor. A data importer component or a component that adds buttons and range sliders but doesn't have an attr

Copy link
Member Author

Choose a reason for hiding this comment

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

If you add your own stuff to a Section the section will be visible. At that point it's up to the developer to add their own visibility handling.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense, I guess the last resort clause should be to make it visible. So I guess in the most generic case you'd add plotProps with isVisible for your own custom handling?

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 that is exactly what CanvasSize and AxesRange and friends use by defining a modifyPlotProps function that overrides plotProps.isVisible

}

// assign plotProps as a prop of children. If they are connectedToContainer
// it will see plotProps and skip recomputing them.
const childProps = Object.assign({plotProps}, child.props);

childProps.key = i;
const childProps = Object.assign(newProps, child.props);
attrChildren.push(cloneElement(child, childProps));
}

Expand All @@ -76,8 +84,7 @@ export default class Section extends Component {

render() {
const hasVisibleChildren =
(this.children && this.children.some(childIsVisible)) ||
Boolean(this.menuPanel);
this.children && this.children.some(childIsVisible);

return hasVisibleChildren ? (
<div className="section">
Expand Down
45 changes: 39 additions & 6 deletions src/components/containers/__tests__/Section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ describe('Section', () => {
const wrapper = mount(
<TestEditor onUpdate={jest.fn()} {...fixtures.scatter()}>
<Section name="test-section">
<Info>INFO</Info>
<div className="extra">special extra</div>
</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');
expect(wrapper.find('.extra').text()).toBe('special extra');
});

it('is not visible if it contains no visible children', () => {
Expand All @@ -81,21 +80,55 @@ describe('Section', () => {
expect(wrapper.find(Numeric).exists()).toBe(false);
});

it('will render first menuPanel even with no visible attrs', () => {
it('will render first menuPanel', () => {
const TraceSection = connectTraceToPlot(Section);
const wrapper = mount(
<TestEditor onUpdate={jest.fn()} {...fixtures.scatter()}>
<Section name="test-section">
<TraceSection name="test-section" traceIndex={0}>
<Numeric attr="opacity" traceIndex={0} />
<MenuPanel show>
<Info>INFO</Info>
</MenuPanel>
<MenuPanel show>
<Info>MISINFORMATION</Info>
</MenuPanel>
</Section>
</TraceSection>
</TestEditor>
).find('[name="test-section"]');

expect(wrapper.find(MenuPanel).length).toBe(1);
expect(wrapper.find(Info).length).toBe(1);
expect(wrapper.find(Info).text()).toBe('INFO');
});

it('will hide with MenuPanel children when attrs not defined', () => {
const TraceSection = connectTraceToPlot(Section);
const wrapper = mount(
<TestEditor onUpdate={jest.fn()} {...fixtures.scatter()}>
<TraceSection name="test-section" traceIndex={0}>
<Numeric attr="badattr" traceIndex={0} />
<MenuPanel show>
<Info>INFO</Info>
</MenuPanel>
</TraceSection>
</TestEditor>
).find('[name="test-section"]');

expect(wrapper.find(MenuPanel).length).toBe(0);
expect(wrapper.find(Info).length).toBe(0);
});

it('will hide with Info children when attrs not defined', () => {
const TraceSection = connectTraceToPlot(Section);
const wrapper = mount(
<TestEditor onUpdate={jest.fn()} {...fixtures.scatter()}>
<TraceSection name="test-section" traceIndex={0}>
<Numeric attr="badattr" traceIndex={0} />
<Info>INFO</Info>
</TraceSection>
</TestEditor>
).find('[name="test-section"]');

expect(wrapper.find(Info).length).toBe(0);
});
});