import { useCallback, useEffect, useRef } from 'react'; import withMask from '../api/withMask'; import { resolveInputRef } from '../core'; import isServer from '../utils/isServer'; import { getUnmaskedValue, setUnmaskedValue } from '../utils'; import type { InputRef } from 'antd'; import type { Mask, Options, UnmaskedValueApi } from '../types'; type UseMaskInputAntdReturn = ((input: InputRef | null) => void) & UnmaskedValueApi; interface UseMaskInputOptions { mask: Mask; register?: (element: HTMLElement) => void; options?: Options; } /** * React hook for applying input masks to Ant Design form elements. * * @param props - Configuration object * @param props.mask - The mask pattern to apply * @param props.register - Optional callback that receives the element * @param props.options - Optional mask configuration options * @returns A ref callback function to attach to the Ant Design Input element */ export default function useMaskInputAntd(props: UseMaskInputOptions): UseMaskInputAntdReturn { const { mask, register, options } = props; const ref = useRef(null); const maskRef = useRef(mask); const optionsRef = useRef(options); const maskedElementRef = useRef(null); const unmaskedValue = useCallback(() => getUnmaskedValue(ref.current), []); maskRef.current = mask; optionsRef.current = options; const refCallback = useCallback((input: InputRef | null): void => { if (!input) { ref.current = null; return; } ref.current = resolveInputRef(input.input); if (ref.current && ref.current !== maskedElementRef.current) { withMask(maskRef.current, optionsRef.current)(ref.current); maskedElementRef.current = ref.current; } }, []); useEffect(() => { if (isServer || !ref.current || !register) return; register(ref.current); }, [register]); if (isServer) { const noop = (() => { // server doesn't have dom, so just do nothing }) as unknown as UseMaskInputAntdReturn; return setUnmaskedValue(noop, () => ''); } return setUnmaskedValue(refCallback as UseMaskInputAntdReturn, unmaskedValue); }