require('./toolBar.less'); const React = require('react'); const { useState, useRef, useEffect } = React; const _ = require('lodash'); const ToolBar = ({ updateZoom, currentPage, onPageChange, totalPages }) => { const [state, setState] = useState({ currentPage: currentPage, totalPages: totalPages, zoomLevel: 100, }); const [pageNumberInput, setPageNumberInput] = useState( state.currentPage ); const [zoomInput, setZoomInput] = useState(state.zoomLevel); useEffect(() => { console.log(`Zoom to: ${state.zoomLevel}`); updateZoom(state.zoomLevel); setZoomInput(state.zoomLevel); }, [state.zoomLevel]); // Update currentPage whenever page prop changes useEffect(() => { setState((prevState) => ({ ...prevState, currentPage: currentPage, })); setPageNumberInput(currentPage); }, [currentPage]); const setZoomLevel = (direction) => { let zoomLevel = state.zoomLevel; if (direction === 'in') { zoomLevel += 10; } else { zoomLevel = zoomLevel - 10; } setState((prevState) => ({ ...prevState, zoomLevel, })); }; const handleInputChange = (value, type) => { const newValue = parseInt(value, 10); // Check the type of input (zoom or page) if (type === 'zoom') { // Check if zoom level is within the allowed range if (newValue >= 10 && newValue <= 300) { setZoomInput(newValue); } } else if (type === 'page') { // Check if page number is within the allowed range if (newValue >= 1 && newValue <= totalPages) { setPageNumberInput(newValue); } } }; return (
handleInputChange(e.target.value, 'zoom')} onBlur={(e) => { const newZoomLevel = parseInt( e.target.value, 10 ); if (newZoomLevel !== state.zoomLevel) { updateZoom(newZoomLevel); } }} onKeyPress={(e) => { if (e.key === 'Enter') { e.target.blur(); } }} /> %
handleInputChange(e.target.value, 'page')} onBlur={(e) => { parseInt(pageNumberInput) === state.currentPage || onPageChange(parseInt(pageNumberInput) - 1); }} onKeyPress={(e) => { if (e.key === 'Enter') { e.target.blur(); } }} />
); }; module.exports = ToolBar;