Skip to content

Instantly share code, notes, and snippets.

@andrewhl
Created November 20, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewhl/b67ab21349b4608f4e6c02771590cb1e to your computer and use it in GitHub Desktop.
Save andrewhl/b67ab21349b4608f4e6c02771590cb1e to your computer and use it in GitHub Desktop.
renderMenuItems() {
let i = 0;
return this.props.sections.map((section, index) => {
i ++;
return [
<MenuItemTarget
onDrop={this.handleDrop}
position={index - 1}
key={i * 100}
/>,
<MenuItem
key={i}
section={section}
active={false}
>
{section.title || 'Untitled'}
</MenuItem>,
(index === this.props.sections.length - 1) ? <MenuItemTarget
onDrop={this.handleDrop}
position={index}
key={i * 1000} />
: null,
]
});
}
// Reorders sections
handleDrop = (props, monitor, component) => {
const sections = _.clone(this.props.sections);
const droppedSection = monitor.getItem();
let newPosition = props.position;
const oldPosition = droppedSection.position;
if (newPosition === oldPosition) {
return;
}
// Funky math just to get the positioning to work properly.
if (newPosition <= oldPosition - 1) {
newPosition += 1;
}
// move is defined above
const movedSections = move(sections, oldPosition, newPosition);
const reorderedSections = movedSections.map((section, index) => {
section.position = index;
return section;
});
return this.props.reorderSections({
articleUuid: this.props.article.uuid,
sections: reorderedSections,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment