Skip to content

Commit

Permalink
Merge pull request #8 from tiaanduplessis/master
Browse files Browse the repository at this point in the history
onError handling and README update
  • Loading branch information
wonsikin committed Apr 25, 2017
2 parents 6ad7287 + e498cdf commit 314307f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,44 @@ format: PropTypes.oneOf([
```

![](./images/example.png)

## Properties

<table style="width:80%">
<tr>
<th>Property</th>
<th>Description</th>
</tr>
<tr>
<td><code>value</code></td>
<td>What the barcode stands for (required).</td>
</tr>
<tr>
<td><code>format</code></td>
<td>Which barcode type to use (default: CODE128).</td>
</tr>
<tr>
<td><code>text</code></td>
<td>Override text that is displayed.</td>
</tr>
<tr>
<td><code>width</code></td>
<td>Width of a single bar (default: 2)</td>
</tr>
<tr>
<td><code>height</code></td>
<td>Height of the barcode (default: 100)</td>
</tr>
<tr>
<td><code>lineColor</code></td>
<td>Color of the bars and text (default: #000000)</td>
</tr>
<tr>
<td><code>background</code></td>
<td>Background color of the barcode (default: #ffffff)</td>
</tr>
<tr>
<td><code>onError</code></td>
<td>Handler for invalid barcode of selected format</td>
</tr>
</table>
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export default class Barcode extends PureComponent {
/* Set the color of the bars and the text. */
lineColor: PropTypes.string,
/* Set the background of the barcode. */
background: PropTypes.string
background: PropTypes.string,
/* Handle error for invalid barcode of selected format */
onError: PropTypes.func
};

static defaultProps = {
Expand All @@ -39,7 +41,8 @@ export default class Barcode extends PureComponent {
width: 2,
height: 100,
lineColor: '#000000',
background: '#ffffff'
background: '#ffffff',
onError: undefined
};

constructor(props) {
Expand Down Expand Up @@ -67,8 +70,11 @@ export default class Barcode extends PureComponent {
update() {
const encoder = barcodes[this.props.format];
const encoded = this.encode(this.props.value, encoder, this.props);
this.state.bars = this.drawSvgBarCode(encoded, this.props);
this.state.barCodeWidth = encoded.data.length * this.props.width;

if (encoded) {
this.state.bars = this.drawSvgBarCode(encoded, this.props);
this.state.barCodeWidth = encoded.data.length * this.props.width;
}
}

drawSvgBarCode(encoding, options = {}) {
Expand Down Expand Up @@ -120,7 +126,14 @@ export default class Barcode extends PureComponent {

// If the input is not valid for the encoder, throw error.
if (!encoder.valid()) {
throw new Error('Invalid barcode for selected format.');

if (this.props.onError) {
this.props.onError(new Error('Invalid barcode for selected format.'));
return
} else {
throw new Error('Invalid barcode for selected format.')
}

}

// Make a request for the binary data (and other infromation) that should be rendered
Expand Down

0 comments on commit 314307f

Please sign in to comment.