Skip to content

Commit 9069e31

Browse files
committed
chore: move-default-id-generation-to-hook
1 parent 4a3cea0 commit 9069e31

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/lib/form/bignumber-field/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useId } from "react";
1+
import React from "react";
22
import SuccessIcon from "../../../assets/svgs/status-icons/success.svg";
33
import WarningIcon from "../../../assets/svgs/status-icons/warning.svg";
44
import ErrorIcon from "../../../assets/svgs/status-icons/error.svg";
@@ -29,15 +29,10 @@ function BigNumberField({
2929
placeholder,
3030
label,
3131
isDisabled,
32-
id: propId,
3332
isReadOnly,
3433
name,
3534
...props
3635
}: Readonly<BigNumberFieldComponentProps>) {
37-
// Generate an ID if one is not provided
38-
const generatedId = useId();
39-
const id = propId || generatedId;
40-
4136
// Use our custom hook to get all the props and state
4237
const {
4338
inputProps,
@@ -48,7 +43,7 @@ function BigNumberField({
4843
descriptionProps,
4944
errorMessageProps,
5045
validationResult,
51-
} = useBigNumberField({ id, isDisabled, placeholder, isReadOnly, ...props });
46+
} = useBigNumberField({ isDisabled, placeholder, isReadOnly, ...props });
5247

5348
return (
5449
<div className={cn("flex w-[278px] flex-col", className)}>
@@ -69,7 +64,6 @@ function BigNumberField({
6964
<>
7065
<Input
7166
{...inputProps}
72-
id="BigNumberField"
7367
aria-errormessage="BigNumberFieldError"
7468
name={name}
7569
className={cn(

src/lib/form/bignumber-field/useBigNumberField.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
FocusEvent,
88
useRef,
99
useCallback,
10+
useId,
1011
} from "react";
1112
import BigNumber from "bignumber.js";
1213

@@ -105,10 +106,14 @@ export function useBigNumberField(props: BigNumberFieldProps) {
105106
isDisabled,
106107
isReadOnly,
107108
isWheelDisabled,
108-
id,
109+
id: propId,
109110
formatOptions,
110111
} = props;
111112

113+
// Generate an ID if one is not provided
114+
const generatedId = useId();
115+
const id = propId || generatedId;
116+
112117
const formatBigNumber = useCallback(
113118
(value: BigNumber) =>
114119
value.toFormat({ ...DEFAULT_FORMAT, ...formatOptions }),
@@ -517,7 +522,7 @@ export function useBigNumberField(props: BigNumberFieldProps) {
517522

518523
// prevent page scrolling when scrolling inside input
519524
useEffect(() => {
520-
const input = document.getElementById("BigNumberField");
525+
const input = document.getElementById(id);
521526

522527
const preventScroll = (e: globalThis.WheelEvent) => {
523528
if (input && document.activeElement === input) {
@@ -531,11 +536,11 @@ export function useBigNumberField(props: BigNumberFieldProps) {
531536
return () => {
532537
input?.removeEventListener("wheel", preventScroll);
533538
};
534-
}, []);
539+
}, [id]);
535540

536541
// Handle wheel events
537542
const handleWheel = (e: WheelEvent<HTMLInputElement>) => {
538-
const input = document.getElementById("BigNumberField");
543+
const input = document.getElementById(id);
539544
if (
540545
isDisabled ||
541546
isReadOnly ||

0 commit comments

Comments
 (0)