Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/ra-core/src/form/FormDataConsumer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,37 @@ describe('FormDataConsumerView', () => {
});
});
});

it('calls its children with the index when inside an ArrayInput', async () => {
let globalIndex;

render(
<AdminContext dataProvider={testDataProvider()}>
<ResourceContextProvider value="posts">
<SimpleForm>
<ArrayInput source="authors">
<SimpleFormIterator>
<FormDataConsumer>
{({ index }) => {
globalIndex = index;
return null;
}}
</FormDataConsumer>
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);

expect(globalIndex).toEqual(undefined);

fireEvent.click(screen.getByLabelText('ra.action.add'));

expect(globalIndex).toEqual(0);

fireEvent.click(screen.getByLabelText('ra.action.add'));

expect(globalIndex).toEqual(1);
});
});
4 changes: 3 additions & 1 deletion packages/ra-core/src/form/FormDataConsumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export const FormDataConsumerView = <
// If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
if (arraySource) {
const scopedFormData = get(formData, arraySource);
result = children({ formData, scopedFormData });
const index = Number(arraySource.match(/\d+$/)?.[0]);
result = children({ formData, scopedFormData, index });
} else {
result = children({ formData });
}
Expand All @@ -98,6 +99,7 @@ export interface FormDataConsumerRenderParams<
> {
formData: TFieldValues;
scopedFormData?: TScopedFieldValues;
index?: number;
}

export type FormDataConsumerRender<
Expand Down