Expose useFieldArray replace and append methods in ExpandableInput to parent components#1074
Expose useFieldArray replace and append methods in ExpandableInput to parent components#1074
Conversation
… parent components Signed-off-by: basseche <bassel.el-cheikh_externe@rte-france.com>
📝 WalkthroughWalkthroughThe Changes
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx (1)
33-33:⚠️ Potential issue | 🟡 MinorGuard against appending
undefinedwheninitialValueis not provided.
initialValueis optional (Line 33), but Line 110 always callsappend(initialValue). Since the component's public API allows omittinginitialValue, passingundefinedtoappend()will add undefined into the field array.Proposed guard
- onClick={() => append(initialValue)} + onClick={() => { + if (initialValue === undefined) { + return; + } + append(initialValue); + }}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx` at line 33, ExpandableInput's optional prop initialValue can be undefined but the component unconditionally calls append(initialValue) (useFieldArray.append), which will add undefined to the field array; update ExpandableInput so you only call append when a real value exists (e.g. if (initialValue !== undefined) append(initialValue)) or provide a safe default (e.g. append(initialValue ?? {}) depending on the expected type). Locate the append(...) invocation in the ExpandableInput component and wrap it with a guard or substitute a sensible default to prevent appending undefined.
🧹 Nitpick comments (1)
src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx (1)
47-63: Type and export the imperative ref contract.Line 48 and lines 76-77 currently leave the new parent API effectively untyped (
ref/any), which makesreplaceItemsandappendItemeasy to misuse from consumers. This is inconsistent with other properly-typedforwardRefcomponents in the codebase (e.g.,CustomAGGrid).Proposed typed ref handle
+export interface ExpandableInputHandle<TItem = unknown> { + replaceItems: (newItems: TItem[]) => void; + appendItem: (newItem: TItem) => void; +} + -export const ExpandableInput = forwardRef( +export const ExpandableInput = forwardRef<ExpandableInputHandle, Readonly<ExpandableInputProps>>( ( { @@ alignItems = 'stretch', // default value for a flex container watchProps = true, disabled = false, disabledDeletion, }: Readonly<ExpandableInputProps>, - ref + ref ) => { const { fields: values, append, remove, replace, } = useFieldArray({ name, }); useImperativeHandle( ref, () => ({ - replaceItems: (newItems: any) => replace(newItems), - appendItem: (newItem: any) => append(newItem), + replaceItems: (newItems) => replace(newItems), + appendItem: (newItem) => append(newItem), }), [append, replace] );🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx` around lines 47 - 63, The component ExpandableInput currently accepts an untyped ref (ref: any) which allows misuse of its imperative methods like replaceItems and appendItem; define and export a typed ref interface (e.g., ExpandableInputHandle) listing the public methods (replaceItems, appendItem, etc.), update the component signature to use forwardRef<ExpandableInputHandle, ExpandableInputProps> and type the ref parameter accordingly, and ensure any internal use of the ref is cast/annotated to that interface so consumers get proper TypeScript autocompletion and type-safety when calling replaceItems and appendItem.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx`:
- Line 33: ExpandableInput's optional prop initialValue can be undefined but the
component unconditionally calls append(initialValue) (useFieldArray.append),
which will add undefined to the field array; update ExpandableInput so you only
call append when a real value exists (e.g. if (initialValue !== undefined)
append(initialValue)) or provide a safe default (e.g. append(initialValue ?? {})
depending on the expected type). Locate the append(...) invocation in the
ExpandableInput component and wrap it with a guard or substitute a sensible
default to prevent appending undefined.
---
Nitpick comments:
In `@src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx`:
- Around line 47-63: The component ExpandableInput currently accepts an untyped
ref (ref: any) which allows misuse of its imperative methods like replaceItems
and appendItem; define and export a typed ref interface (e.g.,
ExpandableInputHandle) listing the public methods (replaceItems, appendItem,
etc.), update the component signature to use forwardRef<ExpandableInputHandle,
ExpandableInputProps> and type the ref parameter accordingly, and ensure any
internal use of the ref is cast/annotated to that interface so consumers get
proper TypeScript autocompletion and type-safety when calling replaceItems and
appendItem.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 62e35ede-fbc0-46a4-9acb-aaaa7db8d2dc
📒 Files selected for processing (1)
src/components/inputs/reactHookForm/expandableInput/ExpandableInput.tsx



PR Summary
Expose useFieldArray replace and append methods in ExpandableInput to parent components
so it state can be updated correctly