'use client'; import { useRef } from 'react'; import { CodeInput, getSegmentCssWidth } from 'rci'; import { useIsFocused } from 'use-is-focused'; import { cn } from '@documenso/ui/lib/utils'; export type PinInputState = 'input' | 'loading' | 'error' | 'success'; export type PinInputProps = { id: string; state: PinInputState; autoFocus?: boolean; onSubmit({ code, input }: { code: string; input: EventTarget & HTMLInputElement }): void; }; const PinInput = ({ id, autoFocus, state, onSubmit }: PinInputProps) => { const inputRef = useRef(null); const focused = useIsFocused(inputRef); const width = getSegmentCssWidth('14px'); return ( { input.value = input.value.replace(/\D+/g, ''); onSubmit({ code: input.value, input }); }} renderSegment={(segment) => { const isCaret = focused && segment.state === 'cursor'; const isSelection = focused && segment.state === 'selected'; const isLoading = state === 'loading'; const isSuccess = state === 'success'; const isError = state === 'error'; const isActive = isSuccess || isError || isSelection || isCaret; return (
); }} /> ); }; export { PinInput };