Skip to content

Commit

Permalink
Create shared RenderRecordFunction type
Browse files Browse the repository at this point in the history
  • Loading branch information
elstgav committed Jun 1, 2023
1 parent 136d102 commit ddde0c5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/demo/src/orders/NbItemsField.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { FunctionField, FunctionFieldRenderer } from 'react-admin';
import { FunctionField, RenderRecordFunction } from 'react-admin';
import { Order } from '../types';

const render: FunctionFieldRenderer<Order> = record => record.basket.length;
const render: RenderRecordFunction<Order> = record => record.basket.length;

const NbItemsField = () => <FunctionField<Order> render={render} />;

Expand Down
5 changes: 2 additions & 3 deletions packages/ra-core/src/controller/record/WithRecord.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReactElement } from 'react';
import { RaRecord } from '../../types';
import { RenderRecordFunction } from '../../types';
import { useRecordContext } from './useRecordContext';

/**
Expand All @@ -24,6 +23,6 @@ export const WithRecord = <RecordType extends Record<string, unknown> = any>({
export interface WithRecordProps<
RecordType extends Record<string, unknown> = any
> {
render: (record: RecordType) => ReactElement;
render: RenderRecordFunction<RecordType>;
label?: string;
}
6 changes: 5 additions & 1 deletion packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, ReactElement, ComponentType } from 'react';
import { ComponentType, ReactElement, ReactNode } from 'react';
import { WithPermissionsChildrenParams } from './auth/WithPermissions';
import { AuthActionType } from './auth/types';

Expand Down Expand Up @@ -361,6 +361,10 @@ export interface ResourceProps {
children?: ReactNode;
}

export type RenderRecordFunction<
RecordType extends Record<string, unknown> = any
> = (record: RecordType, source?: string) => ReactNode;

export type Exporter = (
data: any,
fetchRelatedRecords: (
Expand Down
8 changes: 2 additions & 6 deletions packages/ra-ui-materialui/src/field/FunctionField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useMemo } from 'react';
import { useRecordContext } from 'ra-core';
import { RenderRecordFunction, useRecordContext } from 'ra-core';
import PropTypes from 'prop-types';
import Typography, { TypographyProps } from '@mui/material/Typography';

Expand Down Expand Up @@ -46,13 +46,9 @@ FunctionField.propTypes = {
render: PropTypes.func.isRequired,
};

export type FunctionFieldRenderer<
RecordType extends Record<string, unknown> = any
> = (record: RecordType, source?: string) => React.ReactNode;

export interface FunctionFieldProps<
RecordType extends Record<string, unknown> = any
> extends FieldProps<RecordType>,
Omit<TypographyProps, 'textAlign'> {
render: FunctionFieldRenderer<RecordType>;
render: RenderRecordFunction<RecordType>;
}

0 comments on commit ddde0c5

Please sign in to comment.