Skip to content

Commit

Permalink
fix: AudioPlayer组件ref问题
Browse files Browse the repository at this point in the history
  • Loading branch information
onshinpei committed Jan 12, 2022
1 parent 544b75a commit 6cc1676
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions source/components/AudioPlayer/AudioPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useImperativeHandle } from 'react';
import classNames from 'classnames';
import Slider from '../Slider';
import Icon from '../Icon';
Expand Down Expand Up @@ -51,8 +51,10 @@ export interface AudioPlayerState {
disabled: boolean;
rate: number;
}

const AudioPlayer: React.FC<AudioPlayerProps> = props => {
const InternalAudioPlayer: React.ForwardRefRenderFunction<unknown, AudioPlayerProps> = (
props,
ref,
) => {
const {
prefixCls,
title,
Expand Down Expand Up @@ -81,7 +83,9 @@ const AudioPlayer: React.FC<AudioPlayerProps> = props => {

const [isPlay, setIsPlay] = React.useState<boolean>();
const [isMuted, setIsMuted] = React.useState<boolean>();
const [currentVolume, setCurrentVolume] = React.useState<number>(parseInt(String(props.volume * 100)));
const [currentVolume, setCurrentVolume] = React.useState<number>(
parseInt(String(props.volume * 100)),
);
const [volumeOpen, setVolumeOpen] = React.useState<boolean>(false);
const [rateOpen, setRateOpen] = React.useState<boolean>(false);
const [allTime, setAllTime] = React.useState<number>(0);
Expand All @@ -91,6 +95,12 @@ const AudioPlayer: React.FC<AudioPlayerProps> = props => {

const audioInstance = React.useRef<HTMLAudioElement>();

useImperativeHandle(ref, () => {
return {
audioInstance: audioInstance.current,
};
});

React.useEffect(() => {
controlAudio('changeRate', rate);
if (autoPlay) {
Expand Down Expand Up @@ -344,6 +354,7 @@ const AudioPlayer: React.FC<AudioPlayerProps> = props => {
);
};

const AudioPlayer = React.forwardRef<unknown, AudioPlayerProps>(InternalAudioPlayer);
AudioPlayer.defaultProps = {
prefixCls: 'fishd-audio-player',
className: '',
Expand Down

0 comments on commit 6cc1676

Please sign in to comment.