-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathDropdownMenu.jsx
216 lines (209 loc) · 6.61 KB
/
DropdownMenu.jsx
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import raf from 'raf';
import PropTypes from '../_util/vue-types';
import Menu from '../vc-menu';
import scrollIntoView from 'dom-scroll-into-view';
import { getSelectKeys, preventDefaultEvent } from './util';
import { cloneElement } from '../_util/vnode';
import BaseMixin from '../_util/BaseMixin';
import { getSlotOptions, getComponentFromProp, getListeners } from '../_util/props-util';
export default {
name: 'DropdownMenu',
mixins: [BaseMixin],
props: {
ariaId: PropTypes.string,
defaultActiveFirstOption: PropTypes.bool,
value: PropTypes.any,
dropdownMenuStyle: PropTypes.object,
multiple: PropTypes.bool,
// onPopupFocus: PropTypes.func,
// onPopupScroll: PropTypes.func,
// onMenuDeSelect: PropTypes.func,
// onMenuSelect: PropTypes.func,
prefixCls: PropTypes.string,
menuItems: PropTypes.any,
inputValue: PropTypes.string,
visible: PropTypes.bool,
backfillValue: PropTypes.any,
firstActiveValue: PropTypes.string,
menuItemSelectedIcon: PropTypes.any,
},
watch: {
visible(val) {
if (!val) {
this.lastVisible = val;
} else {
this.$nextTick(() => {
this.scrollActiveItemToView();
});
}
},
},
created() {
this.rafInstance = null;
this.lastInputValue = this.$props.inputValue;
this.lastVisible = false;
},
mounted() {
this.$nextTick(() => {
this.scrollActiveItemToView();
});
this.lastVisible = this.$props.visible;
},
updated() {
const props = this.$props;
// if (!this.prevVisible && props.visible) {
// this.$nextTick(() => {
// this.scrollActiveItemToView();
// });
// }
this.lastVisible = props.visible;
this.lastInputValue = props.inputValue;
this.prevVisible = this.visible;
},
beforeDestroy() {
if (this.rafInstance) {
raf.cancel(this.rafInstance);
}
},
methods: {
scrollActiveItemToView() {
// scroll into view
const itemComponent = this.firstActiveItem && this.firstActiveItem.$el;
const props = this.$props;
const { value, visible, firstActiveValue } = props;
if (!itemComponent || !visible) {
return;
}
const scrollIntoViewOpts = {
onlyScrollIfNeeded: true,
};
if ((!value || value.length === 0) && firstActiveValue) {
scrollIntoViewOpts.alignWithTop = true;
}
// Delay to scroll since current frame item position is not ready when pre view is by filter
// https://github.com/ant-design/ant-design/issues/11268#issuecomment-406634462
this.rafInstance = raf(() => {
scrollIntoView(itemComponent, this.$refs.menuRef.$el, scrollIntoViewOpts);
});
},
renderMenu() {
const props = this.$props;
const {
menuItems,
defaultActiveFirstOption,
value,
prefixCls,
multiple,
inputValue,
firstActiveValue,
dropdownMenuStyle,
backfillValue,
visible,
} = props;
const menuItemSelectedIcon = getComponentFromProp(this, 'menuItemSelectedIcon');
const { menuDeselect, menuSelect, popupScroll } = getListeners(this);
if (menuItems && menuItems.length) {
const selectedKeys = getSelectKeys(menuItems, value);
const menuProps = {
props: {
multiple,
itemIcon: multiple ? menuItemSelectedIcon : null,
selectedKeys,
prefixCls: `${prefixCls}-menu`,
},
on: {},
style: dropdownMenuStyle,
ref: 'menuRef',
attrs: {
role: 'listbox',
},
};
if (popupScroll) {
menuProps.on.scroll = popupScroll;
}
if (multiple) {
menuProps.on.deselect = menuDeselect;
menuProps.on.select = menuSelect;
} else {
menuProps.on.click = menuSelect;
}
const activeKeyProps = {};
let defaultActiveFirst = defaultActiveFirstOption;
let clonedMenuItems = menuItems;
if (selectedKeys.length || firstActiveValue) {
if (props.visible && !this.lastVisible) {
activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue;
} else if (!visible) {
// Do not trigger auto active since we already have selectedKeys
if (selectedKeys[0]) {
defaultActiveFirst = false;
}
activeKeyProps.activeKey = undefined;
}
let foundFirst = false;
// set firstActiveItem via cloning menus
// for scroll into view
const clone = item => {
if (
(!foundFirst && selectedKeys.indexOf(item.key) !== -1) ||
(!foundFirst && !selectedKeys.length && firstActiveValue.indexOf(item.key) !== -1)
) {
foundFirst = true;
return cloneElement(item, {
directives: [
{
name: 'ant-ref',
value: ref => {
this.firstActiveItem = ref;
},
},
],
});
}
return item;
};
clonedMenuItems = menuItems.map(item => {
if (getSlotOptions(item).isMenuItemGroup) {
const children = item.componentOptions.children.map(clone);
return cloneElement(item, { children });
}
return clone(item);
});
} else {
// Clear firstActiveItem when dropdown menu items was empty
// Avoid `Unable to find node on an unmounted component`
// https://github.com/ant-design/ant-design/issues/10774
this.firstActiveItem = null;
}
// clear activeKey when inputValue change
const lastValue = value && value[value.length - 1];
if (inputValue !== this.lastInputValue && (!lastValue || lastValue !== backfillValue)) {
activeKeyProps.activeKey = '';
}
menuProps.props = { ...activeKeyProps, ...menuProps.props, defaultActiveFirst };
return <Menu {...menuProps}>{clonedMenuItems}</Menu>;
}
return null;
},
},
render() {
const renderMenu = this.renderMenu();
const { popupFocus, popupScroll } = getListeners(this);
return renderMenu ? (
<div
style={{
overflow: 'auto',
transform: 'translateZ(0)',
}}
id={this.$props.ariaId}
tabIndex="-1"
onFocus={popupFocus}
onMousedown={preventDefaultEvent}
onScroll={popupScroll}
ref="menuContainer"
>
{renderMenu}
</div>
) : null;
},
};