/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import AddIcon from '@mui/icons-material/Add'; import RemoveIcon from '@mui/icons-material/Remove'; import { Box, IconButton, InputAdornment } from '@mui/material'; import { WidgetProps } from '@rjsf/utils'; import React, { useCallback } from 'react'; import { StyledTextField } from '../components'; export const NumberWidget: React.FC = (props) => { const { id, value, required, readonly, disabled, autofocus, placeholder, onBlur, onFocus, onChange, schema, } = props; const numericValue = typeof value === 'number' ? value : (value ? Number(String(value)) : undefined); const step = schema.multipleOf || 1; const min = typeof schema.minimum === 'number' ? schema.minimum : undefined; const max = typeof schema.maximum === 'number' ? schema.maximum : undefined; const handleChange = useCallback((event: React.ChangeEvent) => { const newValue = event.target.value; if (newValue === '') { onChange(undefined); } else { const parsedValue = Number(newValue); if (!isNaN(parsedValue)) { onChange(parsedValue); } } }, [onChange]); const handleIncrement = useCallback(() => { if (disabled || readonly) return; const currentValue = numericValue || 0; const newValue = currentValue + step; if (max === undefined || newValue <= max) { onChange(newValue); } }, [numericValue, step, max, onChange, disabled, readonly]); const handleDecrement = useCallback(() => { if (disabled || readonly) return; const currentValue = numericValue || 0; const newValue = currentValue - step; if (min === undefined || newValue >= min) { onChange(newValue); } }, [numericValue, step, min, onChange, disabled, readonly]); const handleBlur = useCallback((event: React.FocusEvent) => { onBlur(id, event.target.value); }, [onBlur, id]); const handleFocus = useCallback((event: React.FocusEvent) => { onFocus(id, event.target.value); }, [onFocus, id]); const isDecrementDisabled = disabled || readonly || (min !== undefined && numericValue !== undefined && numericValue <= min); const isIncrementDisabled = disabled || readonly || (max !== undefined && numericValue !== undefined && numericValue >= max); return ( theme.palette.action.hover, '&:hover': { backgroundColor: (theme) => theme.palette.action.focus, }, '&:disabled': { backgroundColor: 'transparent', opacity: 0.5, }, '& .MuiSvgIcon-root': { fontSize: '0.75rem', }, }} > theme.palette.action.hover, '&:hover': { backgroundColor: (theme) => theme.palette.action.focus, }, '&:disabled': { backgroundColor: 'transparent', opacity: 0.5, }, '& .MuiSvgIcon-root': { fontSize: '0.75rem', }, }} > ), }, }} sx={{ '& input[type="number"]::-webkit-outer-spin-button': { WebkitAppearance: 'none', margin: 0, }, '& input[type="number"]::-webkit-inner-spin-button': { WebkitAppearance: 'none', margin: 0, }, }} /> ); };