/* * 自定义浏览框组件 * 弹框选择 * @Author: 黎永顺 * @Date: 2024/8/30 * @Wechat: * @Email: 971387674@qq.com * @description: */ import React, { Component } from "react"; import { WeaDialog, WeaInputSearch, WeaLocaleProvider, WeaNewScroll, WeaTable, WeaTransfer } from "ecCom"; import { Button, Col, Row, Spin } from "antd"; import CustomBrowserMutiLeft from "./customBrowserMutiLeft"; import CustomBrowserMutiRight from "./customBrowserMutiRight"; import CustomBrowserOperation from "./customBrowserOperation"; import { postFetch } from "../../../util/request"; const WeaTransferList = WeaTransfer.list; const getLabel = WeaLocaleProvider.getLabel; class CustomBrowserDialog extends Component { constructor(props) { super(props); this.state = { loading: false, listDatas: [], pageInfo: { current: 1, pageSize: 10, total: 0 }, selectedRowKeys: [], query: { [props.searchParamsKey]: "" }, singleFilterVal: "", leftListSelectedKeys: [], // 左侧table选择的keys leftListSelectedData: [], // 左侧table选择的数据 rightCheckedKeys: [], //右侧选择的keys rightDatas: [] // 右侧展示的数据 }; this.selectedData = {}; } componentWillReceiveProps(nextProps, nextContext) { if (nextProps.visible !== this.props.visible && nextProps.visible) { this.getData(); this.setState({ selectedRowKeys: nextProps.selectedValues, leftListSelectedData: _.values(nextProps.datas), rightDatas: _.values(nextProps.datas) }); } else { this.setState({ pageInfo: { current: 1, pageSize: 10, total: 0 }, query: { [this.props.searchParamsKey]: "" }, rightDatas: [], rightCheckedKeys: [], leftListSelectedData: [], leftListSelectedKeys: [] }); this.selectedData = {}; } } getData = () => { const { pageInfo, query } = this.state; const { dialogType, completeURL, convertDatasource, dataParams = {} } = this.props; let payload = { ...dataParams, ...query }; dialogType === "table" && (payload = { ...pageInfo, ...payload, ...query }); this.setState({ loading: true }); postFetch(completeURL, payload).then(({ status, data }) => { this.setState({ loading: false }); if (status && data.list) { const { pageNum: current, pageSize, total } = data; this.setState({ listDatas: convertDatasource ? convertDatasource(data.list) : data.list, pageInfo: { ...pageInfo, current, pageSize, total } }); } else { this.setState({ listDatas: _.map(data, o => ({ ...o, id: String(o.id) })) }); } }); }; handleRowClick = record => { if (!this.props.isSingle) return; const values = [record.id]; const selectedData = { [record["id"]]: record }; this.props.onCancel(); this.props.onChange && this.props.onChange(values, selectedData); }; handleClear = () => { this.props.onCancel(); this.props.onChange && this.props.onChange([], {}); }; handleOk = () => { const { selectedRowKeys, rightDatas } = this.state, { dialogType } = this.props; const convertSelectedRowKeys = dialogType !== "table" ? rightDatas.map((v) => v.id) : selectedRowKeys; convertSelectedRowKeys.forEach((v) => { let item = this.getItemById(v); if (item) this.selectedData[v] = item; }); this.props.onChange && this.props.onChange(convertSelectedRowKeys, this.selectedData); this.props.onCancel && this.props.onCancel(); }; getItemById = (id) => { const { listDatas } = this.state; if (this.selectedData[id]) return this.selectedData[id]; if (!_.isEmpty(listDatas)) { for (let i = 0; i < listDatas.length; i++) { if (String(id) === String(listDatas[i].id)) return listDatas[i]; } } }; onLeftListCheck = (keys, datas) => { const { leftListSelectedData } = this.state; let targets = leftListSelectedData.concat(datas); targets = _.uniqBy(targets, "id"); targets = targets.filter((t) => keys.indexOf(t["id"]) > -1); this.setState({ leftListSelectedKeys: keys, leftListSelectedData: targets }); }; onleftDoubleClick = (data) => { const { rightDatas } = this.state; this.setState({ rightDatas: rightDatas.concat(data), rightCheckedKeys: [], leftListSelectedData: [], leftListSelectedKeys: [] }); }; onRightDoubleClick = (key) => { const { rightDatas } = this.state; const newRightDatas = rightDatas.filter(item => String(item.id) !== key); this.setState({ rightDatas: newRightDatas, rightCheckedKeys: [] }); }; moveTo = (direction) => { const { rightDatas, rightCheckedKeys, listDatas, leftListSelectedData } = this.state; if (direction === "right") { this.setState({ rightDatas: rightDatas.concat(leftListSelectedData), leftListSelectedData: [], leftListSelectedKeys: [] }); } else if (direction === "left") { this.setState({ rightDatas: rightDatas.filter(item => !rightCheckedKeys.some(checkedKey => String(item.id) === checkedKey)), rightCheckedKeys: [] }); } else if (direction === "allToLeft") { this.setState({ rightDatas: [], rightCheckedKeys: [] }); } else if (direction === "allToRight") { if (this.leftListAllActive()) { this.setState({ rightDatas: rightDatas.concat(listDatas), rightCheckedKeys: [], leftListSelectedData: [], leftListSelectedKeys: [] }); } } }; leftListAllActive = () => { const { rightDatas, listDatas } = this.state; let bool = true; if (_.isEmpty(listDatas)) bool = false; if (!_.isEmpty(listDatas) && !_.isEmpty(rightDatas)) { bool = listDatas.filter((l) => !rightDatas.some(r => l.id === r.id)).length !== 0; } return bool; }; renderTitle = () => { const { dialogType, searchParamsKey, isSingle } = this.props, { query, pageInfo, selectedRowKeys, singleFilterVal } = this.state; return (