I am working on a tip calculator app that has radio inputs for different tip percentages and then a custom input field if you want to add your percentage. The problem I have is that whenever the user clicks one of the radio inputs, the percentage fills in the custom input field. I know this caused by how I set up the value of the custom input field and I was thinking that maybe this could be solved with a conditional statement of some sort where I tell it that if the radio input has been clicked, keep the custom input field as an empty string. I'm just not completely sure how to go about this. This is the code :
import { percentages } from "../data"
export default function SelectTip(props) {
const radioFormElement = percentages.map(percent => {
return (
<div key={percent}
className="select-tip-div">
<input
className="select-tip-radio"
type="radio"
id={percent}
name="tip"
value={percent}
checked={props.formData.tip == {percent}}
onChange={props.handleChange}
/>
<label htmlFor={percent}>{percent}%</label>
</div>)})
return ( <div className='select-tip'> <h2>Select Tip %</h2> <form className="select-tip-form"> {radioFormElement} <input className="custom-tip" type="text" placeholder="Custom" onChange={props.handleChange} name="tip" value={props.formData.tip} /> </form> </div> )}
Heres the repo just in case: https://github.com/andicuevasp/tip-calculator