Skip to content

Commit 0b3c9c0

Browse files
authored
fix(modal): improve modal's size (#452)
* feat(modal): improve modal's size and calc maxHeight for modal's body
1 parent f9672f6 commit 0b3c9c0

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed

src/modal/__tests__/__snapshots__/modal.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`Test Modal Component Should Match snapshot 1`] = `
1717
aria-modal="true"
1818
class="ant-modal dt-modal"
1919
role="dialog"
20-
style="width: 640px;"
20+
style="width: 520px;"
2121
>
2222
<div
2323
aria-hidden="true"

src/modal/__tests__/modal.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Test Modal Component', () => {
2626
test
2727
</Modal>
2828
);
29-
expect(modal.query(container)?.style.width).toBe('640px');
29+
expect(modal.query(container)?.style.width).toBe('520px');
3030

3131
// small size
3232
rerender(
@@ -36,6 +36,14 @@ describe('Test Modal Component', () => {
3636
);
3737
expect(modal.query(container)?.style.width).toBe('400px');
3838

39+
// middle size
40+
rerender(
41+
<Modal visible title="title" getContainer={false} size="middle">
42+
test
43+
</Modal>
44+
);
45+
expect(modal.query(container)?.style.width).toBe('640px');
46+
3947
// large size
4048
rerender(
4149
<Modal visible title="title" getContainer={false} size="large">
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
$modal-height: 600px;
2-
$modal-header: 55px;
3-
$modal-footer: 53px;
1+
$modal-max-height: 80vh;
42

53
.dt-modal {
6-
.ant-modal-body {
7-
max-height: calc($modal-height - $modal-header - $modal-footer);
8-
overflow-y: auto;
9-
}
10-
&-body {
11-
padding: 16px;
4+
.ant-modal-content {
5+
max-height: $modal-max-height;
6+
display: flex;
7+
flex-direction: column;
8+
.ant-modal-body {
9+
overflow: hidden;
10+
flex: 1;
11+
display: flex;
12+
flex-direction: column;
13+
.dt-modal-body {
14+
flex: 1;
15+
padding: 16px 24px;
16+
overflow-y: auto;
17+
min-height: 0;
18+
}
19+
}
20+
.ant-modal-header,
21+
.ant-modal-footer {
22+
flex: 0 1 auto;
23+
}
1224
}
1325
}

src/modal/components/modal/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React from 'react';
22
import { Alert, type AlertProps, Modal, type ModalProps } from 'antd';
3+
import classNames from 'classnames';
34
import { omit } from 'lodash';
45

56
import './index.scss';
67

78
export interface IModalProps extends ModalProps {
8-
size?: 'small' | 'default' | 'large';
9+
size?: 'small' | 'default' | 'middle' | 'large';
910
banner?: AlertProps['message'] | Omit<AlertProps, 'banner'>;
1011
}
1112

1213
const getWidthFromSize = (size: IModalProps['size']) => {
1314
if (size === 'small') return 400;
15+
if (size === 'middle') return 640;
1416
if (size === 'large') return 900;
15-
return 640;
17+
return 520;
1618
};
1719

1820
const isValidBanner = (banner: IModalProps['banner']): banner is AlertProps['message'] => {
@@ -26,13 +28,14 @@ export default function InternalModal({
2628
size = 'default',
2729
children,
2830
width,
31+
className,
2932
...rest
3033
}: IModalProps) {
3134
const finalWidth = width ?? getWidthFromSize(size);
3235

3336
return (
3437
<Modal
35-
className="dt-modal"
38+
className={classNames('dt-modal', className)}
3639
bodyStyle={{ padding: 0, ...bodyStyle }}
3740
width={finalWidth}
3841
{...rest}

src/modal/demos/basic/basic.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function Basic() {
88
return (
99
<>
1010
<Modal
11-
title="最大高度限制 600px"
11+
title="最大高度限制"
1212
visible={visible}
1313
onCancel={() => setVisible(false)}
1414
onOk={() => setVisible(false)}
@@ -22,7 +22,7 @@ export default function Basic() {
2222
</ul>
2323
</Modal>
2424
<Button type="primary" onClick={() => setVisible(true)}>
25-
最大高度限制 600px
25+
最大高度限制
2626
</Button>
2727
</>
2828
);

src/modal/demos/basic/size.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ export default function Size() {
2626
setSize('default');
2727
}}
2828
>
29-
正常尺寸
29+
默认尺寸
30+
</Button>
31+
<Button
32+
type="primary"
33+
onClick={() => {
34+
setVisible(true);
35+
setSize('middle');
36+
}}
37+
>
38+
中等尺寸
3039
</Button>
3140
<Button
3241
type="primary"

src/modal/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ toc: content
1010

1111
## 示例
1212

13-
<code src="./demos/basic/basic.tsx" title="高度限制 600px"></code>
13+
<code src="./demos/basic/basic.tsx" title="最大高度限制"></code>
1414
<code src="./demos/basic/size.tsx" title="尺寸"></code>
1515
<code src="./demos/basic/banner.tsx" title="支持 banner"></code>
1616
<code src="./demos/basic/bannerProps.tsx" title="支持传 banner 的 Props 属性"></code>
@@ -21,10 +21,10 @@ toc: content
2121

2222
[AlertProps](https://4x-ant-design.antgroup.com/components/alert-cn/#API)
2323

24-
| 参数 | 说明 | 类型 | 默认值 |
25-
| ------ | ---- | --------------------------------- | --------- |
26-
| size | 尺寸 | `'small' \| 'default' \| 'large'` | `default` |
27-
| banner | 提示 | `React.ReactNode \| AlertProps` | |
24+
| 参数 | 说明 | 类型 | 默认值 |
25+
| ------ | ---- | --------------------------------------------- | --------- |
26+
| size | 尺寸 | `'small' \| 'default' \| 'middle' \| 'large'` | `default` |
27+
| banner | 提示 | `React.ReactNode \| AlertProps` | |
2828

2929
:::info
3030
其余参数继承 antd4.x 的 [Modal](https://4x.ant.design/components/modal-cn/#API)

0 commit comments

Comments
 (0)