Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: qrcode canvas supports style configuration #48194

Merged
merged 13 commits into from Apr 30, 2024
26 changes: 13 additions & 13 deletions components/qr-code/__tests__/__snapshots__/demo-extend.test.ts.snap
Expand Up @@ -31,7 +31,7 @@ Array [
>
<div
class="ant-qrcode ant-qrcode-borderless"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -56,7 +56,7 @@ exports[`renders components/qr-code/demo/base.tsx extend context correctly 1`] =
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand Down Expand Up @@ -89,7 +89,7 @@ exports[`renders components/qr-code/demo/customColor.tsx extend context correctl
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -102,7 +102,7 @@ exports[`renders components/qr-code/demo/customColor.tsx extend context correctl
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: rgb(245, 245, 245);"
style="background-color: rgb(245, 245, 245); width: 160px; height: 160px;"
>
<canvas
height="160"
Expand Down Expand Up @@ -189,7 +189,7 @@ Array [
</div>,
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -211,7 +211,7 @@ exports[`renders components/qr-code/demo/download.tsx extend context correctly 1
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: rgb(255, 255, 255); margin-bottom: 16px;"
style="background-color: rgb(255, 255, 255); margin-bottom: 16px; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -235,7 +235,7 @@ exports[`renders components/qr-code/demo/errorlevel.tsx extend context correctly
Array [
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent; margin-bottom: 16px;"
style="background-color: transparent; margin-bottom: 16px; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand Down Expand Up @@ -315,7 +315,7 @@ exports[`renders components/qr-code/demo/errorlevel.tsx extend context correctly
exports[`renders components/qr-code/demo/icon.tsx extend context correctly 1`] = `
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -336,7 +336,7 @@ exports[`renders components/qr-code/demo/status.tsx extend context correctly 1`]
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -371,7 +371,7 @@ exports[`renders components/qr-code/demo/status.tsx extend context correctly 1`]
</div>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -420,7 +420,7 @@ exports[`renders components/qr-code/demo/status.tsx extend context correctly 1`]
</div>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -450,7 +450,7 @@ exports[`renders components/qr-code/demo/type.tsx extend context correctly 1`] =
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand All @@ -463,7 +463,7 @@ exports[`renders components/qr-code/demo/type.tsx extend context correctly 1`] =
>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<svg
height="160"
Expand Down
24 changes: 12 additions & 12 deletions components/qr-code/__tests__/__snapshots__/demo.test.ts.snap
Expand Up @@ -20,7 +20,7 @@ exports[`renders components/qr-code/demo/base.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<canvas
height="160"
Expand Down Expand Up @@ -51,7 +51,7 @@ exports[`renders components/qr-code/demo/customColor.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<canvas
height="160"
Expand All @@ -64,7 +64,7 @@ exports[`renders components/qr-code/demo/customColor.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:#f5f5f5"
style="background-color:#f5f5f5;width:160px;height:160px"
>
<canvas
height="160"
Expand Down Expand Up @@ -149,7 +149,7 @@ Array [
</div>,
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<canvas
height="160"
Expand All @@ -169,7 +169,7 @@ exports[`renders components/qr-code/demo/download.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:#fff;margin-bottom:16px"
style="background-color:#fff;margin-bottom:16px;width:160px;height:160px"
>
<canvas
height="160"
Expand All @@ -191,7 +191,7 @@ exports[`renders components/qr-code/demo/errorlevel.tsx correctly 1`] = `
Array [
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent;margin-bottom:16px"
style="background-color:transparent;margin-bottom:16px;width:160px;height:160px"
>
<canvas
height="160"
Expand Down Expand Up @@ -269,7 +269,7 @@ Array [
exports[`renders components/qr-code/demo/icon.tsx correctly 1`] = `
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<canvas
height="160"
Expand All @@ -288,7 +288,7 @@ exports[`renders components/qr-code/demo/status.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -323,7 +323,7 @@ exports[`renders components/qr-code/demo/status.tsx correctly 1`] = `
</div>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -372,7 +372,7 @@ exports[`renders components/qr-code/demo/status.tsx correctly 1`] = `
</div>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<div
class="ant-qrcode-mask"
Expand Down Expand Up @@ -400,7 +400,7 @@ exports[`renders components/qr-code/demo/type.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<canvas
height="160"
Expand All @@ -413,7 +413,7 @@ exports[`renders components/qr-code/demo/type.tsx correctly 1`] = `
>
<div
class="ant-qrcode"
style="width:160px;height:160px;background-color:transparent"
style="background-color:transparent;width:160px;height:160px"
>
<svg
height="160"
Expand Down
Expand Up @@ -6,7 +6,7 @@ exports[`QRCode test should correct render 1`] = `
<div>
<div
class="ant-qrcode"
style="width: 160px; height: 160px; background-color: transparent;"
style="background-color: transparent; width: 160px; height: 160px;"
>
<canvas
height="160"
Expand Down
12 changes: 7 additions & 5 deletions components/qr-code/__tests__/index.test.tsx
Expand Up @@ -40,7 +40,9 @@ describe('QRCode test', () => {

it('support custom size', () => {
const { container } = render(<QRCode value="test" size={100} />);
expect(container.querySelector('.ant-qrcode')).toHaveStyle('width: 100px; height: 100px');
const canvas = container.querySelector<HTMLCanvasElement>('.ant-qrcode > canvas')!;
expect(canvas.width).toBe(100);
expect(canvas.height).toBe(100);
});

it('support refresh', () => {
Expand Down Expand Up @@ -88,10 +90,10 @@ describe('QRCode test', () => {
errSpy.mockRestore();
});

it('correct style order', () => {
const { container } = render(<QRCode value="test" size={80} style={{ width: 100 }} />);
expect(container.querySelector<HTMLDivElement>('.ant-qrcode')).toHaveStyle(
'width: 100px; height: 80px',
it('correct style order for canvas', () => {
const { container } = render(<QRCode value="test" size={80} style={{ width: '100%' }} />);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加了新特性的话,需要新增 test case,不要动原来用例

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(container.querySelector<HTMLCanvasElement>('.ant-qrcode > canvas')).toHaveStyle(
'width: 100%',
);
});
});
6 changes: 3 additions & 3 deletions components/qr-code/index.tsx
Expand Up @@ -52,7 +52,7 @@ const QRCode: React.FC<QRCodeProps> = (props) => {
level: errorLevel,
bgColor,
fgColor: color,
style: { width: undefined, height: undefined },
style: { width: style?.width, height: style?.height },
afc163 marked this conversation as resolved.
Show resolved Hide resolved
imageSettings: icon ? imageSettings : undefined,
};

Expand All @@ -79,10 +79,10 @@ const QRCode: React.FC<QRCodeProps> = (props) => {
});

const mergedStyle: React.CSSProperties = {
width: size,
height: size,
backgroundColor: bgColor,
...style,
width: style?.width ?? size,
height: style?.height ?? size,
};

return wrapCSSVar(
Expand Down
2 changes: 2 additions & 0 deletions components/qr-code/style/index.ts
Expand Up @@ -60,6 +60,8 @@ const genQRCodeStyle: GenerateStyle<QRCodeToken> = (token) => {
},
[`${componentCls}-borderless`]: {
borderColor: 'transparent',
padding: 0,
borderRadius: 0,
},
};
};
Expand Down