I'm only now switching to styled component and could not find an answer for this fairly simple logic: if the validation for a field fails, it should have red borders.
With classic CSS classes:
const validateMail = (e) => {
let mail = e.target.value;
if (!mailRegex.test(mail)) {
e.target.classList.add("error");
return;
}
e.target.classList.remove("error");
}
How can I do something similar to e.target.classList.add("error")? I assume I would have to, somehow, change this list to something that changes the style of the field to begin with. Here's the complete class of the input styled component that triggers this function:
const Field = styled.div`
display: flex;
flex-direction: column;
`;
const Label = styled.label`
font: ${fonts.body1};
font-size: .75rem;
`;
const Input = styled.input`
font: ${fonts.body1};
padding: 10px;
border-radius: 5px;
outline: none;
border: 1px solid rgba(0, 0, 0, 0.3);
width: 100%;
resize: none;
box-sizing: border-box;
&:focus {
border: 1px solid ${palette.primary_blue};
}
`;
function InputField(props) {
return (
<Field>
<Label htmlFor={"txt" + props.name}>{props.name + ":"}</Label>
<Input name={"txt" + props.name} onKeyUp={props.onKeyUp}></Input>
</Field>
);
}
export default InputField;