UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

37 lines (33 loc) 802 B
import { useState, useCallback } from 'react'; import useEvent from "../utils/useEvent"; function usePagination(props) { const [selectedCount, setSelectedCount] = useState(props.selectedCount); const { count } = props; const options = Array(count).fill(null).map((_, i) => i + 1); const handleChange = useCallback(val => { setSelectedCount(val); }, []); const handleNext = useEvent(() => { if (selectedCount === count) { return null; } setSelectedCount(selectedCount + 1); }); const handlePrevious = useEvent(() => { if (selectedCount === 1) { return null; } setSelectedCount(selectedCount - 1); }); return { options, selectedCount, handleChange, handleNext, handlePrevious }; } ; export default usePagination;