|
1 |
| -import * as React from 'react'; |
2 |
| -import { useRef, useEffect } from 'react'; |
3 | 1 | import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
|
4 | 2 | import raf from 'rc-util/lib/raf';
|
| 3 | +import * as React from 'react'; |
| 4 | +import { useEffect, useRef } from 'react'; |
5 | 5 | import type { GetKey } from '../interface';
|
6 | 6 | import CacheMap from '../utils/CacheMap';
|
7 | 7 |
|
| 8 | +function parseNumber(value: string) { |
| 9 | + const num = parseFloat(value); |
| 10 | + return isNaN(num) ? 0 : num; |
| 11 | +} |
| 12 | + |
8 | 13 | export default function useHeights<T>(
|
9 | 14 | getKey: GetKey<T>,
|
10 | 15 | onItemAdd?: (item: T) => void,
|
@@ -32,8 +37,14 @@ export default function useHeights<T>(
|
32 | 37 | if (element && element.offsetParent) {
|
33 | 38 | const htmlElement = findDOMNode<HTMLElement>(element);
|
34 | 39 | const { offsetHeight } = htmlElement;
|
35 |
| - if (heightsRef.current.get(key) !== offsetHeight) { |
36 |
| - heightsRef.current.set(key, htmlElement.offsetHeight); |
| 40 | + const { marginTop, marginBottom } = getComputedStyle(htmlElement); |
| 41 | + |
| 42 | + const marginTopNum = parseNumber(marginTop); |
| 43 | + const marginBottomNum = parseNumber(marginBottom); |
| 44 | + const totalHeight = offsetHeight + marginTopNum + marginBottomNum; |
| 45 | + |
| 46 | + if (heightsRef.current.get(key) !== totalHeight) { |
| 47 | + heightsRef.current.set(key, totalHeight); |
37 | 48 | }
|
38 | 49 | }
|
39 | 50 | });
|
|
0 commit comments