Skip to content

Commit d9d798f

Browse files
author
wiedld
committed
feat(ui-6240): accept collapsed status on resizer panel, during mounting and props change
1 parent 89d0f5d commit d9d798f

5 files changed

Lines changed: 113 additions & 6 deletions

File tree

src/Components/DraggableResizer/Documentation/DraggableResizer.stories.tsx

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ draggableResizerExamplesStories.add(
187187
)
188188

189189
draggableResizerExamplesStories.add(
190-
'4 Panels',
190+
'4 Panels, init rightmost collapsed',
191191
() => {
192192
const [positions, updatePositions] = useState<number[]>([0.25, 0.5, 0.75])
193193

@@ -252,6 +252,7 @@ draggableResizerExamplesStories.add(
252252
<DraggableResizer.Panel
253253
ref={draggableResizerPanelRef4}
254254
isCollapsible={true}
255+
collapsed={true}
255256
>
256257
<div className="mockCard">
257258
<span>4</span>
@@ -270,3 +271,83 @@ draggableResizerExamplesStories.add(
270271
},
271272
}
272273
)
274+
275+
draggableResizerExamplesStories.add(
276+
'3 Horizontal Panels, init middle collapsed',
277+
() => {
278+
const [position, updatePosition] = useState<number[]>([0.25, 0.5])
279+
const draggableResizerPanelRef1 = createRef<DraggableResizerPanelRef>()
280+
const draggableResizerPanelRef2 = createRef<DraggableResizerPanelRef>()
281+
const draggableResizerPanelRef3 = createRef<DraggableResizerPanelRef>()
282+
const defaultBackgroundStyle = {backgroundColor: 'transparent'}
283+
const defaultBarStyle = {backgroundColor: '#ffffff'}
284+
285+
const logRef = (): void => {
286+
/* eslint-disable */
287+
console.log(draggableResizerPanelRef1.current)
288+
console.log(draggableResizerPanelRef2.current)
289+
/* eslint-enable */
290+
}
291+
292+
return (
293+
<div className="mockPage padded">
294+
<DraggableResizer
295+
handleOrientation={
296+
Orientation[
297+
select(
298+
'handleOrientation',
299+
mapEnumKeys(Orientation),
300+
'Horizontal'
301+
)
302+
]
303+
}
304+
handleGradient={
305+
Gradients[
306+
Gradients[
307+
select('handleGradient', mapEnumKeys(Gradients), 'PastelGothic')
308+
]
309+
]
310+
}
311+
backgroundStyle={object('backgroundStyle', defaultBackgroundStyle)}
312+
handleBarStyle={object('handleBarStyle', defaultBarStyle)}
313+
handlePositions={position}
314+
onChangePositions={handlePositions => updatePosition(handlePositions)}
315+
>
316+
<DraggableResizer.Panel
317+
ref={draggableResizerPanelRef1}
318+
isCollapsible={true}
319+
>
320+
<div className="mockCard">
321+
<span>1</span>
322+
</div>
323+
</DraggableResizer.Panel>
324+
<DraggableResizer.Panel
325+
ref={draggableResizerPanelRef2}
326+
isCollapsible={true}
327+
collapsed={true}
328+
>
329+
<div className="mockCard">
330+
<span>2</span>
331+
</div>
332+
</DraggableResizer.Panel>
333+
<DraggableResizer.Panel
334+
ref={draggableResizerPanelRef3}
335+
isCollapsible={true}
336+
>
337+
<div className="mockCard">
338+
<span>3</span>
339+
</div>
340+
</DraggableResizer.Panel>
341+
</DraggableResizer>
342+
<div className="story--test-buttons">
343+
<button onClick={logRef}>Log Ref</button>
344+
</div>
345+
</div>
346+
)
347+
},
348+
{
349+
readme: {
350+
content: marked(DraggableResizerExampleAReadme),
351+
},
352+
}
353+
)

src/Components/DraggableResizer/Documentation/DraggableResizerExampleA.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# 2 Panel Stateful Example
1+
# 3 Panel Stateful Example
22

3-
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.
3+
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.
44

55
### Usage
66
```tsx

src/Components/DraggableResizer/DraggableResizer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,21 @@ export const DraggableResizerRoot: FunctionComponent<DraggableResizerProps> = ({
201201
const dragging = i === dragIndex
202202

203203
const isCollapsibleToLower = child.props.isCollapsible
204+
const collapsedLower = child.props.collapsed
204205
const isCollapsibleToUpper =
205206
!isLastPanel && children && children[i + 1].props.isCollapsible
207+
const collapsedUpper =
208+
!isLastPanel && children && children[i + 1].props.collapsed
206209

207210
return (
208211
<>
209212
{cloneElement(child, {sizePercent: calculatePanelSize(i)})}
210213
{!isLastPanel && (
211214
<DraggableResizerHandle
212215
isCollapsibleToLower={isCollapsibleToLower}
216+
collapsedLower={collapsedLower}
213217
isCollapsibleToUpper={isCollapsibleToUpper}
218+
collapsedUpper={collapsedUpper}
214219
onCollapseButtonClick={onCollapseButtonClick}
215220
dragIndex={i}
216221
onStartDrag={handleStartDrag}

src/Components/DraggableResizer/DraggableResizerHandle.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Libraries
2-
import React, {forwardRef, CSSProperties, useState} from 'react'
2+
import React, {forwardRef, CSSProperties, useState, useEffect} from 'react'
33
import classnames from 'classnames'
44

55
// Components
@@ -19,8 +19,12 @@ import {getColorsFromGradient} from '../../Utils/colors'
1919
export interface DraggableResizerHandleProps extends StandardFunctionProps {
2020
/** Enables a 0.0 direction (Left/Up) Collapse button */
2121
isCollapsibleToLower?: boolean
22+
/** Collapsed state observed from parent */
23+
collapsedLower?: boolean
2224
/** Enables a 1.0 direction (Right/Down) Collapse Button */
2325
isCollapsibleToUpper?: boolean
26+
/** Collapsed state observed from parent */
27+
collapsedUpper?: boolean
2428
/** Function that updates panel positions after collapsing */
2529
onCollapseButtonClick: (direction: number, dragIndex: number) => void
2630
/** Gets passed a function by being a child of DraggableResizer */
@@ -52,7 +56,9 @@ export const DraggableResizerHandle = forwardRef<
5256
className,
5357
orientation,
5458
isCollapsibleToLower = false,
59+
collapsedLower = false,
5560
isCollapsibleToUpper = false,
61+
collapsedUpper = false,
5662
onCollapseButtonClick,
5763
dragging = false,
5864
dragIndex = 9999,
@@ -63,8 +69,21 @@ export const DraggableResizerHandle = forwardRef<
6369
},
6470
ref
6571
) => {
66-
const [collapsibleLower, setCollapsibleLower] = useState<boolean>(false)
67-
const [collapsibleUpper, setCollapsibleUpper] = useState<boolean>(false)
72+
const [collapsibleLower, setCollapsibleLower] = useState<boolean>(
73+
collapsedLower
74+
)
75+
const [collapsibleUpper, setCollapsibleUpper] = useState<boolean>(
76+
collapsedUpper
77+
)
78+
79+
useEffect(() => {
80+
if (isCollapsibleToLower && collapsedLower) {
81+
onCollapseButtonClick(0, dragIndex)
82+
}
83+
if (isCollapsibleToUpper && collapsedUpper) {
84+
onCollapseButtonClick(1, dragIndex)
85+
}
86+
}, [ref, collapsedLower, collapsedUpper])
6887

6988
const handleMouseDown = (): void => {
7089
onStartDrag(dragIndex)

src/Components/DraggableResizer/DraggableResizerPanel.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {StandardFunctionProps} from '../../Types'
88
export interface DraggableResizerPanelProps extends StandardFunctionProps {
99
/** Checks if the current panel is collapsible or not */
1010
isCollapsible?: boolean
11+
/** Collapse state, consumed on component mounting and prop change from parent */
12+
collapsed?: boolean
1113
/** Panel will not shrink past this size (experimental, not guaranteed to work) */
1214
minSizePixels?: number
1315
/** Does not have a value initially, gets passed a value by being a child of DraggableResizer */

0 commit comments

Comments
 (0)