ant-design/components/input/hooks/useRemovePasswordTimeout.ts

38 lines
957 B
TypeScript
Raw Normal View History

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(
2022-11-09 17:36:49 +08:00
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();
}
2022-11-09 17:36:49 +08:00
return () =>
2022-11-13 14:33:07 +08:00
removePasswordTimeoutRef.current.forEach((timer) => {
2022-11-09 17:36:49 +08:00
if (timer) {
clearTimeout(timer);
}
});
}, []);
return removePasswordTimeout;
}