diff --git a/packages/discuz-design/components/audio/__examples__/mini/index.jsx b/packages/discuz-design/components/audio/__examples__/mini/index.jsx index 27f6c227fe7871967752c579f02b93d5fbb653db..0b21f6b217f6e5eb7d74c4c16ae8b8896c848b1e 100644 --- a/packages/discuz-design/components/audio/__examples__/mini/index.jsx +++ b/packages/discuz-design/components/audio/__examples__/mini/index.jsx @@ -3,12 +3,12 @@ import { View } from '@tarojs/components'; import Audio from '../../index'; export default function FlexExample() { - const [src, setSrc] = useState('https://demo.dj63.com//2016/CLUB商业/club中文/20140101/夏日香气_主题曲_左右为难_电视剧歌曲_韩语.mp3'); - const [audio, setAudio] = useState(null); + const [src, setSrc] = useState( + 'https://demo.dj63.com//2016/CLUB商业/club中文/20140101/夏日香气_主题曲_左右为难_电视剧歌曲_韩语.mp3', + ); + const audioRef = React.useRef(); const onClick = () => { - console.log(audio); - setSrc('https://demo.dj63.com//2016/串烧舞曲/20150101/佛山DJ宅神[磁性_男声_女声]歌曲连版串烧[专辑1].mp3'); }; @@ -16,14 +16,17 @@ export default function FlexExample() { console.log('正在播放'); }; + // 获取当前播放时间 const getCurrentTime = () => { - console.log(audio.state.currentTime); + // @ts-ignore + const audioState = audioRef.current.getState(); + console.log(audioState.currentTime); }; return ( 点击切换音频 - ); diff --git a/packages/discuz-design/components/audio/__examples__/web/index.jsx b/packages/discuz-design/components/audio/__examples__/web/index.jsx index 7344446eba332e743aa2c65ffcccf1864e1582b5..8a78fb49a8148434740015e0b6fd99289b45a860 100644 --- a/packages/discuz-design/components/audio/__examples__/web/index.jsx +++ b/packages/discuz-design/components/audio/__examples__/web/index.jsx @@ -2,12 +2,12 @@ import React, { useState } from 'react'; import Audio from '../../index'; export default function FlexExample() { - const [src, setSrc] = useState('https://demo.dj63.com//2016/CLUB商业/club中文/20140101/夏日香气_主题曲_左右为难_电视剧歌曲_韩语.mp3'); - const [audio, setAudio] = useState(null); + const [src, setSrc] = useState( + 'https://demo.dj63.com//2016/CLUB商业/club中文/20140101/夏日香气_主题曲_左右为难_电视剧歌曲_韩语.mp3', + ); + const audioRef = React.useRef(); const onClick = () => { - console.log(audio); - setSrc('https://demo.dj63.com//2016/串烧舞曲/20150101/佛山DJ宅神[磁性_男声_女声]歌曲连版串烧[专辑1].mp3'); }; @@ -15,14 +15,17 @@ export default function FlexExample() { console.log('正在播放'); }; + // 获取当前播放时间 const getCurrentTime = () => { - console.log(audio.state.currentTime); + // @ts-ignore + const audioState = audioRef.current.getState(); + console.log(audioState.currentTime); }; return (
点击切换音频
-
); diff --git a/packages/discuz-design/components/audio/index.tsx b/packages/discuz-design/components/audio/index.tsx index 5dda44f6430d0a9869f462501f6522ac9861eb89..6c504079e72d2507d02be5eca37574c9ac136409 100644 --- a/packages/discuz-design/components/audio/index.tsx +++ b/packages/discuz-design/components/audio/index.tsx @@ -21,8 +21,20 @@ export default class Audio extends baseComponentFactory { + this.setState({ childrenRef: ref }); + }; + + // 获取子组件实例的state值 + getState = () => this.state.childrenRef.state; + render() { const { RenderComponent } = this; - return {this.props.children}; + return ( + + {this.props.children} + + ); } } diff --git a/packages/discuz-design/components/audio/interface.ts b/packages/discuz-design/components/audio/interface.ts index 6407f893a1709eba9776b2ecd1065522918039ec..9649eec374c4bd6dd09d852de3ba61818045249f 100644 --- a/packages/discuz-design/components/audio/interface.ts +++ b/packages/discuz-design/components/audio/interface.ts @@ -1,45 +1,45 @@ import { StyledProps } from 'utils/_type/StyledProps'; export interface StateInterface { - audioCtx: any; - audioImg: string; - currentTime: number; - duration: number; + audioCtx: any + audioImg: string + currentTime: number + duration: number + childrenRef: any } export interface AudioProps extends StyledProps { /** * 音频地址 */ - src: string; + src: string, /** * 是否循环播放 * @default false */ - loop?: boolean; + loop?: boolean, /** * 音频播放错误回调 * @default () => {} */ - onError?: () => void; + onError?: () => void, /** * 音频播放回调 * @default () => {} */ - onPlay?: () => void; + onPlay?: () => void, /** * 音频播放暂停回调 * @default () => {} */ - onPause?: () => void; + onPause?: () => void, /** * 音频播放完成回调 * @default () => {} */ - onEnded?: () => void; + onEnded?: () => void, /** * 获取音频实例 - * @todo 此处定义后期需要调整,不建议使用onRef这种自定义的方式获取组件实例 */ - onRef?: (any) => any; + ref?: any } diff --git a/packages/discuz-design/components/audio/layouts/mini/index.tsx b/packages/discuz-design/components/audio/layouts/mini/index.tsx index 535c5cc599a646a30a8809c48c5e201852b4578a..9980283fdf8768eb6c129fd07b6245ac648672a0 100644 --- a/packages/discuz-design/components/audio/layouts/mini/index.tsx +++ b/packages/discuz-design/components/audio/layouts/mini/index.tsx @@ -23,9 +23,6 @@ export class AudioMiniLayout extends Component { } componentDidMount() { - const { onRef = () => {} } = this.props; - onRef(this); - const { audioCtx, audioImg } = this.state; audioCtx.src = this.props.src; diff --git a/packages/discuz-design/components/audio/layouts/web/index.tsx b/packages/discuz-design/components/audio/layouts/web/index.tsx index 84242ac616c7e82157c75e71f513be180b4a43ed..00cd78ca52a148484497dd0eba25d9243cc799db 100644 --- a/packages/discuz-design/components/audio/layouts/web/index.tsx +++ b/packages/discuz-design/components/audio/layouts/web/index.tsx @@ -22,9 +22,6 @@ export class AudioWebLayout extends Component { } componentDidMount() { - const { onRef = () => {} } = this.props; - onRef(this); - const audioCtx = createInnerAudioContext(); this.audioMount(audioCtx); this.setState({ audioCtx });