Skip to content

Commit e658f38

Browse files
authored
ORV2-2870 - Regression: Editing year on vehicle will frequently save a random year when saving (#1640)
Co-authored-by: GlenAOT <[email protected]>
1 parent 63b9ed9 commit e658f38

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

frontend/src/common/components/form/CustomFormComponents.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface CustomFormComponentProps<T extends FieldValues> {
2424
className?: string;
2525
disabled?: boolean;
2626
readOnly?: boolean;
27+
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
28+
onWheel?: (event: React.WheelEvent<HTMLInputElement>) => void;
2729
}
2830

2931
/**
@@ -91,6 +93,8 @@ export const CustomFormComponent = <T extends ORBC_FormTypes>({
9193
className,
9294
disabled,
9395
readOnly,
96+
onFocus,
97+
onWheel,
9498
}: CustomFormComponentProps<T>): JSX.Element => {
9599
const {
96100
control,
@@ -137,6 +141,8 @@ export const CustomFormComponent = <T extends ORBC_FormTypes>({
137141
inputType={inputType}
138142
disabled={disabled}
139143
readOnly={readOnly}
144+
onFocus={onFocus}
145+
onWheel={onWheel}
140146
/>
141147
);
142148
case "textarea":

frontend/src/common/components/form/subFormComponents/CustomOutlinedInput.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88

99
import { ORBC_FormTypes } from "../../../types/common";
1010
import "./CustomOutlinedInput.scss";
11+
import React from "react";
1112

1213
/**
1314
* Properties of the onrouteBC customized OutlineInput MUI component
@@ -21,6 +22,8 @@ export interface CustomOutlinedInputProps<T extends FieldValues> {
2122
inputType?: "number"; // currently only support number, add "date", "email" and other types later
2223
disabled?: boolean;
2324
readOnly?: boolean;
25+
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
26+
onWheel?: (event: React.WheelEvent<HTMLInputElement>) => void;
2427
}
2528

2629
/**
@@ -50,8 +53,7 @@ export const CustomOutlinedInput = <T extends ORBC_FormTypes>(
5053
updatedInputProps["pattern"] = "[0-9]*";
5154
}
5255

53-
const customInputClassName =
54-
`custom-input ${props.disabled ? "custom-input--disabled" : ""} ${props.invalid ? "custom-input--invalid" : ""}`;
56+
const customInputClassName = `custom-input ${props.disabled ? "custom-input--disabled" : ""} ${props.invalid ? "custom-input--invalid" : ""}`;
5557

5658
return (
5759
<OutlinedInput
@@ -65,6 +67,8 @@ export const CustomOutlinedInput = <T extends ORBC_FormTypes>(
6567
readOnly={props.readOnly}
6668
className={customInputClassName}
6769
{...register(props.name, props.rules)}
70+
onFocus={props.onFocus}
71+
onWheel={props.onWheel}
6872
/>
6973
);
7074
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
3+
// Prevent mouse wheel from changing the value on fields where inputType: "number"
4+
export const disableMouseWheelInputOnNumberField = (
5+
event: React.FocusEvent<HTMLInputElement>,
6+
) => {
7+
const { target } = event;
8+
9+
const handleWheel = (event: WheelEvent) => event.preventDefault();
10+
11+
target.addEventListener("wheel", handleWheel, {
12+
passive: false,
13+
});
14+
15+
target.addEventListener("blur", () =>
16+
target.removeEventListener("wheel", handleWheel),
17+
);
18+
};

frontend/src/features/manageVehicles/components/form/PowerUnitForm.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
invalidYearMin,
3030
requiredMessage,
3131
} from "../../../../common/helpers/validationMessages";
32+
import { disableMouseWheelInputOnNumberField } from "../../../../common/helpers/disableMouseWheelInputOnNumberField";
3233

3334
const FEATURE = "power-unit";
3435

@@ -173,6 +174,7 @@ export const PowerUnitForm = ({
173174
<CustomFormComponent
174175
type="input"
175176
feature={FEATURE}
177+
onFocus={disableMouseWheelInputOnNumberField}
176178
options={{
177179
name: "year",
178180
rules: {
@@ -242,7 +244,7 @@ export const PowerUnitForm = ({
242244
)}
243245
className="power-unit-form__field"
244246
/>
245-
247+
246248
<CountryAndProvince
247249
feature={FEATURE}
248250
countryField="countryCode"

frontend/src/features/manageVehicles/components/form/TrailerForm.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
invalidYearMin,
3030
requiredMessage,
3131
} from "../../../../common/helpers/validationMessages";
32+
import { disableMouseWheelInputOnNumberField } from "../../../../common/helpers/disableMouseWheelInputOnNumberField";
3233

3334
const FEATURE = "trailer";
3435

@@ -123,7 +124,7 @@ export const TrailerForm = ({
123124
const handleClose = () => {
124125
navigate(VEHICLES_ROUTES.TRAILER_TAB);
125126
};
126-
127+
127128
const saveButtonText = isEditMode ? "Save" : "Add To Inventory";
128129

129130
return (
@@ -161,6 +162,7 @@ export const TrailerForm = ({
161162
<CustomFormComponent
162163
type="input"
163164
feature={FEATURE}
165+
onFocus={disableMouseWheelInputOnNumberField}
164166
options={{
165167
name: "year",
166168
rules: {
@@ -272,7 +274,7 @@ export const TrailerForm = ({
272274
provinceClassName="trailer-form__field"
273275
/>
274276
</div>
275-
277+
276278
<Box className="trailer-form__actions">
277279
<Button
278280
key="cancel-save-vehicle-button"

frontend/src/features/permits/pages/Application/components/form/VehicleDetails/VehicleDetails.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { SelectVehicleDropdown } from "./customFields/SelectVehicleDropdown";
2323
import { BANNER_MESSAGES } from "../../../../../../../common/constants/bannerMessages";
2424
import { PermitVehicleDetails } from "../../../../../types/PermitVehicleDetails";
2525
import { selectedVehicleSubtype } from "../../../../../../manageVehicles/helpers/vehicleSubtypes";
26+
import { disableMouseWheelInputOnNumberField } from "../../../../../../../common/helpers/disableMouseWheelInputOnNumberField";
2627
import {
2728
PowerUnit,
2829
Trailer,
@@ -283,6 +284,7 @@ export const VehicleDetails = ({
283284
<CustomFormComponent
284285
type="input"
285286
feature={feature}
287+
onFocus={disableMouseWheelInputOnNumberField}
286288
options={{
287289
name: "permitData.vehicleDetails.year",
288290
rules: {

0 commit comments

Comments
 (0)