From d9d798fbff2ae3c781ffcf017ad241c143edbe02 Mon Sep 17 00:00:00 2001 From: wiedld Date: Tue, 8 Nov 2022 14:50:33 -0800 Subject: [PATCH] feat(ui-6240): accept collapsed status on resizer panel, during mounting and props change --- .../DraggableResizer.stories.tsx | 83 ++++++++++++++++++- .../Documentation/DraggableResizerExampleA.md | 4 +- .../DraggableResizer/DraggableResizer.tsx | 5 ++ .../DraggableResizerHandle.tsx | 25 +++++- .../DraggableResizerPanel.tsx | 2 + 5 files changed, 113 insertions(+), 6 deletions(-) diff --git a/src/Components/DraggableResizer/Documentation/DraggableResizer.stories.tsx b/src/Components/DraggableResizer/Documentation/DraggableResizer.stories.tsx index 56393855..2e7bf4c7 100644 --- a/src/Components/DraggableResizer/Documentation/DraggableResizer.stories.tsx +++ b/src/Components/DraggableResizer/Documentation/DraggableResizer.stories.tsx @@ -187,7 +187,7 @@ draggableResizerExamplesStories.add( ) draggableResizerExamplesStories.add( - '4 Panels', + '4 Panels, init rightmost collapsed', () => { const [positions, updatePositions] = useState([0.25, 0.5, 0.75]) @@ -252,6 +252,7 @@ draggableResizerExamplesStories.add(
4 @@ -270,3 +271,83 @@ draggableResizerExamplesStories.add( }, } ) + +draggableResizerExamplesStories.add( + '3 Horizontal Panels, init middle collapsed', + () => { + const [position, updatePosition] = useState([0.25, 0.5]) + const draggableResizerPanelRef1 = createRef() + const draggableResizerPanelRef2 = createRef() + const draggableResizerPanelRef3 = createRef() + const defaultBackgroundStyle = {backgroundColor: 'transparent'} + const defaultBarStyle = {backgroundColor: '#ffffff'} + + const logRef = (): void => { + /* eslint-disable */ + console.log(draggableResizerPanelRef1.current) + console.log(draggableResizerPanelRef2.current) + /* eslint-enable */ + } + + return ( +
+ updatePosition(handlePositions)} + > + +
+ 1 +
+
+ +
+ 2 +
+
+ +
+ 3 +
+
+
+
+ +
+
+ ) + }, + { + readme: { + content: marked(DraggableResizerExampleAReadme), + }, + } +) diff --git a/src/Components/DraggableResizer/Documentation/DraggableResizerExampleA.md b/src/Components/DraggableResizer/Documentation/DraggableResizerExampleA.md index 692a75bb..df68009b 100644 --- a/src/Components/DraggableResizer/Documentation/DraggableResizerExampleA.md +++ b/src/Components/DraggableResizer/Documentation/DraggableResizerExampleA.md @@ -1,6 +1,6 @@ -# 2 Panel Stateful Example +# 3 Panel Stateful Example -Here's a stateful example of `DraggableResizer` with 2 panels. Because this example contained in a stateful wrapper the example code in the JSX tab is obscured. Try looking at the State tab to see state changes in real time. +Here's a stateful example of `DraggableResizer` with 3 panels. Because this example contained in a stateful wrapper the example code in the JSX tab is obscured. Try looking at the State tab to see state changes in real time. ### Usage ```tsx diff --git a/src/Components/DraggableResizer/DraggableResizer.tsx b/src/Components/DraggableResizer/DraggableResizer.tsx index d91e2bda..1d2d0e3e 100644 --- a/src/Components/DraggableResizer/DraggableResizer.tsx +++ b/src/Components/DraggableResizer/DraggableResizer.tsx @@ -201,8 +201,11 @@ export const DraggableResizerRoot: FunctionComponent = ({ const dragging = i === dragIndex const isCollapsibleToLower = child.props.isCollapsible + const collapsedLower = child.props.collapsed const isCollapsibleToUpper = !isLastPanel && children && children[i + 1].props.isCollapsible + const collapsedUpper = + !isLastPanel && children && children[i + 1].props.collapsed return ( <> @@ -210,7 +213,9 @@ export const DraggableResizerRoot: FunctionComponent = ({ {!isLastPanel && ( void /** Gets passed a function by being a child of DraggableResizer */ @@ -52,7 +56,9 @@ export const DraggableResizerHandle = forwardRef< className, orientation, isCollapsibleToLower = false, + collapsedLower = false, isCollapsibleToUpper = false, + collapsedUpper = false, onCollapseButtonClick, dragging = false, dragIndex = 9999, @@ -63,8 +69,21 @@ export const DraggableResizerHandle = forwardRef< }, ref ) => { - const [collapsibleLower, setCollapsibleLower] = useState(false) - const [collapsibleUpper, setCollapsibleUpper] = useState(false) + const [collapsibleLower, setCollapsibleLower] = useState( + collapsedLower + ) + const [collapsibleUpper, setCollapsibleUpper] = useState( + collapsedUpper + ) + + useEffect(() => { + if (isCollapsibleToLower && collapsedLower) { + onCollapseButtonClick(0, dragIndex) + } + if (isCollapsibleToUpper && collapsedUpper) { + onCollapseButtonClick(1, dragIndex) + } + }, [ref, collapsedLower, collapsedUpper]) const handleMouseDown = (): void => { onStartDrag(dragIndex) diff --git a/src/Components/DraggableResizer/DraggableResizerPanel.tsx b/src/Components/DraggableResizer/DraggableResizerPanel.tsx index 982a38d8..87c2b6d5 100644 --- a/src/Components/DraggableResizer/DraggableResizerPanel.tsx +++ b/src/Components/DraggableResizer/DraggableResizerPanel.tsx @@ -8,6 +8,8 @@ import {StandardFunctionProps} from '../../Types' export interface DraggableResizerPanelProps extends StandardFunctionProps { /** Checks if the current panel is collapsible or not */ isCollapsible?: boolean + /** Collapse state, consumed on component mounting and prop change from parent */ + collapsed?: boolean /** Panel will not shrink past this size (experimental, not guaranteed to work) */ minSizePixels?: number /** Does not have a value initially, gets passed a value by being a child of DraggableResizer */