react-native-barcode-aspen
Version:
react-native-barcode-expo with updates for aspen
1 lines • 10.6 kB
Source Map (JSON)
{"version":3,"file":"index.modern.mjs","sources":["../src/ErrorBoundary.tsx","../src/index.tsx"],"sourcesContent":["import React from 'react';\n\nclass ErrorBoundary extends React.Component<\n { children: any },\n { hasError: boolean }\n> {\n constructor(props) {\n super(props);\n this.state = { hasError: false };\n }\n\n static getDerivedStateFromError(error) {\n return { hasError: true };\n }\n\n componentDidCatch(error: any, errorInfo: any) {\n console.log(error, errorInfo);\n }\n\n render() {\n if (this.state.hasError) {\n // You can render any custom fallback UI\n return <h1>Something went wrong.</h1>;\n }\n\n return this.props.children;\n }\n}\n\nexport default ErrorBoundary;\n","import React, { useState, useEffect } from 'react';\nimport { View, StyleSheet, Text } from 'react-native';\nimport barcodes from 'jsbarcode/src/barcodes';\nimport Svg, { Path } from 'react-native-svg';\n\nimport ErrorBoundary from './ErrorBoundary';\n\ntype props = {\n value: string;\n format: string;\n width: number;\n height: number;\n text?: string;\n textColor: string;\n lineColor: string;\n background: string;\n onError?: (error: Error) => any;\n};\n\nconst Barcode = ({\n value,\n format = 'CODE128',\n width = 2,\n height = 100,\n text,\n textColor = '#000000',\n lineColor = '#000000',\n background = '#ffffff',\n onError,\n}: props) => {\n const [bars, setBars] = useState([]);\n const [barCodeWidth, setBarCodeWidth] = useState(0);\n\n const props = {\n value,\n format,\n width,\n height,\n text,\n textColor,\n lineColor,\n background,\n onError,\n };\n\n useEffect(() => {\n update();\n }, [value]);\n\n const update = () => {\n const encoder = barcodes[format];\n const encoded = encode(value, encoder, props);\n\n if (encoded) {\n setBars(drawSvgBarCode(encoded, props));\n setBarCodeWidth(encoded.data.length * width);\n }\n };\n\n const drawSvgBarCode = (encoding, options: props) => {\n const rects = [];\n // binary data of barcode\n const binary = encoding.data;\n\n let barWidth = 0;\n let x = 0;\n let yFrom = 0;\n\n for (let b = 0; b < binary.length; b++) {\n x = b * options.width;\n if (binary[b] === '1') {\n barWidth++;\n } else if (barWidth > 0) {\n rects[rects.length] = drawRect(\n x - options.width * barWidth,\n yFrom,\n options.width * barWidth,\n options.height,\n );\n barWidth = 0;\n }\n }\n\n // Last draw is needed since the barcode ends with 1\n if (barWidth > 0) {\n rects[rects.length] = drawRect(\n x - options.width * (barWidth - 1),\n yFrom,\n options.width * barWidth,\n options.height,\n );\n }\n\n return rects;\n };\n\n const drawRect = (x, y, width, height) => {\n return `M${x},${y}h${width}v${height}h-${width}z`;\n };\n\n // encode() handles the Encoder call and builds the binary string to be rendered\n const encode = (text: string, Encoder: any, options: props) => {\n // If text is not a non-empty string, throw error.\n if (typeof text !== 'string' || text.length === 0) {\n if (options.onError) {\n options.onError(new Error('Barcode value must be a non-empty string'));\n return;\n }\n throw new Error('Barcode value must be a non-empty string');\n }\n\n let encoder;\n\n try {\n encoder = new Encoder(text, options);\n } catch (error) {\n // If the encoder could not be instantiated, throw error.\n if (options.onError) {\n options.onError(new Error('Invalid barcode format.'));\n return;\n }\n throw new Error('Invalid barcode format.');\n }\n\n // If the input is not valid for the encoder, throw error.\n if (!encoder.valid()) {\n if (options.onError) {\n options.onError(new Error('Invalid barcode for selected format.'));\n return;\n }\n throw new Error('Invalid barcode for selected format.');\n }\n\n // Make a request for the binary data (and other infromation) that should be rendered\n // encoded stucture is {\n // text: 'xxxxx',\n // data: '110100100001....'\n // }\n const encoded = encoder.encode();\n return encoded;\n };\n\n const backgroundStyle = {\n backgroundColor: background,\n };\n return (\n <ErrorBoundary>\n <View style={[styles.svgContainer, backgroundStyle]}>\n <Svg height={height} width={barCodeWidth} fill={lineColor}>\n <Path d={bars.join(' ')} />\n </Svg>\n {typeof text !== 'undefined' && (\n <Text\n style={{\n color: textColor,\n width: barCodeWidth,\n textAlign: 'center',\n }}\n >\n {text}\n </Text>\n )}\n </View>\n </ErrorBoundary>\n );\n};\n\nconst styles = StyleSheet.create({\n svgContainer: {\n alignItems: 'center',\n padding: 10,\n },\n});\n\nexport default Barcode;\n"],"names":["ErrorBoundary","React","Component","constructor","props","state","hasError","getDerivedStateFromError","error","componentDidCatch","errorInfo","console","log","render","children","Barcode","value","format","width","height","text","textColor","lineColor","background","onError","bars","setBars","useState","barCodeWidth","setBarCodeWidth","useEffect","update","encoder","barcodes","encoded","encode","drawSvgBarCode","data","length","encoding","options","rects","binary","barWidth","x","yFrom","b","drawRect","y","Encoder","Error","valid","backgroundStyle","backgroundColor","styles","svgContainer","join","color","textAlign","StyleSheet","create","alignItems","padding"],"mappings":";;;;;AAEA,MAAMA,aAAN,SAA4BC,KAAK,CAACC,SAAlC,CAGC;EACCC,WAAA,CAAYC,KAAZ,EAAiB;AACf,IAAA,KAAA,CAAMA,KAAN,CAAA,CAAA;AACA,IAAA,IAAA,CAAKC,KAAL,GAAa;AAAEC,MAAAA,QAAQ,EAAE,KAAA;KAAzB,CAAA;AACD,GAAA;;EAE8B,OAAxBC,wBAAwB,CAACC,KAAD,EAAM;IACnC,OAAO;AAAEF,MAAAA,QAAQ,EAAE,IAAA;KAAnB,CAAA;AACD,GAAA;;AAEDG,EAAAA,iBAAiB,CAACD,KAAD,EAAaE,SAAb,EAA2B;AAC1CC,IAAAA,OAAO,CAACC,GAAR,CAAYJ,KAAZ,EAAmBE,SAAnB,CAAA,CAAA;AACD,GAAA;;AAEDG,EAAAA,MAAM,GAAA;AACJ,IAAA,IAAI,IAAKR,CAAAA,KAAL,CAAWC,QAAf,EAAyB;AACvB;AACA,MAAA,oBAAO,KAAP,CAAA,aAAA,CAAA,IAAA,EAAA,IAAA,EAAA,uBAAA,CAAA,CAAA;AACD,KAAA;;IAED,OAAO,IAAA,CAAKF,KAAL,CAAWU,QAAlB,CAAA;AACD,GAAA;;AArBF;;ACcKC,MAAAA,OAAO,GAAG,CAAC;EACfC,KADe;EAEfC,MAAM,EAANA,OAAM,GAAG,SAFM;EAGfC,KAAK,EAALA,MAAK,GAAG,CAHO;EAIfC,MAAM,EAANA,OAAM,GAAG,GAJM;EAKfC,IALe;EAMfC,SAAS,EAATA,UAAS,G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