dialog.tsx
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import { Modal, Tag } from 'antd';
import OrgCore from './core';
type OrgSelectorDialogProps = {
title?: string;
visible: boolean;
onCancel: () => void;
data?: [];
max?: number;
multiple?: boolean; //默认不是多选
request: any;
checkStrictly?: boolean;
selectedData?: { id: string; name: string }[]; //已选组织数据
onOk: (selectedKeys: string[], selectedData: any[]) => void;
modalClassName?: string | undefined; // 弹框类名自定义 用于自定义以及覆盖样式
};
const OrgSelectorDialog: React.FC<OrgSelectorDialogProps> = (props) => {
const [selectedData, setSelectedData] = useState<any[]>(props?.selectedData || []);
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
const orgCoreRef = useRef<{
remove: (index: number) => void;
emptySelect: () => void;
setSelected: (selectedKeys: string[], selectedData: any[]) => void;
}>();
useEffect(() => {
if (props.visible) {
orgCoreRef.current?.emptySelect();
const keys = props?.selectedData?.map((data) => data.id) || [];
const data = props?.selectedData || [];
setSelectedKeys(keys);
setSelectedData(data);
orgCoreRef.current?.setSelected(keys, data);
}
}, [props.visible]);
useEffect(() => {
//console.log(selectedData);
}, [selectedData]);
const handleOk = () => {
props.onOk(selectedKeys, selectedData);
};
const handleCancel = () => {
props.onCancel();
};
const handleSelectOrg = (keys: string[], select: any[]) => {
setSelectedKeys(keys);
setSelectedData(select);
};
return (
<Modal
title={props.title || '选择部门'}
width={560}
visible={props.visible}
className={'qx-org-selector__dialog'}
onOk={handleOk}
onCancel={handleCancel}
wrapClassName={props?.modalClassName || ''}
>
<div className={'qx-org-selected__temp'}>
{selectedData &&
selectedData.map(
(item: { key?: string; title?: string; code?: string; name?: string }) => (
<Tag closable color={'blue'} key={item.key || item.code}>
{item.title || item.name}
</Tag>
),
)}
</div>
<div className={'qx-org-selector__content'}>
{props.visible ? (
<OrgCore
request={props.request}
cRef={orgCoreRef}
multiple
max={props.max}
checkStrictly={props.checkStrictly}
params={props?.data || []}
onSelect={handleSelectOrg}
/>
) : null}
</div>
</Modal>
);
};
export default OrgSelectorDialog;