@markup-mitchell
Posted
Hi @anh-vumartell,
I started doing this challenge when I saw your questions because I couldn't remember enough react to answer confidently!
In addition to @Nikuze's comments above, I've discovered the ValidityState()
interface which I'm using in my updatePeople
function:
const updatePeople = (e) => {
if (e.target.validity.valid) {
setPeople(() => e.target.value);
} else {
setPeople(() => 1);
e.target.value = 1;
}
};
I'm sure there's a more elegant way of writing this, but it's what I have for now!
I see you haven't solved the selected % styling yet - here's what I'm doing:
function PercentButton( { percentage, isSelected, handleClick } ) {
let className = "tip__button";
if ( isSelected ) { className += " is-selected"; }
else {
className = "tip__button";
}
return (
<button className={className} onClick={() => handleClick( percentage )}>{percentage}%</button>
);
}
Again, it's not super-elegant, but it does the job.
Marked as helpful
@anh-vumartell
Posted
@markup-mitchell Hi! Thank you for your tips. I really appreciate your time and help. I'll get back to this as soon as I have time to refractor my codes. Kind of busy with two kids :D
@markup-mitchell
Posted
@anh-vumartell haha, same! easier to give feedback than do my own solutions!
@anh-vumartell
Posted
@markup-mitchell : I have found this solution. However there is a glitch which I haven't figured out. So, I have an array of percentages in my App.js const percentages = [5, 10, 15, 25, 50]; I created this function to render a list of buttons. I also use useState to manage the state of a selected button as follows:
const [isSelected, setIsSelected] = useState(true);
//Function to render list of buttons
const renderBtns = percentages.map((unit, i) => (
<button
type="button"
key={i + 1}
value={unit}
isselected={isSelected.toString()}
className={`btn-percent ${isSelected ? "is-selected" : ""} numbers`}
onClick={updateSelectedPercentage}
>
{unit}%
</button>
));
By this way I can conditionally style the % button. The glitch here is when I type in bill or people the style of selected button disappear. SO that it what I should further investigate. Cheers, Anh.