I am working on a countdown watch which have a virtual numpad (given in code as button) and three input (hour,minute and second). now i want that whenever i click input box and type in virtual numpad ,it print the value in that box only.
- there i used three ref to getElement of input and change set their value but dont know how to set for specific box
const inputOne = useRef(null);
const inputTwo = useRef(null);
const inputThree = useRef(null);
const changeValue = (val) => {
inputOne.current.value = setTime({hour: updatedHour + val, minute: updatedMinute, second: updatedSecond});
inputTwo.current.value = setTime({hour: updatedHour, minute: updatedMinute + val, second: updatedSecond});
inputThree.current.value = setTime({hour: updatedHour, minute: updatedMinute, second: updatedSecond + val});
}
const changeTime = (e) => {
setTime({ ...time, [e.target.name]: e.target.value });
};
- this is input fields i use , updatedHour,updatedMinute and updatedSecond is part of setTime state.
<input ref={props.inputOne} onChange={props.changeTime} name="hour" placeholder="Hour" value={props.updatedHour} />
<input ref={props.inputTwo} onChange={props.changeTime} name="minute" placeholder="Minute" value={props.updatedMinute} />
<input ref={props.inputThree} onChange={props.changeTime} name="second" placeholder="Second" value={props.updatedSecond} />
- this is buttons which create virtual keyboard
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('1')}>1</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('2')}>2</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('3')}>3</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('4')}>4</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('5')}>5</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('6')}>6</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('7')}>7</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('8')}>8</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('9')}>9</button>
<button className="grid-item" tpye='button' >Prev</button>
<button className="grid-item" tpye='button' onClick={()=> props.changeValue('0')}>0</button>
<button className="grid-item" tpye='button' >Next</button>