Skip to content

Commit cdd3995

Browse files
authored
Merge pull request #8862 from marmelab/record-types
Update RaRecord type & fix some other types
2 parents 69f2d0c + cdf71f5 commit cdd3995

10 files changed

Lines changed: 41 additions & 32 deletions

File tree

examples/demo/src/dashboard/PendingReviews.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,20 @@ const PendingReviews = () => {
7171
reference="customers"
7272
link={false}
7373
>
74-
<FunctionField
75-
render={(customer: Customer) => (
76-
<Avatar
77-
src={`${customer.avatar}?size=32x32`}
78-
sx={{
79-
bgcolor: 'background.paper',
80-
}}
81-
alt={`${customer.first_name} ${customer.last_name}`}
82-
/>
83-
)}
74+
<FunctionField<Customer>
75+
render={customer =>
76+
customer ? (
77+
<Avatar
78+
src={`${customer.avatar}?size=32x32`}
79+
sx={{
80+
bgcolor: 'background.paper',
81+
}}
82+
alt={`${customer.first_name} ${customer.last_name}`}
83+
/>
84+
) : (
85+
''
86+
)
87+
}
8488
/>
8589
</ReferenceField>
8690
</ListItemAvatar>

examples/demo/src/reviews/ReviewItem.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import {
1616
} from 'react-admin';
1717

1818
import AvatarField from '../visitors/AvatarField';
19-
import { Review, Customer } from './../types';
19+
import { Customer } from './../types';
2020

2121
export const ReviewItem = () => {
22-
const record = useRecordContext<Review>();
22+
const record = useRecordContext();
2323
const createPath = useCreatePath();
2424
if (!record) {
2525
return null;
@@ -53,8 +53,8 @@ export const ReviewItem = () => {
5353
reference="customers"
5454
link={false}
5555
>
56-
<FunctionField
57-
render={(record?: Customer) =>
56+
<FunctionField<Customer>
57+
render={record =>
5858
record
5959
? `${record.first_name} ${record.last_name}`
6060
: ''

packages/ra-core/src/controller/record/useRecordContext.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useContext } from 'react';
2-
import { RaRecord } from '../../types';
32
import { RecordContext } from './RecordContext';
43

54
/**
@@ -31,7 +30,7 @@ import { RecordContext } from './RecordContext';
3130
* @returns {RaRecord} A record object
3231
*/
3332
export const useRecordContext = <
34-
RecordType extends RaRecord | Omit<RaRecord, 'id'> = RaRecord
33+
RecordType extends Record<string, unknown> = Record<string, any>
3534
>(
3635
props?: UseRecordContextParams<RecordType>
3736
): RecordType | undefined => {
@@ -43,7 +42,7 @@ export const useRecordContext = <
4342
};
4443

4544
export interface UseRecordContextParams<
46-
RecordType extends RaRecord | Omit<RaRecord, 'id'> = RaRecord
45+
RecordType extends Record<string, unknown> = Record<string, unknown>
4746
> {
4847
record?: RecordType;
4948
[key: string]: any;

packages/ra-core/src/dataProvider/useGetMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ export const useGetMany = <RecordType extends RaRecord = any>(
113113
};
114114

115115
export type UseGetManyHookValue<
116-
RecordType extends RaRecord = any
116+
RecordType extends RaRecord = RaRecord
117117
> = UseQueryResult<RecordType[], Error>;

packages/ra-core/src/dataProvider/useGetRecordId.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useParams } from 'react-router-dom';
22
import { useRecordContext } from '../controller';
3-
import { Identifier } from '../types';
3+
import { Identifier, RaRecord } from '../types';
44

55
/**
66
* Helper hook to get the current `recordId`.
@@ -15,7 +15,7 @@ import { Identifier } from '../types';
1515
* const recordId = useGetRecordId();
1616
*/
1717
export function useGetRecordId(recordId?: Identifier): Identifier {
18-
const contextRecord = useRecordContext();
18+
const contextRecord = useRecordContext<RaRecord>();
1919
const { id: routeId } = useParams<'id'>();
2020
const actualRecordId = recordId ?? contextRecord?.id ?? routeId;
2121
if (actualRecordId == null)
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { RaRecord } from '../types';
22

3-
export type LinkToFunctionType = (
4-
record: RaRecord,
3+
export type LinkToFunctionType<RecordType extends RaRecord = RaRecord> = (
4+
record: RecordType,
55
reference: string
66
) => string;
77

8-
export type LinkToType = string | false | LinkToFunctionType;
8+
export type LinkToType<RecordType extends RaRecord = RaRecord> =
9+
| string
10+
| false
11+
| LinkToFunctionType<RecordType>;

packages/ra-core/src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { AuthActionType } from './auth/types';
99

1010
export type Identifier = string | number;
1111

12-
export interface RaRecord {
13-
id: Identifier;
14-
[key: string]: any;
12+
export interface RaRecord<IdentifierType extends Identifier = Identifier>
13+
extends Record<string, any> {
14+
id: IdentifierType;
1515
}
1616

1717
export interface SortPayload {
@@ -93,7 +93,7 @@ export type DataProvider<ResourceType extends string = string> = {
9393

9494
getOne: <RecordType extends RaRecord = any>(
9595
resource: ResourceType,
96-
params: GetOneParams
96+
params: GetOneParams<RecordType>
9797
) => Promise<GetOneResult<RecordType>>;
9898

9999
getMany: <RecordType extends RaRecord = any>(

packages/ra-ui-materialui/src/field/FunctionField.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
1818
* />
1919
*/
2020

21-
export const FunctionField = <RecordType extends unknown = any>(
21+
export const FunctionField = <
22+
RecordType extends Record<string, unknown> = Record<string, any>
23+
>(
2224
props: FunctionFieldProps<RecordType>
2325
) => {
2426
const { className, source = '', render, ...rest } = props;
@@ -46,8 +48,9 @@ FunctionField.propTypes = {
4648
render: PropTypes.func.isRequired,
4749
};
4850

49-
export interface FunctionFieldProps<RecordType extends unknown = any>
50-
extends PublicFieldProps,
51+
export interface FunctionFieldProps<
52+
RecordType extends Record<string, unknown> = Record<string, any>
53+
> extends PublicFieldProps,
5154
InjectedFieldProps<RecordType>,
5255
Omit<TypographyProps, 'textAlign'> {
5356
render: (record?: RecordType, source?: string) => any;

packages/ra-ui-materialui/src/field/ReferenceField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface ReferenceFieldProps<RecordType extends RaRecord = any>
112112
resource?: string;
113113
source: string;
114114
translateChoice?: Function | boolean;
115-
link?: LinkToType;
115+
link?: LinkToType<RecordType>;
116116
sx?: SxProps;
117117
}
118118

packages/ra-ui-materialui/src/field/ReferenceOneField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export interface ReferenceOneFieldProps<RecordType extends RaRecord = any>
103103
target: string;
104104
sort?: SortPayload;
105105
filter?: any;
106-
link?: LinkToType;
106+
link?: LinkToType<RecordType>;
107107
queryOptions?: UseQueryOptions<{
108108
data: RecordType[];
109109
total: number;

0 commit comments

Comments
 (0)