Skip to content

Commit 9aaaf96

Browse files
committed
allow custom ordering of default panels
1 parent 9260bb8 commit 9aaaf96

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

dev/App.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,26 @@ class App extends Component {
185185
// fontOptions={[{label:'Arial', value: 'arial'}]}
186186
// chartHelp={chartHelp}
187187
>
188-
<DefaultEditor>
188+
<DefaultEditor
189+
// order={[
190+
// {group: 'Dev', name: 'JSON'},
191+
// {group: 'Dev', name: 'Inspector'},
192+
// {group: 'Structure', name: 'Create'},
193+
// {group: 'Structure', name: 'Subplots'},
194+
// {group: 'Structure', name: 'Transforms'},
195+
// {group: 'Test', name: 'Testing'},
196+
// {group: 'Style', name: 'General'},
197+
// {group: 'Style', name: 'Traces'},
198+
// {group: 'Style', name: 'Axes'},
199+
// {group: 'Style', name: 'Legend'},
200+
// {group: 'Style', name: 'Color Bars'},
201+
// {group: 'Style', name: 'Annotation'},
202+
// {group: 'Style', name: 'Shapes'},
203+
// {group: 'Style', name: 'Images'},
204+
// {group: 'Style', name: 'Sliders'},
205+
// {group: 'Style', name: 'Menus'},
206+
// ]}
207+
>
189208
<Panel group="Dev" name="JSON">
190209
<div className="mocks">
191210
<Select

src/DefaultEditor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class DefaultEditor extends Component {
6969
const logo = this.props.logoSrc && <Logo src={this.props.logoSrc} />;
7070

7171
return (
72-
<PanelMenuWrapper>
72+
<PanelMenuWrapper order={this.props.order}>
7373
{logo ? logo : null}
7474
<GraphCreatePanel group={_('Structure')} name={_('Traces')} />
7575
<GraphSubplotsPanel group={_('Structure')} name={_('Subplots')} />
@@ -95,6 +95,7 @@ class DefaultEditor extends Component {
9595
DefaultEditor.propTypes = {
9696
children: PropTypes.node,
9797
logoSrc: PropTypes.string,
98+
order: PropTypes.array,
9899
};
99100

100101
DefaultEditor.contextTypes = {

src/components/PanelMenuWrapper.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,52 @@ class PanelsWithSidebar extends Component {
4545
);
4646
}
4747

48+
getUniqueValues(value, index, self) {
49+
return self.indexOf(value) === index;
50+
}
51+
4852
computeMenuOptions(props) {
49-
const {children} = props;
53+
const {children, order} = props;
5054
const sections = [];
5155
const groupLookup = {};
5256
let groupIndex;
57+
const orderedChildren = React.Children.toArray(children);
58+
59+
if (order) {
60+
const groupOrder = order.map(panel => panel.group).filter(this.getUniqueValues);
61+
const nameOrder = order.map(panel => panel.name).filter(this.getUniqueValues);
62+
63+
orderedChildren.sort((a, b) => {
64+
// if one of the elements is not in the groupOrder array, then it goes to the end of the list
65+
if (groupOrder.includes(a.props.group) && !groupOrder.includes(b.props.group)) {
66+
return -1;
67+
}
68+
if (!groupOrder.includes(a.props.group) && groupOrder.includes(b.props.group)) {
69+
return 1;
70+
}
71+
72+
// if both elements are not in the groupOrder array, they get sorted alphabetically,
73+
// by group, then by name
74+
if (!groupOrder.includes(a.props.group) && !groupOrder.includes(b.props.group)) {
75+
const sortByGroup =
76+
a.props.group === b.props.group ? 0 : a.props.group < b.props.group ? -1 : 1;
77+
const sortByName =
78+
a.props.name === b.props.name ? 0 : a.props.name < b.props.name ? -1 : 1;
79+
return sortByGroup || sortByName;
80+
}
81+
82+
// if both elements are in the groupOrder array, they get sorted according to their order in
83+
// the groupOrder, then nameOrder arrays.
84+
if (groupOrder.includes(a.props.group) && groupOrder.includes(b.props.group)) {
85+
const sortByGroup = groupOrder.indexOf(a.props.group) - groupOrder.indexOf(b.props.group);
86+
const sortByName = nameOrder.indexOf(a.props.name) - nameOrder.indexOf(b.props.name);
87+
return sortByGroup || sortByName;
88+
}
89+
return 0;
90+
});
91+
}
5392

54-
React.Children.forEach(children, child => {
93+
orderedChildren.forEach(child => {
5594
if (!child) {
5695
return;
5796
}
@@ -101,6 +140,7 @@ class PanelsWithSidebar extends Component {
101140

102141
PanelsWithSidebar.propTypes = {
103142
children: PropTypes.node,
143+
order: PropTypes.array,
104144
};
105145

106146
PanelsWithSidebar.childContextTypes = {

0 commit comments

Comments
 (0)