Skip to content

Commit 9ed6d01

Browse files
committed
Introduce ReferenceArrayInputContext
1 parent 1991de7 commit 9ed6d01

7 files changed

+111
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createContext } from 'react';
2+
import { PaginationPayload, Record, SortPayload } from '../../types';
3+
4+
/**
5+
* Context which provides access to the useReferenceArrayInput features.
6+
*
7+
* @example
8+
* const ReferenceArrayInput = ({ children }) => {
9+
* const controllerProps = useReferenceArrayInputController();
10+
* return (
11+
* <ReferenceArrayInputContextProvider value={controllerProps}>
12+
* {children}
13+
* </ReferenceArrayInputContextProvider>
14+
* )
15+
* }
16+
*/
17+
export const ReferenceArrayInputContext = createContext(undefined);
18+
19+
export interface ReferenceArrayInputContextValue {
20+
choices: Record[];
21+
error?: any;
22+
warning?: any;
23+
loading: boolean;
24+
loaded: boolean;
25+
setFilter: (filter: any) => void;
26+
setPagination: (pagination: PaginationPayload) => void;
27+
setSort: (sort: SortPayload) => void;
28+
setSortForList: (sort: string, order?: string) => void;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from 'react';
2+
import { ReactNode } from 'react';
3+
import {
4+
ReferenceArrayInputContext,
5+
ReferenceArrayInputContextValue,
6+
} from './ReferenceArrayInputContext';
7+
8+
/**
9+
* Provider for the context which provides access to the useReferenceArrayInput features.
10+
*
11+
* @example
12+
* const ReferenceArrayInput = ({ children }) => {
13+
* const controllerProps = useReferenceArrayInputController();
14+
* return (
15+
* <ReferenceArrayInputContextProvider value={controllerProps}>
16+
* {children}
17+
* </ReferenceArrayInputContextProvider>
18+
* )
19+
* }
20+
*/
21+
export const ReferenceArrayInputContextProvider = ({
22+
children,
23+
value,
24+
}: {
25+
children: ReactNode;
26+
value: ReferenceArrayInputContextValue;
27+
}) => (
28+
<ReferenceArrayInputContext.Provider value={value}>
29+
{children}
30+
</ReferenceArrayInputContext.Provider>
31+
);

packages/ra-core/src/controller/input/ReferenceArrayInputController.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ComponentType, ReactElement, useCallback } from 'react';
22
import debounce from 'lodash/debounce';
33

44
import { Record, SortPayload, PaginationPayload } from '../../types';
5-
import useReferenceArrayInputController from './useReferenceArrayInputController';
5+
import { useReferenceArrayInputController } from './useReferenceArrayInputController';
66
import { ListControllerProps } from '..';
77

88
/**
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import ReferenceArrayInputController from './ReferenceArrayInputController';
22
import ReferenceInputController from './ReferenceInputController';
3-
import useReferenceArrayInputController from './useReferenceArrayInputController';
43
import {
54
getStatusForInput,
65
getSelectedReferencesStatus,
76
getStatusForArrayInput,
87
} from './referenceDataStatus';
98

9+
export * from './useReferenceArrayInputController';
1010
export * from './useReferenceInputController';
11+
export * from './ReferenceArrayInputContext';
12+
export * from './ReferenceArrayInputContextProvider';
13+
export * from './useReferenceArrayInputContext';
1114

1215
export {
1316
getStatusForInput,
1417
getSelectedReferencesStatus,
1518
getStatusForArrayInput,
1619
ReferenceArrayInputController,
1720
ReferenceInputController,
18-
useReferenceArrayInputController,
1921
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useContext } from 'react';
2+
import {
3+
ReferenceArrayInputContext,
4+
ReferenceArrayInputContextValue,
5+
} from './ReferenceArrayInputContext';
6+
7+
/**
8+
* Hook to get the ReferenceArrayInputContext.
9+
*/
10+
export const useReferenceArrayInputContext = <
11+
T extends ReferenceArrayInputContextValue = ReferenceArrayInputContextValue
12+
>(
13+
props: T
14+
): ReferenceArrayInputContextValue => {
15+
const context = useContext(ReferenceArrayInputContext);
16+
17+
if (props.choices) {
18+
return props;
19+
}
20+
21+
return context;
22+
};

packages/ra-core/src/controller/input/useReferenceArrayInputController.ts

