Skip to content
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

#13: [Form editor] Drag to reorder form fields #90

Merged
merged 1 commit into from
Jul 19, 2019
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
10 changes: 10 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"react-redux-firebase": "^2.1.8",
"react-router": "^4.3.1",
"react-scripts": "2.1.1",
"react-sortable-hoc": "^1.9.1",
"react-swipeable-views": "^0.12.17",
"react-tint": "^2.0.0",
"redux": "^4.0.0",
Expand Down
80 changes: 71 additions & 9 deletions web/src/components/gnd-feature-type-editor/gnd-form-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ import PropTypes from 'prop-types';
import GndFormElementEditor from './gnd-form-element-editor';
import GndFormWarning from './gnd-form-warning';
import update from 'immutability-helper';
import {Add} from '@material-ui/icons';
import {Add, DragHandle} from '@material-ui/icons';
import Button from '@material-ui/core/Button';
import {withStyles} from '@material-ui/core/styles';
import DeleteForeverIcon from '@material-ui/icons/DeleteForever';
import {
sortableContainer,
sortableElement,
sortableHandle,
} from 'react-sortable-hoc';

const styles = (theme) => ({
button: {
Expand All @@ -46,6 +51,19 @@ const styles = (theme) => ({
display: 'block',
width: '100%',
},
sortableElement: {
zIndex: 1300,
'&:hover div[name=sortableHandle]': {
visibility: 'visible',
}
},
sortableHandle: {
textAlign: 'center',
visibility: 'hidden',
'&:hover': {
cursor: 'move',
}
}
});

class GndFormEditor extends React.Component {
Expand Down Expand Up @@ -102,21 +120,65 @@ class GndFormEditor extends React.Component {
onChange({id: form.id, defn: undefined});
};

onSortEnd = ({oldIndex, newIndex}) => {
const {form, onChange} = this.props;
const oldIndexElement = form.defn.elements[oldIndex];
const newIndexElement = form.defn.elements[newIndex];
onChange(
update(form, {
defn: {
elements: {
[oldIndex]: {$set: newIndexElement},
[newIndex]: {$set: oldIndexElement}
}
},
})
);
};

render() {
const {form, classes} = this.props;
if (!form || !form.defn || !form.defn.elements) {
return null;
}
const formElements = form.defn.elements.map((element, idx) => (
<GndFormElementEditor
key={element.id}
element={element}
onChange={(el) => this.handleElementChange(el, idx)}
/>
));

const SortableHandle = sortableHandle(() =>
<div className={classes.sortableHandle} name="sortableHandle">
<DragHandle fontSize="inherit"/>
</div>
);

const SortableElement = sortableElement(({element, onChange}) =>
<div className={classes.sortableElement}>
<SortableHandle />
<GndFormElementEditor
key={element.id}
element={element}
onChange={onChange}
/>
</div>
);

const SortableContainer = sortableContainer(({children}) =>
<div>{children}</div>
);

return (
<React.Fragment>
{formElements}
<SortableContainer
onSortEnd={this.onSortEnd}
lockAxis='y'
useDragHandle
>
{form.defn.elements.map((element, index) => (
<SortableElement
key={`item-${index}`}
index={index}
element={element}
onChange={(el) => this.handleElementChange(el, index)}
/>
))}
</SortableContainer>
<div className={classes.bottomControls}>
<span className={classes.bottomLeftControls}>
<Button
Expand Down