Skip to content

Commit

Permalink
ORV2-2870 - Regression: Editing year on vehicle will frequently save …
Browse files Browse the repository at this point in the history
…a random year when saving (#1640)

Co-authored-by: GlenAOT <[email protected]>
  • Loading branch information
glen-aot and glen-aot authored Oct 21, 2024
1 parent 63b9ed9 commit e658f38
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
6 changes: 6 additions & 0 deletions frontend/src/common/components/form/CustomFormComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface CustomFormComponentProps<T extends FieldValues> {
className?: string;
disabled?: boolean;
readOnly?: boolean;
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
onWheel?: (event: React.WheelEvent<HTMLInputElement>) => void;
}

/**
Expand Down Expand Up @@ -91,6 +93,8 @@ export const CustomFormComponent = <T extends ORBC_FormTypes>({
className,
disabled,
readOnly,
onFocus,
onWheel,
}: CustomFormComponentProps<T>): JSX.Element => {
const {
control,
Expand Down Expand Up @@ -137,6 +141,8 @@ export const CustomFormComponent = <T extends ORBC_FormTypes>({
inputType={inputType}
disabled={disabled}
readOnly={readOnly}
onFocus={onFocus}
onWheel={onWheel}
/>
);
case "textarea":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,8 @@ export interface CustomOutlinedInputProps<T extends FieldValues> {
inputType?: "number"; // currently only support number, add "date", "email" and other types later
disabled?: boolean;
readOnly?: boolean;
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
onWheel?: (event: React.WheelEvent<HTMLInputElement>) => void;
}

/**
Expand Down Expand Up @@ -50,8 +53,7 @@ export const CustomOutlinedInput = <T extends ORBC_FormTypes>(
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 (
<OutlinedInput
Expand All @@ -65,6 +67,8 @@ export const CustomOutlinedInput = <T extends ORBC_FormTypes>(
readOnly={props.readOnly}
className={customInputClassName}
{...register(props.name, props.rules)}
onFocus={props.onFocus}
onWheel={props.onWheel}
/>
);
};
18 changes: 18 additions & 0 deletions frontend/src/common/helpers/disableMouseWheelInputOnNumberField.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement>,
) => {
const { target } = event;

const handleWheel = (event: WheelEvent) => event.preventDefault();

target.addEventListener("wheel", handleWheel, {
passive: false,
});

target.addEventListener("blur", () =>
target.removeEventListener("wheel", handleWheel),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
invalidYearMin,
requiredMessage,
} from "../../../../common/helpers/validationMessages";
import { disableMouseWheelInputOnNumberField } from "../../../../common/helpers/disableMouseWheelInputOnNumberField";

const FEATURE = "power-unit";

Expand Down Expand Up @@ -173,6 +174,7 @@ export const PowerUnitForm = ({
<CustomFormComponent
type="input"
feature={FEATURE}
onFocus={disableMouseWheelInputOnNumberField}
options={{
name: "year",
rules: {
Expand Down Expand Up @@ -242,7 +244,7 @@ export const PowerUnitForm = ({
)}
className="power-unit-form__field"
/>

<CountryAndProvince
feature={FEATURE}
countryField="countryCode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
invalidYearMin,
requiredMessage,
} from "../../../../common/helpers/validationMessages";
import { disableMouseWheelInputOnNumberField } from "../../../../common/helpers/disableMouseWheelInputOnNumberField";

const FEATURE = "trailer";

Expand Down Expand Up @@ -123,7 +124,7 @@ export const TrailerForm = ({
const handleClose = () => {
navigate(VEHICLES_ROUTES.TRAILER_TAB);
};

const saveButtonText = isEditMode ? "Save" : "Add To Inventory";

return (
Expand Down Expand Up @@ -161,6 +162,7 @@ export const TrailerForm = ({
<CustomFormComponent
type="input"
feature={FEATURE}
onFocus={disableMouseWheelInputOnNumberField}
options={{
name: "year",
rules: {
Expand Down Expand Up @@ -272,7 +274,7 @@ export const TrailerForm = ({
provinceClassName="trailer-form__field"
/>
</div>

<Box className="trailer-form__actions">
<Button
key="cancel-save-vehicle-button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SelectVehicleDropdown } from "./customFields/SelectVehicleDropdown";
import { BANNER_MESSAGES } from "../../../../../../../common/constants/bannerMessages";
import { PermitVehicleDetails } from "../../../../../types/PermitVehicleDetails";
import { selectedVehicleSubtype } from "../../../../../../manageVehicles/helpers/vehicleSubtypes";
import { disableMouseWheelInputOnNumberField } from "../../../../../../../common/helpers/disableMouseWheelInputOnNumberField";
import {
PowerUnit,
Trailer,
Expand Down Expand Up @@ -283,6 +284,7 @@ export const VehicleDetails = ({
<CustomFormComponent
type="input"
feature={feature}
onFocus={disableMouseWheelInputOnNumberField}
options={{
name: "permitData.vehicleDetails.year",
rules: {
Expand Down

0 comments on commit e658f38

Please sign in to comment.