react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Add a meta prop to react-admin reference components

Open Dreamsorcerer opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. I need to pass a boolean flag to the backend when using ReferenceManyField.

Describe the solution you'd like A meta prop that can be used to send data in the request using the optional meta parameter in the dataProvider. i.e. <ReferenceManyField ... meta={orm: true}> would result in a referenceMany() call with meta={orm: true}.

Describe alternatives you've considered As a simple workaround, I've instead done filter={'__meta__': {orm: true}} and then popped it out of the filters in the backend. Though this is a bit of a hack and makes the code less maintainable.

Based on the documentation, it makes it sound like the recommendation is to write a custom component. For such a simple thing, this appears to require copying hundreds of lines of code out of react-admin and maintaining it, which doesn't seem like a viable solution.

Dreamsorcerer avatar Feb 15 '24 22:02 Dreamsorcerer

Thanks for this report.

Indeed, <ReferenceManyField> should accept queryOptions with meta inside. For now, you can use the beforeGetManyReference callback from the withLifecycleCallbacks helper to add meta:

const dataProvider = withLifecycleCallbacks(baseDataProvider, [
    {
        resource: 'comments',
        beforeGetManyReference: async params => {
            return { ...params, meta: { orm: true } };
        },
    },
]);

Feel free to open a PR if you come up with a fix of your own. 🙏 (the <ReferenceOneField> component is a good start).

adguernier avatar Feb 16 '24 16:02 adguernier

Running into this once again. While coding I intuitively add it but I seem to keep forgetting there isn't a queryOptions in ReferenceManyField... It's unfortunate that RA4 is not going to have this feature, since all PR's need to have the next branch as base. Instead I will use useGetManyReference and iterate the data.

christiaanwesterbeek avatar Apr 04 '24 15:04 christiaanwesterbeek

Thanks for this report.

Indeed, <ReferenceManyField> should accept queryOptions with meta inside. For now, you can use the beforeGetManyReference callback from the withLifecycleCallbacks helper to add meta:

const dataProvider = withLifecycleCallbacks(baseDataProvider, [
    {
        resource: 'comments',
        beforeGetManyReference: async params => {
            return { ...params, meta: { orm: true } };
        },
    },
]);

Feel free to open a PR if you come up with a fix of your own. 🙏 (the <ReferenceOneField> component is a good start).

Hey i will like to ask if this issue is still open and if i can work on it?

AustineChi avatar Jul 13 '24 10:07 AustineChi

#9750 is labelled as closing this issue, but seems to have been merged without it actually closing the issue...

Dreamsorcerer avatar Jul 13 '24 10:07 Dreamsorcerer

Indeed, the #9750 adds the possibility to pass queryOptions to the <ReferenceManyField> component as you can see in the WithMeta story. Thanks for pointing this @Dreamsorcerer. So I'm closing this issue.

Thanks for your interest @AustineChi. Feel free to explore other issues. Any contribution is welcome!

adguernier avatar Jul 16 '24 09:07 adguernier