Skip to content

fix: Also apply sort params to existing fields in a ListGuesser #629

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
72 changes: 70 additions & 2 deletions src/list/ListGuesser.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import type { ReactElement } from 'react';
import React, { useEffect, useState } from 'react';
import {
ArrayField,
BooleanField,
ChipField,
Datagrid,
DatagridBody,
DateField,
EditButton,
EmailField,
FileField,
FunctionField,
ImageField,
List,
NumberField,
ReferenceArrayField,
ReferenceField,
ReferenceManyField,
ReferenceOneField,
RichTextField,
SelectField,
ShowButton,
TextField,
TranslatableFields,
UrlField,
WrapperField,
useResourceContext,
useResourceDefinition,
} from 'react-admin';
Expand All @@ -21,6 +41,7 @@ import type {
IntrospectedListGuesserProps,
ListGuesserProps,
} from '../types.js';
import EnumField from '../field/EnumField';

const getOverrideCode = (schema: Resource, fields: Field[]) => {
let code = `If you want to override at least one field, create a ${schema.title}List component with this content:\n`;
Expand Down Expand Up @@ -61,6 +82,30 @@ export const DatagridBodyWithMercure = (props: DatagridBodyProps) => {
return <DatagridBody {...props} />;
};

const reactAdminFieldTypes = [
ArrayField,
BooleanField,
ChipField,
DateField,
EmailField,
EnumField,
FieldGuesser,
FileField,
FunctionField,
ImageField,
NumberField,
ReferenceArrayField,
ReferenceField,
ReferenceManyField,
ReferenceOneField,
RichTextField,
SelectField,
TextField,
TranslatableFields,
UrlField,
WrapperField,
];

export const IntrospectedListGuesser = ({
fields,
readableFields,
Expand Down Expand Up @@ -97,8 +142,8 @@ export const IntrospectedListGuesser = ({

const displayOverrideCode = useDisplayOverrideCode();

let fieldChildren = children;
if (!fieldChildren) {
let fieldChildren;
if (!children) {
fieldChildren = readableFields.map((field) => {
const orderField = orderParameters.find(
(orderParameter) => orderParameter.split('.')[0] === field.name,
Expand All @@ -116,6 +161,29 @@ export const IntrospectedListGuesser = ({
});

displayOverrideCode(getOverrideCode(schema, readableFields));
} else {
// Add missing sort properties on react-admin fields, that have been added manually in the ListGuesser
fieldChildren = children.map((field: ReactElement): ReactElement => {
if (!reactAdminFieldTypes.includes(field.type)) {
return field;
}

const orderField =
field.props.sortBy ??
orderParameters.find(
(orderParameter) =>
orderParameter.split('.')[0] === field.props.source,
);

const fieldProps = {
...field.props,
key: field.props.source + (orderField ? `-${orderField}` : ''),
sortBy: orderField,
sortable: !!orderField,
};
Comment on lines +178 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this is overriding existing props on the field, e.g. if you had set sortable={false} then this is lost.

Besides, IMO this is not the react-admin way to do things.
As of late react-admin tends to avoid inspecting the children and overriding their props directly as this can lead to unexpected behaviors. Instead, I wonder if the best solution would rather be to update the getOverrideCode to include the sortBy and sortable props in the generated code, which would encourage users to keep those props once they move to providing their own children.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this is overriding existing props on the field, e.g. if you had set sortable={false} then this is lost.

Besides, IMO this is not the react-admin way to do things. As of late react-admin tends to avoid inspecting the children and overriding their props directly as this can lead to unexpected behaviors. Instead, I wonder if the best solution would rather be to update the getOverrideCode to include the sortBy and sortable props in the generated code, which would encourage users to keep those props once they move to providing their own children.

thanks for the input, I will have a look.


return <field.type {...fieldProps} />;
});
}

return (
Expand Down