import React, { useEffect, useMemo, useState } from 'react'; import { type FormSection, type FormPage } from '../../../types'; import { isTrue } from '../../../utils/boolean-utils'; import { useTranslation } from 'react-i18next'; import { SectionRenderer } from '../section/section-renderer.component'; import { Waypoint } from 'react-waypoint'; import styles from './page.renderer.scss'; import { Accordion, AccordionItem } from '@carbon/react'; import { ChevronDownIcon, ChevronUpIcon } from '@openmrs/esm-framework'; import classNames from 'classnames'; import { pageObserver } from '../../sidebar/page-observer'; interface PageRendererProps { page: FormPage; isFormExpanded: boolean; } interface CollapsibleSectionContainerProps { section: FormSection; sectionIndex: number; visibleSections: FormSection[]; isFormExpanded: boolean; } function PageRenderer({ page, isFormExpanded }: PageRendererProps) { const { t } = useTranslation(); const [isCollapsed, setIsCollapsed] = useState(false); const visibleSections = useMemo( () => page.sections.filter((section) => { const hasVisibleQuestions = section.questions.some((question) => !isTrue(question.isHidden)); return !isTrue(section.isHidden) && hasVisibleQuestions; }), [page.sections], ); const toggleCollapse = () => setIsCollapsed(!isCollapsed); useEffect(() => { setIsCollapsed(!isFormExpanded); return () => { pageObserver.removeInactivePage(page.id); }; }, [isFormExpanded]); return (
pageObserver.addActivePage(page.id)} onLeave={() => pageObserver.removeInactivePage(page.id)} topOffset="40%" bottomOffset="40%">

{t(page.label)} {isCollapsed ? ( ) : ( )}

{visibleSections.map((section, index) => ( ))}
); } function CollapsibleSectionContainer({ section, sectionIndex, visibleSections, isFormExpanded, }: CollapsibleSectionContainerProps) { const { t } = useTranslation(); const [isSectionOpen, setIsSectionOpen] = useState(isFormExpanded); useEffect(() => { setIsSectionOpen(isFormExpanded); }, [isFormExpanded]); return (
); } export default PageRenderer;