From c319c8298bf2bc1a8123c12469e0a78911e5274b Mon Sep 17 00:00:00 2001 From: Maximus Date: Wed, 24 Aug 2022 22:13:47 +0300 Subject: [PATCH] added columns children and dot notation for columns --- .../layouts/ThreeColumn/ColumnLayout.tsx | 108 +++++++++++------- .../layouts/ThreeColumn/LeftColumn.tsx | 49 ++++++++ .../layouts/ThreeColumn/Maincolumn.tsx | 18 +++ .../layouts/ThreeColumn/RightColumn.tsx | 18 +++ 4 files changed, 151 insertions(+), 42 deletions(-) create mode 100644 src/components/layouts/ThreeColumn/LeftColumn.tsx create mode 100644 src/components/layouts/ThreeColumn/Maincolumn.tsx create mode 100644 src/components/layouts/ThreeColumn/RightColumn.tsx diff --git a/src/components/layouts/ThreeColumn/ColumnLayout.tsx b/src/components/layouts/ThreeColumn/ColumnLayout.tsx index ddd3d1a..164857d 100644 --- a/src/components/layouts/ThreeColumn/ColumnLayout.tsx +++ b/src/components/layouts/ThreeColumn/ColumnLayout.tsx @@ -1,12 +1,21 @@ -import React, { Fragment, useState, useRef, useEffect } from "react"; +import React, { + Fragment, + useState, + useRef, + useEffect, + FC, + isValidElement, +} from "react"; import { Dialog, Transition } from "@headlessui/react"; import { BottomSheetModal } from "components/containers/modal/BottomSheetModal"; import { BottomBarAcceptCookies } from "components/containers/modal/BottomBarAcceptCookies"; import { Button } from "components/Button/Button"; import classNames from "classnames"; import "./ColumnLayout.css"; -import { container } from "webpack"; - +import { Children } from "react"; +import MainColumn from "./Maincolumn"; +import LeftColumn from "./LeftColumn"; +import RightColumn from "./RightColumn"; /* -------------------------------------------------------------------------- */ /* Custom hook to track container width */ /* -------------------------------------------------------------------------- */ @@ -28,14 +37,41 @@ const useScreenWidth = () => { return [ref, width]; }; +/* -------------------------------------------------------------------------- */ +/* Component extentions */ +/* -------------------------------------------------------------------------- */ + +type ColumnExtentions = { + Left: React.FC<{ + children: React.ReactNode; + openLeftBar?: boolean; + widthElement?: number; + className?: string; + }>; + Main: React.FC<{ + children: React.ReactNode; + className?: string; + }>; + Right: React.FC<{ + children: React.ReactNode; + className?: string; + }>; +}; + /* -------------------------------------------------------------------------- */ /* Component properties */ /* -------------------------------------------------------------------------- */ +type ColumnLayoutProps = { + children: React.ReactNode; //Column layout gets as children not more than three children +} & Omit, "">; + /* -------------------------------------------------------------------------- */ /* Component function */ /* -------------------------------------------------------------------------- */ -export function ColumnLayout() { +export const ColumnLayout: React.FC & ColumnExtentions = ({ + children, +}) => { const mdScreen = 768; //Hooks @@ -44,6 +80,7 @@ export function ColumnLayout() { function leftBar() { return setOpenLeftBar(!openLeftBar); } + // Change openLeftBar when width of screen > 768 useEffect(() => { if (widthElement > mdScreen) { @@ -51,45 +88,32 @@ export function ColumnLayout() { } }); - return ( -
- -
- ffadfag -
-
+ const countChildren = React.Children.count(children); -
- Lorem ipsum dolor sit, amet consectetur adipisicing elit. Accusamus ad, - autem quas hic architecto iusto maxime veritatis laborum. Quas ducimus - sequi impedit harum tempora est nesciunt porro assumenda animi - temporibus? -
-
-
-
-
-
+ const columns = React.Children.map(children, (child) => { + if ( + child && + React.isValidElement(child) && + React.Children.only(child).type === LeftColumn + ) { + return React.cloneElement(child, { + openLeftBar: openLeftBar, + widthElement: widthElement, + }); + } else { + return child; + } + }); + + return ( +
+
+ {countChildren <= 3 ? columns : undefined}
-
dflajdfjlj
); -} +}; + +ColumnLayout.Left = LeftColumn; +ColumnLayout.Main = MainColumn; +ColumnLayout.Right = RightColumn; diff --git a/src/components/layouts/ThreeColumn/LeftColumn.tsx b/src/components/layouts/ThreeColumn/LeftColumn.tsx new file mode 100644 index 0000000..3acdf18 --- /dev/null +++ b/src/components/layouts/ThreeColumn/LeftColumn.tsx @@ -0,0 +1,49 @@ +import React, { Fragment, FunctionComponent as FC } from "react"; +import { Transition } from "@headlessui/react"; +import classNames from "classnames"; + +type LeftColumnProps = { + children: React.ReactNode; + openLeftBar?: boolean; + widthElement?: number; + className?: string; +} & Omit, "">; + +const LeftColumn: React.FC = ({ + className, + children, + widthElement = 0, + openLeftBar = false, +}): JSX.Element => { + const mdScreen = 768; + return ( + +
+ {children} +
+
+ ); +}; + +LeftColumn.displayName = "LeftColumn"; +export default LeftColumn; diff --git a/src/components/layouts/ThreeColumn/Maincolumn.tsx b/src/components/layouts/ThreeColumn/Maincolumn.tsx new file mode 100644 index 0000000..1c81441 --- /dev/null +++ b/src/components/layouts/ThreeColumn/Maincolumn.tsx @@ -0,0 +1,18 @@ +import classNames from "classnames"; +import React from "react"; + +type MainColumnProps = { + children: React.ReactNode; + className?: string; +}; + +function MainColumn({ children, className }: MainColumnProps) { + return ( +
+ {children} +
+ ); +} + +export default MainColumn; +MainColumn.displayName = "MainColumn"; diff --git a/src/components/layouts/ThreeColumn/RightColumn.tsx b/src/components/layouts/ThreeColumn/RightColumn.tsx new file mode 100644 index 0000000..648b9a0 --- /dev/null +++ b/src/components/layouts/ThreeColumn/RightColumn.tsx @@ -0,0 +1,18 @@ +import classNames from "classnames"; +import React from "react"; + +type RightColumnProps = { + children: React.ReactNode; + className?: string; +}; + +function RightColumn({ children, className }: RightColumnProps) { + return ( +
+ {children} +
+ ); +} + +export default RightColumn; +RightColumn.displayName = "RightColumn";