/* * @Author: wangqiang@feewee.cn * @Date: 2022-05-25 10:03:00 * @LastEditors: wangqiang@feewee.cn * @LastEditTime: 2022-10-08 16:33:31 */ import { InitialReturn } from "@/hooks/useInitail"; import { Button, Checkbox, Divider, Input, Modal, Popconfirm, Popover, Radio, Row, Select, Spin, Table, } from "antd"; import { CheckboxChangeEvent } from "antd/lib/checkbox"; import { debounce } from "lodash"; import React from "react"; import { ShopItem, BizType, DealerOrBrand, Value, Option, QueryParams, ShopSelectNewOptions, } from "../api"; interface Props { visible: boolean; setVisible: React.Dispatch>; selected: Value[]; setSelected: React.Dispatch>; tableType: "列表" | "选择"; setTableType: React.Dispatch>; checkInfo: { indeterminate: boolean; checkAll: boolean; }; onChange?: Function; type: 1 | 2; // 1 集团维度 2 自定义门店维度 multiple?: boolean; optionInitial: InitialReturn; listInitial: InitialReturn; defaultOptions?: ShopSelectNewOptions; } export default function ShopModal({ visible, setVisible, selected, setSelected, tableType, setTableType, checkInfo, onChange, type, multiple, optionInitial, listInitial, defaultOptions = {}, }: Props) { const fetchListByName = debounce( (keywords) => listInitial.setParams({ type, keywords }, true), 500 ); /** * @description: 当一个门店被点击时,触发该回调函数。 * @param {ShopItem} record 当前门店数据 * @param {boolean} _selected 是否选中 * @param {Object[]} selectedRows 已被选择的数据 * @param {Event} nativeEvent 点击事件 */ const onSelect = ( record: ShopItem, _selected: boolean, selectedRows: Object[], nativeEvent: Event ) => { if (multiple) { if (_selected) { // 若被选择,则将该数据处理后,插入已选择列表中 setSelected([ ...selected, { ...record, value: record.shopId, label: record.shopShortName }, ]); } else { // 若被取消选择,则将该数据,从已选择列表中删除 setSelected(selected.filter((item) => item.shopId !== record.shopId)); } } else { true; if (_selected) { setSelected([ { ...record, value: record.shopId, label: record.shopShortName }, ]); } else { setSelected([]); } } }; /** * @description: 当全选CheckBox被点击时,调用该回调函数 * @description: 只有 多选才会调用该全选函数,故起函数内部不需要判断是否多选情况 * @param {boolean} _selected 是否选中 * @param {ShopItem[]} selectedRows 已被选择的数据列表 * @param {ShopItem[]} changeRows 点击全选CheckBox后,受影响的数据列表。该数据与_selected参数共同使用,可以得知是应该将changeRows数据加入已选中数据还是删除 */ const onSelectAll = ( _selected: boolean, selectedRows: ShopItem[], changeRows: ShopItem[] ) => { if (_selected) { // 若选中全选CheckBox,则将changeRows中的还未被选入selected中的数据插入 setSelected( selected.concat( changeRows.map((row) => ({ ...row, value: row.shopId, label: row.shopShortName, })) ) ); } else { // 若取消选择全选CheckBox,则将changeRows中的数据全部从selected中删除 const cancelIds = changeRows.map((row) => row.shopId); // @ts-ignore setSelected(selected.filter((item) => !cancelIds.includes(item.value))); } }; const rowClick = (record: Value) => { if (multiple) { if (selected.some((item) => item.value === record.shopId)) { setSelected(selected.filter((item) => item.value !== record.shopId)); } else { setSelected( selected.concat({ ...record, value: record.shopId, label: record.shopShortName, }) ); } } else { setSelected([ { ...record, value: record.shopId, label: record.shopShortName }, ]); } }; const onOk = () => { onChange && onChange(selected); setVisible(false); }; const checkAll = (e: CheckboxChangeEvent) => { const checked = e.target.checked; setSelected( checked ? listInitial.data.map((item) => ({ ...item, value: item.shopId, label: item.shopShortName, })) : [] ); }; return ( setVisible(false)} onOk={() => onOk()} > {multiple ? ( <> setTableType(e.target.value)} > 列表 当前选择({selected.length} 个) {multiple ? ( 全选 ) : null} { setSelected([]); setTableType("列表"); }} > ) : null} fetchListByName(e.target.value)} style={{ maxWidth: 260, marginRight: 10, marginBottom: 10 }} /> ({ onClick: () => rowClick(record), })} rowSelection={{ type: multiple ? "checkbox" : "radio", selectedRowKeys: selected.map( (item) => item.value || item.shopId || -1 ), onSelect, onSelectAll, }} > shopFullName || "-"} /> shopFullName || "-"} /> (bizType ? BizType[bizType] : "-")} /> dealer?.name || "-"} /> region?.fullName || "-"} /> (brandList?.length ? ( {brandList?.map((brand) => (
{brand.name}
))} } > {brandList?.map((brand) => brand.name).join(";")}
) : ( "-" ))} /> casShopName || "-"} />
); }