UNPKG

taro-code

Version:

Taro.js barcode & qrcode, Image & Canvas

36 lines 1.87 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { useMemo, forwardRef, useImperativeHandle } from 'react'; import { Image } from '@tarojs/components'; import { createQrCodeImg } from '../../common/qrcode'; const QRCode = forwardRef((_a, ref) => { var { className, text = '', size = 100, scale = 4, typeNumber = 2, errorCorrectLevel = 'M', style = {}, foregroundColor = '#000000', backgroundColor = '#FFFFFF' } = _a, imageProps = __rest(_a, ["className", "text", "size", "scale", "typeNumber", "errorCorrectLevel", "style", "foregroundColor", "backgroundColor"]); const image = useMemo(() => { const options = { errorCorrectLevel, typeNumber, size: size * scale, black: foregroundColor, white: backgroundColor, }; return createQrCodeImg(text, options); }, [errorCorrectLevel, typeNumber, size, scale, foregroundColor, backgroundColor, text]); const widthString = size != null ? `${size}px` : ''; const heightString = size != null ? `${size}px` : ''; const finalStyle = Object.assign({ width: widthString, height: heightString }, style); useImperativeHandle(ref, () => { return { image }; }, [image]); return React.createElement(Image, Object.assign({}, imageProps, { className: className, style: finalStyle, src: image })); }); export default QRCode; //# sourceMappingURL=index.js.map