mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-18 09:35:20 +02:00
28 lines
746 B
TypeScript
28 lines
746 B
TypeScript
|
import { OnCheckboxChange } from './types';
|
||
|
import { useEffect } from 'react';
|
||
|
|
||
|
const useItemEventHandlers = (rootElement: HTMLDivElement, itemElement: HTMLDivElement, onCheckboxChange: OnCheckboxChange) => {
|
||
|
useEffect(() => {
|
||
|
if (!itemElement) return () => {};
|
||
|
|
||
|
const inputs = itemElement.getElementsByTagName('input');
|
||
|
|
||
|
const mods: HTMLInputElement[] = [];
|
||
|
|
||
|
for (const input of inputs) {
|
||
|
if (input.type === 'checkbox') {
|
||
|
input.addEventListener('change', onCheckboxChange as any);
|
||
|
mods.push(input);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return () => {
|
||
|
for (const input of mods) {
|
||
|
input.removeEventListener('change', onCheckboxChange as any);
|
||
|
}
|
||
|
};
|
||
|
}, [itemElement, rootElement, onCheckboxChange]);
|
||
|
};
|
||
|
|
||
|
export default useItemEventHandlers;
|