Skip to content

Commit 8b916b3

Browse files
committed
Update RaRecord type & fix some other types
1 parent 69f2d0c commit 8b916b3

14 files changed

Lines changed: 42 additions & 31 deletions

File tree

examples/crm/src/deals/DealShow.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CompanyAvatar } from '../companies/CompanyAvatar';
1515
import { NotesIterator } from '../notes';
1616
import { ContactList } from './ContactList';
1717
import { stageNames } from './stages';
18+
import { Deal } from '../types';
1819

1920
export const DealShow = ({ open, id }: { open: boolean; id?: string }) => {
2021
const redirect = useRedirect();
@@ -48,7 +49,7 @@ export const DealShow = ({ open, id }: { open: boolean; id?: string }) => {
4849
};
4950

5051
const DealShowContent = () => {
51-
const record = useRecordContext();
52+
const record = useRecordContext<Deal>();
5253
if (!record) return null;
5354
return (
5455
<div>

examples/crm/src/notes/NewNote.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { Box, TextField as TextInput, Button } from '@mui/material';
1414

1515
import { StatusSelector } from './StatusSelector';
16+
import { Contact, Deal } from '../types';
1617

1718
export const NewNote = ({
1819
showStatus,
@@ -22,7 +23,7 @@ export const NewNote = ({
2223
reference: 'contacts' | 'deals';
2324
}) => {
2425
const resource = useResourceContext();
25-
const record = useRecordContext();
26+
const record = useRecordContext<Deal | Contact>();
2627
const { refetch } = useListContext();
2728
const [text, setText] = useState('');
2829
const [status, setStatus] = useState(record && record.status);

examples/crm/src/notes/Note.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import EditIcon from '@mui/icons-material/Edit';
2222
import TrashIcon from '@mui/icons-material/Delete';
2323

2424
import { Status } from '../misc/Status';
25+
import { Contact, Deal } from '../types';
2526

2627
export const Note = ({
2728
showStatus,
@@ -37,7 +38,7 @@ export const Note = ({
3738
const [isEditing, setEditing] = useState(false);
3839
const [noteText, setNoteText] = useState(note.text);
3940
const resource = useResourceContext();
40-
const record = useRecordContext();
41+
const record = useRecordContext<Deal | Contact>();
4142
const notify = useNotify();
4243

4344
const [update, { isLoading }] = useUpdate();

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
: ''

examples/demo/src/visitors/Aside.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ const mixOrdersAndReviews = (
200200
};
201201

202202
const Order = () => {
203-
const record = useRecordContext();
203+
const record = useRecordContext<OrderRecord>();
204204
const translate = useTranslate();
205205
if (!record) return null;
206206
return (

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, unknown>
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>;

0 commit comments

Comments
 (0)