{"version":3,"file":"index.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","constructor","props","super","state","hasError","getDerivedStateFromError","error","componentDidCatch","errorInfo","console","log","render","children","React","Component","const","Barcode","text","onError","useState","bars","setBars","barCodeWidth","setBarCodeWidth","value","format","width","height","textColor","lineColor","background","useEffect","update","encoder","barcodes","encoded","encode","drawSvgBarCode","data","length","encoding","options","rects","binary","let","barWidth","x","yFrom","b","drawRect","y","Encoder","Error","valid","backgroundStyle","backgroundColor","styles","svgContainer","join","color","textAlign","StyleSheet","create","alignItems","padding"],"mappings":";;;;;AAEA,IAAMA,aAAN;EAIEC,uBAAYC;IACVC,sBAAMD,KAAN;IACA,KAAKE,KAAL,GAAa;MAAEC,QAAQ,EAAE;KAAzB;;;;;;;EAG6B,cAAxBC,8DAAyBC,KAAD;IAC7B,OAAO;MAAEF,QAAQ,EAAE;KAAnB;;;0BAGFG,gDAAkBD,KAAD,EAAaE,SAAb;IACfC,OAAO,CAACC,GAAR,CAAYJ,KAAZ,EAAmBE,SAAnB;;;0BAGFG;IACE,IAAI,KAAKR,KAAL,CAAWC,QAAf,EAAyB;;MAEvB,OAAO,iCAAI,uBAAqB,EAAhC;;;IAGF,OAAO,KAAKH,KAAL,CAAWW,QAAlB;;;;EAvBwBC,KAAK,CAACC;;ACiBlCC,IAAMC,OAAO,gBAAG;;6DAEL;yDACD;6DACC,IACTC;;yEACY;yEACA;6EACC,UACbC;;;cAEwBC,QAAQ,CAAC,EAAD;EAAzBC;EAAMC;cAC2BF,QAAQ,CAAC,CAAD;EAAzCG;EAAcC;EAErBR,IAAMd,KAAK,GAAG;WACZuB,KADY;YAEZC,MAFY;WAGZC,KAHY;YAIZC,MAJY;UAKZV,IALY;eAMZW,SANY;eAOZC,SAPY;gBAQZC,UARY;aASZZ;GATF;EAYAa,SAAS;IACPC,MAAM;GADC,EAEN,CAACR,KAAD,C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