Skip to content

Commit

Permalink
🪟 🐛 Properly update builder fields when values change (#10097)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmossman committed Dec 4, 2023
1 parent 61176d5 commit 8ec6d76
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isEqual from "lodash/isEqual";
import toPath from "lodash/toPath";
import { ReactNode, useEffect, useRef } from "react";
import { useController } from "react-hook-form";
import { useController, useWatch } from "react-hook-form";
import { FormattedMessage } from "react-intl";
import { Rnd } from "react-rnd";

Expand Down Expand Up @@ -135,6 +135,9 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
...props
}) => {
const { field, fieldState } = useController({ name: path });
// Must use useWatch instead of field.value from useController because the latter is not updated
// when setValue is called on a parent path in a way that changes the value of this field.
const fieldValue = useWatch({ name: path });
const hasError = !!fieldState.error;

const { label, tooltip } = getLabelAndTooltip(
Expand Down Expand Up @@ -163,7 +166,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
// Call handler in here to make sure it handles new refs
handleScrollToField(elementRef, path, scrollToField, setScrollToField);
}}
checked={field.value as boolean}
checked={fieldValue as boolean}
label={
<>
{label} {tooltip && <InfoTooltip placement="top-start">{tooltip}</InfoTooltip>}
Expand Down Expand Up @@ -207,7 +210,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
}}
className={props.className}
type={props.type}
value={(field.value as string | number | undefined) ?? ""}
value={(fieldValue as string | number | undefined) ?? ""}
error={hasError}
readOnly={readOnly}
adornment={adornment}
Expand All @@ -223,7 +226,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
error={hasError}
withTime={props.type === "date-time"}
onChange={setValue}
value={(field.value as string) ?? ""}
value={(fieldValue as string) ?? ""}
onBlur={field.onBlur}
/>
)}
Expand All @@ -234,7 +237,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
setValue(e.target.value);
}}
className={props.className}
value={(field.value as string) ?? ""}
value={(fieldValue as string) ?? ""}
error={hasError}
readOnly={readOnly}
onBlur={field.onBlur}
Expand Down Expand Up @@ -266,7 +269,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
<CodeEditor
key={path}
automaticLayout
value={field.value || ""}
value={fieldValue || ""}
language="json"
onChange={(val: string | undefined) => {
setValue(val);
Expand All @@ -279,7 +282,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
<div data-testid={`tag-input-${path}`}>
<ArrayField
name={path}
value={(field.value as string[] | undefined) ?? []}
value={(fieldValue as string[] | undefined) ?? []}
itemType={props.itemType}
setValue={setValue}
error={hasError}
Expand All @@ -290,7 +293,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
{props.type === "enum" && (
<EnumField
options={props.options}
value={field.value as string}
value={fieldValue as string}
setValue={setValue}
error={hasError}
data-testid={path}
Expand All @@ -299,7 +302,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
{props.type === "combobox" && (
<ComboBox
options={props.options}
value={field.value as string}
value={fieldValue as string}
onChange={setValue}
error={hasError}
adornment={adornment}
Expand All @@ -318,7 +321,7 @@ const InnerBuilderField: React.FC<BuilderFieldProps> = ({
<MultiComboBox
name={path}
options={props.options}
value={field.value as string[]}
value={fieldValue as string[]}
onChange={setValue}
error={hasError}
data-testid={path}
Expand Down

0 comments on commit 8ec6d76

Please sign in to comment.