Skip to content

Commit ba6e05a

Browse files
committed
fix: modify isControlled logic and test units
1 parent a0087a8 commit ba6e05a

File tree

2 files changed

+17
-57
lines changed

2 files changed

+17
-57
lines changed

src/DrawerPopup.tsx

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -284,59 +284,28 @@ const DrawerPopup: React.ForwardRefRenderFunction<
284284
break;
285285
}
286286
}
287-
// Track if props changed to detect controlled mode
288-
const prevSizeRef = React.useRef<{
289-
width?: number | string;
290-
height?: number | string;
291-
}>({});
292-
const isControlledMode = React.useMemo(() => {
293-
// Consider controlled if width/height changes between renders (external updates)
294-
const currentSize = isHorizontal ? width : height;
295-
const prevSize = isHorizontal
296-
? prevSizeRef.current.width
297-
: prevSizeRef.current.height;
298-
299-
const hasExternalUpdate =
300-
currentSize !== undefined &&
301-
currentSize !== prevSize &&
302-
prevSize !== undefined;
303-
304-
// Update ref for next comparison
305-
prevSizeRef.current = { width, height };
306-
307-
return hasExternalUpdate;
308-
}, [width, height, isHorizontal]);
309-
310-
// Size calculation with controlled mode consideration
287+
288+
const isControlled = isHorizontal
289+
? width !== undefined
290+
: height !== undefined;
291+
311292
if (isHorizontal) {
312293
let finalWidth: number | string;
313-
if (isControlledMode) {
314-
// In controlled mode, prioritize external width
315-
finalWidth = width !== undefined ? width : defaultWidth;
294+
if (isControlled) {
295+
finalWidth = width!;
316296
} else if (currentSize !== undefined) {
317-
// In uncontrolled mode, user dragged size takes precedence
318297
finalWidth = currentSize;
319-
} else if (width !== undefined) {
320-
// Fallback to provided width
321-
finalWidth = width;
322298
} else {
323-
// Final fallback to default
324299
finalWidth = defaultWidth;
325300
}
326301
wrapperStyle.width = parseWidthHeight(finalWidth);
327302
} else {
328303
let finalHeight: number | string;
329-
if (isControlledMode) {
330-
// In controlled mode, prioritize external height
331-
finalHeight = height !== undefined ? height : defaultHeight;
304+
if (isControlled) {
305+
finalHeight = height!;
332306
} else if (currentSize !== undefined) {
333-
// In uncontrolled mode, user dragged size takes precedence
334307
finalHeight = currentSize;
335-
} else if (height !== undefined) {
336-
// Fallback to provided height
337-
finalHeight = height;
338308
} else {
339-
// Final fallback to default
340309
finalHeight = defaultHeight;
341310
}
342311
wrapperStyle.height = parseWidthHeight(finalHeight);
@@ -388,12 +357,12 @@ const DrawerPopup: React.ForwardRefRenderFunction<
388357
const handleResize = React.useCallback(
389358
(size: number) => {
390359
// In controlled mode, only trigger callback without updating internal state
391-
if (!isControlledMode) {
360+
if (!isControlled) {
392361
setCurrentSize(size);
393362
}
394363
resizable?.onResize?.(size);
395364
},
396-
[resizable, isControlledMode],
365+
[resizable, isControlled],
397366
);
398367

399368
const handleResizeStart = React.useCallback(() => {

tests/index.spec.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,7 @@ describe('rc-drawer-menu', () => {
478478
});
479479

480480
it('should support resizable horizontal', () => {
481-
let currentWidth = '200px';
482-
const setWidth = (newWidth: number) => {
483-
currentWidth = newWidth + 'px';
484-
};
481+
const setWidth = jest.fn();
485482
const onResizeStart = jest.fn();
486483
const onResizeEnd = jest.fn();
487484

@@ -499,7 +496,7 @@ describe('rc-drawer-menu', () => {
499496
}}
500497
open
501498
placement="right"
502-
width={currentWidth}
499+
defaultWidth="200px"
503500
/>
504501
</div>,
505502
);
@@ -569,10 +566,7 @@ describe('rc-drawer-menu', () => {
569566
});
570567

571568
it('should respect minSize and maxSize constraints', () => {
572-
let currentWidth = 200;
573-
const setWidth = (newWidth: number) => {
574-
currentWidth = newWidth;
575-
};
569+
const setWidth = jest.fn();
576570

577571
const { unmount } = render(
578572
<div style={{ width: '500px', height: '400px', position: 'relative' }}>
@@ -583,7 +577,7 @@ describe('rc-drawer-menu', () => {
583577
}}
584578
open
585579
placement="left"
586-
width={currentWidth}
580+
defaultWidth={200}
587581
/>
588582
</div>,
589583
);
@@ -637,10 +631,7 @@ describe('rc-drawer-menu', () => {
637631
});
638632

639633
it('should support resizable vertical', () => {
640-
let currentHeight = 200;
641-
const setHeight = (newHeight: number) => {
642-
currentHeight = newHeight;
643-
};
634+
const setHeight = jest.fn();
644635

645636
const { unmount } = render(
646637
<Drawer
@@ -649,7 +640,7 @@ describe('rc-drawer-menu', () => {
649640
}}
650641
open
651642
placement="top"
652-
height={currentHeight}
643+
defaultHeight={200}
653644
/>,
654645
);
655646

0 commit comments

Comments
 (0)