king
8 天以前 a1e9b18a4dbfd21e1bf4d5cb60974ac2f0115efd
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button } from 'antd'
 
import MkIcon from '@/components/mk-icon'
import MKEmitter from '@/utils/events.js'
// import './index.scss'
 
class EditLine extends Component {
  static propTpyes = {
    btn: PropTypes.object,            // 按钮
    disabled: PropTypes.any,          // 行按钮禁用
  }
 
  state = {
    disabled: false,
    hidden: false
  }
 
  UNSAFE_componentWillMount () {
    const { btn, selectedData, BData, disabled } = this.props
 
    if (btn.controlField) {
      this.setStatus(btn, selectedData || [], BData, disabled)
    } else if (disabled) {
      this.setState({disabled: true})
    }
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { btn } = this.props
 
    if (btn.controlField) {
      this.setStatus(btn, nextProps.selectedData || [], nextProps.BData, nextProps.disabled)
    } else {
      this.setState({disabled: nextProps.disabled === true})
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  setStatus = (btn, data, BData, disprop) => {
    let disabled = false
    let hidden = false
 
    if (btn.control !== 'parent') {
      if (data.length > 0) {
        data.forEach(item => {
          let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
          if (btn.controlVals.includes(s)) {
            disabled = true
          }
        })
      } else if (btn.controlVals.includes('')) {
        disabled = true
      }
    } else {
      if (!BData || !BData.hasOwnProperty(btn.controlField)) {
        hidden = true
      } else {
        let s = BData[btn.controlField] + ''
        if (btn.controlVals.includes(s)) {
          hidden = true
        }
      }
    }
 
    if (disabled && btn.control === 'hidden') {
      hidden = true
    }
 
    if (disprop) {
      disabled = true
    }
 
    this.setState({hidden, disabled})
  }
 
  /**
   * @description 触发按钮操作
   */
  actionTrigger = () => {
    const { btn, selectedData } = this.props
 
    if (this.state.disabled) return
 
    let data = selectedData || []
    
    if (btn.funcType === 'addline') {
      MKEmitter.emit('addRecord' + btn.$tableId, '', data[0] ? data[0].$$uuid : null)
    } else {
      MKEmitter.emit('delRecord' + btn.$tableId, data[0] || null)
    }
  }
 
  render() {
    const { btn } = this.props
    const { disabled, hidden } = this.state
 
    if (hidden) return null
 
    let label = ''
 
    if (btn.show === 'link') {
      label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon} /> : ''}</span>
    } else if (btn.show === 'icon') {
      label = <MkIcon type={btn.icon} />
    } else {
      label = <span>{btn.icon ? <MkIcon style={{marginRight: '8px'}} type={btn.icon} /> : ''}{btn.label}</span>
    }
 
    return (
      <Button
        type="link"
        title={disabled ? (btn.reason || '') : (btn.show === 'icon' ? btn.label : '')}
        disabled={disabled}
        style={btn.style}
        className={btn.hover || ''}
        onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
      >{label}</Button>
    )
  }
}
 
export default EditLine