Skip to content

Commit

Permalink
Add 'option' generic types for resource definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmVisco committed Apr 6, 2023
1 parent 6d04d1b commit 7610fd3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/ra-core/src/core/ResourceDefinitionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as React from 'react';
import { createContext, useCallback, useState, useMemo } from 'react';
import isEqual from 'lodash/isEqual';

import { ResourceDefinition } from '../types';
import { ResourceDefinition, ResourceOptions } from '../types';

export type ResourceDefinitions = {
[name: string]: ResourceDefinition;
export type ResourceDefinitions<OptionsType extends ResourceOptions = any> = {
[name: string]: ResourceDefinition<OptionsType>;
};

export type ResourceDefinitionContextValue = {
Expand Down
8 changes: 5 additions & 3 deletions packages/ra-core/src/core/useResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import defaults from 'lodash/defaults';

import { useResourceDefinitions } from './useResourceDefinitions';
import { useResourceContext } from './useResourceContext';
import { ResourceDefinition } from '../types';
import { ResourceDefinition, ResourceOptions } from '../types';

/**
* Hook to get the definition of a given resource
Expand All @@ -26,9 +26,11 @@ import { ResourceDefinition } from '../types';
*
* const definition = useResourceDefinition({ resource: 'posts' });
*/
export const useResourceDefinition = (
export const useResourceDefinition = <
OptionsType extends ResourceOptions = any
>(
props?: UseResourceDefinitionOptions
): ResourceDefinition => {
): ResourceDefinition<OptionsType> => {
const resource = useResourceContext(props);
const resourceDefinitions = useResourceDefinitions();
const { hasCreate, hasEdit, hasList, hasShow, recordRepresentation } =
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/core/useResourceDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext';
* // recordRepresentation: 'title',
* // }
*/
export const useResourceDefinitions = (): ResourceDefinitions =>
export const useResourceDefinitions = <
OptionsType = any
>(): ResourceDefinitions<OptionsType> =>
useResourceDefinitionContext().definitions;
4 changes: 2 additions & 2 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ export type LegacyDataProvider = (

export type RecordToStringFunction = (record: any) => string;

export interface ResourceDefinition {
export interface ResourceDefinition<OptionsType extends ResourceOptions = any> {
readonly name: string;
readonly options?: any;
readonly options?: OptionsType;
readonly hasList?: boolean;
readonly hasEdit?: boolean;
readonly hasShow?: boolean;
Expand Down

0 comments on commit 7610fd3

Please sign in to comment.