-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathsidebar.component.tsx
More file actions
130 lines (123 loc) · 3.87 KB
/
sidebar.component.tsx
File metadata and controls
130 lines (123 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Button, InlineLoading } from '@carbon/react';
import { useLayoutType } from '@openmrs/esm-framework';
import { isPageContentVisible } from '../../utils/form-helper';
import { useCurrentActivePage } from './useCurrentActivePage';
import { usePageObserver } from './usePageObserver';
import type { FormPage, SessionMode } from '../../types';
import styles from './sidebar.scss';
import { Printer } from '@carbon/react/icons';
interface SidebarProps {
defaultPage: string;
isFormSubmitting: boolean;
sessionMode: SessionMode;
onCancel: () => void;
handleClose: () => void;
hideFormCollapseToggle: () => void;
handlePrint: () => void;
}
const Sidebar: React.FC<SidebarProps> = ({
defaultPage,
isFormSubmitting,
sessionMode,
onCancel,
handleClose,
hideFormCollapseToggle,
handlePrint,
}) => {
const { t } = useTranslation();
const { pages, pagesWithErrors, activePages, evaluatedPagesVisibility } = usePageObserver();
const { currentActivePage, requestPage } = useCurrentActivePage({
pages,
defaultPage,
activePages,
evaluatedPagesVisibility,
});
const layout = useLayoutType();
const responsiveSize = useMemo(() => {
const isTablet = layout === 'tablet';
return isTablet ? 'lg' : 'sm';
}, [layout]);
return (
<div className={styles.sidebar}>
{pages
.filter((page) => isPageContentVisible(page))
.map((page) => (
<PageLink
key={page.id}
page={page}
currentActivePage={currentActivePage}
pagesWithErrors={pagesWithErrors}
requestPage={requestPage}
/>
))}
{sessionMode !== 'view' && <hr className={styles.divider} />}
<div className={styles.sideNavActions}>
{sessionMode !== 'view' && (
<Button className={styles.saveButton} disabled={isFormSubmitting} type="submit" size={responsiveSize}>
{isFormSubmitting ? (
<InlineLoading description={t('submitting', 'Submitting') + '...'} />
) : (
<span>{`${t('save', 'Save')}`}</span>
)}
</Button>
)}
<Button
className={classNames(styles.closeButton, {
[styles.topMargin]: sessionMode === 'view',
})}
kind="tertiary"
onClick={() => {
onCancel?.();
handleClose?.();
hideFormCollapseToggle();
}}
size={responsiveSize}>
{sessionMode === 'view' ? t('close', 'Close') : t('cancel', 'Cancel')}
</Button>
<Button
className={classNames(styles.printButton, {
[styles.topMargin]: sessionMode === 'view',
})}
kind="tertiary"
onClick={() => handlePrint()}
size={responsiveSize}>
{t('printForm', 'Print form')}
<Printer />
</Button>
</div>
</div>
);
};
interface PageLinkProps {
page: FormPage;
currentActivePage: string;
pagesWithErrors: string[];
requestPage: (page: string) => void;
}
function PageLink({ page, currentActivePage, pagesWithErrors, requestPage }: PageLinkProps) {
const { t } = useTranslation();
const isActive = page.id === currentActivePage;
const hasError = pagesWithErrors.includes(page.id);
const isTablet = useLayoutType() === 'tablet';
return (
<div
className={classNames(styles.pageLink, {
[styles.activePage]: isActive && !hasError,
[styles.errorPage]: hasError && !isActive,
[styles.activeErrorPage]: hasError && isActive,
[styles.pageLinkTablet]: isTablet,
})}>
<button
onClick={(e) => {
e.preventDefault();
requestPage(page.id);
}}>
<span>{t(page.label)}</span>
</button>
</div>
);
}
export default Sidebar;