From 02d516fca2b4784e127f187229f762b95aebeafd Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 6 Apr 2023 22:35:43 +0300 Subject: [PATCH 1/2] Add 'option' generic types for resource definitions --- packages/ra-core/src/core/ResourceDefinitionContext.tsx | 6 +++--- packages/ra-core/src/core/useResourceDefinition.ts | 8 +++++--- packages/ra-core/src/core/useResourceDefinitions.ts | 4 +++- packages/ra-core/src/types.ts | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/ra-core/src/core/ResourceDefinitionContext.tsx b/packages/ra-core/src/core/ResourceDefinitionContext.tsx index 34836ea8779..844400e5260 100644 --- a/packages/ra-core/src/core/ResourceDefinitionContext.tsx +++ b/packages/ra-core/src/core/ResourceDefinitionContext.tsx @@ -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 = { + [name: string]: ResourceDefinition; }; export type ResourceDefinitionContextValue = { diff --git a/packages/ra-core/src/core/useResourceDefinition.ts b/packages/ra-core/src/core/useResourceDefinition.ts index 34897a4f821..5cad9f3509d 100644 --- a/packages/ra-core/src/core/useResourceDefinition.ts +++ b/packages/ra-core/src/core/useResourceDefinition.ts @@ -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 @@ -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 => { const resource = useResourceContext(props); const resourceDefinitions = useResourceDefinitions(); const { hasCreate, hasEdit, hasList, hasShow, recordRepresentation } = diff --git a/packages/ra-core/src/core/useResourceDefinitions.ts b/packages/ra-core/src/core/useResourceDefinitions.ts index af48e0e2f41..c7c8c2d46dc 100644 --- a/packages/ra-core/src/core/useResourceDefinitions.ts +++ b/packages/ra-core/src/core/useResourceDefinitions.ts @@ -19,5 +19,7 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext'; * // recordRepresentation: 'title', * // } */ -export const useResourceDefinitions = (): ResourceDefinitions => +export const useResourceDefinitions = < + OptionsType = any +>(): ResourceDefinitions => useResourceDefinitionContext().definitions; diff --git a/packages/ra-core/src/types.ts b/packages/ra-core/src/types.ts index 174c6cd32d8..388d5ee72d9 100644 --- a/packages/ra-core/src/types.ts +++ b/packages/ra-core/src/types.ts @@ -275,9 +275,9 @@ export type LegacyDataProvider = ( export type RecordToStringFunction = (record: any) => string; -export interface ResourceDefinition { +export interface ResourceDefinition { readonly name: string; - readonly options?: any; + readonly options?: OptionsType; readonly hasList?: boolean; readonly hasEdit?: boolean; readonly hasShow?: boolean; From 15586b4807f497937b1a7fecfafcab66d8f8cc55 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 6 Apr 2023 22:26:14 +0300 Subject: [PATCH 2/2] Add generic to useInput --- packages/ra-core/src/form/useInput.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/ra-core/src/form/useInput.ts b/packages/ra-core/src/form/useInput.ts index 663669b0c06..0ce7621c948 100644 --- a/packages/ra-core/src/form/useInput.ts +++ b/packages/ra-core/src/form/useInput.ts @@ -23,7 +23,9 @@ const defaultFormat = (value: any) => (value == null ? '' : value); // parse empty string into null as it's more suitable for a majority of backends const defaultParse = (value: string) => (value === '' ? null : value); -export const useInput = (props: InputProps): UseInputValue => { +export const useInput = ( + props: InputProps +): UseInputValue => { const { defaultValue, format = defaultFormat,