2024-11-04 14:50:00 +08:00
|
|
|
import React, { useContext, type AriaAttributes } from 'react';
|
2024-06-21 11:42:21 +08:00
|
|
|
import { QRCodeCanvas, QRCodeSVG } from '@rc-component/qrcode';
|
2024-07-12 10:49:47 +08:00
|
|
|
import classNames from 'classnames';
|
2024-11-04 14:50:00 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import pickAttrs from 'rc-util/lib/pickAttrs';
|
2023-09-07 10:15:58 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
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';
|
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';
|
2024-07-12 10:49:47 +08:00
|
|
|
import QRcodeStatus from './QrcodeStatus';
|
2024-11-04 14:50:00 +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,
|
2024-06-10 17:47:45 +08:00
|
|
|
iconSize,
|
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',
|
2024-07-12 10:49:47 +08:00
|
|
|
statusRender,
|
2024-06-10 17:47:45 +08:00
|
|
|
...rest
|
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,
|
2024-11-04 14:50:00 +08:00
|
|
|
height: typeof iconSize === 'number' ? iconSize : (iconSize?.height ?? 40),
|
|
|
|
width: typeof iconSize === 'number' ? iconSize : (iconSize?.width ?? 40),
|
2023-05-25 13:32:48 +08:00
|
|
|
excavate: true,
|
2024-06-21 11:42:21 +08:00
|
|
|
crossOrigin: 'anonymous',
|
2023-05-25 13:32:48 +08:00
|
|
|
};
|
2023-05-25 21:03:38 +08:00
|
|
|
|
2024-11-04 14:50:00 +08:00
|
|
|
const a11yProps = pickAttrs(rest, true);
|
|
|
|
const restProps = omit<React.HTMLAttributes<HTMLDivElement>, keyof AriaAttributes>(
|
|
|
|
rest,
|
|
|
|
Object.keys(a11yProps) as Array<keyof AriaAttributes>,
|
|
|
|
);
|
|
|
|
|
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,
|
2024-11-04 14:50:00 +08:00
|
|
|
...a11yProps,
|
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-11-04 14:50:00 +08:00
|
|
|
<div {...restProps} className={mergedCls} style={mergedStyle}>
|
2023-02-22 18:18:26 +08:00
|
|
|
{status !== 'active' && (
|
|
|
|
<div className={`${prefixCls}-mask`}>
|
2024-07-12 10:49:47 +08:00
|
|
|
<QRcodeStatus
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
locale={locale}
|
|
|
|
status={status}
|
|
|
|
onRefresh={onRefresh}
|
|
|
|
statusRender={statusRender}
|
|
|
|
/>
|
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;
|