@zykj2024/much-library 1.1.4 → 1.1.5-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ declare const _default: () => import("react/jsx-runtime").JSX.Element;
2
+ export default _default;
@@ -0,0 +1,83 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ /**
3
+ * title: McContainer自定义table body场景中
4
+ * description: 翻页后的数据会重新进入懒加载流程,比如天玑-素材管理
5
+ */
6
+ import { McLazyLoader } from "../..";
7
+ import { useState } from 'react';
8
+ import McContainer from "../../McContainer";
9
+ import McInput from "../../McInput";
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ import { Fragment as _Fragment } from "react/jsx-runtime";
12
+ var getTextList = function getTextList(cur) {
13
+ return Array.from({
14
+ length: 40
15
+ }, function (_, index) {
16
+ return {
17
+ key: index + cur,
18
+ value: index
19
+ };
20
+ });
21
+ };
22
+ export default (function () {
23
+ var _useState = useState(1),
24
+ _useState2 = _slicedToArray(_useState, 2),
25
+ cur = _useState2[0],
26
+ setCur = _useState2[1];
27
+ console.log(cur);
28
+ return /*#__PURE__*/_jsx("div", {
29
+ children: /*#__PURE__*/_jsx(McContainer, {
30
+ queryItems: /*#__PURE__*/_jsx(_Fragment, {
31
+ children: /*#__PURE__*/_jsx(McInput, {
32
+ label: "\u59D3\u540D",
33
+ name: "name",
34
+ style: {
35
+ width: 270
36
+ }
37
+ })
38
+ }),
39
+ height: "500px",
40
+ tableProps: {
41
+ dataSource: getTextList(cur),
42
+ // scroll: {y: 200},
43
+ pagination: {
44
+ pageSize: 40,
45
+ current: cur,
46
+ total: 120
47
+ },
48
+ onChange: function onChange(info) {
49
+ var _info$current;
50
+ setCur((_info$current = info === null || info === void 0 ? void 0 : info.current) !== null && _info$current !== void 0 ? _info$current : 1);
51
+ },
52
+ components: {
53
+ body: function body(data) {
54
+ return /*#__PURE__*/_jsx(McLazyLoader, {
55
+ style: {
56
+ height: 200,
57
+ width: 500,
58
+ overflow: 'auto',
59
+ display: 'flex',
60
+ flexWrap: 'wrap'
61
+ },
62
+ onItemVisible: function onItemVisible(index) {
63
+ console.log('visible', index);
64
+ },
65
+ itemMinHeight: 50,
66
+ itemMinWidth: 200,
67
+ children: data.map(function (x) {
68
+ return /*#__PURE__*/_jsx("img", {
69
+ width: "auto",
70
+ height: 50,
71
+ onLoad: function onLoad() {
72
+ console.log('[window root: loaded]', new Date(), x.key, cur);
73
+ },
74
+ src: "http://pre.data.muchcloud.com/static/img/logo-t.34c4816d.png"
75
+ }, x.key + '_' + cur);
76
+ })
77
+ });
78
+ }
79
+ }
80
+ }
81
+ })
82
+ });
83
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: () => import("react/jsx-runtime").JSX.Element;
2
+ export default _default;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * title: grid布局用法
3
+ * description: 元素出现在视窗时渲染, 日志见console.log。
4
+ */
5
+ import { McLazyLoader } from "../..";
6
+ import { jsx as _jsx } from "react/jsx-runtime";
7
+ var textList = Array.from({
8
+ length: 100
9
+ }, function (_, index) {
10
+ return index;
11
+ });
12
+ export default (function () {
13
+ return /*#__PURE__*/_jsx("div", {
14
+ children: /*#__PURE__*/_jsx(McLazyLoader, {
15
+ style: {
16
+ height: 200,
17
+ overflow: 'auto',
18
+ display: 'flex',
19
+ flexWrap: 'wrap'
20
+ },
21
+ onItemVisible: function onItemVisible(index) {
22
+ console.log('visible', index);
23
+ },
24
+ itemMinHeight: 50,
25
+ itemMinWidth: 200,
26
+ children: textList.map(function (v) {
27
+ return /*#__PURE__*/_jsx("img", {
28
+ width: "auto",
29
+ height: 50,
30
+ onLoad: function onLoad() {
31
+ console.log('[window root: loaded]', new Date(), v);
32
+ },
33
+ src: "http://pre.data.muchcloud.com/static/img/logo-t.34c4816d.png",
34
+ alt: v.toString()
35
+ }, v);
36
+ })
37
+ })
38
+ });
39
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: () => import("react/jsx-runtime").JSX.Element;
2
+ export default _default;
@@ -0,0 +1,73 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ /**
3
+ * title: 在Antd Table翻页场景中使用
4
+ * description: 翻页后的数据会重新进入懒加载流程
5
+ */
6
+ import { Table } from 'antd';
7
+ import { McLazyLoader } from "../..";
8
+ import { useState } from 'react';
9
+ import { jsx as _jsx } from "react/jsx-runtime";
10
+ var getTextList = function getTextList(cur) {
11
+ return Array.from({
12
+ length: 40
13
+ }, function (_, index) {
14
+ return {
15
+ key: index + cur,
16
+ value: index
17
+ };
18
+ });
19
+ };
20
+ export default (function () {
21
+ var _useState = useState(1),
22
+ _useState2 = _slicedToArray(_useState, 2),
23
+ cur = _useState2[0],
24
+ setCur = _useState2[1];
25
+ console.log(cur);
26
+ return /*#__PURE__*/_jsx("div", {
27
+ children: /*#__PURE__*/_jsx(Table, {
28
+ dataSource: getTextList(cur),
29
+ scroll: {
30
+ y: 200
31
+ },
32
+ pagination: {
33
+ pageSize: 40,
34
+ current: cur,
35
+ total: 120
36
+ },
37
+ onChange: function onChange(info) {
38
+ var _info$current;
39
+ setCur((_info$current = info === null || info === void 0 ? void 0 : info.current) !== null && _info$current !== void 0 ? _info$current : 1);
40
+ },
41
+ components: {
42
+ body: function body(data) {
43
+ return /*#__PURE__*/_jsx(McLazyLoader, {
44
+ style: {
45
+ height: 200,
46
+ width: 430,
47
+ overflow: 'auto',
48
+ display: 'flex',
49
+ flexWrap: 'wrap'
50
+ },
51
+ onItemVisible: function onItemVisible(index) {
52
+ console.log('visible', index);
53
+ },
54
+ itemMinHeight: 50
55
+ // 不加这个的话,水平排列的元素不占空间,没有懒加载
56
+ ,
57
+ itemMinWidth: 200,
58
+ children: data.map(function (x) {
59
+ return /*#__PURE__*/_jsx("img", {
60
+ width: "auto",
61
+ height: 50,
62
+ onLoad: function onLoad() {
63
+ console.log('[window root: loaded]', new Date(), x.key, cur);
64
+ },
65
+ src: "http://pre.data.muchcloud.com/static/img/logo-t.34c4816d.png"
66
+ }, x.key + '_' + cur);
67
+ })
68
+ });
69
+ }
70
+ }
71
+ })
72
+ });
73
+ });
@@ -1,4 +1,3 @@
1
- /** 监听容器内的list每个元素,当元素进入视图则展示,展示后不再消失 */
2
1
  import React from 'react';
3
2
  interface ObserverInsertionWrapperProps {
4
3
  children: React.ReactNode[];
@@ -6,7 +5,16 @@ interface ObserverInsertionWrapperProps {
6
5
  * @description 最小高度,不能为0。为0时懒加载可能会无效
7
6
  * @default 20
8
7
  */
9
- itemMinHeight?: number;
8
+ itemMinHeight: number;
9
+ /**
10
+ * @description 最小宽度,水平排列时不能为0
11
+ * @default unset
12
+ */
13
+ itemMinWidth?: number;
14
+ /**
15
+ * @description 元素进入视图回调
16
+ */
17
+ onItemVisible?: (index: number, key: string) => void;
10
18
  /**
11
19
  * @description 距离视图多少像素时触发。出于用户体验考虑,建议设置一个正数
12
20
  * @default 0
@@ -22,6 +30,9 @@ interface ObserverInsertionWrapperProps {
22
30
  * @default null
23
31
  */
24
32
  root?: Element | Document | null;
33
+ /**
34
+ * @description 外层容器样式
35
+ */
25
36
  style?: React.CSSProperties;
26
37
  }
27
38
  declare const ObserverInsertionWrapper: React.FC<ObserverInsertionWrapperProps>;
@@ -1,9 +1,10 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
- /** 监听容器内的list每个元素,当元素进入视图则展示,展示后不再消失 */
4
-
5
2
  import React, { useEffect, useRef, useState } from 'react';
6
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
+ var getKey = function getKey(child, index) {
5
+ var _child$key;
6
+ return /*#__PURE__*/React.isValidElement(child) ? String((_child$key = child.key) !== null && _child$key !== void 0 ? _child$key : index) : String(index);
7
+ };
7
8
  var ObserverInsertionWrapper = function ObserverInsertionWrapper(_ref) {
8
9
  var children = _ref.children,
9
10
  style = _ref.style,
@@ -14,30 +15,44 @@ var ObserverInsertionWrapper = function ObserverInsertionWrapper(_ref) {
14
15
  _ref$root = _ref.root,
15
16
  root = _ref$root === void 0 ? null : _ref$root,
16
17
  _ref$itemMinHeight = _ref.itemMinHeight,
17
- itemMinHeight = _ref$itemMinHeight === void 0 ? 20 : _ref$itemMinHeight;
18
+ itemMinHeight = _ref$itemMinHeight === void 0 ? 20 : _ref$itemMinHeight,
19
+ itemMinWidth = _ref.itemMinWidth,
20
+ onItemVisible = _ref.onItemVisible;
18
21
  var wrapperRef = useRef(null);
19
- var _useState = useState(new Array(children.length).fill(false)),
22
+
23
+ // 已经显示过的 key 列表
24
+ var _useState = useState(new Set()),
20
25
  _useState2 = _slicedToArray(_useState, 2),
21
26
  visibleItems = _useState2[0],
22
27
  setVisibleItems = _useState2[1];
28
+
29
+ // 为避免 children 每次渲染变化导致 useEffect 重复执行,构建一个 keyList 哈希
30
+ var keyList = React.Children.toArray(children).map(function (child, index) {
31
+ return getKey(child, index);
32
+ });
33
+ var keyListHash = JSON.stringify(keyList);
34
+
35
+ // 每次 keyList 变动重置显示列表
36
+ useEffect(function () {
37
+ setVisibleItems(new Set());
38
+ }, [keyListHash]);
23
39
  useEffect(function () {
24
40
  var _wrapperRef$current;
25
41
  var observer = new IntersectionObserver(function (entries) {
26
42
  entries.forEach(function (entry) {
27
43
  var index = Number(entry.target.getAttribute('data-index'));
28
- if (entry.isIntersecting) {
29
- observer.unobserve(entry.target);
44
+ var key = String(entry.target.getAttribute('data-key'));
45
+ if (entry.isIntersecting && !visibleItems.has(key)) {
30
46
  setVisibleItems(function (prev) {
31
- console.log('in:', index);
32
- var newVisibleItems = _toConsumableArray(prev);
33
- newVisibleItems[index] = true;
34
- return newVisibleItems;
47
+ var next = new Set(prev);
48
+ next.add(key);
49
+ onItemVisible === null || onItemVisible === void 0 || onItemVisible(index, key);
50
+ return next;
35
51
  });
36
52
  }
37
53
  });
38
54
  }, {
39
55
  root: root,
40
- // 监听浏览器窗口
41
56
  rootMargin: "".concat(offset, "px"),
42
57
  threshold: threshold
43
58
  });
@@ -50,18 +65,20 @@ var ObserverInsertionWrapper = function ObserverInsertionWrapper(_ref) {
50
65
  observer.unobserve(element);
51
66
  });
52
67
  };
53
- }, [offset, threshold, root, children.length]);
68
+ }, [offset, threshold, root, keyListHash, visibleItems]);
54
69
  return /*#__PURE__*/_jsx("div", {
55
70
  ref: wrapperRef,
56
71
  style: style,
57
72
  children: React.Children.map(children, function (child, index) {
58
- var key = /*#__PURE__*/React.isValidElement(child) ? child.key : index;
73
+ var key = getKey(child, index);
59
74
  return /*#__PURE__*/_jsx("div", {
75
+ "data-key": key,
76
+ "data-index": index,
60
77
  style: {
61
- minHeight: itemMinHeight
78
+ minHeight: itemMinHeight,
79
+ minWidth: itemMinWidth !== null && itemMinWidth !== void 0 ? itemMinWidth : 'unset'
62
80
  },
63
- "data-index": index,
64
- children: visibleItems[index] ? child : null
81
+ children: visibleItems.has(key) ? child : null
65
82
  }, key);
66
83
  })
67
84
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zykj2024/much-library",
3
- "version": "1.1.4",
3
+ "version": "1.1.5-beta.3",
4
4
  "description": "react library",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",