Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('useDeleteWithConfirmController', () => {

const button = await screen.findByText('Delete');
fireEvent.click(button);
waitFor(() => expect(receivedMeta).toEqual('metadata'), {
await waitFor(() => expect(receivedMeta).toEqual('metadata'), {
timeout: 1000,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import expect from 'expect';
import { Route, Routes } from 'react-router';
import { fireEvent, screen, render, waitFor } from '@testing-library/react';

import { testDataProvider } from '../../dataProvider';
import {
testDataProvider,
UndoableMutation,
useTakeUndoableMutation,
} from '../../dataProvider';
import { CoreAdminContext } from '../../core';
import useDeleteWithUndoController, {
UseDeleteWithUndoControllerParams,
Expand All @@ -22,6 +26,12 @@ describe('useDeleteWithUndoController', () => {
}),
});

let takeMutation: () => UndoableMutation | void;
const MutationTrigger = () => {
takeMutation = useTakeUndoableMutation();
return null;
};

const MockComponent = () => {
const { handleDelete } = useDeleteWithUndoController({
record: { id: 1 },
Expand All @@ -38,13 +48,20 @@ describe('useDeleteWithUndoController', () => {
<Routes>
<Route path="/" element={<MockComponent />} />
</Routes>
<MutationTrigger />
</CoreAdminContext>
</TestMemoryRouter>
);

const button = await screen.findByText('Delete');
fireEvent.click(button);
waitFor(() => expect(receivedMeta).toEqual('metadata'), {

// Trigger the mutation.
await waitFor(() => new Promise(resolve => setTimeout(resolve, 0)));
const mutation = takeMutation();
if (mutation) mutation({ isUndo: false });

await waitFor(() => expect(receivedMeta).toEqual('metadata'), {
timeout: 1000,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { useState, useCallback, ReactElement } from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import expect from 'expect';

import { useReferenceInputController } from './useReferenceInputController';
import {
useReferenceInputController,
UseReferenceInputControllerParams,
} from './useReferenceInputController';
import { CoreAdminContext } from '../../core';
import { Form, useInput } from '../../form';
import { ChoicesContextValue, Form, useInput } from '../../form';
import { testDataProvider } from '../../dataProvider';
import { SortPayload } from '../../types';

const ReferenceInputController = props => {
const ReferenceInputController = (
props: UseReferenceInputControllerParams & {
children: (options: ChoicesContextValue) => React.ReactNode;
}
) => {
const { children, ...rest } = props;
const inputProps = useInput({
...rest,
Expand Down Expand Up @@ -160,7 +168,6 @@ describe('useReferenceInputController', () => {
<Form defaultValues={{ post_id: 1 }}>
<ReferenceInputController
{...defaultProps}
loading
sort={{ field: 'title', order: 'ASC' }}
>
{children}
Expand Down Expand Up @@ -210,7 +217,10 @@ describe('useReferenceInputController', () => {
it('should refetch reference getList when its props change', async () => {
const children = jest.fn().mockReturnValue(<p>child</p>);
const Component = () => {
const [sort, setSort] = useState({ field: 'title', order: 'ASC' });
const [sort, setSort] = useState<SortPayload>({
field: 'title',
order: 'ASC',
});
const handleClick = useCallback(
() => setSort({ field: 'body', order: 'DESC' }),
[setSort]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useWatch } from 'react-hook-form';
import type { UseQueryOptions } from '@tanstack/react-query';
import { keepPreviousData, type UseQueryOptions } from '@tanstack/react-query';

import { useGetList } from '../../dataProvider';
import { useReference } from '../useReference';
import { useReferenceParams } from './useReferenceParams';
import { useWrappedSource } from '../../core';
import type { FilterPayload, RaRecord, SortPayload } from '../../types';
import type {
FilterPayload,
GetListResult,
RaRecord,
SortPayload,
} from '../../types';
import type { ChoicesContextValue } from '../../form';

/**
Expand Down Expand Up @@ -45,7 +50,7 @@ import type { ChoicesContextValue } from '../../form';
* });
*/
export const useReferenceInputController = <RecordType extends RaRecord = any>(
props: UseReferenceInputControllerParams
props: UseReferenceInputControllerParams<RecordType>
): ChoicesContextValue<RecordType> => {
const {
debounce,
Expand Down Expand Up @@ -100,8 +105,10 @@ export const useReferenceInputController = <RecordType extends RaRecord = any>(
},
{
enabled: isGetMatchingEnabled,
placeholderData: previousData => previousData,
...otherQueryOptions,
placeholderData: keepPreviousData,
...(otherQueryOptions as UseQueryOptions<
GetListResult<RecordType>
>),
}
);

Expand Down Expand Up @@ -205,14 +212,11 @@ export interface UseReferenceInputControllerParams<
debounce?: number;
filter?: FilterPayload;
queryOptions?: Omit<
UseQueryOptions<{
data: RecordType[];
total?: number;
pageInfo?: {
hasNextPage?: boolean;
hasPreviousPage?: boolean;
};
}>,
UseQueryOptions<
| GetListResult<RecordType>
// useReference calls getManyAggregate, which returns a an array of records
| RecordType[]
>,
'queryFn' | 'queryKey'
> & { meta?: any };
page?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const BookEdit = () => {
<DatagridInput
source="author"
choices={choices}
fullWidth
rowClick="toggleSelection"
>
<TextField source="name" />
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/input/DatagridInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const DatagridInput = (inProps: DatagridInputProps) => {

export type DatagridInputProps = Omit<
CommonInputProps,
'source' | 'readOnly' | 'disabled'
'fullWidth' | 'source' | 'readOnly' | 'disabled'
> &
ChoicesProps &
Omit<SupportCreateSuggestionOptions, 'handleChange'> &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ export const sanitizeInputRestProps = ({
validate,
validateFields,
value,
fullWidth,
...rest
}: any) => rest;