-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(connect-react): Fix pre-configured props handling issues #18782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4d54aca
cde427b
899e224
eb4315a
f853fb6
97aa7dd
0a0d9f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
}, [ | ||
component.key, | ||
]); | ||
|
||
// XXX pass this down? (in case we make it hash or set backed, but then also provide {add,remove} instead of set) | ||
const optionalPropIsEnabled = (prop: ConfigurableProp) => enabledOptionalProps[prop.name]; | ||
|
||
|
@@ -275,6 +276,32 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
reloadPropIdx, | ||
]); | ||
|
||
// Auto-enable optional props that have values in configuredProps | ||
// This ensures optional fields with saved values are shown when mounting with pre-configured props | ||
useEffect(() => { | ||
const propsToEnable: Record<string, boolean> = {}; | ||
|
||
for (const prop of configurableProps) { | ||
if (prop.optional) { | ||
const value = configuredProps[prop.name as keyof ConfiguredProps<T>]; | ||
if (value !== undefined) { | ||
propsToEnable[prop.name] = true; | ||
} | ||
} | ||
} | ||
|
||
if (Object.keys(propsToEnable).length > 0) { | ||
setEnabledOptionalProps((prev) => ({ | ||
...prev, | ||
...propsToEnable, | ||
})); | ||
} | ||
}, [ | ||
component.key, | ||
configurableProps, | ||
configuredProps, | ||
]); | ||
|
||
// these validations are necessary because they might override PropInput for number case for instance | ||
// so can't rely on that base control form validation | ||
const propErrors = (prop: ConfigurableProp, value: unknown): string[] => { | ||
|
@@ -355,12 +382,21 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
}; | ||
|
||
useEffect(() => { | ||
// Initialize queryDisabledIdx on load so that we don't force users | ||
// to reconfigure a prop they've already configured whenever the page | ||
// or component is reloaded | ||
updateConfiguredPropsQueryDisabledIdx(_configuredProps) | ||
// Initialize queryDisabledIdx using actual configuredProps (includes parent-passed values in controlled mode) | ||
// instead of _configuredProps which starts empty. This ensures that when mounting with pre-configured | ||
// values, remote options queries are not incorrectly blocked. | ||
updateConfiguredPropsQueryDisabledIdx(configuredProps) | ||
}, [ | ||
_configuredProps, | ||
component.key, | ||
]); | ||
|
||
// Update queryDisabledIdx reactively when configuredProps changes. | ||
// This prevents race conditions where queryDisabledIdx updates synchronously before | ||
// configuredProps completes its state update, causing duplicate API calls with stale data. | ||
useEffect(() => { | ||
updateConfiguredPropsQueryDisabledIdx(configuredProps); | ||
}, [ | ||
configuredProps, | ||
]); | ||
|
||
Comment on lines
+393
to
401
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reactive sync looks right; remove synchronous duplicate to prevent double work. Now that queryDisabledIdx derives from configuredProps reactively, also calling updateConfiguredPropsQueryDisabledIdx inside updateConfiguredProps can double-trigger downstream logic. Apply: const updateConfiguredProps = (configuredProps: ConfiguredProps<T>) => {
setConfiguredProps(configuredProps);
- updateConfiguredPropsQueryDisabledIdx(configuredProps);
updateConfigurationErrors(configuredProps)
}; (lines 365–369)
🤖 Prompt for AI Agents
|
||
useEffect(() => { | ||
|
@@ -386,8 +422,13 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
if (skippablePropTypes.includes(prop.type)) { | ||
continue; | ||
} | ||
// if prop.optional and not shown, we skip and do on un-collapse | ||
// if prop.optional and not shown, we still preserve the value if it exists | ||
// This prevents losing saved values for optional props that haven't been enabled yet | ||
if (prop.optional && !optionalPropIsEnabled(prop)) { | ||
const value = configuredProps[prop.name as keyof ConfiguredProps<T>]; | ||
if (value !== undefined) { | ||
newConfiguredProps[prop.name as keyof ConfiguredProps<T>] = value; | ||
} | ||
continue; | ||
} | ||
const value = configuredProps[prop.name as keyof ConfiguredProps<T>]; | ||
|
@@ -398,7 +439,18 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
} | ||
} else { | ||
if (prop.type === "integer" && typeof value !== "number") { | ||
delete newConfiguredProps[prop.name as keyof ConfiguredProps<T>]; | ||
// Preserve label-value format from remote options dropdowns | ||
// Remote options store values as {__lv: {label: "...", value: ...}} | ||
// For multi-select fields, this will be an array of __lv objects | ||
const isLabelValue = value && typeof value === "object" && "__lv" in value; | ||
const isArrayOfLabelValues = Array.isArray(value) && value.length > 0 && | ||
value.every((item) => item && typeof item === "object" && "__lv" in item); | ||
|
||
if (!(isLabelValue || isArrayOfLabelValues)) { | ||
delete newConfiguredProps[prop.name as keyof ConfiguredProps<T>]; | ||
} else { | ||
newConfiguredProps[prop.name as keyof ConfiguredProps<T>] = value; | ||
} | ||
} else { | ||
newConfiguredProps[prop.name as keyof ConfiguredProps<T>] = value; | ||
} | ||
|
@@ -440,9 +492,6 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
if (prop.reloadProps) { | ||
setReloadPropIdx(idx); | ||
} | ||
if (prop.type === "app" || prop.remoteOptions) { | ||
updateConfiguredPropsQueryDisabledIdx(newConfiguredProps); | ||
} | ||
const errs = propErrors(prop, value); | ||
const newErrors = { | ||
...errors, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Init queryDisabledIdx from configuredProps: good. Also initialize state to undefined to avoid transient block.
The effect correctly derives the initial idx from actual configuredProps. To eliminate the brief 0 default, initialize with undefined.
Apply:
(lines 142–144)
🤖 Prompt for AI Agents