I am incorporating a modal that corresponds each element of the object newCompanies
to a specific row:
{newCompanies.map((company, index) => {
return (
<div>
<div
className="side-by-side"
>
<ModalInput
type="text"
id="name"
value={company.name}
onChange={(e) =>
this.editCompanyArr(index, "name", e, "validName")
}
></ModalInput>
<ModalInput
type="text"
id="website"
value={company.website}
onChange={(e) =>
this.editCompanyArr(
index,
"website",
e,
"validWebsite"
)
}
></ModalInput>
<ModalInput
type="text"
id="sector"
value={company.class}
onChange={(e) =>
this.editCompanyArr(
index,
"sector",
e,
"validSector"
)
}
></ModalInput>
ModalInput
is specifically defined in styles.js
:
export const ModalInput = styled.input`
padding: 10px;
margin: 10px 0;
border: 1px solid #f5f6f9;
border-radius: 20px;
background-color: #f5f6f9;
height: 100%;
width: ${props => props.width || 'auto'};
`;
This function is responsible for verifying if a user has inputted a correct value. For example, if numbers are detected within the name field, the value of validName
will be set to false
:
editCompanyArr(i, obj, e, stateObject) {
const { newCompanies, validName } = this.state;
if (e.target.value.match("^[a-zA-Z ]*$") != null) {
this.setState({ [stateObject]: true });
} else {
this.setState({ [stateObject]: false });
}
//edited out more body to update value in newCompanies
}
My concern arises in ensuring that if a user provides an incorrect input for any specific row, only that particular input and not all others receive a red border. How can I achieve this?