ToolJet/frontend/src/Editor/Components/Checkbox.jsx
Kiran Ashok a7092c6580
Feat :: Reducing no of rerenders due to exposed variables (#7473)
* csa reducing rerenders

* perf optimisation : reducing rerenders for rest of the components

* cleanup

* fix :: child components exposed variables

* fix :: table breaking in subcontainer , review fixes

* fix :: modal bug

* fix :: clear text not working for textarea
2023-12-11 15:11:00 +05:30

78 lines
2.2 KiB
JavaScript

import React, { useEffect } from 'react';
export const Checkbox = function Checkbox({
height,
properties,
styles,
fireEvent,
setExposedVariable,
setExposedVariables,
darkMode,
dataCy,
}) {
const defaultValueFromProperties = properties.defaultValue ?? false;
const [defaultValue, setDefaultvalue] = React.useState(defaultValueFromProperties);
const [checked, setChecked] = React.useState(defaultValueFromProperties);
const { label } = properties;
const { visibility, disabledState, checkboxColor, boxShadow } = styles;
const textColor = darkMode && styles.textColor === '#000' ? '#fff' : styles.textColor;
function toggleValue(e) {
const isChecked = e.target.checked;
setChecked(isChecked);
setExposedVariable('value', isChecked);
if (isChecked) {
fireEvent('onCheck');
} else {
fireEvent('onUnCheck');
}
}
useEffect(() => {
const setCheckedAndNotify = async (status) => {
await setExposedVariable('value', status);
if (status) {
fireEvent('onCheck');
} else {
fireEvent('onUnCheck');
}
setChecked(status);
};
const exposedVariables = {
value: defaultValueFromProperties,
setChecked: setCheckedAndNotify,
};
setDefaultvalue(defaultValueFromProperties);
setChecked(defaultValueFromProperties);
setExposedVariables(exposedVariables);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultValueFromProperties, setChecked]);
return (
<div
data-disabled={disabledState}
className="row py-1"
style={{ height, display: visibility ? '' : 'none', boxShadow }}
data-cy={dataCy}
>
<div className="col px-1 py-0 mt-0">
<label className="mx-1 form-check form-check-inline">
<input
className="form-check-input"
type="checkbox"
onClick={(e) => {
toggleValue(e);
}}
defaultChecked={defaultValue}
checked={checked}
style={{ backgroundColor: checked ? `${checkboxColor}` : 'white', marginTop: '1px' }}
/>
<span className="form-check-label" style={{ color: textColor }}>
{label}
</span>
</label>
</div>
</div>
);
};