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 5 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
12 changes: 9 additions & 3 deletions examples/demo/src/orders/OrderList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ const TabbedDatagrid = () => {
<>
{filterValues.status === 'ordered' && (
<ListContextProvider
value={{ ...listContext, ids: ordered }}
value={{
...listContext,
data: ordered,
}}
>
<Datagrid optimized rowClick="edit">
<DateField source="date" showTime />
Expand Down Expand Up @@ -198,7 +201,7 @@ const TabbedDatagrid = () => {
)}
{filterValues.status === 'delivered' && (
<ListContextProvider
value={{ ...listContext, ids: delivered }}
value={{ ...listContext, data: delivered }}
>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
Expand Down Expand Up @@ -230,7 +233,10 @@ const TabbedDatagrid = () => {
)}
{filterValues.status === 'cancelled' && (
<ListContextProvider
value={{ ...listContext, ids: cancelled }}
value={{
...listContext,
data: cancelled,
}}
>
<Datagrid rowClick="edit">
<DateField source="date" showTime />
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
52 changes: 39 additions & 13 deletions packages/ra-ui-materialui/src/list/filter/FilterListItem.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ import * as React from 'react';
import expect from 'expect';
import { render, cleanup } from '@testing-library/react';

import { ListContextProvider } from 'ra-core';
import { ListContextProvider, ListControllerResult } from 'ra-core';
import { FilterListItem } from './FilterListItem';

const defaultListContext: ListControllerResult = {
data: [],
displayedFilters: null,
filterValues: null,
hasNextPage: false,
hasPreviousPage: false,
hideFilter: (filterName: string) => {},
isFetching: false,
isLoading: false,
onSelect: (ids: any[]) => {},
onToggleItem: (id: any) => {},
onUnselectItems: () => {},
page: 1,
perPage: 10,
refetch: () => {},
resource: 'posts',
selectedIds: [],
setFilters: (filters: any) => {},
setPage: (page: number) => {},
setPerPage: (perPage: number) => {},
setSort: (sort: any) => {},
showFilter: (filterName: string) => {},
sort: { field: '', order: 'ASC' },
total: 0,
};

describe('<FilterListItem/>', () => {
afterEach(cleanup);

it("should display the item label when it's a string", () => {
const { queryByText } = render(
<ListContextProvider value={{ hideFilter: true }}>
<ListContextProvider value={defaultListContext}>
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
</ListContextProvider>
);
Expand All @@ -19,7 +45,7 @@ describe('<FilterListItem/>', () => {

it("should display the item label when it's an element", () => {
const { queryByTestId } = render(
<ListContextProvider value={{ hideFilter: true }}>
<ListContextProvider value={defaultListContext}>
<FilterListItem
label={<span data-testid="123">Foo</span>}
value={{ foo: 'bar' }}
Expand All @@ -31,40 +57,40 @@ describe('<FilterListItem/>', () => {

it('should not appear selected if filterValues is empty', () => {
const { getByText } = render(
<ListContextProvider value={{ hideFilter: true }}>
<ListContextProvider value={defaultListContext}>
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
</ListContextProvider>
);
expect(getByText('Foo').parentElement.dataset.selected).toBe('false');
expect(getByText('Foo').parentElement?.dataset.selected).toBe('false');
});

it('should not appear selected if filterValues does not contain value', () => {
const { getByText } = render(
<ListContextProvider
value={{ hideFilter: true, filterValues: { bar: 'baz' } }}
value={{ ...defaultListContext, filterValues: { bar: 'baz' } }}
>
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
</ListContextProvider>
);
expect(getByText('Foo').parentElement.dataset.selected).toBe('false');
expect(getByText('Foo').parentElement?.dataset.selected).toBe('false');
});

it('should appear selected if filterValues is equal to value', () => {
const { getByText } = render(
<ListContextProvider
value={{ hideFilter: true, filterValues: { foo: 'bar' } }}
value={{ ...defaultListContext, filterValues: { foo: 'bar' } }}
>
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
</ListContextProvider>
);
expect(getByText('Foo').parentElement.dataset.selected).toBe('true');
expect(getByText('Foo').parentElement?.dataset.selected).toBe('true');
});

it('should appear selected if filterValues is equal to value for nested filters', () => {
const { getByText } = render(
<ListContextProvider
value={{
hideFilter: true,
...defaultListContext,
filterValues: {
$and: [
{
Expand Down Expand Up @@ -100,20 +126,20 @@ describe('<FilterListItem/>', () => {
/>
</ListContextProvider>
);
expect(getByText('Foo').parentElement.dataset.selected).toBe('true');
expect(getByText('Foo').parentElement?.dataset.selected).toBe('true');
});

it('should appear selected if filterValues contains value', () => {
const { getByText } = render(
<ListContextProvider
value={{
hideFilter: true,
...defaultListContext,
filterValues: { foo: 'bar', bar: 'baz' },
}}
>
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
</ListContextProvider>
);
expect(getByText('Foo').parentElement.dataset.selected).toBe('true');
expect(getByText('Foo').parentElement?.dataset.selected).toBe('true');
});
});