Skip to content

Commit

Permalink
feat: add renderAudioTitle api
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Oct 11, 2020
1 parent f3d4b10 commit 18fbe03
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ ReactDOM.render(
| onCoverClick | `function(mode,audioLists,audioInfo)` | `-` | audio cover clicked handle |
| onPlayIndexChange | `function(playIndex)` | `-` | audio play index change handle |
| quietUpdate | `boolean` | `false` | [Detail](#bulb-quiet-update) |
| renderAudioTitle | `(audioInfo, isMobile) => ReactNode` | `-` | use `locale.audioTitle` to set `audio` tag title, the api can render custom jsx element for display |

## :bulb: Custom operation ui

Expand Down Expand Up @@ -540,7 +541,7 @@ export interface ReactJkMusicPlayerIcon {
* [A,B] => [C]
* (C) is playing
*/
function App() {
const [audioLists, setAudioLists] = useState([{ musicSrc: 'A' }, { musicSrc: 'B' }])
Expand Down
8 changes: 4 additions & 4 deletions __tests__/tests/__snapshots__/icon.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ exports[`Player custom icon test should render custom icon 1`] = `
>
<span
class="audio-title"
title="audioName "
title="audioName"
>
audioName
audioName
</span>
<section
class="audio-main"
Expand Down Expand Up @@ -338,7 +338,7 @@ exports[`Player custom icon test should render custom icon 1`] = `
<audio
class="music-player-audio"
src="xx"
title="audioName "
title="audioName"
/>
</div>
`;
Expand Down Expand Up @@ -677,7 +677,7 @@ exports[`Player custom icon test should render custom icon in mobile mode 1`] =
<audio
class="music-player-audio"
src="xx"
title="audioName "
title="audioName"
/>
</div>
`;
2 changes: 1 addition & 1 deletion __tests__/tests/__snapshots__/instance.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports[`AudioInstance test should get audio instance 1`] = `
<audio
class="music-player-audio"
src="a"
title=" "
title=""
/>
`;
10 changes: 5 additions & 5 deletions __tests__/tests/__snapshots__/locale.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ exports[`Locale test should render default locale with en_US 1`] = `
<audio
class="music-player-audio"
src=""
title=" "
title=""
/>
</div>
</body>
Expand Down Expand Up @@ -681,7 +681,7 @@ exports[`Locale test should render default locale with en_US 1`] = `
<audio
className="music-player-audio"
src=""
title=" "
title=""
/>
</div>
</Portal>
Expand Down Expand Up @@ -946,7 +946,7 @@ exports[`Locale test should render locale with zh_CN 1`] = `
<audio
class="music-player-audio"
src=""
title=" "
title=""
/>
</div>
<div
Expand Down Expand Up @@ -1112,7 +1112,7 @@ exports[`Locale test should render locale with zh_CN 1`] = `
<audio
class="music-player-audio"
src=""
title=" "
title=""
/>
</div>
</body>
Expand Down Expand Up @@ -1535,7 +1535,7 @@ exports[`Locale test should render locale with zh_CN 1`] = `
<audio
className="music-player-audio"
src=""
title=" "
title=""
/>
</div>
</Portal>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/tests/__snapshots__/player.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ exports[`<ReactJkMusicPlayer/> should render a <ReactJkMusicPlayer/> components
<audio
class="music-player-audio"
src=""
title=" "
title=""
/>
</div>
`;
49 changes: 49 additions & 0 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1503,4 +1503,53 @@ describe('<ReactJkMusicPlayer/>', () => {
wrapper.find('.react-jinke-music-player-mobile-header-right'),
).toHaveLength(0)
})

it('should render default audio title', () => {
const wrapper = mount(
<ReactJkMusicPlayer mode="full" audioLists={[{ name: 'test' }]} />,
)
expect(wrapper.find('.audio-title').text()).toEqual('test')
expect(wrapper.find('.audio-title').prop('title')).toEqual('test')
wrapper.setState({ isMobile: true })
expect(
wrapper.find('.react-jinke-music-player-mobile-header-title').text(),
).toEqual('test')
expect(
wrapper
.find('.react-jinke-music-player-mobile-header-title')
.prop('title'),
).toEqual('test')
})

it('should support custom render audio title', () => {
const renderAudioTitle = (audioInfo) => (
<div className="custom-audio-title">{audioInfo.name}</div>
)
const wrapper = mount(
<ReactJkMusicPlayer
mode="full"
audioLists={[{ name: 'test' }]}
renderAudioTitle={renderAudioTitle}
/>,
)
expect(wrapper.find('.audio-title .custom-audio-title')).toHaveLength(1)
expect(wrapper.find('.audio-title .custom-audio-title').text()).toEqual(
'test',
)

wrapper.setState({ isMobile: true })

expect(
wrapper.find(
'.react-jinke-music-player-mobile-header-title .custom-audio-title',
),
).toHaveLength(1)
expect(
wrapper
.find(
'.react-jinke-music-player-mobile-header-title .custom-audio-title',
)
.text(),
).toEqual('test')
})
})
22 changes: 22 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ const options = {
console.log('onCoverClick: ', mode, audioLists, audioInfo)
},

// custom audio title
// renderAudioTitle(audioInfo) {
// return <a href="#">{audioInfo.name}</a>
// },

// onPlayIndexChange (playIndex) {
// console.log('onPlayIndexChange: ', playIndex);
// }
Expand Down Expand Up @@ -594,6 +599,20 @@ class Demo extends React.PureComponent {
this.updateParams({ playMode: e.target.value })
}

renderCustomAudioTitle = () => {
this.updateParams({
renderAudioTitle: (audioInfo, isMobile) => {
console.log('audioInfo: ', audioInfo, isMobile)
return (
<>
<a href="#">{audioInfo.name}</a>
<span className="tag">Hot</span>
</>
)
},
})
}

renderCustomUI = () => {
return (
<>
Expand Down Expand Up @@ -760,6 +779,9 @@ class Demo extends React.PureComponent {
<button type="button" onClick={this.unmountPlayer}>
unmount player
</button>
<button type="button" onClick={this.renderCustomAudioTitle}>
render custom audio title
</button>
<br />
<br />
<label htmlFor="glassBg">
Expand Down
15 changes: 15 additions & 0 deletions example/example.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ body {
padding: 20px;
}

a {
color: @primary-color;
text-decoration: none;
}

.tag {
display: inline-block;
padding: 2px 10px;
font-size: 12px;
color: #fff;
background-color: @primary-color;
border-radius: 10px;
margin-left: 5px;
}

#root {
padding-bottom: @music-player-panel-height + 20px;
}
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export interface ReactJkMusicPlayerProps {
audioInfo: ReactJkMusicPlayerAudioInfo,
) => void,
onPlayIndexChange?: (playIndex: number) => void
renderAudioTitle?: (audioInfo: ReactJkMusicPlayerAudioInfo, isMobile: boolean) => React.ReactNode | string
}

export default class ReactJkMusicPlayer extends React.PureComponent<
Expand Down
4 changes: 3 additions & 1 deletion src/components/PlayerMobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const PlayerMobile = ({
icon,
locale,
toggleMode,
renderAudioTitle,
}) => (
<div className={cls(prefix, { 'default-bg': !glassBg, 'glass-bg': glassBg })}>
<PlayModeTip
Expand All @@ -40,7 +41,7 @@ const PlayerMobile = ({
<div className={`${prefix}-header group`}>
<div className={`${prefix}-header-left`} />
<div className={`${prefix}-header-title`} title={name}>
{name}
{renderAudioTitle()}
</div>
{toggleMode && (
<div className={`${prefix}-header-right`} onClick={onClose}>
Expand Down Expand Up @@ -123,6 +124,7 @@ const PlayerMobile = ({

PlayerMobile.defaultProps = {
icon: {},
renderAudioTitle: () => {},
}

export default memo(PlayerMobile)
1 change: 1 addition & 0 deletions src/config/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ export default {
audioTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
responsive: PropTypes.bool,
quietUpdate: PropTypes.bool,
renderAudioTitle: PropTypes.func,
}
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
onCoverClick={this.onCoverClick}
locale={locale}
toggleMode={toggleMode}
renderAudioTitle={this.renderAudioTitle}
/>
)}

Expand Down Expand Up @@ -521,7 +522,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
)}
<div className="progress-bar-content">
<span className="audio-title" title={audioTitle}>
{audioTitle}
{this.renderAudioTitle()}
</span>
<section className="audio-main">
<span className="current-time" title={formattedCurrentTime}>
Expand Down Expand Up @@ -693,13 +694,20 @@ export default class ReactJkMusicPlayer extends PureComponent {
}

getAudioTitle = () => {
// 暂时兼容
const { audioTitle } = this.locale || {}
const { name, singer } = this.state
if (typeof audioTitle === 'function' && this.audio) {
return audioTitle(this.getBaseAudioInfo())
}
return audioTitle || `${name} ${singer ? `- ${singer}` : ''}`
return audioTitle || `${name}${singer ? ` - ${singer}` : ''}`
}

renderAudioTitle = () => {
const { isMobile, name } = this.state
if (this.props.renderAudioTitle) {
return this.props.renderAudioTitle(this.getBaseAudioInfo(), isMobile)
}
return isMobile ? name : this.getAudioTitle()
}

toggleAudioLyric = () => {
Expand Down

0 comments on commit 18fbe03

Please sign in to comment.