parent
d5baae790c
commit
1751cef896
2 changed files with 72 additions and 0 deletions
@ -0,0 +1,59 @@ |
||||
import { useCallback, useMemo, useState } from 'react'; |
||||
|
||||
import buildObjectStateSetterCallback from '../lib/buildObjectStateSetterCallback'; |
||||
|
||||
import FormSummary from '../components/FormSummary'; |
||||
|
||||
const useChecklist = (): { |
||||
buildDeleteDialogProps: BuildDeleteDialogPropsFunction; |
||||
checklist: Checklist; |
||||
checks: ArrayChecklist; |
||||
getCheck: GetCheckFunction; |
||||
hasChecks: boolean; |
||||
setCheck: SetCheckFunction; |
||||
} => { |
||||
const [checklist, setChecklist] = useState<Checklist>({}); |
||||
|
||||
const checks = useMemo(() => Object.entries(checklist), [checklist]); |
||||
|
||||
const hasChecks = useMemo(() => checks.length > 0, [checks.length]); |
||||
|
||||
const buildDeleteDialogProps = useCallback<BuildDeleteDialogPropsFunction>( |
||||
({ |
||||
confirmDialogProps = {}, |
||||
formSummaryProps = {}, |
||||
getConfirmDialogTitle, |
||||
}) => ({ |
||||
actionProceedText: 'Delete', |
||||
content: ( |
||||
<FormSummary entries={checklist} maxDepth={0} {...formSummaryProps} /> |
||||
), |
||||
proceedColour: 'red', |
||||
titleText: getConfirmDialogTitle(checks.length), |
||||
...confirmDialogProps, |
||||
}), |
||||
[checklist, checks.length], |
||||
); |
||||
|
||||
const getCheck = useCallback<GetCheckFunction>( |
||||
(key) => checklist[key], |
||||
[checklist], |
||||
); |
||||
|
||||
const setCheck = useCallback<SetCheckFunction>( |
||||
(key, checked) => |
||||
setChecklist(buildObjectStateSetterCallback(key, checked || undefined)), |
||||
[], |
||||
); |
||||
|
||||
return { |
||||
buildDeleteDialogProps, |
||||
checklist, |
||||
checks, |
||||
getCheck, |
||||
hasChecks, |
||||
setCheck, |
||||
}; |
||||
}; |
||||
|
||||
export default useChecklist; |
@ -0,0 +1,13 @@ |
||||
type Checklist = Record<string, boolean>; |
||||
|
||||
type ArrayChecklist = [keyof Checklist, Checklist[string]][]; |
||||
|
||||
type BuildDeleteDialogPropsFunction = (args: { |
||||
confirmDialogProps?: Partial<Omit<ConfirmDialogProps, 'content'>>; |
||||
formSummaryProps?: Omit<FormSummaryProps<Checklist>, 'entries'>; |
||||
getConfirmDialogTitle: (length: number) => ReactNode; |
||||
}) => ConfirmDialogProps; |
||||
|
||||
type GetCheckFunction = (key: string) => boolean; |
||||
|
||||
type SetCheckFunction = (key: string, checked?: boolean) => void; |
Loading…
Reference in new issue