2023-09-07 10:15:58 +08:00
|
|
|
import React, { useContext } from 'react';
|
2023-07-31 23:55:25 +08:00
|
|
|
import ReloadOutlined from '@ant-design/icons/ReloadOutlined';
|
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';
|
2023-09-07 10:15:58 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } 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';
|
2023-09-07 10:15:58 +08:00
|
|
|
import { useToken } from '../theme/internal';
|
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 QRCode: React.FC<QRCodeProps> = (props) => {
|
2023-09-07 10:15:58 +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);
|
2023-11-14 10:34:01 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-12-05 14:15:26 +08:00
|
|
|
|
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,
|
2023-11-13 18:35:23 +08:00
|
|
|
size,
|
2023-05-25 13:32:48 +08:00
|
|
|
level: errorLevel,
|
|
|
|
bgColor,
|
2023-06-24 18:19:44 +08:00
|
|
|
fgColor: color,
|
2024-04-30 17:03:00 +08:00
|
|
|
style: { width: style?.width, height: style?.height },
|
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-07 23:32:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('QRCode');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning(!!value, 'usage', 'need to receive `value` props');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-12-07 23:32:04 +08:00
|
|
|
warning(
|
|
|
|
!(icon && errorLevel === 'L'),
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-12-07 23:32:04 +08:00
|
|
|
'ErrorLevel `L` is not recommended to be used with `icon`, for scanning result would be affected by low level.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (!value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const mergedCls = classNames(prefixCls, className, rootClassName, hashId, cssVarCls, {
|
2022-12-05 14:15:26 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
});
|
|
|
|
|
2024-03-25 10:59:44 +08:00
|
|
|
const mergedStyle: React.CSSProperties = {
|
|
|
|
backgroundColor: bgColor,
|
|
|
|
...style,
|
2024-04-30 17:03:00 +08:00
|
|
|
width: style?.width ?? size,
|
|
|
|
height: style?.height ?? size,
|
2024-03-25 10:59:44 +08:00
|
|
|
};
|
|
|
|
|
2023-11-14 10:34:01 +08:00
|
|
|
return wrapCSSVar(
|
2024-03-25 10:59:44 +08:00
|
|
|
<div className={mergedCls} style={mergedStyle}>
|
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
|
|
|
)}
|
2023-12-30 14:52:08 +08:00
|
|
|
{status === 'scanned' && <p className={`${prefixCls}-scanned`}>{locale?.scanned}</p>}
|
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;
|