'use client'; import { Button } from '@headlessui/react'; import { useState, useEffect, useRef } from 'react'; export const Navigator = ({ currentPage, numPages, skipToLocation }: { currentPage: number; numPages: number | undefined; skipToLocation: (location: string | number, shouldPause?: boolean) => void; }) => { const [inputValue, setInputValue] = useState(currentPage.toString()); const inputRef = useRef(null); useEffect(() => { setInputValue(currentPage.toString()); }, [currentPage]); const handleInputChange = (e: React.ChangeEvent) => { // Only allow numbers const value = e.target.value.replace(/[^0-9]/g, ''); setInputValue(value); }; const handleInputConfirm = () => { let page = parseInt(inputValue, 10); if (isNaN(page)) return; const maxPage = numPages || 1; if (page < 1) page = 1; if (page > maxPage) page = maxPage; if (page !== currentPage) { skipToLocation(page, true); } else { setInputValue(page.toString()); // reset input if unchanged } }; const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleInputConfirm(); inputRef.current?.blur(); } }; return (
{/* Page back */} {/* Page number input */}
/ {numPages || 1}
{/* Page forward */}
); }