From 5e20411066bc38dc0c49d0a89b1dc73b9d87ef70 Mon Sep 17 00:00:00 2001 From: Buzurgmehr Arjmandi Date: Fri, 19 Jun 2020 18:10:07 +0300 Subject: [PATCH] fix: Multiple pane collapse initial size calculation incorrect --- src/components/Pane.tsx | 94 +++++++++---------- .../hooks/callbacks/useRecalculateSizes.ts | 7 +- src/components/SplitPane/index.tsx | 2 +- stories/InitialState.stories.tsx | 29 ++++++ 4 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src/components/Pane.tsx b/src/components/Pane.tsx index e8d9f8a..98bdf87 100644 --- a/src/components/Pane.tsx +++ b/src/components/Pane.tsx @@ -44,55 +44,55 @@ export interface PaneProps { children: React.ReactNode; transitionTimeout: number | undefined; } -export const Pane = React.memo( - ({ - size, - minSize, - isCollapsed, - collapseOverlayCss = { background: 'rgba(220,220,220, 0.1)' }, - split, - className, - children, - forwardRef, - collapsedIndices, +const UnMemoizedPane = ({ + size, + minSize, + isCollapsed, + collapseOverlayCss = { background: 'rgba(220,220,220, 0.1)' }, + split, + className, + children, + forwardRef, + collapsedIndices, + transitionTimeout, +}: PaneProps) => { + const classes = useMergeClasses(['Pane', split, className]); + const timeout = useMemo(() => transitionTimeout ?? DEFAULT_COLLAPSE_TRANSITION_TIMEOUT, [ transitionTimeout, - }: PaneProps) => { - const classes = useMergeClasses(['Pane', split, className]); - const timeout = useMemo(() => transitionTimeout ?? DEFAULT_COLLAPSE_TRANSITION_TIMEOUT, [ - transitionTimeout, - ]); - const [shouldAnimate, setShouldAnimate] = useState(false); + ]); + const [shouldAnimate, setShouldAnimate] = useState(false); - const didMount = useRef(false); + const didMount = useRef(false); - useEffect(() => { - if (didMount.current) { - if (timeout !== 0) { - setShouldAnimate(true); - setTimeout(() => setShouldAnimate(false), 500); - } - } else { - didMount.current = true; + useEffect(() => { + if (didMount.current) { + if (timeout !== 0) { + setShouldAnimate(true); + setTimeout(() => setShouldAnimate(false), 500); } - }, [setShouldAnimate, collapsedIndices, timeout]); + } else { + didMount.current = true; + } + }, [setShouldAnimate, collapsedIndices, timeout]); - const minStyle = useMemo( - () => (split === 'vertical' ? { minWidth: minSize } : { minHeight: minSize }), - [minSize, split] - ); - const collapseOverlayStyle = isCollapsed ? { ...collapseOverlayCss, ...minStyle } : minStyle; - return ( - - {children} - - ); - } -); -Pane.displayName = 'Pane'; + const minStyle = useMemo( + () => (split === 'vertical' ? { minWidth: minSize } : { minHeight: minSize }), + [minSize, split] + ); + const collapseOverlayStyle = isCollapsed ? { ...collapseOverlayCss, ...minStyle } : minStyle; + return ( + + {children} + + ); +}; + +UnMemoizedPane.displayName = 'Pane'; +export const Pane = React.memo(UnMemoizedPane); diff --git a/src/components/SplitPane/hooks/callbacks/useRecalculateSizes.ts b/src/components/SplitPane/hooks/callbacks/useRecalculateSizes.ts index dc21ddd..50af0f8 100644 --- a/src/components/SplitPane/hooks/callbacks/useRecalculateSizes.ts +++ b/src/components/SplitPane/hooks/callbacks/useRecalculateSizes.ts @@ -29,7 +29,12 @@ export function useRecalculateSizes({ return collapsedSize; } if (collapsedIndices.includes(idx - 1)) { - return size + curSizes[idx - 1] - collapsedSize; + const totalPrevSizeToAdd = addArray( + collapsedIndices + .filter((_collapsedIdx, index) => index <= idx) + .map((_i, index) => initialRatioSizes[index] - collapsedSize) + ); + return size + totalPrevSizeToAdd; } return size; }); diff --git a/src/components/SplitPane/index.tsx b/src/components/SplitPane/index.tsx index f10181e..93a88cb 100644 --- a/src/components/SplitPane/index.tsx +++ b/src/components/SplitPane/index.tsx @@ -118,7 +118,7 @@ export const SplitPane = ({ className = '', ...props }: SplitPaneProps) => { }); return ( - + {entries} ); diff --git a/stories/InitialState.stories.tsx b/stories/InitialState.stories.tsx index 1101b64..2316ee6 100644 --- a/stories/InitialState.stories.tsx +++ b/stories/InitialState.stories.tsx @@ -51,6 +51,35 @@ storiesOf('Initial States', module) ); }) + .add('Ltr, First Two Panes Collapsed', () => { + const verticalCollapseDirection = select( + 'Vertical Direction', + { left: 'left', right: 'right' }, + 'left' + ); + const shouldCollapse = boolean('Collapsable?', true); + const verticalCollapseOptions = shouldCollapse + ? { + beforeToggleButton: , + afterToggleButton: , + collapsedSize: 40, + collapseDirection: verticalCollapseDirection, + } + : undefined; + const VerticalSplitPane = (props: Omit) => ( + + {props.children} + + ); + + return ( + +
This is a div
+
This is a second div
+
This is a third div
+
+ ); + }) .add('Initial Sizes as Flex-Basis proportions', () => { return (