index.tsx
4.91 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* @Author: wangqiang@feewee.cn
* @Date: 2022-05-25 09:14:31
* @LastEditors: wangqiang@feewee.cn
* @LastEditTime: 2022-10-08 16:36:39
*/
import useInitial from "@/hooks/useInitail";
import { Tag } from "antd";
import React, {
forwardRef,
Ref,
useCallback,
useEffect,
useImperativeHandle,
useState,
} from "react";
import {
Value,
getShopListChooseOptionsApi,
getShopListApi,
ShopSelectNewOptions,
} from "./api";
import Close from "./components/Close";
import ShopModal from "./components/ShopModal";
import "./style.less";
interface Props {
style?: React.CSSProperties;
value?: Value[];
onChange?: Function;
multiple?: boolean;
disabled?: boolean;
placeholder?: string;
type?: 1 | 2; // 1 集团维度 2 自定义门店维度
shopIds?: number[]; // 仅查询门店ID列表,与 type = 2 时搭配使用
/**
* 默认筛选参数,下拉框中不可删除;
* 用于特殊业务,例如 售前CRM-零售任务 选择门店时,需要默认查询 新车销售 的门店信息,则传入
defaultOptions={{
bizTypes: "1",
}}
*/
defaultOptions?: ShopSelectNewOptions;
}
export interface ShopSelectNewRef {
reset: () => void;
}
export default forwardRef(ShopSelectNew);
function ShopSelectNew(
{
style,
value = [],
onChange,
multiple = false,
placeholder = "请选择门店",
type = 1,
disabled = false,
shopIds,
defaultOptions = {},
}: Props,
ref: Ref<ShopSelectNewRef>
) {
const [visible, setVisible] = useState(false);
const [delay, setDelay] = useState(type === 2);
const optionInitial = useInitial(
getShopListChooseOptionsApi,
{},
{ type },
delay
);
const listInitial = useInitial(
getShopListApi,
[],
{
type,
...defaultOptions,
},
delay
);
const [selected, setSelected] = useState<Value[]>([]);
const [tableType, setTableType] = useState<"列表" | "选择">("列表");
const [checkInfo, setCheckInfo] = useState({
indeterminate: false,
checkAll: false,
}); // 全选按钮的信息,indeterminate 半选中状态,checkAll 全选状态
useEffect(() => {
if (visible) {
setTableType("列表");
delay && setDelay(false);
const params: any = { type };
if (type === 2 && shopIds?.length) {
params.shops = shopIds.join(",");
}
optionInitial.setParams(params, true);
listInitial.setParams(params, true);
}
}, [visible, delay, shopIds]);
useEffect(() => {
setCheckInfo({
indeterminate:
selected.length > 0 && listInitial.data.length !== selected.length,
checkAll:
selected.length > 0 && listInitial.data.length === selected.length,
});
}, [selected, listInitial.data]);
useImperativeHandle(ref, () => ({
reset: () => {
setSelected([]);
setCheckInfo({
indeterminate: false,
checkAll: false,
});
},
}));
const del = (item: Value) => {
onChange && onChange(value.filter((i) => i.value !== item.value));
};
const deleteAll = () => {
setSelected([]);
onChange && onChange([]);
};
const openModal = useCallback(() => {
if (value.length > 0) {
// 为了使查看当前项展示完整表格数据,需要将selected处理
const ids = value.map((v) => v.value);
setSelected(
listInitial.data
.filter((item) => ids.includes(item.shopId))
.map((item) => ({
...item,
value: item.shopId,
label: item.shopShortName,
}))
);
}
setVisible(true);
}, [value, listInitial.data]);
return (
<div style={style}>
<span
className="ant-input-affix-wrapper"
onClick={disabled ? undefined : openModal}
>
<div className="ShopSelectNew_Container">
{value?.length ? (
multiple ? (
value.map((item) => (
<Tag
className="tag"
closable={!disabled}
key={item.value}
onClose={() => del(item)}
>
{item.label}
</Tag>
))
) : (
// @ts-ignore
<span>{value[0].label || "-"}</span>
)
) : (
<span className="placeholder">{placeholder}</span>
)}
</div>
{!disabled && value?.length ? <Close deleteAll={deleteAll} /> : null}
</span>
{disabled ? null : (
<ShopModal
onChange={onChange}
type={type}
multiple={multiple}
tableType={tableType}
setTableType={setTableType}
selected={selected}
setSelected={setSelected}
checkInfo={checkInfo}
visible={visible}
setVisible={setVisible}
optionInitial={optionInitial}
listInitial={listInitial}
defaultOptions={defaultOptions}
/>
)}
</div>
);
}