@yh-ui/hooks 0.1.17 → 0.1.21

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.
@@ -7,45 +7,36 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.useVirtualScroll = useVirtualScroll;
8
8
  var _vue = require("vue");
9
9
  function useVirtualScroll(options) {
10
- const {
11
- itemHeight,
12
- containerHeight,
13
- overscan = 3
14
- } = options;
15
10
  const containerRef = (0, _vue.ref)(null);
16
11
  const scrollTop = (0, _vue.ref)(0);
17
12
  const itemsRef = (0, _vue.computed)(() => {
18
- const items = options.items;
19
- return Array.isArray(items) ? items : items.value;
13
+ return Array.isArray(options.items) ? options.items : options.items.value;
20
14
  });
21
- const totalHeight = (0, _vue.computed)(() => itemsRef.value.length * itemHeight);
22
- const visibleCount = (0, _vue.computed)(() => Math.ceil(containerHeight / itemHeight));
15
+ const itemHeightRef = (0, _vue.computed)(() => (0, _vue.unref)(options.itemHeight));
16
+ const containerHeightRef = (0, _vue.computed)(() => (0, _vue.unref)(options.containerHeight));
17
+ const overscanRef = (0, _vue.computed)(() => (0, _vue.unref)(options.overscan) ?? 3);
18
+ const totalHeight = (0, _vue.computed)(() => itemsRef.value.length * itemHeightRef.value);
23
19
  const startIndex = (0, _vue.computed)(() => {
24
- const items = itemsRef.value;
25
- if (items.length === 0) return 0;
26
- const start = Math.floor(scrollTop.value / itemHeight);
27
- return Math.max(0, start - overscan);
20
+ if (itemsRef.value.length === 0) return 0;
21
+ const start = Math.floor(scrollTop.value / itemHeightRef.value);
22
+ return Math.max(0, start - overscanRef.value);
28
23
  });
29
24
  const endIndex = (0, _vue.computed)(() => {
30
- const items = itemsRef.value;
31
- if (items.length === 0) return 0;
32
- const start = Math.floor(scrollTop.value / itemHeight);
33
- const end = start + visibleCount.value;
34
- return Math.min(items.length, end + overscan);
25
+ if (itemsRef.value.length === 0) return 0;
26
+ const end = Math.ceil((scrollTop.value + containerHeightRef.value) / itemHeightRef.value);
27
+ return Math.min(itemsRef.value.length, end + overscanRef.value);
35
28
  });
36
29
  const visibleItems = (0, _vue.computed)(() => {
37
- const items = itemsRef.value;
38
- if (items.length === 0) return [];
39
- return items.slice(startIndex.value, endIndex.value);
30
+ return itemsRef.value.slice(startIndex.value, endIndex.value);
40
31
  });
41
- const offsetY = (0, _vue.computed)(() => startIndex.value * itemHeight);
32
+ const offsetY = (0, _vue.computed)(() => startIndex.value * itemHeightRef.value);
42
33
  const onScroll = event => {
43
34
  const target = event.target;
44
- scrollTop.value = target.scrollTop;
35
+ if (target) scrollTop.value = target.scrollTop;
45
36
  };
46
37
  const scrollToIndex = index => {
47
38
  if (containerRef.value) {
48
- const targetScrollTop = index * itemHeight;
39
+ const targetScrollTop = index * itemHeightRef.value;
49
40
  containerRef.value.scrollTop = targetScrollTop;
50
41
  scrollTop.value = targetScrollTop;
51
42
  }
@@ -54,8 +45,8 @@ function useVirtualScroll(options) {
54
45
  visibleItems,
55
46
  totalHeight,
56
47
  offsetY,
57
- startIndex: (0, _vue.computed)(() => startIndex.value),
58
- endIndex: (0, _vue.computed)(() => endIndex.value),
48
+ startIndex,
49
+ endIndex,
59
50
  onScroll,
60
51
  scrollToIndex,
61
52
  containerRef
@@ -1,17 +1,13 @@
1
- /**
2
- * useVirtualScroll - 虚拟滚动 Hook
3
- * @description 用于大数据量列表的虚拟滚动渲染
4
- */
5
1
  import { type Ref, type ComputedRef } from 'vue';
6
2
  export interface VirtualScrollOptions<T = unknown> {
7
3
  /** 每项高度 */
8
- itemHeight: number;
4
+ itemHeight: number | Ref<number>;
9
5
  /** 容器高度 */
10
- containerHeight: number;
6
+ containerHeight: number | Ref<number>;
11
7
  /** 数据列表 */
12
8
  items: Ref<T[]> | T[];
13
9
  /** 上下额外渲染数量 */
14
- overscan?: number;
10
+ overscan?: number | Ref<number>;
15
11
  }
16
12
  export interface VirtualScrollReturn<T = unknown> {
17
13
  /** 可见项列表 */
@@ -21,9 +17,9 @@ export interface VirtualScrollReturn<T = unknown> {
21
17
  /** Y轴偏移量 */
22
18
  offsetY: ComputedRef<number>;
23
19
  /** 起始索引 */
24
- startIndex: Ref<number>;
20
+ startIndex: ComputedRef<number>;
25
21
  /** 结束索引 */
26
- endIndex: Ref<number>;
22
+ endIndex: ComputedRef<number>;
27
23
  /** 滚动事件处理 */
28
24
  onScroll: (event: Event) => void;
29
25
  /** 滚动到指定索引 */
@@ -1,40 +1,35 @@
1
- import { ref, computed } from "vue";
1
+ import { ref, computed, unref } from "vue";
2
2
  export function useVirtualScroll(options) {
3
- const { itemHeight, containerHeight, overscan = 3 } = options;
4
3
  const containerRef = ref(null);
5
4
  const scrollTop = ref(0);
6
5
  const itemsRef = computed(() => {
7
- const items = options.items;
8
- return Array.isArray(items) ? items : items.value;
6
+ return Array.isArray(options.items) ? options.items : options.items.value;
9
7
  });
10
- const totalHeight = computed(() => itemsRef.value.length * itemHeight);
11
- const visibleCount = computed(() => Math.ceil(containerHeight / itemHeight));
8
+ const itemHeightRef = computed(() => unref(options.itemHeight));
9
+ const containerHeightRef = computed(() => unref(options.containerHeight));
10
+ const overscanRef = computed(() => unref(options.overscan) ?? 3);
11
+ const totalHeight = computed(() => itemsRef.value.length * itemHeightRef.value);
12
12
  const startIndex = computed(() => {
13
- const items = itemsRef.value;
14
- if (items.length === 0) return 0;
15
- const start = Math.floor(scrollTop.value / itemHeight);
16
- return Math.max(0, start - overscan);
13
+ if (itemsRef.value.length === 0) return 0;
14
+ const start = Math.floor(scrollTop.value / itemHeightRef.value);
15
+ return Math.max(0, start - overscanRef.value);
17
16
  });
18
17
  const endIndex = computed(() => {
19
- const items = itemsRef.value;
20
- if (items.length === 0) return 0;
21
- const start = Math.floor(scrollTop.value / itemHeight);
22
- const end = start + visibleCount.value;
23
- return Math.min(items.length, end + overscan);
18
+ if (itemsRef.value.length === 0) return 0;
19
+ const end = Math.ceil((scrollTop.value + containerHeightRef.value) / itemHeightRef.value);
20
+ return Math.min(itemsRef.value.length, end + overscanRef.value);
24
21
  });
25
22
  const visibleItems = computed(() => {
26
- const items = itemsRef.value;
27
- if (items.length === 0) return [];
28
- return items.slice(startIndex.value, endIndex.value);
23
+ return itemsRef.value.slice(startIndex.value, endIndex.value);
29
24
  });
30
- const offsetY = computed(() => startIndex.value * itemHeight);
25
+ const offsetY = computed(() => startIndex.value * itemHeightRef.value);
31
26
  const onScroll = (event) => {
32
27
  const target = event.target;
33
- scrollTop.value = target.scrollTop;
28
+ if (target) scrollTop.value = target.scrollTop;
34
29
  };
35
30
  const scrollToIndex = (index) => {
36
31
  if (containerRef.value) {
37
- const targetScrollTop = index * itemHeight;
32
+ const targetScrollTop = index * itemHeightRef.value;
38
33
  containerRef.value.scrollTop = targetScrollTop;
39
34
  scrollTop.value = targetScrollTop;
40
35
  }
@@ -43,8 +38,8 @@ export function useVirtualScroll(options) {
43
38
  visibleItems,
44
39
  totalHeight,
45
40
  offsetY,
46
- startIndex: computed(() => startIndex.value),
47
- endIndex: computed(() => endIndex.value),
41
+ startIndex,
42
+ endIndex,
48
43
  onScroll,
49
44
  scrollToIndex,
50
45
  containerRef
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/hooks",
3
- "version": "0.1.17",
3
+ "version": "0.1.21",
4
4
  "description": "YH-UI composition hooks",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -24,8 +24,8 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "dayjs": "^1.11.19",
27
- "@yh-ui/locale": "0.1.17",
28
- "@yh-ui/utils": "0.1.17"
27
+ "@yh-ui/locale": "0.1.21",
28
+ "@yh-ui/utils": "0.1.21"
29
29
  },
30
30
  "devDependencies": {
31
31
  "vue": "^3.5.27",
@@ -52,6 +52,8 @@
52
52
  "license": "MIT",
53
53
  "scripts": {
54
54
  "build": "unbuild",
55
- "dev": "unbuild --stub"
55
+ "dev": "unbuild --stub",
56
+ "typecheck": "vue-tsc --noEmit",
57
+ "lint": "eslint ."
56
58
  }
57
59
  }