React JsBarcode使用

瞎扯

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

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

今天正好封装了个组件.

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

JsBarcode

几种应用:

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


这种用classname找的方法.感觉不好.


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


最后这个才像是需要的.

封装

/**
 * 简单生成条形码
 * {
 * width: 2,//较细处条形码的宽度
 * height: 100, //条形码的宽度,无高度直接设置项,由位数决定,可以通过CSS去调整,见下
 * quite: 10,
 * format: "CODE128",
 * displayValue: false,//是否在条形码下方显示文字
 * font:"monospace",
 * textAlign:"center",
 * fontSize: 12,
 * backgroundColor:"",
 * lineColor:"#000"//条形码颜色
 * }
 */
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, // 是否由Barcode显示二维码下面的值
            width, // 每条线的宽度
            height, // 高度
            margin, //边距
        });
    };

    render() {
        const {
            labelClassName, label, labelStyle, className, style, displayValue = true,
        } = this.props;
        return (
            <div className={className} style={style}>
                <svg
                    ref={(ref) => {
                        this.barcode = ref;
                    }}
                />
                {displayValue ? null : <p className={labelClassName} style={labelStyle}>{label}</p>} // 自定义样式的值显示
            </div>
        );
    }
}

复制代码

简单使用

   <SimpleBarcode
        label={123123}
        height="45"
        width="1"//这里只能是大于1的整数
    />
复制代码

期待你的评论,点赞

转载于:https://juejin.im/post/5ce35492f265da1b904bb518

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值