-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
21 deletions.
There are no files selected for viewing
Empty file.
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,70 @@ | ||
import { useState, useCallback } from 'react'; | ||
import SortableList, { Attributes, Listeners } from '#components/SortableList'; | ||
|
||
import styles from './index.module.css'; | ||
|
||
interface QuestionGroup { | ||
id: string; | ||
label: string; | ||
name: string; | ||
parentId: string; | ||
questionnaireId: string; | ||
relevant?: string; | ||
} | ||
|
||
function reorder<T extends { order?: number }>(data: T[]) { | ||
return data.map((v, i) => ({ ...v, order: i + 1 })); | ||
} | ||
|
||
const keySelector = (g: QuestionGroup) => g.id; | ||
|
||
interface Props<P> { | ||
parentId: string | null; | ||
options: QuestionGroup[]; | ||
renderer: (props: P & { | ||
listeners?: Listeners; | ||
attributes?: Attributes; | ||
}) => JSX.Element; | ||
rendererParams: P; | ||
} | ||
|
||
function TocList<P>(props: Props<P>) { | ||
const { | ||
parentId, | ||
options, | ||
renderer, | ||
rendererParams, | ||
} = props; | ||
|
||
const filteredOptions = options?.filter( | ||
(group: QuestionGroup) => group.parentId === parentId, | ||
); | ||
|
||
const [ | ||
orderedFilteredOptions, | ||
setFilteredOrderedOptions, | ||
] = useState<QuestionGroup[] | undefined>(filteredOptions); | ||
|
||
const handleGroupOrderChange = useCallback((...args: QuestionGroup[]) => { | ||
setFilteredOrderedOptions(args); | ||
}, []); | ||
|
||
return ( | ||
<SortableList | ||
className={styles.sortableList} | ||
name="toc" | ||
onChange={handleGroupOrderChange} | ||
data={orderedFilteredOptions} | ||
keySelector={keySelector} | ||
renderer={renderer} | ||
rendererParams={rendererParams} | ||
direction="vertical" | ||
emptyMessage="No groups found" | ||
messageShown | ||
messageIconShown | ||
compactEmptyMessage | ||
/> | ||
); | ||
} | ||
|
||
export default TocList; |
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