{
+const ToolBar = ({ updateZoom, currentPage, onPageChange, totalPages }) => {
const [state, setState] = useState({
- currentPage: 1,
- totalPages: 10,
+ currentPage: currentPage,
+ totalPages: totalPages,
zoomLevel: 100,
});
+ const [pageNumberInput, setPageNumberInput] = useState(
+ state.currentPage + 1
+ );
+ const [zoomInput, setZoomInput] = useState(state.zoomLevel);
+
useEffect(() => {
console.log(`Zoom to: ${state.zoomLevel}`);
updateZoom(state.zoomLevel);
}, [state.zoomLevel]);
+ // Update currentPage whenever page prop changes
+ useEffect(() => {
+ console.log(`page number from props ${currentPage}`);
+ setState((prevState) => ({
+ ...prevState,
+ currentPage: currentPage,
+ }));
+ setPageNumberInput(currentPage + 1);
+ }, [currentPage]);
+
const setZoomLevel = (direction) => {
let zoomLevel = state.zoomLevel;
if (direction === 'in') {
@@ -29,17 +44,22 @@ const ToolBar = ({updateZoom}) => {
}));
};
- const scrollToPage = (direction) => {
- let currentPage = state.currentPage;
- if (direction === 'next') {
- currentPage += 1;
- } else {
- currentPage = currentPage - 1;
+ const handleInputChange = (value, type) => {
+ // Remove the "%" symbol from the input value
+ const newValue = parseInt(value.replace('%', ''), 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 + '%'); // Add "%" back to the value
+ }
+ } else if (type === 'page') {
+ // Check if page number is within the allowed range
+ if (newValue >= 1 && newValue <= totalPages) {
+ setPageNumberInput(newValue);
+ }
}
- setState((prevState) => ({
- ...prevState,
- currentPage,
- }));
};
return (
@@ -53,7 +73,27 @@ const ToolBar = ({updateZoom}) => {
- {state.zoomLevel}%
+
+ handleInputChange(e.target.value, 'zoom')}
+ onBlur={(e) => {
+ const newZoomLevel = parseInt(
+ e.target.value.replace('%', ''),
+ 10
+ );
+ if (newZoomLevel !== state.zoomLevel) {
+ updateZoom(newZoomLevel);
+ }
+ }}
+ />
+ %
+
+