ant-design/components/input/hooks/useRemovePasswordTimeout.ts
二货爱吃白萝卜 72b449a1a4
docs: update faq (#44198)
* docs: update faq

* chore: update lint

* chore: update lint

* chore: use returnType
2023-08-14 13:32:57 +08:00

38 lines
957 B
TypeScript

import { useEffect, useRef } from 'react';
import type { InputRef } from '../Input';
export default function useRemovePasswordTimeout(
inputRef: React.RefObject<InputRef>,
triggerOnMount?: boolean,
) {
const removePasswordTimeoutRef = useRef<ReturnType<typeof setTimeout>[]>([]);
const removePasswordTimeout = () => {
removePasswordTimeoutRef.current.push(
setTimeout(() => {
if (
inputRef.current?.input &&
inputRef.current?.input.getAttribute('type') === 'password' &&
inputRef.current?.input.hasAttribute('value')
) {
inputRef.current?.input.removeAttribute('value');
}
}),
);
};
useEffect(() => {
if (triggerOnMount) {
removePasswordTimeout();
}
return () =>
removePasswordTimeoutRef.current.forEach((timer) => {
if (timer) {
clearTimeout(timer);
}
});
}, []);
return removePasswordTimeout;
}