forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColumn.tsx
181 lines (170 loc) · 5.35 KB
/
Column.tsx
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
import { isLeaf, toPathKey } from '../utils/commonUtil';
import Checkbox from './Checkbox';
import type { DefaultOptionType, SingleValueType } from '../Cascader';
import { SEARCH_MARK } from '../hooks/useSearchOptions';
import type { Key } from '../../_util/type';
import { useInjectCascader } from '../context';
import { cloneVNode } from 'vue';
export const FIX_LABEL = '__cascader_fix_label__';
export interface ColumnProps {
prefixCls: string;
multiple?: boolean;
options: DefaultOptionType[];
/** Current Column opened item key */
activeValue?: Key;
/** The value path before current column */
prevValuePath: Key[];
onToggleOpen: (open: boolean) => void;
onSelect: (valuePath: SingleValueType, leaf: boolean) => void;
onActive: (valuePath: SingleValueType) => void;
checkedSet: Set<Key>;
halfCheckedSet: Set<Key>;
loadingKeys: Key[];
isSelectable: (option: DefaultOptionType) => boolean;
}
export default function Column({
prefixCls,
multiple,
options,
activeValue,
prevValuePath,
onToggleOpen,
onSelect,
onActive,
checkedSet,
halfCheckedSet,
loadingKeys,
isSelectable,
}: ColumnProps) {
const menuPrefixCls = `${prefixCls}-menu`;
const menuItemPrefixCls = `${prefixCls}-menu-item`;
const {
fieldNames,
changeOnSelect,
expandTrigger,
expandIcon: expandIconRef,
loadingIcon: loadingIconRef,
dropdownMenuColumnStyle,
customSlots,
} = useInjectCascader();
const expandIcon = expandIconRef.value ?? customSlots.value.expandIcon?.();
const loadingIcon = loadingIconRef.value ?? customSlots.value.loadingIcon?.();
const hoverOpen = expandTrigger.value === 'hover';
// ============================ Render ============================
return (
<ul class={menuPrefixCls} role="menu">
{options.map(option => {
const { disabled } = option;
const searchOptions = option[SEARCH_MARK];
const label = option[FIX_LABEL] ?? option[fieldNames.value.label];
const value = option[fieldNames.value.value];
const isMergedLeaf = isLeaf(option, fieldNames.value);
// Get real value of option. Search option is different way.
const fullPath = searchOptions
? searchOptions.map(opt => opt[fieldNames.value.value])
: [...prevValuePath, value];
const fullPathKey = toPathKey(fullPath);
const isLoading = loadingKeys.includes(fullPathKey);
// >>>>> checked
const checked = checkedSet.has(fullPathKey);
// >>>>> halfChecked
const halfChecked = halfCheckedSet.has(fullPathKey);
// >>>>> Open
const triggerOpenPath = () => {
if (!disabled && (!hoverOpen || !isMergedLeaf)) {
onActive(fullPath);
}
};
// >>>>> Selection
const triggerSelect = () => {
if (isSelectable(option)) {
onSelect(fullPath, isMergedLeaf);
}
};
// >>>>> Title
let title: string;
if (typeof option.title === 'string') {
title = option.title;
} else if (typeof label === 'string') {
title = label;
}
// >>>>> Render
return (
<li
key={fullPathKey}
class={[
menuItemPrefixCls,
{
[`${menuItemPrefixCls}-expand`]: !isMergedLeaf,
[`${menuItemPrefixCls}-active`]: activeValue === value,
[`${menuItemPrefixCls}-disabled`]: disabled,
[`${menuItemPrefixCls}-loading`]: isLoading,
},
]}
style={dropdownMenuColumnStyle.value}
role="menuitemcheckbox"
title={title}
aria-checked={checked}
data-path-key={fullPathKey}
onClick={() => {
triggerOpenPath();
if (!multiple || isMergedLeaf) {
triggerSelect();
}
}}
onDblclick={() => {
if (changeOnSelect.value) {
onToggleOpen(false);
}
}}
onMouseenter={() => {
if (hoverOpen) {
triggerOpenPath();
}
}}
onMousedown={e => {
// Prevent selector from blurring
e.preventDefault();
}}
>
{multiple && (
<Checkbox
prefixCls={`${prefixCls}-checkbox`}
checked={checked}
halfChecked={halfChecked}
disabled={disabled}
onClick={(e: MouseEvent) => {
e.stopPropagation();
triggerSelect();
}}
/>
)}
<div class={`${menuItemPrefixCls}-content`}>{label}</div>
{!isLoading && expandIcon && !isMergedLeaf && (
<div class={`${menuItemPrefixCls}-expand-icon`}>{cloneVNode(expandIcon)}</div>
)}
{isLoading && loadingIcon && (
<div class={`${menuItemPrefixCls}-loading-icon`}>{cloneVNode(loadingIcon)}</div>
)}
</li>
);
})}
</ul>
);
}
Column.props = [
'prefixCls',
'multiple',
'options',
'activeValue',
'prevValuePath',
'onToggleOpen',
'onSelect',
'onActive',
'checkedSet',
'halfCheckedSet',
'loadingKeys',
'isSelectable',
];
Column.displayName = 'Column';
Column.inheritAttrs = false;