DropdownMenu.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import raf from 'raf';
  3. import PropTypes from '../_util/vue-types';
  4. import Menu from '../vc-menu';
  5. import scrollIntoView from 'dom-scroll-into-view';
  6. import { getSelectKeys, preventDefaultEvent } from './util';
  7. import { cloneElement } from '../_util/vnode';
  8. import BaseMixin from '../_util/BaseMixin';
  9. import { getSlotOptions, getComponentFromProp, getListeners } from '../_util/props-util';
  10. export default {
  11. name: 'DropdownMenu',
  12. mixins: [BaseMixin],
  13. props: {
  14. ariaId: PropTypes.string,
  15. defaultActiveFirstOption: PropTypes.bool,
  16. value: PropTypes.any,
  17. dropdownMenuStyle: PropTypes.object,
  18. multiple: PropTypes.bool,
  19. // onPopupFocus: PropTypes.func,
  20. // onPopupScroll: PropTypes.func,
  21. // onMenuDeSelect: PropTypes.func,
  22. // onMenuSelect: PropTypes.func,
  23. prefixCls: PropTypes.string,
  24. menuItems: PropTypes.any,
  25. inputValue: PropTypes.string,
  26. visible: PropTypes.bool,
  27. backfillValue: PropTypes.any,
  28. firstActiveValue: PropTypes.string,
  29. menuItemSelectedIcon: PropTypes.any
  30. },
  31. watch: {
  32. visible: function visible(val) {
  33. var _this = this;
  34. if (!val) {
  35. this.lastVisible = val;
  36. } else {
  37. this.$nextTick(function () {
  38. _this.scrollActiveItemToView();
  39. });
  40. }
  41. }
  42. },
  43. created: function created() {
  44. this.rafInstance = null;
  45. this.lastInputValue = this.$props.inputValue;
  46. this.lastVisible = false;
  47. },
  48. mounted: function mounted() {
  49. var _this2 = this;
  50. this.$nextTick(function () {
  51. _this2.scrollActiveItemToView();
  52. });
  53. this.lastVisible = this.$props.visible;
  54. },
  55. updated: function updated() {
  56. var props = this.$props;
  57. // if (!this.prevVisible && props.visible) {
  58. // this.$nextTick(() => {
  59. // this.scrollActiveItemToView();
  60. // });
  61. // }
  62. this.lastVisible = props.visible;
  63. this.lastInputValue = props.inputValue;
  64. this.prevVisible = this.visible;
  65. },
  66. beforeDestroy: function beforeDestroy() {
  67. if (this.rafInstance) {
  68. raf.cancel(this.rafInstance);
  69. }
  70. },
  71. methods: {
  72. scrollActiveItemToView: function scrollActiveItemToView() {
  73. var _this3 = this;
  74. // scroll into view
  75. var itemComponent = this.firstActiveItem && this.firstActiveItem.$el;
  76. var props = this.$props;
  77. var value = props.value,
  78. visible = props.visible,
  79. firstActiveValue = props.firstActiveValue;
  80. if (!itemComponent || !visible) {
  81. return;
  82. }
  83. var scrollIntoViewOpts = {
  84. onlyScrollIfNeeded: true
  85. };
  86. if ((!value || value.length === 0) && firstActiveValue) {
  87. scrollIntoViewOpts.alignWithTop = true;
  88. }
  89. // Delay to scroll since current frame item position is not ready when pre view is by filter
  90. // https://github.com/ant-design/ant-design/issues/11268#issuecomment-406634462
  91. this.rafInstance = raf(function () {
  92. scrollIntoView(itemComponent, _this3.$refs.menuRef.$el, scrollIntoViewOpts);
  93. });
  94. },
  95. renderMenu: function renderMenu() {
  96. var _this4 = this;
  97. var h = this.$createElement;
  98. var props = this.$props;
  99. var menuItems = props.menuItems,
  100. defaultActiveFirstOption = props.defaultActiveFirstOption,
  101. value = props.value,
  102. prefixCls = props.prefixCls,
  103. multiple = props.multiple,
  104. inputValue = props.inputValue,
  105. firstActiveValue = props.firstActiveValue,
  106. dropdownMenuStyle = props.dropdownMenuStyle,
  107. backfillValue = props.backfillValue,
  108. visible = props.visible;
  109. var menuItemSelectedIcon = getComponentFromProp(this, 'menuItemSelectedIcon');
  110. var _getListeners = getListeners(this),
  111. menuDeselect = _getListeners.menuDeselect,
  112. menuSelect = _getListeners.menuSelect,
  113. popupScroll = _getListeners.popupScroll;
  114. if (menuItems && menuItems.length) {
  115. var selectedKeys = getSelectKeys(menuItems, value);
  116. var menuProps = {
  117. props: {
  118. multiple: multiple,
  119. itemIcon: multiple ? menuItemSelectedIcon : null,
  120. selectedKeys: selectedKeys,
  121. prefixCls: prefixCls + '-menu'
  122. },
  123. on: {},
  124. style: dropdownMenuStyle,
  125. ref: 'menuRef',
  126. attrs: {
  127. role: 'listbox'
  128. }
  129. };
  130. if (popupScroll) {
  131. menuProps.on.scroll = popupScroll;
  132. }
  133. if (multiple) {
  134. menuProps.on.deselect = menuDeselect;
  135. menuProps.on.select = menuSelect;
  136. } else {
  137. menuProps.on.click = menuSelect;
  138. }
  139. var activeKeyProps = {};
  140. var defaultActiveFirst = defaultActiveFirstOption;
  141. var clonedMenuItems = menuItems;
  142. if (selectedKeys.length || firstActiveValue) {
  143. if (props.visible && !this.lastVisible) {
  144. activeKeyProps.activeKey = selectedKeys[0] || firstActiveValue;
  145. } else if (!visible) {
  146. // Do not trigger auto active since we already have selectedKeys
  147. if (selectedKeys[0]) {
  148. defaultActiveFirst = false;
  149. }
  150. activeKeyProps.activeKey = undefined;
  151. }
  152. var foundFirst = false;
  153. // set firstActiveItem via cloning menus
  154. // for scroll into view
  155. var clone = function clone(item) {
  156. if (!foundFirst && selectedKeys.indexOf(item.key) !== -1 || !foundFirst && !selectedKeys.length && firstActiveValue.indexOf(item.key) !== -1) {
  157. foundFirst = true;
  158. return cloneElement(item, {
  159. directives: [{
  160. name: 'ant-ref',
  161. value: function value(ref) {
  162. _this4.firstActiveItem = ref;
  163. }
  164. }]
  165. });
  166. }
  167. return item;
  168. };
  169. clonedMenuItems = menuItems.map(function (item) {
  170. if (getSlotOptions(item).isMenuItemGroup) {
  171. var children = item.componentOptions.children.map(clone);
  172. return cloneElement(item, { children: children });
  173. }
  174. return clone(item);
  175. });
  176. } else {
  177. // Clear firstActiveItem when dropdown menu items was empty
  178. // Avoid `Unable to find node on an unmounted component`
  179. // https://github.com/ant-design/ant-design/issues/10774
  180. this.firstActiveItem = null;
  181. }
  182. // clear activeKey when inputValue change
  183. var lastValue = value && value[value.length - 1];
  184. if (inputValue !== this.lastInputValue && (!lastValue || lastValue !== backfillValue)) {
  185. activeKeyProps.activeKey = '';
  186. }
  187. menuProps.props = _extends({}, activeKeyProps, menuProps.props, { defaultActiveFirst: defaultActiveFirst });
  188. return h(
  189. Menu,
  190. menuProps,
  191. [clonedMenuItems]
  192. );
  193. }
  194. return null;
  195. }
  196. },
  197. render: function render() {
  198. var h = arguments[0];
  199. var renderMenu = this.renderMenu();
  200. var _getListeners2 = getListeners(this),
  201. popupFocus = _getListeners2.popupFocus,
  202. popupScroll = _getListeners2.popupScroll;
  203. return renderMenu ? h(
  204. 'div',
  205. {
  206. style: {
  207. overflow: 'auto',
  208. transform: 'translateZ(0)'
  209. },
  210. attrs: { id: this.$props.ariaId,
  211. tabIndex: '-1'
  212. },
  213. on: {
  214. 'focus': popupFocus,
  215. 'mousedown': preventDefaultEvent,
  216. 'scroll': popupScroll
  217. },
  218. ref: 'menuContainer'
  219. },
  220. [renderMenu]
  221. ) : null;
  222. }
  223. };