Skip to content
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

[Typescript] Fix ListContextProvider types #8020

Merged
merged 6 commits into from
Jul 29, 2022
Merged
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
205 changes: 77 additions & 128 deletions examples/demo/src/orders/OrderList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as React from 'react';
import { Fragment, useCallback, useEffect, useState } from 'react';
import { Fragment, useCallback } from 'react';
import {
AutocompleteInput,
BooleanField,
Datagrid,
DateField,
DateInput,
RaRecord,
List,
ListContextProvider,
NullableBooleanInput,
NumberField,
ReferenceInput,
Expand Down Expand Up @@ -87,38 +85,12 @@ const useGetTotals = (filterValues: any) => {

const TabbedDatagrid = () => {
const listContext = useListContext();
const {
data,
filterValues,
setFilters,
displayedFilters,
isLoading,
} = listContext;
const { filterValues, setFilters, displayedFilters } = listContext;
const isXSmall = useMediaQuery<Theme>(theme =>
theme.breakpoints.down('sm')
);
const [ordered, setOrdered] = useState<RaRecord[]>([]);
const [delivered, setDelivered] = useState<RaRecord[]>([]);
const [cancelled, setCancelled] = useState<RaRecord[]>([]);
const totals = useGetTotals(filterValues) as any;

useEffect(() => {
if (isLoading) {
return;
}
switch (filterValues.status) {
case 'ordered':
setOrdered(data);
break;
case 'delivered':
setDelivered(data);
break;
case 'cancelled':
setCancelled(data);
break;
}
}, [data, isLoading, filterValues.status]);

const handleChange = useCallback(
(event: React.ChangeEvent<{}>, value: any) => {
setFilters &&
Expand All @@ -131,13 +103,6 @@ const TabbedDatagrid = () => {
[displayedFilters, filterValues, setFilters]
);

const selectedData =
filterValues.status === 'ordered'
? ordered
: filterValues.status === 'delivered'
? delivered
: cancelled;

return (
<Fragment>
<Tabs
Expand All @@ -161,104 +126,88 @@ const TabbedDatagrid = () => {
</Tabs>
<Divider />
{isXSmall ? (
<ListContextProvider
value={{ ...listContext, data: selectedData }}
>
<MobileGrid data={selectedData} />
</ListContextProvider>
<MobileGrid />
) : (
<>
{filterValues.status === 'ordered' && (
<ListContextProvider
value={{ ...listContext, ids: ordered }}
>
<Datagrid optimized rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
</Datagrid>
</ListContextProvider>
<Datagrid optimized rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
</Datagrid>
)}
{filterValues.status === 'delivered' && (
<ListContextProvider
value={{ ...listContext, ids: delivered }}
>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
<BooleanField
source="returned"
sx={{ mt: -0.5, mb: -0.5 }}
/>
</Datagrid>
</ListContextProvider>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
<BooleanField
source="returned"
sx={{ mt: -0.5, mb: -0.5 }}
/>
</Datagrid>
)}
{filterValues.status === 'cancelled' && (
<ListContextProvider
value={{ ...listContext, ids: cancelled }}
>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
<BooleanField
source="returned"
sx={{ mt: -0.5, mb: -0.5 }}
/>
</Datagrid>
</ListContextProvider>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
<TextField source="reference" />
<CustomerReferenceField />
<ReferenceField
source="customer_id"
reference="customers"
link={false}
label="resources.commands.fields.address"
>
<AddressField />
</ReferenceField>
<NbItemsField />
<NumberField
source="total"
options={{
style: 'currency',
currency: 'USD',
}}
sx={{ fontWeight: 'bold' }}
/>
<BooleanField
source="returned"
sx={{ mt: -0.5, mb: -0.5 }}
/>
</Datagrid>
)}
</>
)}
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-core/src/controller/list/ListContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ListPaginationContext,
usePickPaginationContext,
} from './ListPaginationContext';
import { ListControllerResult } from './useListController';

/**
* Create a List Context and several thematic List subcontext.
Expand Down Expand Up @@ -34,7 +35,13 @@ import {
* @see ListContext
* @see ListFilterContext
*/
export const ListContextProvider = ({ value, children }) => (
export const ListContextProvider = ({
value,
children,
}: {
value: ListControllerResult;
children: React.ReactNode;
}) => (
<ListContext.Provider value={value}>
<ListFilterContext.Provider value={usePickFilterContext(value)}>
<ListSortContext.Provider value={usePickSortContext(value)}>
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/list/useListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export interface ListControllerResult<RecordType extends RaRecord = any> {
onUnselectItems: () => void;
page: number;
perPage: number;
refetch: UseGetListHookValue<RecordType>['refetch'];
refetch: (() => void) | UseGetListHookValue<RecordType>['refetch'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should remove the (() => void)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done to continue the compatibility between ListContext and ChoicesContext for DatagridInput

resource: string;
selectedIds: RecordType['id'][];
setFilters: (
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-core/src/form/choices/ChoicesContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createContext } from 'react';
import { UseGetListHookValue } from '../../dataProvider/useGetList';
import { FilterPayload, RaRecord, SortPayload } from '../../types';

/**
Expand All @@ -24,7 +25,7 @@ export type ChoicesContextValue<RecordType extends RaRecord = any> = {
isLoading: boolean;
page: number;
perPage: number;
refetch: () => void;
refetch: (() => void) | UseGetListHookValue<RecordType>['refetch'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why UseGetListHookValue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Idem -> Done to continue the compatibility between ListContext and ChoicesContext for DatagridInput

resource: string;
selectedChoices: RecordType[];
setFilters: (
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-ui-materialui/src/field/ArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export const ArrayField: FC<ArrayFieldProps> = memo(props => {
sort: { field: null, order: null },
displayedFilters: null,
filterValues: null,
hasCreate: null,
hasNextPage: null,
hasPreviousPage: null,
hideFilter: null,
isFetching: false,
isLoading: false,
Expand All @@ -96,6 +97,7 @@ export const ArrayField: FC<ArrayFieldProps> = memo(props => {
onUnselectItems: null,
page: null,
perPage: null,
refetch: null,
resource,
setFilters: null,
setPage: null,
Expand Down
Loading