2023-07-15 12:57:03 +08:00
|
|
|
'use client';
|
|
|
|
|
2022-12-05 14:15:26 +08:00
|
|
|
import { ReloadOutlined } from '@ant-design/icons';
|
2023-02-22 18:18:26 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-25 13:32:48 +08:00
|
|
|
import { QRCodeCanvas, QRCodeSVG } from 'qrcode.react';
|
|
|
|
import React, { useContext } from 'react';
|
2023-03-19 16:13:41 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-02-22 18:18:26 +08:00
|
|
|
import Button from '../button';
|
2022-12-05 14:15:26 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2023-02-22 18:18:26 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-03-21 13:08:43 +08:00
|
|
|
import { useLocale } from '../locale';
|
2022-12-05 14:15:26 +08:00
|
|
|
import Spin from '../spin';
|
|
|
|
import theme from '../theme';
|
2023-05-25 13:32:48 +08:00
|
|
|
import type { QRCodeProps, QRProps } from './interface';
|
2023-02-22 18:18:26 +08:00
|
|
|
import useStyle from './style/index';
|
2022-12-05 14:15:26 +08:00
|
|
|
|
|
|
|
const { useToken } = theme;
|
|
|
|
|
|
|
|
const QRCode: React.FC<QRCodeProps> = (props) => {
|
2023-06-24 18:19:44 +08:00
|
|
|
const { token } = useToken();
|
2022-12-05 14:15:26 +08:00
|
|
|
const {
|
|
|
|
value,
|
2023-05-25 13:32:48 +08:00
|
|
|
type = 'canvas',
|
2022-12-05 14:15:26 +08:00
|
|
|
icon = '',
|
|
|
|
size = 160,
|
|
|
|
iconSize = 40,
|
2023-06-24 18:19:44 +08:00
|
|
|
color = token.colorText,
|
2022-12-05 14:15:26 +08:00
|
|
|
errorLevel = 'M',
|
|
|
|
status = 'active',
|
|
|
|
bordered = true,
|
|
|
|
onRefresh,
|
|
|
|
style,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-12-05 14:15:26 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2023-05-09 17:24:28 +08:00
|
|
|
bgColor = 'transparent',
|
2022-12-05 14:15:26 +08:00
|
|
|
} = props;
|
|
|
|
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('qrcode', customizePrefixCls);
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2023-05-25 13:32:48 +08:00
|
|
|
const imageSettings: QRProps['imageSettings'] = {
|
|
|
|
src: icon,
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
height: iconSize,
|
|
|
|
width: iconSize,
|
|
|
|
excavate: true,
|
|
|
|
};
|
2023-05-25 21:03:38 +08:00
|
|
|
|
2023-05-25 13:32:48 +08:00
|
|
|
const qrCodeProps = {
|
|
|
|
value,
|
|
|
|
size: size - (token.paddingSM + token.lineWidth) * 2,
|
|
|
|
level: errorLevel,
|
|
|
|
bgColor,
|
2023-06-24 18:19:44 +08:00
|
|
|
fgColor: color,
|
2023-05-25 13:32:48 +08:00
|
|
|
imageSettings: icon ? imageSettings : undefined,
|
2023-05-25 21:03:38 +08:00
|
|
|
};
|
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [locale] = useLocale('QRCode');
|
2023-02-22 18:18:26 +08:00
|
|
|
|
2022-12-05 14:15:26 +08:00
|
|
|
if (!value) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(false, 'QRCode', 'need to receive `value` props');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:32:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
!(icon && errorLevel === 'L'),
|
|
|
|
'QRCode',
|
|
|
|
'ErrorLevel `L` is not recommended to be used with `icon`, for scanning result would be affected by low level.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
const cls = classNames(prefixCls, className, rootClassName, hashId, {
|
2022-12-05 14:15:26 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
});
|
|
|
|
|
|
|
|
return wrapSSR(
|
2023-05-09 17:24:28 +08:00
|
|
|
<div style={{ ...style, width: size, height: size, backgroundColor: bgColor }} className={cls}>
|
2023-02-22 18:18:26 +08:00
|
|
|
{status !== 'active' && (
|
|
|
|
<div className={`${prefixCls}-mask`}>
|
|
|
|
{status === 'loading' && <Spin />}
|
|
|
|
{status === 'expired' && (
|
|
|
|
<>
|
|
|
|
<p className={`${prefixCls}-expired`}>{locale?.expired}</p>
|
2023-03-19 16:13:41 +08:00
|
|
|
{onRefresh && (
|
2023-02-22 18:18:26 +08:00
|
|
|
<Button type="link" icon={<ReloadOutlined />} onClick={onRefresh}>
|
|
|
|
{locale?.refresh}
|
|
|
|
</Button>
|
2022-12-05 14:15:26 +08:00
|
|
|
)}
|
2023-02-22 18:18:26 +08:00
|
|
|
</>
|
2022-12-05 14:15:26 +08:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2023-05-25 21:03:38 +08:00
|
|
|
{type === 'canvas' ? <QRCodeCanvas {...qrCodeProps} /> : <QRCodeSVG {...qrCodeProps} />}
|
2023-02-22 18:18:26 +08:00
|
|
|
</div>,
|
2022-12-05 14:15:26 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
QRCode.displayName = 'QRCode';
|
|
|
|
}
|
|
|
|
|
2022-12-05 14:15:26 +08:00
|
|
|
export default QRCode;
|