parent
cb40933ad7
commit
cc71c39b88
15 changed files with 129 additions and 29 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,75 @@ |
||||
import { use, useEffect, useRef, useState } from "react"; |
||||
|
||||
const KEY_ENTER='q'; |
||||
const KEY_BACKSPACE='a'; |
||||
|
||||
export default function NumPad({onSend}){ |
||||
|
||||
const [input, _setInput]=useState(); |
||||
const refInput=useRef(); |
||||
const setInput=(valueFunc)=>{ |
||||
if(typeof valueFunc==='function'){ |
||||
_setInput(valueFunc(refInput.current)); |
||||
refInput.current=valueFunc(refInput.current); |
||||
}else{ |
||||
_setInput(valueFunc); |
||||
refInput.current=valueFunc; |
||||
} |
||||
} |
||||
|
||||
const refAudio=useRef([]); |
||||
|
||||
function onkeydown(e){ |
||||
// console.log(e.key); |
||||
|
||||
if(e.key===KEY_ENTER){ |
||||
|
||||
if(refInput.current && refInput.current.length>0){ |
||||
const num=parseInt(refInput.current); |
||||
if(num>=1 && num<=24) onSend(num); |
||||
setInput(()=>''); |
||||
} |
||||
return; |
||||
}else if(e.key===KEY_BACKSPACE || e.key==='Backspace' || e.key==='Delete'){ |
||||
setInput((prev)=>prev?.slice(0,-1)); |
||||
return; |
||||
} |
||||
|
||||
const numKey = parseInt(e.key); |
||||
if(isNaN(numKey) || numKey < 0 || numKey > 9) return; // Ignore non-numeric keys |
||||
|
||||
refAudio.current[numKey]?.play(); |
||||
setInput((prev)=>`${prev||''}${e.key}`); |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
useEffect(() => { |
||||
|
||||
refAudio.current = [...Array(10).keys()].map(index =>{ |
||||
const file=`assets/number/${index}.mp3`; |
||||
// console.log(`Loading audio file: ${file}`); |
||||
return new Audio(file); |
||||
}); |
||||
|
||||
},[]); |
||||
|
||||
|
||||
useEffect(()=>{ |
||||
|
||||
window.onkeydown=onkeydown; |
||||
|
||||
return () => { |
||||
window.onkeydown=null; // Clean up the event listener |
||||
}; |
||||
|
||||
},[]); |
||||
|
||||
return ( |
||||
<div className="flex flex-col justify-center bg-yellow-400 text-4xl overflow-hidden"> |
||||
{input} |
||||
</div> |
||||
); |
||||
|
||||
} |
||||
Loading…
Reference in new issue