I made a custom Checkbox component to handle events differently, code below:
const DoubleClickCheckbox = ({ filter, handleDoubleClick, handleSingleClick }: DCCheckboxProps) => {
const delay = 400;
const timer = useRef(null);
const classes = useStyles();
const flags = useFlags();
useEffect(() => {
return () => {
timer.current && clearTimeout(timer.current);
};
}, []);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const onClick = (e) => {
if (flags.csRequireFilters) {
e.stopPropagation();
handleSingleClick(filter);
return;
}
switch (e.detail) {
case 1:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
timer.current = setTimeout(() => {
handleSingleClick(filter);
}, delay);
break;
case 2:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
clearTimeout(timer.current);
handleDoubleClick(filter);
break;
default:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
clearTimeout(timer.current);
return;
}
};
return (
<div className={classes.filter} onClick={onClick} onKeyDown={() => {}} role="button" tabIndex={0}>
<Checkbox
label={filter.label}
className={classes.checkbox_custom}
checked={filter.selected}
checkedIcon={filter.mandatory ? <Icons.CheckboxDoubleClick /> : <Icons.CheckboxSingleClick />}
/>
</div>
);
};
I would like to dump my eslint-disable-next-line @typescript-eslint/ban-ts-comment and ts-ignore.
If I rewrite my code it reads as below, I get a typescript error:
case 1:
timer.current = setTimeout(() => {
handleSingleClick(filter);
}, delay);
break;
The error is: Type 'Timeout' is not assignable to type 'null'. I have tried various other tricks, for example: const timer = useRef<React.MutableRefObject<ReturnType<typeof setTimeout>>>(null);.
It did not help either.
Please advise
setTimeoutreturns a number that represents an id of that timer. You can use eithernumbertype orNodeJS.Timeoutalongside with null.