Skip to content

Commit 72f6c1c

Browse files
authored
fix: Not break string size but warning (#284)
1 parent d8e7bf1 commit 72f6c1c

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/DrawerPopup.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
77
import DrawerContext from './context';
88
import type { DrawerContextProps } from './context';
99
import KeyCode from 'rc-util/lib/KeyCode';
10+
import { parseWidthHeight } from './util';
1011

1112
const sentinelStyle: React.CSSProperties = {
1213
width: 0,
@@ -262,9 +263,9 @@ export default function DrawerPopup(props: DrawerPopupProps) {
262263
}
263264

264265
if (placement === 'left' || placement === 'right') {
265-
wrapperStyle.width = width;
266+
wrapperStyle.width = parseWidthHeight(width);
266267
} else {
267-
wrapperStyle.height = height;
268+
wrapperStyle.height = parseWidthHeight(height);
268269
}
269270

270271
const panelNode: React.ReactNode = (

src/util.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import warning from 'rc-util/lib/warning';
2+
3+
export function parseWidthHeight(value?: number | string) {
4+
if (typeof value === 'string' && String(Number(value)) === value) {
5+
warning(
6+
false,
7+
'Invalid value type of `width` or `height` which should be number type instead.',
8+
);
9+
return Number(value);
10+
}
11+
12+
return value;
13+
}

tests/index.spec.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { cleanup, fireEvent, render, act } from '@testing-library/react';
22
import KeyCode from 'rc-util/lib/KeyCode';
3+
import { resetWarned } from 'rc-util/lib/warning';
34
import React from 'react';
45
import type { DrawerProps } from '../src';
56
import Drawer from '../src';
@@ -34,7 +35,9 @@ describe('rc-drawer-menu', () => {
3435
act(() => {
3536
jest.runAllTimers();
3637
});
37-
expect(document.querySelector('.rc-drawer-content-hidden')).toBeTruthy();
38+
expect(
39+
document.querySelector('.rc-drawer-content-wrapper-hidden'),
40+
).toBeTruthy();
3841
});
3942

4043
describe('push', () => {
@@ -311,12 +314,34 @@ describe('rc-drawer-menu', () => {
311314
});
312315

313316
it('width on the correct element', () => {
317+
resetWarned();
318+
const errSpy = jest.spyOn(console, 'error');
314319
const { container } = render(
315320
<Drawer width="93%" open getContainer={false} />,
316321
);
317322

318323
expect(container.querySelector('.rc-drawer-content-wrapper')).toHaveStyle({
319324
width: '93%',
320325
});
326+
327+
expect(errSpy).not.toHaveBeenCalled();
328+
errSpy.mockRestore();
329+
});
330+
331+
it('warning for string width', () => {
332+
resetWarned();
333+
const errSpy = jest.spyOn(console, 'error');
334+
const { container } = render(
335+
<Drawer width="93" open getContainer={false} />,
336+
);
337+
338+
expect(container.querySelector('.rc-drawer-content-wrapper')).toHaveStyle({
339+
width: '93px',
340+
});
341+
342+
expect(errSpy).toHaveBeenCalledWith(
343+
'Warning: Invalid value type of `width` or `height` which should be number type instead.',
344+
);
345+
errSpy.mockRestore();
321346
});
322347
});

0 commit comments

Comments
 (0)