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] Update RaRecord type & fix some other types #8862

Merged
merged 3 commits into from
May 11, 2023
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
24 changes: 14 additions & 10 deletions examples/demo/src/dashboard/PendingReviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ const PendingReviews = () => {
reference="customers"
link={false}
>
<FunctionField
render={(customer: Customer) => (
<Avatar
src={`${customer.avatar}?size=32x32`}
sx={{
bgcolor: 'background.paper',
}}
alt={`${customer.first_name} ${customer.last_name}`}
/>
)}
<FunctionField<Customer>
render={customer =>
customer ? (
<Avatar
src={`${customer.avatar}?size=32x32`}
sx={{
bgcolor: 'background.paper',
}}
alt={`${customer.first_name} ${customer.last_name}`}
/>
) : (
''
)
}
/>
</ReferenceField>
</ListItemAvatar>
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/reviews/ReviewItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
} from 'react-admin';

import AvatarField from '../visitors/AvatarField';
import { Review, Customer } from './../types';
import { Customer } from './../types';

export const ReviewItem = () => {
const record = useRecordContext<Review>();
const record = useRecordContext();
const createPath = useCreatePath();
if (!record) {
return null;
Expand Down Expand Up @@ -53,8 +53,8 @@ export const ReviewItem = () => {
reference="customers"
link={false}
>
<FunctionField
render={(record?: Customer) =>
<FunctionField<Customer>
render={record =>
record
? `${record.first_name} ${record.last_name}`
: ''
Expand Down
5 changes: 2 additions & 3 deletions packages/ra-core/src/controller/record/useRecordContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useContext } from 'react';
import { RaRecord } from '../../types';
import { RecordContext } from './RecordContext';

/**
Expand Down Expand Up @@ -31,7 +30,7 @@ import { RecordContext } from './RecordContext';
* @returns {RaRecord} A record object
*/
export const useRecordContext = <
RecordType extends RaRecord | Omit<RaRecord, 'id'> = RaRecord
RecordType extends Record<string, unknown> = Record<string, any>
>(
props?: UseRecordContextParams<RecordType>
): RecordType | undefined => {
Expand All @@ -43,7 +42,7 @@ export const useRecordContext = <
};

export interface UseRecordContextParams<
RecordType extends RaRecord | Omit<RaRecord, 'id'> = RaRecord
RecordType extends Record<string, unknown> = Record<string, unknown>
> {
record?: RecordType;
[key: string]: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useGetMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ export const useGetMany = <RecordType extends RaRecord = any>(
};

export type UseGetManyHookValue<
RecordType extends RaRecord = any
RecordType extends RaRecord = RaRecord
> = UseQueryResult<RecordType[], Error>;
4 changes: 2 additions & 2 deletions packages/ra-core/src/dataProvider/useGetRecordId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useParams } from 'react-router-dom';
import { useRecordContext } from '../controller';
import { Identifier } from '../types';
import { Identifier, RaRecord } from '../types';

/**
* Helper hook to get the current `recordId`.
Expand All @@ -15,7 +15,7 @@ import { Identifier } from '../types';
* const recordId = useGetRecordId();
*/
export function useGetRecordId(recordId?: Identifier): Identifier {
const contextRecord = useRecordContext();
const contextRecord = useRecordContext<RaRecord>();
const { id: routeId } = useParams<'id'>();
const actualRecordId = recordId ?? contextRecord?.id ?? routeId;
if (actualRecordId == null)
Expand Down
9 changes: 6 additions & 3 deletions packages/ra-core/src/routing/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { RaRecord } from '../types';

export type LinkToFunctionType = (
record: RaRecord,
export type LinkToFunctionType<RecordType extends RaRecord = RaRecord> = (
record: RecordType,
reference: string
) => string;

export type LinkToType = string | false | LinkToFunctionType;
export type LinkToType<RecordType extends RaRecord = RaRecord> =
| string
| false
| LinkToFunctionType<RecordType>;
8 changes: 4 additions & 4 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { AuthActionType } from './auth/types';

export type Identifier = string | number;

export interface RaRecord {
id: Identifier;
[key: string]: any;
export interface RaRecord<IdentifierType extends Identifier = Identifier>
extends Record<string, any> {
id: IdentifierType;
}

export interface SortPayload {
Expand Down Expand Up @@ -93,7 +93,7 @@ export type DataProvider<ResourceType extends string = string> = {

getOne: <RecordType extends RaRecord = any>(
resource: ResourceType,
params: GetOneParams
params: GetOneParams<RecordType>
) => Promise<GetOneResult<RecordType>>;

getMany: <RecordType extends RaRecord = any>(
Expand Down
9 changes: 6 additions & 3 deletions packages/ra-ui-materialui/src/field/FunctionField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
* />
*/

export const FunctionField = <RecordType extends unknown = any>(
export const FunctionField = <
RecordType extends Record<string, unknown> = Record<string, any>
>(
props: FunctionFieldProps<RecordType>
) => {
const { className, source = '', render, ...rest } = props;
Expand Down Expand Up @@ -46,8 +48,9 @@ FunctionField.propTypes = {
render: PropTypes.func.isRequired,
};

export interface FunctionFieldProps<RecordType extends unknown = any>
extends PublicFieldProps,
export interface FunctionFieldProps<
RecordType extends Record<string, unknown> = Record<string, any>
> extends PublicFieldProps,
InjectedFieldProps<RecordType>,
Omit<TypographyProps, 'textAlign'> {
render: (record?: RecordType, source?: string) => any;
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/ReferenceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface ReferenceFieldProps<RecordType extends RaRecord = any>
resource?: string;
source: string;
translateChoice?: Function | boolean;
link?: LinkToType;
link?: LinkToType<RecordType>;
sx?: SxProps;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/ReferenceOneField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface ReferenceOneFieldProps<RecordType extends RaRecord = any>
target: string;
sort?: SortPayload;
filter?: any;
link?: LinkToType;
link?: LinkToType<RecordType>;
queryOptions?: UseQueryOptions<{
data: RecordType[];
total: number;
Expand Down