import { useFormik } from 'formik'; import { ChangeEvent, KeyboardEvent } from 'react'; import { object, number } from 'yup'; import { Button } from '@@/buttons'; import { Input } from '@@/form-components/Input'; interface Values { page: number | ''; } interface Props { onChange(page: number): void; totalPages: number; } export function PageInput({ onChange, totalPages }: Props) { const { handleSubmit, setFieldValue, values, isValid } = useFormik({ initialValues: { page: '' }, onSubmit: async ({ page }) => page && onChange(page), validateOnMount: true, validationSchema: () => object({ page: number().required().max(totalPages).min(1) }), }); return (
); function preventNotNumber(e: KeyboardEvent) { if (e.key.match(/^\D$/)) { e.preventDefault(); } } function handleChange(e: ChangeEvent) { const value = parseInt(e.target.value, 10); setFieldValue('page', Number.isNaN(value) ? '' : value); } }