diff --git a/src/components/DataKeyPair.tsx b/src/components/DataKeyPair.tsx index fc5928d3..d38b678b 100644 --- a/src/components/DataKeyPair.tsx +++ b/src/components/DataKeyPair.tsx @@ -65,7 +65,7 @@ export const DataKeyPair: React.FC = (props) => { const numberKeyColor = useJsonViewerStore( store => store.colorspace.base0C) const { Component, PreComponent, PostComponent, Editor } = useTypeComponents( - value) + value, path) const quotesOnKeys = useJsonViewerStore(store => store.quotesOnKeys) const rootName = useJsonViewerStore(store => store.rootName) const isRoot = root === value diff --git a/src/stores/typeRegistry.tsx b/src/stores/typeRegistry.tsx index 4a55edf4..fdd44dba 100644 --- a/src/stores/typeRegistry.tsx +++ b/src/stores/typeRegistry.tsx @@ -16,7 +16,7 @@ import { PostObjectType, PreObjectType } from '../components/DataTypes/Object' -import type { DataItemProps, DataType } from '../type' +import type { DataItemProps, DataType, Path } from '../type' import { useJsonViewerStore } from './JsonViewerStore' type TypeRegistryState = { @@ -59,10 +59,10 @@ const objectType: DataType = { } export function matchTypeComponents ( - value: Value, registry: TypeRegistryState['registry']): DataType { + value: Value, path: Path, registry: TypeRegistryState['registry']): DataType { let potential: DataType | undefined for (const T of registry) { - if (T.is(value)) { + if (T.is(value, path)) { potential = T if (typeof value === 'object') { // early return for case like `null` @@ -79,9 +79,9 @@ export function matchTypeComponents ( return potential } -export function useTypeComponents (value: unknown) { +export function useTypeComponents (value: unknown, path: Path) { const registry = useTypeRegistryStore(store => store.registry) - return useMemo(() => matchTypeComponents(value, registry), [value, registry]) + return useMemo(() => matchTypeComponents(value, path, registry), [value, path, registry]) } export function predefined (): DataType[] { diff --git a/src/type.ts b/src/type.ts index 8e3cbf2c..af4d3fe4 100644 --- a/src/type.ts +++ b/src/type.ts @@ -25,7 +25,7 @@ export type DataType = { /** * Whether the value belongs to the data type */ - is: (value: unknown) => boolean + is: (value: unknown, path: Path) => boolean Component: React.ComponentType> Editor?: React.ComponentType> PreComponent?: React.ComponentType> diff --git a/src/utils/index.ts b/src/utils/index.ts index f0d60bb3..9fc33c50 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,6 @@ import type React from 'react' -import type { DataItemProps, EditorProps } from '../type' +import type { DataItemProps, EditorProps, Path } from '../type' export const applyValue = (obj: any, path: (string | number)[], value: any) => { if (typeof obj !== 'object' || obj === null) { @@ -28,44 +28,44 @@ export const applyValue = (obj: any, path: (string | number)[], value: any) => { // case 1: you only render with a single component export function createDataType ( - is: (value: unknown) => boolean, + is: (value: unknown, path: Path) => boolean, Component: React.ComponentType> ): { - is: (value: unknown) => boolean + is: (value: unknown, path: Path) => boolean Component: React.ComponentType> } // case 2: you only render with a single component with editor export function createDataType ( - is: (value: unknown) => boolean, + is: (value: unknown, path: Path) => boolean, Component: React.ComponentType>, Editor: React.ComponentType> ): { - is: (value: unknown) => boolean + is: (value: unknown, path: Path) => boolean Component: React.ComponentType> Editor: React.ComponentType> } // case 3: you only render with a component with pre and post. export function createDataType ( - is: (value: unknown) => boolean, + is: (value: unknown, path: Path) => boolean, Component: React.ComponentType>, Editor: undefined, PreComponent: React.ComponentType>, PostComponent: React.ComponentType> ): { - is: (value: unknown) => boolean + is: (value: unknown, path: Path) => boolean Component: React.ComponentType> PreComponent: React.ComponentType> PostComponent: React.ComponentType> } // case 4: need all of these export function createDataType ( - is: (value: unknown) => boolean, + is: (value: unknown, path: Path) => boolean, Component: React.ComponentType>, Editor: React.ComponentType>, PreComponent: React.ComponentType>, PostComponent: React.ComponentType> ): { - is: (value: unknown) => boolean + is: (value: unknown, path: Path) => boolean Component: React.ComponentType> Editor: React.ComponentType> PreComponent: React.ComponentType> @@ -73,7 +73,7 @@ export function createDataType ( } export function createDataType ( - is: (value: unknown) => boolean, + is: (value: unknown, path: Path) => boolean, Component: React.ComponentType>, Editor?: React.ComponentType> | undefined, PreComponent?: React.ComponentType> | undefined,