mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-27 10:32:58 +02:00
38 lines
1023 B
TypeScript
38 lines
1023 B
TypeScript
import { OnInputChange } from './types';
|
|
import { useEffect } from 'react';
|
|
|
|
const useItemEventHandlers = (rootElement: HTMLDivElement, itemElement: HTMLDivElement, onInputChange: OnInputChange) => {
|
|
useEffect(() => {
|
|
if (!itemElement) return () => {};
|
|
|
|
const inputs = itemElement.getElementsByTagName('input');
|
|
|
|
const checkboxes: HTMLInputElement[] = [];
|
|
const textInputs: HTMLInputElement[] = [];
|
|
|
|
for (const input of inputs) {
|
|
if (input.type === 'checkbox') {
|
|
input.addEventListener('change', onInputChange as any);
|
|
checkboxes.push(input);
|
|
}
|
|
|
|
if (input.type === 'text') {
|
|
input.addEventListener('change', onInputChange as any);
|
|
textInputs.push(input);
|
|
}
|
|
}
|
|
|
|
return () => {
|
|
for (const input of checkboxes) {
|
|
input.removeEventListener('change', onInputChange as any);
|
|
}
|
|
|
|
for (const input of textInputs) {
|
|
input.removeEventListener('change', onInputChange as any);
|
|
}
|
|
};
|
|
}, [itemElement, rootElement, onInputChange]);
|
|
};
|
|
|
|
export default useItemEventHandlers;
|