From 3d91644c7db7089911bfcd9af491d49d993f1404 Mon Sep 17 00:00:00 2001 From: akai Date: Wed, 6 Jan 2021 17:32:53 +0800 Subject: [PATCH] fix: [Video] show the play button when the cover is specified --- src/components/Video/index.tsx | 75 +++++++++++++++-------------- src/components/Video/style.less | 4 ++ storybook/stories/Video.stories.tsx | 21 ++++++++ 3 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 storybook/stories/Video.stories.tsx diff --git a/src/components/Video/index.tsx b/src/components/Video/index.tsx index 6c2bf050..263ae770 100644 --- a/src/components/Video/index.tsx +++ b/src/components/Video/index.tsx @@ -1,22 +1,33 @@ -import React, { useState, useRef, useEffect } from 'react'; +import React, { useState, useRef } from 'react'; import clsx from 'clsx'; export interface VideoProps { className?: string; - src: string; + src?: string; cover?: string; duration?: string | number; style?: React.CSSProperties; + videoRef?: React.RefObject; onClick?: (paused: boolean, event: React.MouseEvent) => void; onCoverLoad?: (event: React.SyntheticEvent) => void; } export const Video: React.FC = (props) => { - const { className, src, cover, duration, onClick, onCoverLoad, style, ...other } = props; + const { + className, + src, + cover, + duration, + onClick, + onCoverLoad, + style, + videoRef = useRef(null), + children, + ...other + } = props; const [isPlayed, setIsPlayed] = useState(false); const [paused, setPaused] = useState(true); - const videoRef = useRef(null); function handleClick(e: React.MouseEvent) { setIsPlayed(true); @@ -34,52 +45,42 @@ export const Video: React.FC = (props) => { } } - useEffect(() => { - const video = videoRef.current; - const handlePlay = () => { - setPaused(false); - }; - const handlePause = () => { - setPaused(true); - }; + function handlePlay() { + setPaused(false); + } - if (video) { - video.addEventListener('play', handlePlay); - video.addEventListener('pause', handlePause); - video.addEventListener('ended', handlePause); - } + function handlePause() { + setPaused(true); + } - return () => { - if (video) { - video.removeEventListener('play', handlePlay); - video.removeEventListener('pause', handlePause); - video.removeEventListener('ended', handlePause); - } - }; - }, []); + const hasCover = !isPlayed && !!cover; + const hasDuration = hasCover && !!duration; return (
- {!isPlayed && ( - <> - {cover && } - {duration && {duration}} - - )} + {hasCover && } + {hasDuration && {duration}} + {hasCover && ( + + )}
); -}; +}; \ No newline at end of file diff --git a/src/components/Video/style.less b/src/components/Video/style.less index 2a20daae..06644d77 100644 --- a/src/components/Video/style.less +++ b/src/components/Video/style.less @@ -33,6 +33,10 @@ padding: 0; border: 0; background: transparent; + + &:hover { + cursor: pointer; + } } .Video-playIcon { diff --git a/storybook/stories/Video.stories.tsx b/storybook/stories/Video.stories.tsx new file mode 100644 index 00000000..07d8f9e3 --- /dev/null +++ b/storybook/stories/Video.stories.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { Story, Meta } from '@storybook/react/types-6-0'; + +import { Video, VideoProps } from '../../src'; +// import '../../src/components/Video/style.less'; + +export default { + title: 'Video', + component: Video, +} as Meta; + +const Template: Story = (args) =>