UNPKG

react-qrcode

Version:

🤳 A React component for QR code generation with `qrcode`

151 lines (142 loc) • 4.14 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var react = require('react'); var QRCode$1 = require('qrcode'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var QRCode__default = /*#__PURE__*/_interopDefaultLegacy(QRCode$1); var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols; var __hasOwnProp$1 = Object.prototype.hasOwnProperty; var __propIsEnum$1 = Object.prototype.propertyIsEnumerable; var __objRest$1 = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp$1.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols$1) for (var prop of __getOwnPropSymbols$1(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum$1.call(source, prop)) target[prop] = source[prop]; } return target; }; const isQRCodeValue = (valueOrOptions) => typeof valueOrOptions === "string" || Array.isArray(valueOrOptions); const useQRCode = (valueOrOptions) => { const [dataURL, setDataURL] = react.useState(); react.useEffect(() => { const isValue = isQRCodeValue(valueOrOptions); const value = isValue ? valueOrOptions : valueOrOptions.value; let options; if (!isValue) { const _a = valueOrOptions, { quality } = _a, props = __objRest$1(_a, ["quality"]); options = Object.assign(props, { renderOptions: { quality } }); } if (!value) { return setDataURL(""); } QRCode__default["default"].toDataURL(value, options).then(setDataURL); }, [valueOrOptions]); return dataURL; }; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; const QRCode = (_a) => { var _b = _a, { color, errorCorrectionLevel, margin, maskPattern, quality, scale, toSJISFunc, type, value, version, width } = _b, props = __objRest(_b, [ "color", "errorCorrectionLevel", "margin", "maskPattern", "quality", "scale", "toSJISFunc", "type", "value", "version", "width" ]); return react.createElement("img", __spreadProps(__spreadValues({}, props), { src: useQRCode({ color, errorCorrectionLevel, margin, maskPattern, quality, scale, toSJISFunc, type, value, version, width }) })); }; const LEVELS = [ "low", "medium", "quartile", "high", "L", "M", "Q", "H" ]; const MASK_PATTERNS = [0, 1, 2, 3, 4, 5, 6, 7]; const MODES = [ "auto", "alphanumeric", "numeric", "kanji", "byte" ]; const TYPES = ["image/png", "image/jpeg", "image/webp"]; exports.LEVELS = LEVELS; exports.MASK_PATTERNS = MASK_PATTERNS; exports.MODES = MODES; exports.QRCode = QRCode; exports.TYPES = TYPES; exports.isQRCodeValue = isQRCodeValue; exports.useQRCode = useQRCode;