2024-01-21 23:50:56 +11:00
|
|
|
import { ReactNode, RefObject, useCallback, useRef, useState } from 'react';
|
|
|
|
|
import { useDebounce } from '../hooks/useDebounce';
|
|
|
|
|
|
|
|
|
|
type ConfirmPasswordMatchProps = {
|
|
|
|
|
initialValue: boolean;
|
|
|
|
|
children: (
|
|
|
|
|
match: boolean,
|
|
|
|
|
doMatch: () => void,
|
|
|
|
|
passRef: RefObject<HTMLInputElement>,
|
2026-05-21 23:30:50 -04:00
|
|
|
confPassRef: RefObject<HTMLInputElement>,
|
2024-01-21 23:50:56 +11:00
|
|
|
) => ReactNode;
|
|
|
|
|
};
|
|
|
|
|
export function ConfirmPasswordMatch({ initialValue, children }: ConfirmPasswordMatchProps) {
|
|
|
|
|
const [match, setMatch] = useState(initialValue);
|
|
|
|
|
const passRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const confPassRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
const doMatch = useDebounce(
|
|
|
|
|
useCallback(() => {
|
|
|
|
|
const pass = passRef.current?.value;
|
|
|
|
|
const confPass = confPassRef.current?.value;
|
|
|
|
|
if (!confPass) {
|
|
|
|
|
setMatch(initialValue);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setMatch(pass === confPass);
|
|
|
|
|
}, [initialValue]),
|
|
|
|
|
{
|
|
|
|
|
wait: 500,
|
|
|
|
|
immediate: false,
|
2026-05-21 23:30:50 -04:00
|
|
|
},
|
2024-01-21 23:50:56 +11:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return children(match, doMatch, passRef, confPassRef);
|
|
|
|
|
}
|