File size: 3,369 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import React, { useContext } from 'react';
import { QRCodeCanvas, QRCodeSVG } from '@rc-component/qrcode';
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import pickAttrs from 'rc-util/lib/pickAttrs';
import { devUseWarning } from '../_util/warning';
import type { ConfigConsumerProps } from '../config-provider';
import { ConfigContext } from '../config-provider';
import { useLocale } from '../locale';
import { useToken } from '../theme/internal';
import type { QRCodeProps, QRProps } from './interface';
import QRcodeStatus from './QrcodeStatus';
import useStyle from './style/index';
const QRCode: React.FC<QRCodeProps> = (props) => {
const [, token] = useToken();
const {
value,
type = 'canvas',
icon = '',
size = 160,
iconSize,
color = token.colorText,
errorLevel = 'M',
status = 'active',
bordered = true,
onRefresh,
style,
className,
rootClassName,
prefixCls: customizePrefixCls,
bgColor = 'transparent',
statusRender,
...rest
} = props;
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
const prefixCls = getPrefixCls('qrcode', customizePrefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
const imageSettings: QRProps['imageSettings'] = {
src: icon,
x: undefined,
y: undefined,
height: typeof iconSize === 'number' ? iconSize : (iconSize?.height ?? 40),
width: typeof iconSize === 'number' ? iconSize : (iconSize?.width ?? 40),
excavate: true,
crossOrigin: 'anonymous',
};
const a11yProps = pickAttrs(rest, true);
const restProps = omit<React.HTMLAttributes<HTMLDivElement>, keyof React.AriaAttributes>(
rest,
Object.keys(a11yProps) as (keyof React.AriaAttributes)[],
);
const qrCodeProps = {
value,
size,
level: errorLevel,
bgColor,
fgColor: color,
style: { width: style?.width, height: style?.height },
imageSettings: icon ? imageSettings : undefined,
...a11yProps,
};
const [locale] = useLocale('QRCode');
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('QRCode');
warning(!!value, 'usage', 'need to receive `value` props');
warning(
!(icon && errorLevel === 'L'),
'usage',
'ErrorLevel `L` is not recommended to be used with `icon`, for scanning result would be affected by low level.',
);
}
if (!value) {
return null;
}
const mergedCls = classNames(prefixCls, className, rootClassName, hashId, cssVarCls, {
[`${prefixCls}-borderless`]: !bordered,
});
const mergedStyle: React.CSSProperties = {
backgroundColor: bgColor,
...style,
width: style?.width ?? size,
height: style?.height ?? size,
};
return wrapCSSVar(
<div {...restProps} className={mergedCls} style={mergedStyle}>
{status !== 'active' && (
<div className={`${prefixCls}-mask`}>
<QRcodeStatus
prefixCls={prefixCls}
locale={locale}
status={status}
onRefresh={onRefresh}
statusRender={statusRender}
/>
</div>
)}
{type === 'canvas' ? <QRCodeCanvas {...qrCodeProps} /> : <QRCodeSVG {...qrCodeProps} />}
</div>,
);
};
if (process.env.NODE_ENV !== 'production') {
QRCode.displayName = 'QRCode';
}
export default QRCode;
|