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
|