Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class Barcode extends PureComponent {
/* Set the background of the barcode. */
background: PropTypes.string,
/* Handle error for invalid barcode of selected format */
onError: PropTypes.func
onError: PropTypes.func,
/* Maximum width of the barcode */
maxWidth: PropTypes.number
};

static defaultProps = {
Expand All @@ -37,7 +39,8 @@ export default class Barcode extends PureComponent {
lineColor: '#000000',
textColor: '#000000',
background: '#ffffff',
onError: undefined
onError: undefined,
maxWidth: undefined
};

constructor(props) {
Expand All @@ -64,11 +67,20 @@ export default class Barcode extends PureComponent {

update() {
const encoder = barcodes[this.props.format];
const encoded = this.encode(this.props.value, encoder, this.props);
let encoded = this.encode(this.props.value, encoder, this.props);
let { width, maxWidth } = this.props;

if (maxWidth && encoded) {
const length = encoded.data.length * width;
if (length > maxWidth) {
width = maxWidth / encoded.data.length;
encoded = this.encode(this.props.value, encoder, {...this.props, width})
}
}

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

Expand Down