Overflow.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  3. var _excluded = ["class", "style"];
  4. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  5. import { computed, defineComponent, ref, watch } from 'vue';
  6. import ResizeObserver from '../vc-resize-observer';
  7. import classNames from '../_util/classNames';
  8. import PropTypes from '../_util/vue-types';
  9. import { OverflowContextProvider } from './context';
  10. import Item from './Item';
  11. import RawItem from './RawItem';
  12. var RESPONSIVE = 'responsive';
  13. var INVALIDATE = 'invalidate';
  14. function defaultRenderRest(omittedItems) {
  15. return "+ ".concat(omittedItems.length, " ...");
  16. }
  17. var overflowProps = function overflowProps() {
  18. return {
  19. id: String,
  20. prefixCls: String,
  21. data: Array,
  22. itemKey: [String, Number, Function],
  23. /** Used for `responsive`. It will limit render node to avoid perf issue */
  24. itemWidth: {
  25. type: Number,
  26. default: 10
  27. },
  28. renderItem: Function,
  29. /** @private Do not use in your production. Render raw node that need wrap Item by developer self */
  30. renderRawItem: Function,
  31. maxCount: [Number, String],
  32. renderRest: Function,
  33. /** @private Do not use in your production. Render raw node that need wrap Item by developer self */
  34. renderRawRest: Function,
  35. suffix: PropTypes.any,
  36. component: String,
  37. itemComponent: PropTypes.any,
  38. /** @private This API may be refactor since not well design */
  39. onVisibleChange: Function,
  40. /** When set to `full`, ssr will render full items by default and remove at client side */
  41. ssr: String,
  42. onMousedown: Function
  43. };
  44. };
  45. var Overflow = defineComponent({
  46. name: 'Overflow',
  47. inheritAttrs: false,
  48. props: overflowProps(),
  49. emits: ['visibleChange'],
  50. setup: function setup(props, _ref) {
  51. var attrs = _ref.attrs,
  52. emit = _ref.emit,
  53. slots = _ref.slots;
  54. var fullySSR = computed(function () {
  55. return props.ssr === 'full';
  56. });
  57. var containerWidth = ref(null);
  58. var mergedContainerWidth = computed(function () {
  59. return containerWidth.value || 0;
  60. });
  61. var itemWidths = ref(new Map());
  62. var prevRestWidth = ref(0);
  63. var restWidth = ref(0);
  64. var suffixWidth = ref(0);
  65. var suffixFixedStart = ref(null);
  66. var displayCount = ref(null);
  67. var mergedDisplayCount = computed(function () {
  68. if (displayCount.value === null && fullySSR.value) {
  69. return Number.MAX_SAFE_INTEGER;
  70. }
  71. return displayCount.value || 0;
  72. });
  73. var restReady = ref(false);
  74. var itemPrefixCls = computed(function () {
  75. return "".concat(props.prefixCls, "-item");
  76. });
  77. // Always use the max width to avoid blink
  78. var mergedRestWidth = computed(function () {
  79. return Math.max(prevRestWidth.value, restWidth.value);
  80. });
  81. // ================================= Data =================================
  82. var isResponsive = computed(function () {
  83. return !!(props.data.length && props.maxCount === RESPONSIVE);
  84. });
  85. var invalidate = computed(function () {
  86. return props.maxCount === INVALIDATE;
  87. });
  88. /**
  89. * When is `responsive`, we will always render rest node to get the real width of it for calculation
  90. */
  91. var showRest = computed(function () {
  92. return isResponsive.value || typeof props.maxCount === 'number' && props.data.length > props.maxCount;
  93. });
  94. var mergedData = computed(function () {
  95. var items = props.data;
  96. if (isResponsive.value) {
  97. if (containerWidth.value === null && fullySSR.value) {
  98. items = props.data;
  99. } else {
  100. items = props.data.slice(0, Math.min(props.data.length, mergedContainerWidth.value / props.itemWidth));
  101. }
  102. } else if (typeof props.maxCount === 'number') {
  103. items = props.data.slice(0, props.maxCount);
  104. }
  105. return items;
  106. });
  107. var omittedItems = computed(function () {
  108. if (isResponsive.value) {
  109. return props.data.slice(mergedDisplayCount.value + 1);
  110. }
  111. return props.data.slice(mergedData.value.length);
  112. });
  113. // ================================= Item =================================
  114. var getKey = function getKey(item, index) {
  115. var _ref2;
  116. if (typeof props.itemKey === 'function') {
  117. return props.itemKey(item);
  118. }
  119. return (_ref2 = props.itemKey && (item === null || item === void 0 ? void 0 : item[props.itemKey])) !== null && _ref2 !== void 0 ? _ref2 : index;
  120. };
  121. var mergedRenderItem = computed(function () {
  122. return props.renderItem || function (item) {
  123. return item;
  124. };
  125. });
  126. var updateDisplayCount = function updateDisplayCount(count, notReady) {
  127. displayCount.value = count;
  128. if (!notReady) {
  129. restReady.value = count < props.data.length - 1;
  130. emit('visibleChange', count);
  131. }
  132. };
  133. // ================================= Size =================================
  134. var onOverflowResize = function onOverflowResize(_, element) {
  135. containerWidth.value = element.clientWidth;
  136. };
  137. var registerSize = function registerSize(key, width) {
  138. var clone = new Map(itemWidths.value);
  139. if (width === null) {
  140. clone.delete(key);
  141. } else {
  142. clone.set(key, width);
  143. }
  144. itemWidths.value = clone;
  145. };
  146. var registerOverflowSize = function registerOverflowSize(_, width) {
  147. prevRestWidth.value = restWidth.value;
  148. restWidth.value = width;
  149. };
  150. var registerSuffixSize = function registerSuffixSize(_, width) {
  151. suffixWidth.value = width;
  152. };
  153. // ================================ Effect ================================
  154. var getItemWidth = function getItemWidth(index) {
  155. return itemWidths.value.get(getKey(mergedData.value[index], index));
  156. };
  157. watch([mergedContainerWidth, itemWidths, restWidth, suffixWidth, function () {
  158. return props.itemKey;
  159. }, mergedData], function () {
  160. if (mergedContainerWidth.value && mergedRestWidth.value && mergedData.value) {
  161. var totalWidth = suffixWidth.value;
  162. var len = mergedData.value.length;
  163. var lastIndex = len - 1;
  164. // When data count change to 0, reset this since not loop will reach
  165. if (!len) {
  166. updateDisplayCount(0);
  167. suffixFixedStart.value = null;
  168. return;
  169. }
  170. for (var i = 0; i < len; i += 1) {
  171. var currentItemWidth = getItemWidth(i);
  172. // Break since data not ready
  173. if (currentItemWidth === undefined) {
  174. updateDisplayCount(i - 1, true);
  175. break;
  176. }
  177. // Find best match
  178. totalWidth += currentItemWidth;
  179. if (
  180. // Only one means `totalWidth` is the final width
  181. lastIndex === 0 && totalWidth <= mergedContainerWidth.value ||
  182. // Last two width will be the final width
  183. i === lastIndex - 1 && totalWidth + getItemWidth(lastIndex) <= mergedContainerWidth.value) {
  184. // Additional check if match the end
  185. updateDisplayCount(lastIndex);
  186. suffixFixedStart.value = null;
  187. break;
  188. } else if (totalWidth + mergedRestWidth.value > mergedContainerWidth.value) {
  189. // Can not hold all the content to show rest
  190. updateDisplayCount(i - 1);
  191. suffixFixedStart.value = totalWidth - currentItemWidth - suffixWidth.value + restWidth.value;
  192. break;
  193. }
  194. }
  195. if (props.suffix && getItemWidth(0) + suffixWidth.value > mergedContainerWidth.value) {
  196. suffixFixedStart.value = null;
  197. }
  198. }
  199. });
  200. return function () {
  201. // ================================ Render ================================
  202. var displayRest = restReady.value && !!omittedItems.value.length;
  203. var itemComponent = props.itemComponent,
  204. renderRawItem = props.renderRawItem,
  205. renderRawRest = props.renderRawRest,
  206. renderRest = props.renderRest,
  207. _props$prefixCls = props.prefixCls,
  208. prefixCls = _props$prefixCls === void 0 ? 'rc-overflow' : _props$prefixCls,
  209. suffix = props.suffix,
  210. _props$component = props.component,
  211. Component = _props$component === void 0 ? 'div' : _props$component,
  212. id = props.id,
  213. onMousedown = props.onMousedown;
  214. var className = attrs.class,
  215. style = attrs.style,
  216. restAttrs = _objectWithoutProperties(attrs, _excluded);
  217. var suffixStyle = {};
  218. if (suffixFixedStart.value !== null && isResponsive.value) {
  219. suffixStyle = {
  220. position: 'absolute',
  221. left: "".concat(suffixFixedStart.value, "px"),
  222. top: 0
  223. };
  224. }
  225. var itemSharedProps = {
  226. prefixCls: itemPrefixCls.value,
  227. responsive: isResponsive.value,
  228. component: itemComponent,
  229. invalidate: invalidate.value
  230. };
  231. // >>>>> Choice render fun by `renderRawItem`
  232. var internalRenderItemNode = renderRawItem ? function (item, index) {
  233. var key = getKey(item, index);
  234. return _createVNode(OverflowContextProvider, {
  235. "key": key,
  236. "value": _objectSpread(_objectSpread({}, itemSharedProps), {}, {
  237. order: index,
  238. item: item,
  239. itemKey: key,
  240. registerSize: registerSize,
  241. display: index <= mergedDisplayCount.value
  242. })
  243. }, {
  244. default: function _default() {
  245. return [renderRawItem(item, index)];
  246. }
  247. });
  248. } : function (item, index) {
  249. var key = getKey(item, index);
  250. return _createVNode(Item, _objectSpread(_objectSpread({}, itemSharedProps), {}, {
  251. "order": index,
  252. "key": key,
  253. "item": item,
  254. "renderItem": mergedRenderItem.value,
  255. "itemKey": key,
  256. "registerSize": registerSize,
  257. "display": index <= mergedDisplayCount.value
  258. }), null);
  259. };
  260. // >>>>> Rest node
  261. var restNode = function restNode() {
  262. return null;
  263. };
  264. var restContextProps = {
  265. order: displayRest ? mergedDisplayCount.value : Number.MAX_SAFE_INTEGER,
  266. className: "".concat(itemPrefixCls.value, " ").concat(itemPrefixCls.value, "-rest"),
  267. registerSize: registerOverflowSize,
  268. display: displayRest
  269. };
  270. if (!renderRawRest) {
  271. var mergedRenderRest = renderRest || defaultRenderRest;
  272. restNode = function restNode() {
  273. return _createVNode(Item, _objectSpread(_objectSpread({}, itemSharedProps), restContextProps), {
  274. default: function _default() {
  275. return typeof mergedRenderRest === 'function' ? mergedRenderRest(omittedItems.value) : mergedRenderRest;
  276. }
  277. });
  278. };
  279. } else if (renderRawRest) {
  280. restNode = function restNode() {
  281. return _createVNode(OverflowContextProvider, {
  282. "value": _objectSpread(_objectSpread({}, itemSharedProps), restContextProps)
  283. }, {
  284. default: function _default() {
  285. return [renderRawRest(omittedItems.value)];
  286. }
  287. });
  288. };
  289. }
  290. var overflowNode = function overflowNode() {
  291. var _slots$default;
  292. return _createVNode(Component, _objectSpread({
  293. "id": id,
  294. "class": classNames(!invalidate.value && prefixCls, className),
  295. "style": style,
  296. "onMousedown": onMousedown
  297. }, restAttrs), {
  298. default: function _default() {
  299. return [mergedData.value.map(internalRenderItemNode), showRest.value ? restNode() : null, suffix && _createVNode(Item, _objectSpread(_objectSpread({}, itemSharedProps), {}, {
  300. "order": mergedDisplayCount.value,
  301. "class": "".concat(itemPrefixCls.value, "-suffix"),
  302. "registerSize": registerSuffixSize,
  303. "display": true,
  304. "style": suffixStyle
  305. }), {
  306. default: function _default() {
  307. return suffix;
  308. }
  309. }), (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)];
  310. }
  311. });
  312. };
  313. // 使用 disabled 避免结构不一致 导致子组件 rerender
  314. return _createVNode(ResizeObserver, {
  315. "disabled": !isResponsive.value,
  316. "onResize": onOverflowResize
  317. }, {
  318. default: overflowNode
  319. });
  320. };
  321. }
  322. });
  323. Overflow.Item = RawItem;
  324. Overflow.RESPONSIVE = RESPONSIVE;
  325. Overflow.INVALIDATE = INVALIDATE;
  326. export default Overflow;