|
1 | 1 | import * as React from 'react';
|
2 | 2 |
|
3 |
| -import { DeviceBreakpointsType } from '@/types/breakpoints'; |
4 | 3 | import { pickAriaProps } from '@/utils/aria/aria';
|
5 | 4 |
|
6 | 5 | // inner components
|
7 |
| -import { FooterContent } from './components'; |
| 6 | +import { FooterSection } from './components'; |
8 | 7 | import { FooterStyled } from './footer.styled';
|
9 |
| -import { FooterMobileColumnFlow, FooterPositionType, IFooterStandAlone, MobileSort } from './types'; |
| 8 | +import { |
| 9 | + ContentDirectionType, |
| 10 | + FooterMobileColumnFlow, |
| 11 | + FooterPositionType, |
| 12 | + IFooterStandAlone, |
| 13 | +} from './types'; |
10 | 14 |
|
11 | 15 | // simple container tag
|
12 | 16 | const FOOTER = 'footer';
|
13 |
| -// default footer sort |
14 |
| -const DEFAULT_SORT: MobileSort = { |
15 |
| - column: FooterMobileColumnFlow.REVERSE, |
16 |
| - firstContent: FooterPositionType.RIGHT, |
17 |
| - secondContent: FooterPositionType.CENTER, |
18 |
| - thirdContent: FooterPositionType.LEFT, |
19 |
| -}; |
20 | 17 |
|
21 | 18 | const FooterStandAloneComponent = (
|
22 |
| - { footerMobileSortConfig = DEFAULT_SORT, ...props }: IFooterStandAlone, |
| 19 | + { ...props }: IFooterStandAlone, |
23 | 20 | ref: React.ForwardedRef<HTMLElement> | undefined | null
|
24 | 21 | ): JSX.Element | null => {
|
| 22 | + if (!props.children) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
25 | 26 | const ariaProps = pickAriaProps(props);
|
26 | 27 |
|
27 |
| - const getPositionContent = (position: FooterPositionType) => { |
28 |
| - const childrens = React.Children.toArray(props.children).filter((child: React.ReactNode) => { |
29 |
| - return (child as JSX.Element)?.props?.['data-position'] === position; |
| 28 | + const asFooter = props.simpleContainer ? undefined : FOOTER; |
| 29 | + |
| 30 | + const renderChildren = (children: JSX.Element | JSX.Element[]) => { |
| 31 | + const childrenArray = React.Children.toArray(children); |
| 32 | + |
| 33 | + const positions = { |
| 34 | + [FooterPositionType.LEFT]: [], |
| 35 | + [FooterPositionType.CENTER]: [], |
| 36 | + [FooterPositionType.RIGHT]: [], |
| 37 | + }; |
| 38 | + |
| 39 | + let defaultPositionCounter = 0; |
| 40 | + |
| 41 | + childrenArray.forEach((child: React.ReactNode) => { |
| 42 | + let position = (child as JSX.Element)?.props?.['data-position']; |
| 43 | + |
| 44 | + // START CONTENT: Remove when footerMobileSortConfig is removed |
| 45 | + if (props.footerMobileSortConfig) { |
| 46 | + if (props.footerMobileSortConfig.firstContent === position) { |
| 47 | + position = FooterPositionType.LEFT; |
| 48 | + } else if (props.footerMobileSortConfig.secondContent === position) { |
| 49 | + position = FooterPositionType.CENTER; |
| 50 | + } else if (props.footerMobileSortConfig.thirdContent === position) { |
| 51 | + position = FooterPositionType.RIGHT; |
| 52 | + } |
| 53 | + } |
| 54 | + // END CONTENT |
| 55 | + |
| 56 | + if (!position) { |
| 57 | + if (defaultPositionCounter === 0) { |
| 58 | + position = FooterPositionType.LEFT; |
| 59 | + } else if (defaultPositionCounter === 1) { |
| 60 | + position = FooterPositionType.RIGHT; |
| 61 | + } |
| 62 | + defaultPositionCounter++; |
| 63 | + } |
| 64 | + |
| 65 | + if (positions[position]) { |
| 66 | + positions[position].push(child); |
| 67 | + } |
30 | 68 | });
|
31 | 69 |
|
32 |
| - if (props.device !== DeviceBreakpointsType.MOBILE) { |
33 |
| - return childrens; |
34 |
| - } |
| 70 | + const sections = Object.entries(positions).map(([position, children]) => ( |
| 71 | + <FooterSection |
| 72 | + key={position} |
| 73 | + forceVertical={forceVertical} |
| 74 | + position={position} |
| 75 | + styles={props.styles} |
| 76 | + tabInverse={tabInverse} |
| 77 | + > |
| 78 | + {children} |
| 79 | + </FooterSection> |
| 80 | + )); |
35 | 81 |
|
36 |
| - return footerMobileSortConfig.column === FooterMobileColumnFlow.DEFAULT |
37 |
| - ? childrens |
38 |
| - : childrens.reverse(); |
| 82 | + return <>{tabInverse ? sections.reverse() : sections}</>; |
39 | 83 | };
|
40 | 84 |
|
41 |
| - const firstContent = (() => |
42 |
| - footerMobileSortConfig?.firstContent && props.device === DeviceBreakpointsType.MOBILE |
43 |
| - ? getPositionContent(footerMobileSortConfig.firstContent) |
44 |
| - : getPositionContent(FooterPositionType.LEFT))(); |
45 |
| - const secondContent = (() => |
46 |
| - footerMobileSortConfig?.secondContent && props.device === DeviceBreakpointsType.MOBILE |
47 |
| - ? getPositionContent(footerMobileSortConfig.secondContent) |
48 |
| - : getPositionContent(FooterPositionType.CENTER))(); |
49 |
| - const thridContent = (() => |
50 |
| - footerMobileSortConfig?.thirdContent && props.device === DeviceBreakpointsType.MOBILE |
51 |
| - ? getPositionContent(footerMobileSortConfig.thirdContent) |
52 |
| - : getPositionContent(FooterPositionType.RIGHT))(); |
53 |
| - |
54 |
| - const showFooter = |
55 |
| - props.children || |
56 |
| - firstContent.length > 0 || |
57 |
| - thridContent.length > 0 || |
58 |
| - secondContent.length > 0; |
59 |
| - if (!showFooter) { |
60 |
| - return null; |
61 |
| - } |
| 85 | + // START CONTENT: Remove when contentDirection is removed |
| 86 | + const forceVertical = |
| 87 | + props.forceVertical || props.contentDirection === ContentDirectionType.VERTICAL; |
| 88 | + // END CONTENT |
62 | 89 |
|
63 |
| - const asFooter = props.simpleContainer ? undefined : FOOTER; |
| 90 | + // START CONTENT: Remove when footerMobileSortConfig is removed |
| 91 | + const tabInverse = |
| 92 | + props.tabInverse || props.footerMobileSortConfig?.column === FooterMobileColumnFlow.REVERSE; |
| 93 | + // END CONTENT |
64 | 94 |
|
65 | 95 | return (
|
66 | 96 | <FooterStyled
|
67 | 97 | {...ariaProps}
|
| 98 | + {...props} |
68 | 99 | ref={ref}
|
69 |
| - alignItems={props.alignItems} |
| 100 | + $forceVertical={forceVertical} |
| 101 | + $tabInverse={tabInverse} |
70 | 102 | as={asFooter}
|
71 |
| - contentDirection={props.contentDirection} |
72 | 103 | data-testid={props.dataTestId}
|
73 |
| - {...props} |
74 | 104 | lineSeparatorLineStyles={props.lineSeparatorLineStyles}
|
75 | 105 | role={props.role}
|
76 | 106 | styles={props.styles}
|
77 | 107 | >
|
78 |
| - {Array.isArray(props.children) ? ( |
79 |
| - <> |
80 |
| - <FooterContent |
81 |
| - contentDirection={props.contentDirection} |
82 |
| - forceVertical={props.forceVertical} |
83 |
| - marginRight={true} |
84 |
| - styles={props.styles} |
85 |
| - > |
86 |
| - {firstContent} |
87 |
| - </FooterContent> |
88 |
| - <FooterContent |
89 |
| - contentDirection={props.contentDirection} |
90 |
| - forceVertical={props.forceVertical} |
91 |
| - margin={true} |
92 |
| - styles={props.styles} |
93 |
| - > |
94 |
| - {secondContent} |
95 |
| - </FooterContent> |
96 |
| - <FooterContent |
97 |
| - contentDirection={props.contentDirection} |
98 |
| - forceVertical={props.forceVertical} |
99 |
| - marginLeft={true} |
100 |
| - styles={props.styles} |
101 |
| - > |
102 |
| - {thridContent} |
103 |
| - </FooterContent> |
104 |
| - </> |
105 |
| - ) : ( |
106 |
| - props.children |
107 |
| - )} |
| 108 | + {renderChildren(props.children)} |
108 | 109 | </FooterStyled>
|
109 | 110 | );
|
110 | 111 | };
|
|
0 commit comments