Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
brauliodiez committed Aug 11, 2024
1 parent f1c45a2 commit 3fbeb42
Showing 3 changed files with 34 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/common/components/front-rich-components/accordion.business.ts
Original file line number Diff line number Diff line change
@@ -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;
};
19 changes: 8 additions & 11 deletions src/common/components/front-rich-components/accordion.tsx
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { ShapeProps } from '../front-components/shape.model';
import { AccordionAllParts } from './components';
import {
calculateDynamicContentSizeRestriction,
calculateSelectedAccordionHeight,
mapTextToSections,
} from './accordion.business';

@@ -26,31 +27,27 @@ const minimumAccordionBodyHeight = 60;
export const AccordionShape = forwardRef<any, ShapeProps>(
({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => {
const [sections, setSections] = useState<string[]>([
'Sección A',
'Sección B',
'[*] Sectión A',
'Sectión B',
]);
const [selectedSectionIndex, setSelectedSectionIndex] = useState(0);

useEffect(() => {
if (text) {
const { sections, selectedSectionIndex } = mapTextToSections(text);
setSections(sections);
// right now let's set a default value, TODO enhance this
setSelectedSectionIndex(selectedSectionIndex);
} else {
setSections([]);
}
}, [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 } =
Original file line number Diff line number Diff line change
@@ -22,11 +22,12 @@ export const AccordionAllParts: React.FC<Props> = props => {

const renderAccordionBody = (headerIndex: number) => {
accordionBodyAppliedOffset = accordionSelectedBodyHeight;
const marginLeft = 10;
return (
<AccordionBody
x={10}
x={marginLeft}
y={(headerIndex + 1) * singleHeaderHeight}
width={width - 10}
width={width - marginLeft}
height={accordionSelectedBodyHeight}
/>
);

0 comments on commit 3fbeb42

Please sign in to comment.