I would like to create a checkbox with basic checkbox functionality (ie. you click it, the checkbox is selected, you click it again, it is unchecked).
However, this action is not happening (the checkbox remains checked regardless of whether I click it or not).
Here is my code:
export const check: checkComponent = ({ id, checked = false, disabled, onChange }) => {
...
return (
...
<label
className={classNames("inputCheckboxLabel", {
"inputCheckboxLabel--checked": checked,
"inputCheckboxLabel--disabled": disabled,
})}
/>
<input
id={inputId}
type="checkbox"
className="inputCheckboxLabel--input"
checked={checked}
disabled={disabled}
onChange={() => onChange(!checked)}
/>
</div>
)
}
Here is my css:
.inputCheckboxLabel--input {
display: none;
}
.inputCheckboxLabel {
border: 1px solid var(--color-light-shade);
background-color: var(--color-lighter-shade);
display: flex;
justify-content: center;
align-items: center;
padding: 0.125rem;
width: 1.25rem;
height: 1.25rem;
cursor: pointer;
}
.inputCheckboxLabel--checked:before {
content: " ";
width: 100%;
height: 100%;
border-color: var(--color-constructive);
background-color: var(--color-constructive);
}
.inputCheckboxLabel--disabled {
cursor: progress;
}
Here is how the checked
prop is getting passed down:
const [approved, setApproved] = useState(transaction.approved)
...
<InputCheckbox
id={transaction.id}
checked={approved}
disabled={loading}
onChange={async (newValue) => {
await consumerSetTransactionApproval({
transactionId: transaction.id,
newValue,
})
setApproved(newValue)
}}
/>