({
referance = null,
lockable = false,
toBottomHeight = 300,
downButton,
...props
})
| 8 | import { IMessageListProps, MessageListEvent } from '../type' |
| 9 | |
| 10 | const MessageList: FC<IMessageListProps> = ({ |
| 11 | referance = null, |
| 12 | lockable = false, |
| 13 | toBottomHeight = 300, |
| 14 | downButton, |
| 15 | ...props |
| 16 | }) => { |
| 17 | const [scrollBottom, setScrollBottom] = useState(0) |
| 18 | const [_downButton, setDownButton] = useState(false) |
| 19 | const prevProps = useRef(props) |
| 20 | |
| 21 | const checkScroll = () => { |
| 22 | var e = referance |
| 23 | if (!e || !e.current) return |
| 24 | |
| 25 | if (toBottomHeight === '100%' || (toBottomHeight && scrollBottom < toBottomHeight)) { |
| 26 | e.current.scrollTop = e.current.scrollHeight // scroll to bottom |
| 27 | } else { |
| 28 | if (lockable === true) { |
| 29 | e.current.scrollTop = e.current.scrollHeight - e.current.offsetHeight - scrollBottom |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | useEffect(() => { |
| 35 | if (!referance) return |
| 36 | |
| 37 | if (prevProps.current.dataSource.length !== props.dataSource.length) { |
| 38 | setScrollBottom(getBottom(referance)) |
| 39 | checkScroll() |
| 40 | } |
| 41 | |
| 42 | prevProps.current = props |
| 43 | }, [prevProps, props]) |
| 44 | |
| 45 | const getBottom = (e: any) => { |
| 46 | if (e.current) return e.current.scrollHeight - e.current.scrollTop - e.current.offsetHeight |
| 47 | return e.scrollHeight - e.scrollTop - e.offsetHeight |
| 48 | } |
| 49 | |
| 50 | const onOpen: MessageListEvent = (item, index, event) => { |
| 51 | if (props.onOpen instanceof Function) props.onOpen(item, index, event) |
| 52 | } |
| 53 | |
| 54 | const onDownload: MessageListEvent = (item, index, event) => { |
| 55 | if (props.onDownload instanceof Function) props.onDownload(item, index, event) |
| 56 | } |
| 57 | |
| 58 | const onPhotoError: MessageListEvent = (item, index, event) => { |
| 59 | if (props.onPhotoError instanceof Function) props.onPhotoError(item, index, event) |
| 60 | } |
| 61 | |
| 62 | const onClick: MessageListEvent = (item, index, event) => { |
| 63 | if (props.onClick instanceof Function) props.onClick(item, index, event) |
| 64 | } |
| 65 | |
| 66 | const onTitleClick: MessageListEvent = (item, index, event) => { |
| 67 | if (props.onTitleClick instanceof Function) props.onTitleClick(item, index, event) |
nothing calls this directly
no test coverage detected
searching dependent graphs…