king
2020-11-13 dfb7b774d9934f4f5b3edf1a39bf73be9c90c55d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import JsBarcode from 'jsbarcode'
 
import './index.scss'
 
class BarCode extends Component {
  static propTpyes = {
    card: PropTypes.object,  // 条码设置
    value: PropTypes.any,    // 条码值
  }
 
  componentDidMount () {
    this.resetBarcode()
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if ((nextProps.value && this.props.value !== nextProps.value) || !is(fromJS(nextProps.card), fromJS(this.props.card))) {
      this.setState({}, () => {
        this.resetBarcode()
      })
    }
  }
 
  resetBarcode = () => {
    const { value, card } = this.props
 
    let style = card.style || {}
 
    JsBarcode(this.barcode, value, {
      displayValue: card.displayValue === 'true',
      width: card.interval || 1,
      height: card.barHeight || 25,
      margin: 0,
      fontOptions: `${style.fontWeight || ''} ${style.fontStyle || ''}`,
      textAlign: style.textAlign || 'left',
      fontSize: (style.fontSize || 14) + 'px',
      lineColor: style.color || '#000000'
    })
 
  }
 
  render() {
    return (
      <div className="barcode-box">
        <svg ref={(ref) => { this.barcode = ref }}/>
      </div>
    )
  }
}
 
export default BarCode