Skip to content

Commit 99e998d

Browse files
author
Illia Obukhau
committed
style(accordion-web): change how renderContent initialized
1 parent a701e46 commit 99e998d

File tree

5 files changed

+99
-13
lines changed

5 files changed

+99
-13
lines changed

packages/pluggableWidgets/accordion-web/src/Accordion.editorPreview.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function preview(props: AccordionPreviewProps): ReactElement {
3232
renderer: ({}: { caption: string; children: ReactElement }) => (
3333
<div>Add groups in order to place widgets here.</div>
3434
)
35-
}
35+
},
36+
loadContent: "always"
3637
} as GroupsPreviewType
3738
];
3839

@@ -50,6 +51,7 @@ export function preview(props: AccordionPreviewProps): ReactElement {
5051
<div />
5152
</group.content.renderer>
5253
),
54+
loadContent: group.loadContent,
5355
visible: group.visible as unknown as boolean,
5456
dynamicClassName: group.dynamicClass.slice(1, -1) // expression result is surrounded by single quotes
5557
}));

packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createElement, KeyboardEvent, ReactElement, ReactNode, useCallback, useEffect, useRef, useState } from "react";
22
import classNames from "classnames";
33
import "../ui/accordion-main.scss";
4+
import { LoadContentEnum } from "typings/AccordionProps";
45

56
/* eslint-disable no-unused-vars */
67
export const enum Target {
@@ -27,7 +28,7 @@ export interface AccordionGroupProps {
2728
animateContent?: boolean;
2829
generateHeaderIcon?: (collapsed: boolean) => ReactElement;
2930
showHeaderIcon?: "right" | "left" | "no";
30-
loadContent?: "always" | "whenExpanded";
31+
loadContent: LoadContentEnum;
3132
}
3233

3334
export function AccordionGroup(props: AccordionGroupProps): ReactElement | null {
@@ -47,10 +48,9 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null
4748
const rootRef = useRef<HTMLDivElement>(null);
4849
const contentWrapperRef = useRef<HTMLDivElement>(null);
4950
const contentRef = useRef<HTMLDivElement>(null);
50-
const lazyRender = loadContent === "whenExpanded";
51-
const renderContent = useRef(false);
51+
const renderContent = useRef(loadContent === "always");
5252

53-
renderContent.current ||= !lazyRender || !renderCollapsed;
53+
renderContent.current ||= !renderCollapsed;
5454

5555
const completeTransitioning = useCallback((): void => {
5656
if (contentWrapperRef.current && rootRef.current && animatingContent.current) {
@@ -191,7 +191,7 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null
191191
aria-labelledby={`${props.id}HeaderButton`}
192192
>
193193
<div ref={contentRef} className={"widget-accordion-group-content"}>
194-
{renderContent.current ? props.content : null}
194+
{renderContent.current && props.content}
195195
</div>
196196
</div>
197197
</section>

packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ describe("Accordion", () => {
1212
style: { height: "500px" },
1313
tabIndex: 1,
1414
groups: [
15-
{ header: "header", content: <span>content</span>, initiallyCollapsed: true, visible: true },
16-
{ header: "header2", content: <span>content2</span>, initiallyCollapsed: true, visible: false }
15+
{
16+
header: "header",
17+
content: <span>content</span>,
18+
initiallyCollapsed: true,
19+
visible: true,
20+
loadContent: "always"
21+
},
22+
{
23+
header: "header2",
24+
content: <span>content2</span>,
25+
initiallyCollapsed: true,
26+
visible: false,
27+
loadContent: "always"
28+
}
1729
],
1830
collapsible,
1931
singleExpandedGroup,
@@ -84,7 +96,7 @@ describe("Accordion", () => {
8496
it("gives the first accordion group button focus on Home key down", () => {
8597
const groups = [
8698
...defaultProps.groups,
87-
{ header: "header3", content: <span>content3</span>, visible: true }
99+
{ header: "header3", content: <span>content3</span>, visible: true, loadContent: "always" as const }
88100
];
89101
groups[1].visible = true;
90102

@@ -109,7 +121,7 @@ describe("Accordion", () => {
109121
it("gives the last accordion group button focus on End key down", () => {
110122
const groups = [
111123
...defaultProps.groups,
112-
{ header: "header3", content: <span>content3</span>, visible: true }
124+
{ header: "header3", content: <span>content3</span>, visible: true, loadContent: "always" as const }
113125
];
114126
groups[1].visible = true;
115127

@@ -134,7 +146,7 @@ describe("Accordion", () => {
134146
it("remains focus on the last accordion group button on arrow down key down", () => {
135147
const groups = [
136148
...defaultProps.groups,
137-
{ header: "header3", content: <span>content3</span>, visible: true }
149+
{ header: "header3", content: <span>content3</span>, visible: true, loadContent: "always" as const }
138150
];
139151
groups[1].visible = true;
140152

@@ -253,7 +265,13 @@ describe("Accordion", () => {
253265

254266
it("inits with group initially collapsed settings", () => {
255267
const groups = [...defaultProps.groups].map(group => ({ ...group, initiallyCollapsed: false }));
256-
groups.push({ header: "header3", content: <span>content3</span>, initiallyCollapsed: true, visible: true });
268+
groups.push({
269+
header: "header3",
270+
content: <span>content3</span>,
271+
initiallyCollapsed: true,
272+
visible: true,
273+
loadContent: "always" as const
274+
});
257275

258276
const accordion = shallow(<Accordion {...defaultProps} groups={groups} />);
259277
expect(accordion).toMatchSnapshot();

packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe("AccordionGroup", () => {
1616
collapsible: false,
1717
animateContent: false, // testing animations with Enzyme doesn't work
1818
generateHeaderIcon: jest.fn(),
19-
showHeaderIcon: "right"
19+
showHeaderIcon: "right",
20+
loadContent: "always"
2021
};
2122
});
2223

0 commit comments

Comments
 (0)