forked from DTStack/dt-react-component
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.tsx
More file actions
199 lines (178 loc) · 6.79 KB
/
select.tsx
File metadata and controls
199 lines (178 loc) · 6.79 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
197
198
199
import React, { ReactNode, useEffect, useMemo, useState } from 'react';
import { Button, Checkbox, Col, Dropdown, type DropDownProps, Row, Space } from 'antd';
import type { CheckboxChangeEvent } from 'antd/lib/checkbox';
import type {
CheckboxGroupProps,
CheckboxOptionType,
CheckboxValueType,
} from 'antd/lib/checkbox/Group';
import classNames from 'classnames';
import { isEqual } from 'lodash-es';
import List from 'rc-virtual-list';
import './style.scss';
interface IDropdownSelectProps
extends Pick<DropDownProps, 'getPopupContainer'>,
Pick<CheckboxGroupProps, 'value' | 'defaultValue' | 'options' | 'onChange'> {
children: ReactNode;
className?: string;
}
const prefix = 'dtc-dropdown-select';
const ITEM_HEIGHT = 32;
const MAX_HEIGHT = 264;
export default function Select({
className,
value,
defaultValue,
options: rawOptions,
children,
getPopupContainer,
onChange,
}: IDropdownSelectProps) {
const [visible, setVisible] = useState(false);
const [selected, setSelected] = useState<CheckboxValueType[]>(value || defaultValue || []);
const handleCheckedAll = (e: CheckboxChangeEvent) => {
if (e.target.checked) {
setSelected(options?.map((i) => i.value) || []);
} else {
handleReset();
}
};
const handleSubmit = () => {
onChange?.(selected);
setVisible(false);
};
const handleReset = () => {
// Clear checked but disabled item
setSelected(disabledValue);
};
const handleChange = (e: CheckboxChangeEvent) => {
const { checked, value } = e.target;
const next = checked ? [...selected, value] : selected?.filter((i) => i !== value);
setSelected(next);
};
const handleShadow = (target: HTMLDivElement) => {
if (target) {
if (!target.querySelector(`.${prefix}__shadow`)) {
const shadow = document.createElement('div');
shadow.classList.add(`${prefix}__shadow`);
target.insertBefore(shadow, target.firstChild);
}
const scrollbar_thumb = target.querySelector<HTMLDivElement>(
'.rc-virtual-list-scrollbar-thumb'
);
const shadow = target.querySelector<HTMLDivElement>(`.${prefix}__shadow`);
if (parseFloat(scrollbar_thumb?.style.top as string) > 0) {
shadow?.classList.add('active');
} else {
shadow?.classList.remove('active');
}
}
};
useEffect(() => {
if (value !== undefined && value !== selected) {
setSelected(value || []);
}
}, [value]);
// Always turn string and number options into complex options
const options = useMemo<CheckboxOptionType[]>(() => {
return (
rawOptions?.map((i) => {
if (typeof i === 'string' || typeof i === 'number') {
return {
label: i,
value: i,
};
}
return i;
}) || []
);
}, [rawOptions]);
const disabledValue = useMemo<CheckboxValueType[]>(() => {
return options?.filter((i) => i.disabled).map((i) => i.value) || [];
}, [options]);
const resetDisabled = selected.every((i) => disabledValue?.includes(i));
// If options' number is larger then the maxHeight, then enable virtual list
const virtual = options.length > Math.floor(MAX_HEIGHT / ITEM_HEIGHT);
// ONLY the options are all be pushed into value array means select all
const checkAll =
!!selected?.length && isEqual(options.map((i) => i.value).sort(), [...selected].sort());
// At least one option's value is included in value array but not all options means indeterminate select
const indeterminate =
!!selected?.length &&
!isEqual(options.map((i) => i.value).sort(), [...selected].sort()) &&
options.some((o) => selected.includes(o.value));
const overlay = (
<>
<Row>
<Col span={24} className={`${prefix}__col`}>
<Checkbox
onChange={handleCheckedAll}
checked={checkAll}
indeterminate={indeterminate}
>
全选
</Checkbox>
</Col>
<Col span={24} className={`${prefix}__menu`}>
<Checkbox.Group value={selected}>
<List
data={options}
itemKey="value"
itemHeight={ITEM_HEIGHT}
// ONLY access height when enable virtual
height={virtual ? MAX_HEIGHT : undefined}
virtual={virtual}
onScroll={(e) =>
handleShadow(
(e.target as HTMLDivElement).parentElement as HTMLDivElement
)
}
>
{(option) => (
<Col
span={24}
key={option.value.toString()}
className={`${prefix}__col`}
>
<Checkbox
disabled={option.disabled}
value={option.value}
onChange={handleChange}
>
{option.label}
</Checkbox>
</Col>
)}
</List>
</Checkbox.Group>
</Col>
</Row>
<Space size={8} className={`${prefix}__btns`}>
<Button size="small" disabled={resetDisabled} onClick={handleReset}>
重置
</Button>
<Button size="small" type="primary" onClick={handleSubmit}>
确定
</Button>
</Space>
</>
);
return (
<Dropdown
visible={visible}
overlayClassName={classNames(`${prefix}__container`, className)}
trigger={['click']}
overlay={overlay}
getPopupContainer={getPopupContainer}
onVisibleChange={(visible) => {
if (visible) {
setVisible(true);
} else {
handleSubmit();
}
}}
>
{children}
</Dropdown>
);
}