diff --git a/src/custom-components/ReorderableList.tsx b/src/custom-components/ReorderableList.tsx index 4e8cc5a..0c210f7 100644 --- a/src/custom-components/ReorderableList.tsx +++ b/src/custom-components/ReorderableList.tsx @@ -1,26 +1,35 @@ -import { Fragment, JSXElementConstructor, ReactElement, useEffect, useState } from "react"; -import { Field, FieldProps, Focusable, GamepadButton } from "../deck-components"; +import { CSSProperties, Fragment, JSXElementConstructor, ReactElement, useEffect, useState } from 'react'; +import { Field, FieldProps, Focusable, GamepadButton } from '../deck-components'; + +/** + * A ReorderableList entry of type . + */ export type ReorderableEntry = { - label: string, - data?:T, - position:number -} + label: string; + data?: T; + position: number; +}; +/** + * Properties for a ReorderableList component of type . + */ type ListProps = { - entries: ReorderableEntry[], - onSave: (entries: ReorderableEntry[]) => void, - interactables?: JSXElementConstructor<{entry:ReorderableEntry}>, - fieldProps?: FieldProps -} + entries: ReorderableEntry[]; + onSave: (entries: ReorderableEntry[]) => void; + interactables?: JSXElementConstructor<{ entry: ReorderableEntry }>; + fieldProps?: FieldProps; +}; /** * A component for creating reorderable lists. - * - * Implementation example can be found {@link https://github.com/Tormak9970/Component-Testing-Plugin/blob/main/src/testing-window/ReorderableListTest.tsx here}. + * + * See an example implementation {@linkplain https://github.com/Tormak9970/Component-Testing-Plugin/blob/main/src/testing-window/ReorderableListTest.tsx here}. */ export function ReorderableList(props: ListProps) { - const [entryList, setEntryList] = useState[]>(props.entries.sort((a:ReorderableEntry, b:ReorderableEntry) => a.position - b.position)); + const [entryList, setEntryList] = useState[]>( + props.entries.sort((a: ReorderableEntry, b: ReorderableEntry) => a.position - b.position), + ); const [reorderEnabled, setReorderEnabled] = useState(false); useEffect(() => { @@ -31,52 +40,58 @@ export function ReorderableList(props: ListProps) { let newReorderValue = !reorderEnabled; setReorderEnabled(newReorderValue); - if (!newReorderValue){ + if (!newReorderValue) { props.onSave(entryList); } } return ( - -
+
- { - entryList.map((entry: ReorderableEntry) => ( - - {props.interactables ? : null} - - )) - } + onSecondaryActionDescription={reorderEnabled ? 'Save Order' : 'Reorder'} + onClick={toggleReorderEnabled} + > + {entryList.map((entry: ReorderableEntry) => ( + + {props.interactables ? : null} + + ))}
); } +/** + * Properties for a ReorderableItem component of type + */ type ListEntryProps = { - fieldProps?: FieldProps, - listData: ReorderableEntry[], - entryData: ReorderableEntry, - reorderEntryFunc: CallableFunction, - reorderEnabled: boolean, - children: ReactElement | null -} + fieldProps?: FieldProps; + listData: ReorderableEntry[]; + entryData: ReorderableEntry; + reorderEntryFunc: CallableFunction; + reorderEnabled: boolean; + children: ReactElement | null; +}; function ReorderableItem(props: ListEntryProps) { const listEntries = props.listData; @@ -91,10 +106,10 @@ function ReorderableItem(props: ListEntryProps) { let targetPosition: number = -1; if (event.detail.button == GamepadButton.DIR_DOWN) { - targetPosition = currentIdxValue.position+1; + targetPosition = currentIdxValue.position + 1; } else if (event.detail.button == GamepadButton.DIR_UP) { - targetPosition = currentIdxValue.position-1; - } + targetPosition = currentIdxValue.position - 1; + } if (targetPosition >= listEntries.length || targetPosition < 0) return; @@ -106,22 +121,27 @@ function ReorderableItem(props: ListEntryProps) { currentIdxValue.position = otherToUpdate.position; otherToUpdate.position = currentPosition; - props.reorderEntryFunc([...listEntries].sort((a:ReorderableEntry, b:ReorderableEntry) => a.position - b.position)); + props.reorderEntryFunc( + [...listEntries].sort((a: ReorderableEntry, b: ReorderableEntry) => a.position - b.position), + ); } - const baseCssProps = { - display: "flex", - flexDirection: "row", - justifyContent: "space-between", - width: "100%" + const baseCssProps: CSSProperties = { + display: 'flex', + flexDirection: 'row', + justifyContent: 'space-between', + width: '100%', }; - return( - // @ts-ignore - - - {props.children} - + return ( + + {props.children} ); -} \ No newline at end of file +}