Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: remove initial onChange fires

Brad Barbin9ffb2c98a158
diff --git a/apps/carbon/app/components/Form/Abilities.tsx b/apps/carbon/app/components/Form/Abilities.tsx
index 966ff379cd..72dc9dd5b4 100644
--- a/apps/carbon/app/components/Form/Abilities.tsx
+++ b/apps/carbon/app/components/Form/Abilities.tsx
@@ -58,9 +58,7 @@ const Abilities = ({
) => {
const newValue = selections.map((s) => s.value as string) || [];
setValue(newValue);
- if (onChange && typeof onChange === "function") {
- onChange(selections);
- }
+ onChange?.(selections);
};
const controlledValue = useMemo(
diff --git a/apps/carbon/app/components/Form/Ability.tsx b/apps/carbon/app/components/Form/Ability.tsx
index 0b5464c969..5c0f55fc1b 100644
--- a/apps/carbon/app/components/Form/Ability.tsx
+++ b/apps/carbon/app/components/Form/Ability.tsx
@@ -7,7 +7,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type { getAbilitiesList } from "~/modules/resources";
import { path } from "~/utils/path";
@@ -32,7 +32,7 @@ const Ability = ({
onChange,
...props
}: AbilitySelectProps) => {
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const abilityFetcher =
@@ -61,9 +61,7 @@ const Ability = ({
) => {
const newValue = (selection?.value as string) || undefined;
setValue(newValue);
- if (onChange && typeof onChange === "function") {
- onChange(selection);
- }
+ onChange?.(selection);
};
const controlledValue = useMemo(
@@ -71,14 +69,6 @@ const Ability = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Account.tsx b/apps/carbon/app/components/Form/Account.tsx
index c7db2efd3c..9658c05e6f 100644
--- a/apps/carbon/app/components/Form/Account.tsx
+++ b/apps/carbon/app/components/Form/Account.tsx
@@ -57,9 +57,7 @@ const Account = ({
}) => {
const newValue = (selection.value as string) || undefined;
setValue(newValue);
- if (onChange && typeof onChange === "function") {
- onChange(selection);
- }
+ onChange?.(selection);
};
const controlledValue = useMemo(
diff --git a/apps/carbon/app/components/Form/AccountCategory.tsx b/apps/carbon/app/components/Form/AccountCategory.tsx
index 70350be07f..48acd6285b 100644
--- a/apps/carbon/app/components/Form/AccountCategory.tsx
+++ b/apps/carbon/app/components/Form/AccountCategory.tsx
@@ -6,7 +6,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type {
AccountCategory as AccountCategoryType,
@@ -29,7 +29,7 @@ const AccountCategory = ({
onChange,
...props
}: AccountCategorySelectProps) => {
- const { getInputProps, error, defaultValue } = useField(name);
+ const { getInputProps, error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const accountCategoryFetcher =
@@ -76,14 +76,6 @@ const AccountCategory = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Currency.tsx b/apps/carbon/app/components/Form/Currency.tsx
index 8bd03bc8b6..b0723008fa 100644
--- a/apps/carbon/app/components/Form/Currency.tsx
+++ b/apps/carbon/app/components/Form/Currency.tsx
@@ -6,7 +6,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type { getCurrenciesList } from "~/modules/accounting";
import { path } from "~/utils/path";
@@ -24,7 +24,7 @@ const Currency = ({
onChange,
...props
}: CurrencySelectProps) => {
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const currencyFetcher =
@@ -62,14 +62,6 @@ const Currency = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Customer.tsx b/apps/carbon/app/components/Form/Customer.tsx
index 0f8319efad..c721e81aa4 100644
--- a/apps/carbon/app/components/Form/Customer.tsx
+++ b/apps/carbon/app/components/Form/Customer.tsx
@@ -5,7 +5,7 @@ import {
FormHelperText,
FormLabel,
} from "@chakra-ui/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import { useCustomers } from "~/stores";
import type { SelectProps } from "./Select";
@@ -22,7 +22,7 @@ const Customer = ({
onChange,
...props
}: CustomerSelectProps) => {
- const { getInputProps, error, defaultValue } = useField(name);
+ const { getInputProps, error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const [customers] = useCustomers();
@@ -53,14 +53,6 @@ const Customer = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/CustomerContact.tsx b/apps/carbon/app/components/Form/CustomerContact.tsx
index cb80417476..80e41a9b89 100644
--- a/apps/carbon/app/components/Form/CustomerContact.tsx
+++ b/apps/carbon/app/components/Form/CustomerContact.tsx
@@ -32,7 +32,7 @@ const CustomerContact = ({
...props
}: CustomerContactSelectProps) => {
const initialLoad = useRef(true);
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const customerContactFetcher =
@@ -88,14 +88,6 @@ const CustomerContact = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/CustomerLocation.tsx b/apps/carbon/app/components/Form/CustomerLocation.tsx
index 528aa869ac..83808cd170 100644
--- a/apps/carbon/app/components/Form/CustomerLocation.tsx
+++ b/apps/carbon/app/components/Form/CustomerLocation.tsx
@@ -32,7 +32,7 @@ const CustomerLocation = ({
...props
}: CustomerLocationSelectProps) => {
const initialLoad = useRef(true);
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const customerLocationFetcher =
@@ -87,14 +87,6 @@ const CustomerLocation = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Department.tsx b/apps/carbon/app/components/Form/Department.tsx
index 21a0e39d38..31692b86ec 100644
--- a/apps/carbon/app/components/Form/Department.tsx
+++ b/apps/carbon/app/components/Form/Department.tsx
@@ -6,7 +6,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type { getDepartmentsList } from "~/modules/resources";
import { path } from "~/utils/path";
@@ -24,7 +24,7 @@ const Department = ({
onChange,
...props
}: DepartmentSelectProps) => {
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const departmentFetcher =
@@ -62,14 +62,6 @@ const Department = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Location.tsx b/apps/carbon/app/components/Form/Location.tsx
index 3bf7737812..16c9cc55b6 100644
--- a/apps/carbon/app/components/Form/Location.tsx
+++ b/apps/carbon/app/components/Form/Location.tsx
@@ -6,7 +6,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type { getLocations } from "~/modules/resources";
import { path } from "~/utils/path";
@@ -32,7 +32,7 @@ const Location = ({
onChange,
...props
}: LocationSelectProps) => {
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | null>(name);
const locationFetcher =
@@ -70,14 +70,6 @@ const Location = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Sequence.tsx b/apps/carbon/app/components/Form/Sequence.tsx
index 09fd4bea6d..aa5220bcc8 100644
--- a/apps/carbon/app/components/Form/Sequence.tsx
+++ b/apps/carbon/app/components/Form/Sequence.tsx
@@ -6,7 +6,7 @@ import {
FormLabel,
} from "@chakra-ui/react";
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import type { getSequencesList } from "~/modules/settings";
import { path } from "~/utils/path";
@@ -27,7 +27,7 @@ const Sequence = ({
onChange,
...props
}: SequenceSelectProps) => {
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const sequenceFetcher =
@@ -67,14 +67,6 @@ const Sequence = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/Supplier.tsx b/apps/carbon/app/components/Form/Supplier.tsx
index e019c6611a..817b89a9ae 100644
--- a/apps/carbon/app/components/Form/Supplier.tsx
+++ b/apps/carbon/app/components/Form/Supplier.tsx
@@ -5,7 +5,7 @@ import {
FormHelperText,
FormLabel,
} from "@chakra-ui/react";
-import { useEffect, useMemo } from "react";
+import { useMemo } from "react";
import { useControlField, useField } from "remix-validated-form";
import { useSuppliers } from "~/stores";
import type { SelectProps } from "./Select";
@@ -22,7 +22,7 @@ const Supplier = ({
onChange,
...props
}: SupplierSelectProps) => {
- const { getInputProps, error, defaultValue } = useField(name);
+ const { getInputProps, error } = useField(name);
const [value, setValue] = useControlField<string | undefined>(name);
const [suppliers] = useSuppliers();
@@ -53,14 +53,6 @@ const Supplier = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/SupplierContact.tsx b/apps/carbon/app/components/Form/SupplierContact.tsx
index 104bbafc9b..b1f52a96c7 100644
--- a/apps/carbon/app/components/Form/SupplierContact.tsx
+++ b/apps/carbon/app/components/Form/SupplierContact.tsx
@@ -17,7 +17,7 @@ import type { SelectProps } from "./Select";
type SupplierContactSelectProps = Omit<SelectProps, "options" | "onChange"> & {
supplier?: string;
- onChange?: (supplierContact: SupplierContactType | undefined) => void;
+ onChange?: (supplierContact: SupplierContactType | null) => void;
};
const SupplierContact = ({
@@ -32,7 +32,7 @@ const SupplierContact = ({
...props
}: SupplierContactSelectProps) => {
const initialLoad = useRef(true);
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | null>(name);
const supplierContactFetcher =
@@ -80,7 +80,7 @@ const SupplierContact = ({
(c) => c.id === newValue
);
- onChange(contact);
+ onChange(contact ?? null);
}
};
@@ -90,14 +90,6 @@ const SupplierContact = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
diff --git a/apps/carbon/app/components/Form/SupplierLocation.tsx b/apps/carbon/app/components/Form/SupplierLocation.tsx
index 2818ee337b..af555c8fe5 100644
--- a/apps/carbon/app/components/Form/SupplierLocation.tsx
+++ b/apps/carbon/app/components/Form/SupplierLocation.tsx
@@ -32,7 +32,7 @@ const SupplierLocation = ({
...props
}: SupplierLocationSelectProps) => {
const initialLoad = useRef(true);
- const { error, defaultValue } = useField(name);
+ const { error } = useField(name);
const [value, setValue] = useControlField<string | null>(name);
const supplierLocationFetcher =
@@ -75,11 +75,11 @@ const SupplierLocation = ({
setValue(newValue);
if (onChange && typeof onChange === "function") {
if (newValue === null) onChange(newValue);
- const contact = supplierLocationFetcher.data?.data?.find(
+ const location = supplierLocationFetcher.data?.data?.find(
(c) => c.id === newValue
);
- onChange(contact);
+ onChange(location ?? null);
}
};
@@ -89,14 +89,6 @@ const SupplierLocation = ({
[value, options]
);
- // so that we can call onChange on load
- useEffect(() => {
- if (controlledValue && controlledValue.value === defaultValue) {
- handleChange(controlledValue);
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [controlledValue?.value]);
-
return (
<FormControl isInvalid={!!error}>
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}