+6-37
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { useMemo, useState, useEffect, useRef, useCallback } from 'react';
22
import { useSelector } from 'react-redux';
33
import isEqual from 'lodash/isEqual';
4-
import get from 'lodash/get';
5-
import sortBy from 'lodash/sortBy';
64
import difference from 'lodash/difference';
7-
import {
8-
PaginationPayload,
9-
Record,
10-
SortPayload,
11-
ReduxState,
12-
Identifier,
13-
} from '../../types';
5+
import { Record, SortPayload, ReduxState, Identifier } from '../../types';
146
import { useGetMany } from '../../dataProvider';
157
import { FieldInputProps, useForm } from 'react-final-form';
168
import useGetMatching from '../../dataProvider/useGetMatching';
@@ -20,7 +12,7 @@ import { useResourceContext } from '../../core';
2012
import { usePaginationState, useSortState } from '..';
2113
import { ListControllerProps } from '../useListController';
2214
import { indexById, removeEmpty, useSafeSetState } from '../../util';
23-
import { SORT_DESC } from '../../reducer/admin/resource/list/queryReducer';
15+
import { ReferenceArrayInputContextValue } from './ReferenceArrayInputContext';
2416

2517
/**
2618
* Prepare data for the ReferenceArrayInput components
@@ -46,9 +38,9 @@ import { SORT_DESC } from '../../reducer/admin/resource/list/queryReducer';
4638
*
4739
* @return {Object} controllerProps Fetched data and callbacks for the ReferenceArrayInput components
4840
*/
49-
const useReferenceArrayInputController = (
50-
props: Option
51-
): ReferenceArrayInputProps & Omit<ListControllerProps, 'setSort'> => {
41+
export const useReferenceArrayInputController = (
42+
props: UseReferenceArrayInputOptions
43+
): ReferenceArrayInputContextValue & Omit<ListControllerProps, 'setSort'> => {
5244
const {
5345
basePath,
5446
filter: defaultFilter,
@@ -340,30 +332,7 @@ const mergeReferences = (ref1: Record[], ref2: Record[]): Record[] => {
340332
return res;
341333
};
342334

343-
export default useReferenceArrayInputController;
344-
345-
/**
346-
* @typedef ReferenceArrayProps
347-
* @type {Object}
348-
* @property {Array} ids the list of ids.
349-
* @property {Object} data Object holding the reference data by their ids
350-
* @property {Object} error the error returned by the dataProvider
351-
* @property {boolean} loading is the reference currently loading
352-
* @property {boolean} loaded has the reference already been loaded
353-
*/
354-
interface ReferenceArrayInputProps {
355-
choices: Record[];
356-
error?: any;
357-
warning?: any;
358-
loading: boolean;
359-
loaded: boolean;
360-
setFilter: (filter: any) => void;
361-
setPagination: (pagination: PaginationPayload) => void;
362-
setSort: (sort: SortPayload) => void;
363-
setSortForList: (sort: string, order?: string) => void;
364-
}
365-
366-
interface Option {
335+
export interface UseReferenceArrayInputOptions {
367336
basePath?: string;
368337
filter?: any;
369338
filterToQuery?: (filter: any) => any;

packages/ra-ui-materialui/src/input/ReferenceArrayInput.tsx

+18-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
PaginationPayload,
1212
Translate,
1313
ResourceContextProvider,
14+
ReferenceArrayInputContextProvider,
1415
ListContextProvider,
1516
} from 'ra-core';
1617

@@ -137,40 +138,37 @@ const ReferenceArrayInput = ({
137138
...props,
138139
});
139140

140-
const {
141-
setSort,
142-
setSortForList,
143-
...controllerProps
144-
} = useReferenceArrayInputController({
141+
const controllerProps = useReferenceArrayInputController({
145142
...props,
146143
input,
147144
});
148145

149146
const listContext = useMemo(
150147
() => ({
151148
...controllerProps,
152-
setSort: setSortForList,
149+
setSort: controllerProps.setSortForList,
153150
}),
154-
[controllerProps, setSortForList]
151+
[controllerProps]
155152
);
156153

157154
const translate = useTranslate();
158155

159156
return (
160157
<ResourceContextProvider value={props.reference}>
161-
<ListContextProvider value={listContext}>
162-
<ReferenceArrayInputView
163-
id={id}
164-
input={input}
165-
isRequired={isRequired}
166-
meta={meta}
167-
translate={translate}
168-
children={children}
169-
setSort={setSort}
170-
{...props}
171-
{...controllerProps}
172-
/>
173-
</ListContextProvider>
158+
<ReferenceArrayInputContextProvider value={controllerProps}>
159+
<ListContextProvider value={listContext}>
160+
<ReferenceArrayInputView
161+
id={id}
162+
input={input}
163+
isRequired={isRequired}
164+
meta={meta}
165+
translate={translate}
166+
children={children}
167+
{...props}
168+
{...controllerProps}
169+
/>
170+
</ListContextProvider>
171+
</ReferenceArrayInputContextProvider>
174172
</ResourceContextProvider>
175173
);
176174
};

0 commit comments

Comments
 (0)