Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
FormEditorView: reduce braindamage in swapFields
Browse files Browse the repository at this point in the history
  • Loading branch information
japsu committed Feb 2, 2019
1 parent 3af5b7f commit c297b9e
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/components/FormEditorView.tsx
Expand Up @@ -259,19 +259,18 @@ export default class FormEditorView extends React.Component<RouteComponentProps<

protected moveUp(fieldName: string) {
const index = this.state.fields.findIndex(field => field.name === fieldName);
this.swapFields(index - 1, index);
this.swapWithNextField(index - 1);
}

protected moveDown(fieldName: string) {
const index = this.state.fields.findIndex(field => field.name === fieldName);
this.swapFields(index, index + 1);
this.swapWithNextField(index);
}

protected swapFields(smallerIndex: number, largerIndex: number) {
protected swapWithNextField(index: number) {
const { fields } = this.state;
const newFields = fields.slice(0, smallerIndex)
.concat([fields[largerIndex], fields[smallerIndex]])
.concat(fields.slice(largerIndex + 1));
const newFields = fields.slice();
newFields.splice(index + 1, 0, newFields.splice(index, 1)[0]);
this.setState({ fields: newFields });
}
}

0 comments on commit c297b9e

Please sign in to comment.