diff --git a/frontend/src/common/components/form/CustomFormComponents.tsx b/frontend/src/common/components/form/CustomFormComponents.tsx index f62284bc4..9a45225b8 100644 --- a/frontend/src/common/components/form/CustomFormComponents.tsx +++ b/frontend/src/common/components/form/CustomFormComponents.tsx @@ -24,6 +24,8 @@ export interface CustomFormComponentProps { className?: string; disabled?: boolean; readOnly?: boolean; + onFocus?: (event: React.FocusEvent) => void; + onWheel?: (event: React.WheelEvent) => void; } /** @@ -91,6 +93,8 @@ export const CustomFormComponent = ({ className, disabled, readOnly, + onFocus, + onWheel, }: CustomFormComponentProps): JSX.Element => { const { control, @@ -137,6 +141,8 @@ export const CustomFormComponent = ({ inputType={inputType} disabled={disabled} readOnly={readOnly} + onFocus={onFocus} + onWheel={onWheel} /> ); case "textarea": diff --git a/frontend/src/common/components/form/subFormComponents/CustomOutlinedInput.tsx b/frontend/src/common/components/form/subFormComponents/CustomOutlinedInput.tsx index a77065ece..052e087a5 100644 --- a/frontend/src/common/components/form/subFormComponents/CustomOutlinedInput.tsx +++ b/frontend/src/common/components/form/subFormComponents/CustomOutlinedInput.tsx @@ -8,6 +8,7 @@ import { import { ORBC_FormTypes } from "../../../types/common"; import "./CustomOutlinedInput.scss"; +import React from "react"; /** * Properties of the onrouteBC customized OutlineInput MUI component @@ -21,6 +22,8 @@ export interface CustomOutlinedInputProps { inputType?: "number"; // currently only support number, add "date", "email" and other types later disabled?: boolean; readOnly?: boolean; + onFocus?: (event: React.FocusEvent) => void; + onWheel?: (event: React.WheelEvent) => void; } /** @@ -50,8 +53,7 @@ export const CustomOutlinedInput = ( updatedInputProps["pattern"] = "[0-9]*"; } - const customInputClassName = - `custom-input ${props.disabled ? "custom-input--disabled" : ""} ${props.invalid ? "custom-input--invalid" : ""}`; + const customInputClassName = `custom-input ${props.disabled ? "custom-input--disabled" : ""} ${props.invalid ? "custom-input--invalid" : ""}`; return ( ( readOnly={props.readOnly} className={customInputClassName} {...register(props.name, props.rules)} + onFocus={props.onFocus} + onWheel={props.onWheel} /> ); }; diff --git a/frontend/src/common/helpers/disableMouseWheelInputOnNumberField.ts b/frontend/src/common/helpers/disableMouseWheelInputOnNumberField.ts new file mode 100644 index 000000000..548842eca --- /dev/null +++ b/frontend/src/common/helpers/disableMouseWheelInputOnNumberField.ts @@ -0,0 +1,18 @@ +import React from "react"; + +// Prevent mouse wheel from changing the value on fields where inputType: "number" +export const disableMouseWheelInputOnNumberField = ( + event: React.FocusEvent, +) => { + const { target } = event; + + const handleWheel = (event: WheelEvent) => event.preventDefault(); + + target.addEventListener("wheel", handleWheel, { + passive: false, + }); + + target.addEventListener("blur", () => + target.removeEventListener("wheel", handleWheel), + ); +}; diff --git a/frontend/src/features/manageVehicles/components/form/PowerUnitForm.tsx b/frontend/src/features/manageVehicles/components/form/PowerUnitForm.tsx index 9515bd2be..62b1216f9 100644 --- a/frontend/src/features/manageVehicles/components/form/PowerUnitForm.tsx +++ b/frontend/src/features/manageVehicles/components/form/PowerUnitForm.tsx @@ -29,6 +29,7 @@ import { invalidYearMin, requiredMessage, } from "../../../../common/helpers/validationMessages"; +import { disableMouseWheelInputOnNumberField } from "../../../../common/helpers/disableMouseWheelInputOnNumberField"; const FEATURE = "power-unit"; @@ -173,6 +174,7 @@ export const PowerUnitForm = ({ - + { navigate(VEHICLES_ROUTES.TRAILER_TAB); }; - + const saveButtonText = isEditMode ? "Save" : "Add To Inventory"; return ( @@ -161,6 +162,7 @@ export const TrailerForm = ({ - +