import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import {connect} from 'react-redux'
|
import { is, fromJS } from 'immutable'
|
import { Button, notification } from 'antd'
|
|
import zhCN from '@/locales/zh-CN/main.js'
|
import enUS from '@/locales/en-US/main.js'
|
import MKEmitter from '@/utils/events.js'
|
import MkIcon from '@/components/mk-icon'
|
// import './index.scss'
|
|
class TabButton extends Component {
|
static propTpyes = {
|
show: PropTypes.any, // 按钮显示样式控制
|
BData: PropTypes.any, // 主表数据
|
MenuID: PropTypes.string, // 菜单ID
|
btn: PropTypes.object, // 按钮
|
selectedData: PropTypes.any, // 子表中选择数据
|
setting: PropTypes.any, // 页面通用设置
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
disabled: false,
|
hidden: false,
|
primaryId: ''
|
}
|
|
UNSAFE_componentWillMount () {
|
const { btn, selectedData, BData } = this.props
|
let disabled = false
|
|
if (btn.controlField && selectedData && selectedData.length > 0) { // 表格中按钮隐藏控制
|
selectedData.forEach(item => {
|
let s = item[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
disabled = true
|
}
|
})
|
this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
|
} else if (btn.control === 'parent') {
|
if (!BData || !BData.hasOwnProperty(btn.controlField)) {
|
this.setState({hidden: true})
|
} else {
|
let s = BData[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
this.setState({hidden: true})
|
} else {
|
this.setState({hidden: false})
|
}
|
}
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('triggerBtnId', this.actionTrigger)
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { btn, selectedData } = this.props
|
|
if (btn.controlField && !is(fromJS(nextProps.selectedData || []), fromJS(selectedData || []))) {
|
let disabled = false
|
|
if (nextProps.selectedData && nextProps.selectedData.length > 0) { // 表格中按钮隐藏控制
|
nextProps.selectedData.forEach(item => {
|
let s = item[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
disabled = true
|
}
|
})
|
}
|
this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
|
} else if (btn.control === 'parent') {
|
if (!nextProps.BData || !nextProps.BData.hasOwnProperty(btn.controlField)) {
|
this.setState({hidden: true})
|
} else {
|
let s = nextProps.BData[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
this.setState({hidden: true})
|
} else {
|
this.setState({hidden: false})
|
}
|
}
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (triggerId, record, type) => {
|
const { setting, btn, selectedData } = this.props
|
const { disabled } = this.state
|
|
if ((triggerId && btn.uuid !== triggerId) || disabled) return
|
|
if (type === 'linkbtn' && selectedData && selectedData.length === 1) {
|
if (record[0].$Index !== selectedData[0].$Index) {
|
return
|
}
|
}
|
|
let data = record || selectedData || []
|
|
if (btn.Ot === 'requiredSgl' && data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.state.dict['main.action.confirm.selectSingleLine'],
|
duration: 5
|
})
|
return
|
}
|
|
let primaryId = ''
|
|
if (btn.Ot === 'requiredSgl' && setting.primaryKey) {
|
primaryId = data[0][setting.primaryKey] || ''
|
}
|
|
let newtab = {}
|
|
if (btn.tabTemplate === 'FormTab') {
|
newtab = {
|
MenuNo: '',
|
MenuID: btn.uuid,
|
MenuName: btn.label,
|
type: btn.tabTemplate,
|
param: {
|
parentId: this.props.MenuID,
|
btn: btn,
|
data: data[0] || null,
|
primaryId: primaryId
|
}
|
}
|
} else {
|
let menu = null
|
|
if (btn.MenuID) {
|
menu = {
|
MenuID: btn.MenuID,
|
MenuName: btn.MenuName,
|
MenuNo: btn.MenuNo,
|
type: btn.tabType
|
}
|
} else if (btn.linkmenu && btn.linkmenu.length > 0) {
|
let menu_id = btn.linkmenu.slice(-1)[0]
|
menu = this.props.permMenus.filter(m => m.MenuID === menu_id)[0] || ''
|
}
|
|
if (!menu) {
|
notification.warning({
|
top: 92,
|
message: '菜单已删除或没有访问权限!',
|
duration: 5
|
})
|
return
|
}
|
|
newtab = {
|
...menu,
|
param: {
|
$BID: primaryId
|
}
|
}
|
}
|
|
if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) {
|
MKEmitter.emit('modifyTabs', newtab, 'replace')
|
} else {
|
MKEmitter.emit('modifyTabs', newtab, 'plus', true)
|
}
|
|
MKEmitter.emit('openNewTab')
|
if (window.GLOB.systemType === 'production') {
|
MKEmitter.emit('queryTrigger', {menuId: btn.uuid, name: '标签页'})
|
}
|
}
|
|
render() {
|
const { btn, show } = this.props
|
const { disabled, hidden } = this.state
|
|
if (hidden) return null
|
|
if (show === 'actionList') {
|
return (
|
<Button
|
className={'mk-btn mk-' + btn.class}
|
icon={btn.icon}
|
disabled={disabled}
|
onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
|
>{btn.label}</Button>
|
)
|
} else { // icon、text、 all 卡片
|
let label = ''
|
let icon = ''
|
|
if (show === 'button') {
|
label = btn.label
|
icon = btn.icon || ''
|
} else if (show === 'link') {
|
label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
|
icon = ''
|
} else if (show === 'icon') {
|
icon = btn.icon || ''
|
// } else if (show === 'text') {
|
} else {
|
label = btn.label
|
}
|
|
return (
|
<Button
|
type="link"
|
title={show === 'icon' ? btn.label : ''}
|
style={btn.style}
|
disabled={disabled}
|
icon={icon}
|
onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
|
>{label}</Button>
|
)
|
}
|
}
|
}
|
|
const mapStateToProps = (state) => {
|
return {
|
permMenus: state.permMenus,
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(TabButton)
|