-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add move helpers
- Loading branch information
Showing
5 changed files
with
48 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { moveUp } from "./moveUp"; | ||
import { moveDown } from "./moveDown"; | ||
|
||
export { moveUp, moveDown }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TemplateStoreState, SetStateFunction } from "../../types"; | ||
import { moveDown as moveElementDown } from "@lib/utils/form-builder"; | ||
|
||
type MoveDownType = (set: SetStateFunction) => TemplateStoreState["moveDown"]; | ||
|
||
export const moveDown: MoveDownType = (set) => (elIndex, groupId) => | ||
set((state) => { | ||
state.form.layout = moveElementDown(state.form.layout, elIndex); | ||
const allowGroups = state.allowGroupsFlag; | ||
if (allowGroups && groupId) { | ||
const group = state.form.groups && state.form.groups[groupId]; | ||
if (group) { | ||
// Convert the elements array to a number array | ||
const els = group.elements.map((el) => Number(el)); | ||
// Move the element down and convert back to string array | ||
group.elements = moveElementDown(els, elIndex).map((el) => String(el)); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TemplateStoreState, SetStateFunction } from "../../types"; | ||
import { moveUp as moveElementUp } from "@lib/utils/form-builder"; | ||
|
||
type MoveUpType = (set: SetStateFunction) => TemplateStoreState["moveUp"]; | ||
|
||
export const moveUp: MoveUpType = (set) => (elIndex, groupId) => | ||
set((state) => { | ||
state.form.layout = moveElementUp(state.form.layout, elIndex); | ||
const allowGroups = state.allowGroupsFlag; | ||
if (allowGroups && groupId) { | ||
const group = state.form.groups && state.form.groups[groupId]; | ||
if (group) { | ||
// Convert the elements array to a number array | ||
const els = group.elements.map((el) => Number(el)); | ||
// Move the element up and convert back to string array | ||
group.elements = moveElementUp(els, elIndex).map((el) => String(el)); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters