diff --git a/src/hooks/useWindowHeight.ts b/src/hooks/useWindowHeight.ts new file mode 100644 index 00000000..3cc85cdb --- /dev/null +++ b/src/hooks/useWindowHeight.ts @@ -0,0 +1,12 @@ +import { useState } from 'react'; + +const useWindowHeight = () => { + const [height, setHeight] = useState(window.innerHeight); + window.onresize = () => { + const height = window.innerHeight; + setHeight(height); + }; + return height; +}; + +export default useWindowHeight; diff --git a/src/list/list.tsx b/src/list/list.tsx index c45a1f44..63802f8a 100644 --- a/src/list/list.tsx +++ b/src/list/list.tsx @@ -1,36 +1,17 @@ -import React, { useState, useRef, useEffect, useCallback } from 'react'; +import React, { useRef, useEffect, useCallback } from 'react'; import { TdListProps } from './type'; import useConfig from '../_util/useConfig'; import TLoading from '../loading'; import parseTNode from '../_util/parseTNode'; +import getScrollParent from '../_util/getScrollParent'; +import useWindowHeight from '../hooks/useWindowHeight'; export interface ListProps extends TdListProps { required?: boolean; readonly?: boolean; } -function isElement(node: Element) { - const ELEMENT_NODE_TYPE = 1; - return node.tagName !== 'HTML' && node.tagName !== 'BODY' && node.nodeType === ELEMENT_NODE_TYPE; -} - -const overflowScrollReg = /scroll|auto/i; - -function getScrollParent(el: Element, root = window) { - let node = el; - - while (node && node !== root && isElement(node)) { - const { overflowY } = window.getComputedStyle(node); - if (overflowScrollReg.test(overflowY)) { - return node; - } - node = node.parentNode as Element; - } - - return root; -} - const List: React.FC = (props) => { const { classPrefix } = useConfig(); const { asyncLoading, header, footer, children } = props; @@ -43,14 +24,6 @@ const List: React.FC = (props) => { const root = useRef(null); - const useWindowHeight = () => { - const [height, setHeight] = useState(window.innerHeight); - window.onresize = () => { - const height = window.innerHeight; - setHeight(height); - }; - return height; - }; const height = useWindowHeight(); const onLoadMore = () => {