Skip to content

Commit 7393623

Browse files
authored
fix: adjust the order of QRCode style prop (ant-design#48053)
* fix: adjust the order of style prop * test: update snap * test: add test case
1 parent a4bf443 commit 7393623

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

components/qr-code/__tests__/__snapshots__/demo-extend.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ exports[`renders components/qr-code/demo/download.tsx extend context correctly 1
211211
>
212212
<div
213213
class="ant-qrcode"
214-
style="margin-bottom: 16px; width: 160px; height: 160px; background-color: rgb(255, 255, 255);"
214+
style="width: 160px; height: 160px; background-color: rgb(255, 255, 255); margin-bottom: 16px;"
215215
>
216216
<canvas
217217
height="160"
@@ -235,7 +235,7 @@ exports[`renders components/qr-code/demo/errorlevel.tsx extend context correctly
235235
Array [
236236
<div
237237
class="ant-qrcode"
238-
style="margin-bottom: 16px; width: 160px; height: 160px; background-color: transparent;"
238+
style="width: 160px; height: 160px; background-color: transparent; margin-bottom: 16px;"
239239
>
240240
<canvas
241241
height="160"

components/qr-code/__tests__/__snapshots__/demo.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ exports[`renders components/qr-code/demo/download.tsx correctly 1`] = `
169169
>
170170
<div
171171
class="ant-qrcode"
172-
style="margin-bottom:16px;width:160px;height:160px;background-color:#fff"
172+
style="width:160px;height:160px;background-color:#fff;margin-bottom:16px"
173173
>
174174
<canvas
175175
height="160"
@@ -191,7 +191,7 @@ exports[`renders components/qr-code/demo/errorlevel.tsx correctly 1`] = `
191191
Array [
192192
<div
193193
class="ant-qrcode"
194-
style="margin-bottom:16px;width:160px;height:160px;background-color:transparent"
194+
style="width:160px;height:160px;background-color:transparent;margin-bottom:16px"
195195
>
196196
<canvas
197197
height="160"

components/qr-code/__tests__/index.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,11 @@ describe('QRCode test', () => {
8787
);
8888
errSpy.mockRestore();
8989
});
90+
91+
it('correct style order', () => {
92+
const { container } = render(<QRCode value="test" size={80} style={{ width: 100 }} />);
93+
expect(container.querySelector<HTMLDivElement>('.ant-qrcode')).toHaveStyle(
94+
'width: 100px; height: 80px',
95+
);
96+
});
9097
});

components/qr-code/demo/customSize.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import React, { useState } from 'react';
22
import { MinusOutlined, PlusOutlined } from '@ant-design/icons';
3-
import { QRCode, Button } from 'antd';
3+
import { Button, QRCode } from 'antd';
4+
5+
const MIN_SIZE = 48;
6+
const MAX_SIZE = 300;
47

58
const App: React.FC = () => {
69
const [size, setSize] = useState<number>(160);
710

811
const increase = () => {
912
setSize((prevSize) => {
1013
const newSize = prevSize + 10;
11-
if (newSize > 300) {
12-
return 300;
14+
if (newSize >= MAX_SIZE) {
15+
return MAX_SIZE;
1316
}
1417
return newSize;
1518
});
@@ -18,8 +21,8 @@ const App: React.FC = () => {
1821
const decline = () => {
1922
setSize((prevSize) => {
2023
const newSize = prevSize - 10;
21-
if (newSize < 48) {
22-
return 48;
24+
if (newSize <= MIN_SIZE) {
25+
return MIN_SIZE;
2326
}
2427
return newSize;
2528
});
@@ -28,10 +31,10 @@ const App: React.FC = () => {
2831
return (
2932
<>
3033
<Button.Group style={{ marginBottom: 16 }}>
31-
<Button onClick={decline} disabled={size <= 48} icon={<MinusOutlined />}>
34+
<Button onClick={decline} disabled={size <= MIN_SIZE} icon={<MinusOutlined />}>
3235
Smaller
3336
</Button>
34-
<Button onClick={increase} disabled={size >= 300} icon={<PlusOutlined />}>
37+
<Button onClick={increase} disabled={size >= MAX_SIZE} icon={<PlusOutlined />}>
3538
Larger
3639
</Button>
3740
</Button.Group>

components/qr-code/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ const QRCode: React.FC<QRCodeProps> = (props) => {
7878
[`${prefixCls}-borderless`]: !bordered,
7979
});
8080

81+
const mergedStyle: React.CSSProperties = {
82+
width: size,
83+
height: size,
84+
backgroundColor: bgColor,
85+
...style,
86+
};
87+
8188
return wrapCSSVar(
82-
<div
83-
className={mergedCls}
84-
style={{ ...style, width: size, height: size, backgroundColor: bgColor }}
85-
>
89+
<div className={mergedCls} style={mergedStyle}>
8690
{status !== 'active' && (
8791
<div className={`${prefixCls}-mask`}>
8892
{status === 'loading' && <Spin />}

0 commit comments

Comments
 (0)