ant-design/components/qr-code/demo/download.tsx
EmilyyyLiu 47eb1b661e
feat(Space): unify orientation attribute usage (#53669)
* feat[Space]:Unified use of orientation attribute

* feat: use useOrientation and change doc, add test

* feat: add warning

* test: update snapshots

* Update components/space/Compact.tsx

Co-authored-by: thinkasany <480968828@qq.com>
Signed-off-by: EmilyyyLiu <100924403+EmilyyyLiu@users.noreply.github.com>

* test: reset snapshots,delete direction default value

* feat: change demo direnction->orentation

* feat: demos direction ->orientation

* feat: add vertical, and change demos , doc

* feat: change demo space -> flex

* feat: change demo space -> flex

* feat: space -> flex

* Update components/space/index.tsx

Signed-off-by: thinkasany <480968828@qq.com>

* Update components/space/Compact.tsx

Signed-off-by: thinkasany <480968828@qq.com>

* add warning toHaveBeenCalledWith

---------

Signed-off-by: EmilyyyLiu <100924403+EmilyyyLiu@users.noreply.github.com>
Signed-off-by: thinkasany <480968828@qq.com>
Co-authored-by: 刘欢 <lh01217311@antgroup.com>
Co-authored-by: thinkasany <480968828@qq.com>
2025-05-23 15:20:33 +08:00

56 lines
1.6 KiB
TypeScript

import React from 'react';
import { Button, QRCode, Segmented, Space } from 'antd';
import type { QRCodeProps } from 'antd';
function doDownload(url: string, fileName: string) {
const a = document.createElement('a');
a.download = fileName;
a.href = url;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
const downloadCanvasQRCode = () => {
const canvas = document.getElementById('myqrcode')?.querySelector<HTMLCanvasElement>('canvas');
if (canvas) {
const url = canvas.toDataURL();
doDownload(url, 'QRCode.png');
}
};
const downloadSvgQRCode = () => {
const svg = document.getElementById('myqrcode')?.querySelector<SVGElement>('svg');
const svgData = new XMLSerializer().serializeToString(svg!);
const blob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(blob);
doDownload(url, 'QRCode.svg');
};
const App: React.FC = () => {
const [renderType, setRenderType] = React.useState<QRCodeProps['type']>('canvas');
return (
<Space id="myqrcode" vertical>
<Segmented options={['canvas', 'svg']} value={renderType} onChange={setRenderType} />
<div>
<QRCode
type={renderType}
value="https://ant.design/"
bgColor="#fff"
style={{ marginBottom: 16 }}
icon="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg"
/>
<Button
type="primary"
onClick={renderType === 'canvas' ? downloadCanvasQRCode : downloadSvgQRCode}
>
Download
</Button>
</div>
</Space>
);
};
export default App;