Skip to content

Commit 0ab5d80

Browse files
committed
refactor: change inputValue from string to integer
1 parent 07eaa76 commit 0ab5d80

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/components/MusicPlayer.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const MusicPlayer: React.FC<IMusicPlayerProps> = (props) => {
1919
const { playingState, setCurrentQueueSong } = props;
2020
const selectedPlaylist = useAtomValue(selectedPlaylistAtom);
2121
const [isPlaying, setIsPlaying] = useAtom(isPlayingAtom);
22-
const [inputValue, setInputValue] = useState<string>(
23-
(playingState.currentQueueSong + 1).toString()
22+
const [inputValue, setInputValue] = useState(
23+
playingState.currentQueueSong + 1
2424
);
2525

2626
useEvent('pausevideo', () => {
@@ -30,34 +30,30 @@ export const MusicPlayer: React.FC<IMusicPlayerProps> = (props) => {
3030
const onPreviousQueueSong = () => {
3131
if (playingState.currentQueueSong < 1) return;
3232
setCurrentQueueSong(playingState.currentQueueSong - 1);
33-
setInputValue(playingState.currentQueueSong.toString());
33+
setInputValue(playingState.currentQueueSong);
3434
};
3535

3636
const onNextQueueSong = () => {
3737
if (playingState.currentQueueSong === playingState.currentQueue.length - 1)
3838
return;
3939
setCurrentQueueSong(playingState.currentQueueSong + 1);
40-
setInputValue((playingState.currentQueueSong + 2).toString());
40+
setInputValue(playingState.currentQueueSong + 2);
4141
};
4242

4343
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
44-
const newValue = e.target.value;
45-
if (Number.isInteger(Number(newValue))) {
46-
setInputValue(newValue);
47-
}
48-
};
49-
50-
const handleInputBlur = () => {
51-
const userInput = parseInt(inputValue, 10) - 1;
44+
const userInput = parseInt(e.target.value, 10);
5245
if (isNaN(userInput)) {
5346
return;
5447
}
55-
const newQueueSong = Math.max(
56-
0,
57-
Math.min(userInput, playingState.currentQueue.length - 1)
48+
const newInputValue = Math.max(
49+
1,
50+
Math.min(userInput, playingState.currentQueue.length)
5851
);
59-
setCurrentQueueSong(newQueueSong);
60-
setInputValue((newQueueSong + 1).toString());
52+
setInputValue(newInputValue);
53+
};
54+
55+
const handleInputBlur = () => {
56+
setCurrentQueueSong(inputValue - 1);
6157
};
6258

6359
const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {

0 commit comments

Comments
 (0)