日更(五十)-react-JsBarcode

瞎扯

百度,google搜了一下,发现很多教怎么用的,就是没有封装组件的.

实际这个东西,用起来还是很简单的.

今天正好封装了个组件.

今天算是写react的第三个月了.react还是比较好上手的.

JsBarcode

几种应用:

这种是JQuery的.明显不是想要的,略过


这种用classname找的方法.好像也不行.



这种有点不明白.看起来也不是想要的.


最后这个才像是需要的.

封装

import React, { Component } from 'react';
import * as Barcode from 'jsbarcode';

/**
 * 简单生成条形码
 */
class SimpleBarcode extends Component {
    componentDidMount() {
        this.createBarcode();
    }

    componentWillReceiveProps(nextProps) {
        if (this.props !== nextProps) {
            this.createBarcode();
        }
    }

    createBarcode = () => {
        if (!this.barcode) return;
        const {
            width = 1, height = 35, margin = 0, label, displayValue=true
        } = this.props;
        if (!label) {
            return;
        }
        Barcode(this.barcode, label, {
            displayValue, //是否在下面显示具体文字
            width, //线的宽度系数,1是正常,2是两倍,数越大越粗.
            height,// 条形码高度
            margin,
        });
    };

    render() {
        const { labelClassName, label, className , displayValue=true} = this.props;
        return (
            <div className={className}>
                <svg
                    ref={(ref) => {
                        this.barcode = ref;
                    }}
                />
                {displayValue?null:<p className={labelClassName}>{label}</p>}
            </div>
        );
    }
}

export default SimpleBarcode;


您的喜欢与回复是我最大的动力-_-
交流群:493180098

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章