From a9ac16fecc0cf9bc66dfaefe4e9b35fa3c722812 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期二, 10 十一月 2020 16:58:43 +0800 Subject: [PATCH] 2020-11-10 --- src/tabviews/custom/components/card/prop-card/index.jsx | 158 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 133 insertions(+), 25 deletions(-) diff --git a/src/tabviews/custom/components/card/prop-card/index.jsx b/src/tabviews/custom/components/card/prop-card/index.jsx index ae6f094..7073848 100644 --- a/src/tabviews/custom/components/card/prop-card/index.jsx +++ b/src/tabviews/custom/components/card/prop-card/index.jsx @@ -1,13 +1,12 @@ import React, {Component} from 'react' import PropTypes from 'prop-types' import { is, fromJS } from 'immutable' -import { Spin, notification } from 'antd' +import { Spin, notification, Col } from 'antd' import asyncComponent from '@/utils/asyncComponent' import Api from '@/api' import UtilsDM from '@/utils/utils-datamanage.js' -import zhCN from '@/locales/zh-CN/main.js' -import enUS from '@/locales/en-US/main.js' +import MKEmitter from '@/utils/events.js' import './index.scss' const CardItem = asyncComponent(() => import('../cardItem')) @@ -17,60 +16,141 @@ BID: PropTypes.any, // 鐖剁骇Id data: PropTypes.array, // 缁熶竴鏌ヨ鏁版嵁 config: PropTypes.object, // 缁勪欢閰嶇疆淇℃伅 - mainSearch: PropTypes.any, // 鍏ㄥ眬鎼滅储鏉′欢 + mainSearch: PropTypes.any, // 澶栧眰鎼滅储鏉′欢 menuType: PropTypes.any, // 鑿滃崟绫诲瀷 dataManager: PropTypes.any, // 鏁版嵁鏉冮檺 } state = { - dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS, // 瀛楀吀 config: null, // 鍥捐〃閰嶇疆淇℃伅 - loading: true, // 鏁版嵁鍔犺浇鐘舵�� + loading: false, // 鏁版嵁鍔犺浇鐘舵�� + activeKey: '', // 閫変腑鏁版嵁 + sync: false, // 鏄惁缁熶竴璇锋眰鏁版嵁 data: null, // 鏁版嵁 } UNSAFE_componentWillMount () { + const { data } = this.props let _config = fromJS(this.props.config).toJS() + let _cols = new Map() + + let _data = null + let _sync = false + if (_config.setting && _config.wrap.datatype !== 'static') { + _sync = _config.setting.sync === 'true' + + if (_config.setting.sync === 'true' && data) { + _data = data[_config.dataName] + if (_data && Array.isArray(_data)) { + _data = _data[0] + } + _sync = false + } + } else { + _data = [] + } + + _config.columns.forEach(item => { + _cols.set(item.field, item) + }) + + _config.subcards.forEach(card => { + card.elements = card.elements.map(item => { + if (item.field && _cols.has(item.field)) { + item.col = _cols.get(item.field) + } + return item + }) + card.backElements = card.backElements.map(item => { + if (item.field && _cols.has(item.field)) { + item.col = _cols.get(item.field) + } + return item + }) + }) this.setState({ + sync: _sync, + data: _data, config: _config, arr_field: _config.columns.map(col => col.field).join(','), }, () => { - this.loadData() + if (_config.wrap.datatype !== 'static' && _config.setting && _config.setting.sync !== 'true' && _config.setting.onload === 'true') { + this.loadData() + } else if (_sync && !_data) { + this.setState({ + loading: true + }) + } }) } - /** - * @description 鏍¢獙鍥捐〃鐨勬寜閽粍锛屽鏋滀负缁熻鍥捐〃锛岃绠楀浘琛ㄥ瓧娈� - */ componentDidMount () { - - } - - /** - * @description 鍥捐〃鏁版嵁鏇存柊锛屽埛鏂板唴瀹� - */ - UNSAFE_componentWillReceiveProps (nextProps) { - + MKEmitter.addListener('syncRefreshComponentId', this.reload) } shouldComponentUpdate (nextProps, nextState) { return !is(fromJS(this.state), fromJS(nextState)) } + componentWillUnmount () { + this.setState = () => { + return + } + MKEmitter.removeListener('syncRefreshComponentId', this.reload) + } + + /** + * @description 鍥捐〃鏁版嵁鏇存柊锛屽埛鏂板唴瀹� + */ + UNSAFE_componentWillReceiveProps (nextProps) { + const { sync, config } = this.state + + if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) { + let _data = {} + if (nextProps.data && nextProps.data[config.dataName]) { + _data = nextProps.data[config.dataName] + if (_data && Array.isArray(_data)) { + _data = _data[0] + } + } + + this.setState({sync: false, loading: false, data: _data}) + } else if (!is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { + if (config.wrap.datatype !== 'static' && config.setting.syncRefresh === 'true') { + this.setState({}, () => { + this.loadData() + }) + } + } + } + + reload = (syncId) => { + const { config } = this.state + + if (syncId && syncId !== config.uuid) return + + this.loadData() + } + async loadData () { const { mainSearch, BID, menuType, dataManager } = this.props const { config, arr_field } = this.state - + let searches = [] if (mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢 - searches = [...mainSearch, ...searches] + let keys = searches.map(item => item.key) + mainSearch.forEach(item => { + if (!keys.includes(item.key)) { + searches.push(item) + } + }) } if (config.wrap.datatype === 'static') { this.setState({ loading: false, - data: [] + data: {} }) return } else { @@ -85,7 +165,8 @@ let result = await Api.genericInterface(param) if (result.status) { this.setState({ - data: result.data, + activeKey: '', + data: result.data && result.data[0] ? result.data[0] : {}, loading: false }) } else { @@ -100,11 +181,36 @@ } } + updateStatus = (type, position, btn) => { + const { config } = this.state + + if (type === 'refresh' && position === 'grid') { + this.loadData() + if (btn && btn.syncComponent && btn.syncComponent[0]) { + let syncId = btn.syncComponent[btn.syncComponent.length - 1] + if (config.uuid !== syncId) { + MKEmitter.emit('syncRefreshComponentId', syncId) + } + } + } + } + + changeCard = (index, item) => { + const { config } = this.state + + if (config.wrap.switch !== 'true') return + + this.setState({ + activeKey: index + }) + } + render() { - const { config, loading, data } = this.state + const { BID } = this.props + const { config, loading, data, activeKey } = this.state return ( - <div className="custom-card-box" style={config.style}> + <div className="custom-prop-card-box" style={{...config.style, minHeight: config.wrap.minHeight}}> {loading ? <div className="loading-mask"> <div className="ant-spin-blur"></div> @@ -113,7 +219,9 @@ } {data ? <div className="card-row-list"> {config.subcards.map((item, index) => ( - <CardItem key={index} card={item} cards={config} data={data[0] || {}} /> + <Col className={activeKey === index ? 'active' : ''} key={index} span={item.setting.width || 6} onClick={() => {this.changeCard(index, item)}}> + <CardItem BID={BID} card={item} cards={config} data={data} updateStatus={this.updateStatus}/> + </Col> ))} </div> : null} </div> -- Gitblit v1.8.0