From 3fbeb4207dc9e52663260b179e4a1d19ae00f646 Mon Sep 17 00:00:00 2001 From: Braulio Date: Sun, 11 Aug 2024 13:25:09 +0200 Subject: [PATCH] refactor --- .../accordion.business.ts | 23 +++++++++++++++++++ .../front-rich-components/accordion.tsx | 19 +++++++-------- .../accordion-all-parts.component.tsx | 5 ++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/common/components/front-rich-components/accordion.business.ts b/src/common/components/front-rich-components/accordion.business.ts index 79a9409d..f858a31c 100644 --- a/src/common/components/front-rich-components/accordion.business.ts +++ b/src/common/components/front-rich-components/accordion.business.ts @@ -69,3 +69,26 @@ export const mapTextToSections = (text: string): SectionsInfo => { selectedSectionIndex === -1 ? 0 : selectedSectionIndex, }; }; + +// TODO: Add unit tests +interface SelectedAccordionSizeInfo { + height: number; + minimumAccordionBodyHeight: number; + singleHeaderHeight: number; +} + +export const calculateSelectedAccordionHeight = ( + sections: string[], + sizeInfo: SelectedAccordionSizeInfo +) => { + const { height, minimumAccordionBodyHeight, singleHeaderHeight } = sizeInfo; + + const accordionsHeadersHeight = singleHeaderHeight * sections.length; + let accordionSelectedBodyHeight = height - accordionsHeadersHeight; + + if (accordionSelectedBodyHeight < 0) { + accordionSelectedBodyHeight = minimumAccordionBodyHeight; + } + + return accordionSelectedBodyHeight; +}; diff --git a/src/common/components/front-rich-components/accordion.tsx b/src/common/components/front-rich-components/accordion.tsx index 81cbdcae..dd7a0113 100644 --- a/src/common/components/front-rich-components/accordion.tsx +++ b/src/common/components/front-rich-components/accordion.tsx @@ -5,6 +5,7 @@ import { ShapeProps } from '../front-components/shape.model'; import { AccordionAllParts } from './components'; import { calculateDynamicContentSizeRestriction, + calculateSelectedAccordionHeight, mapTextToSections, } from './accordion.business'; @@ -26,8 +27,8 @@ const minimumAccordionBodyHeight = 60; export const AccordionShape = forwardRef( ({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => { const [sections, setSections] = useState([ - 'Sección A', - 'Sección B', + '[*] Sectión A', + 'Sectión B', ]); const [selectedSectionIndex, setSelectedSectionIndex] = useState(0); @@ -35,7 +36,6 @@ export const AccordionShape = forwardRef( if (text) { const { sections, selectedSectionIndex } = mapTextToSections(text); setSections(sections); - // right now let's set a default value, TODO enhance this setSelectedSectionIndex(selectedSectionIndex); } else { setSections([]); @@ -43,14 +43,11 @@ export const AccordionShape = forwardRef( }, [text]); const accordionSelectedBodyHeight = useMemo(() => { - const accordionsHeadersHeight = singleHeaderHeight * sections.length; - let accordionSelectedBodyHeight = height - accordionsHeadersHeight; - - if (accordionSelectedBodyHeight < 0) { - accordionSelectedBodyHeight = minimumAccordionBodyHeight; - } - - return accordionSelectedBodyHeight; + return calculateSelectedAccordionHeight(sections, { + height, + minimumAccordionBodyHeight, + singleHeaderHeight, + }); }, [sections, height]); const { width: restrictedWidth, height: restrictedHeight } = diff --git a/src/common/components/front-rich-components/components/accordion-all-parts.component.tsx b/src/common/components/front-rich-components/components/accordion-all-parts.component.tsx index 37c34d0e..71e94b7e 100644 --- a/src/common/components/front-rich-components/components/accordion-all-parts.component.tsx +++ b/src/common/components/front-rich-components/components/accordion-all-parts.component.tsx @@ -22,11 +22,12 @@ export const AccordionAllParts: React.FC = props => { const renderAccordionBody = (headerIndex: number) => { accordionBodyAppliedOffset = accordionSelectedBodyHeight; + const marginLeft = 10; return ( );