Skip to content

Commit dad14b9

Browse files
authored
feat: support path in dataType (#107)
1 parent 2cf375c commit dad14b9

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/components/DataKeyPair.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
6565
const numberKeyColor = useJsonViewerStore(
6666
store => store.colorspace.base0C)
6767
const { Component, PreComponent, PostComponent, Editor } = useTypeComponents(
68-
value)
68+
value, path)
6969
const quotesOnKeys = useJsonViewerStore(store => store.quotesOnKeys)
7070
const rootName = useJsonViewerStore(store => store.rootName)
7171
const isRoot = root === value

src/stores/typeRegistry.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
PostObjectType,
1717
PreObjectType
1818
} from '../components/DataTypes/Object'
19-
import type { DataItemProps, DataType } from '../type'
19+
import type { DataItemProps, DataType, Path } from '../type'
2020
import { useJsonViewerStore } from './JsonViewerStore'
2121

2222
type TypeRegistryState = {
@@ -59,10 +59,10 @@ const objectType: DataType<object> = {
5959
}
6060

6161
export function matchTypeComponents<Value> (
62-
value: Value, registry: TypeRegistryState['registry']): DataType<Value> {
62+
value: Value, path: Path, registry: TypeRegistryState['registry']): DataType<Value> {
6363
let potential: DataType<Value> | undefined
6464
for (const T of registry) {
65-
if (T.is(value)) {
65+
if (T.is(value, path)) {
6666
potential = T
6767
if (typeof value === 'object') {
6868
// early return for case like `null`
@@ -79,9 +79,9 @@ export function matchTypeComponents<Value> (
7979
return potential
8080
}
8181

82-
export function useTypeComponents (value: unknown) {
82+
export function useTypeComponents (value: unknown, path: Path) {
8383
const registry = useTypeRegistryStore(store => store.registry)
84-
return useMemo(() => matchTypeComponents(value, registry), [value, registry])
84+
return useMemo(() => matchTypeComponents(value, path, registry), [value, path, registry])
8585
}
8686

8787
export function predefined (): DataType<any>[] {

src/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type DataType<ValueType = unknown> = {
2525
/**
2626
* Whether the value belongs to the data type
2727
*/
28-
is: (value: unknown) => boolean
28+
is: (value: unknown, path: Path) => boolean
2929
Component: React.ComponentType<DataItemProps<ValueType>>
3030
Editor?: React.ComponentType<EditorProps<ValueType>>
3131
PreComponent?: React.ComponentType<DataItemProps<ValueType>>

src/utils/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type React from 'react'
22

3-
import type { DataItemProps, EditorProps } from '../type'
3+
import type { DataItemProps, EditorProps, Path } from '../type'
44

55
export const applyValue = (obj: any, path: (string | number)[], value: any) => {
66
if (typeof obj !== 'object' || obj === null) {
@@ -28,52 +28,52 @@ export const applyValue = (obj: any, path: (string | number)[], value: any) => {
2828

2929
// case 1: you only render with a single component
3030
export function createDataType<ValueType = unknown> (
31-
is: (value: unknown) => boolean,
31+
is: (value: unknown, path: Path) => boolean,
3232
Component: React.ComponentType<DataItemProps<ValueType>>
3333
): {
34-
is: (value: unknown) => boolean
34+
is: (value: unknown, path: Path) => boolean
3535
Component: React.ComponentType<DataItemProps<ValueType>>
3636
}
3737
// case 2: you only render with a single component with editor
3838
export function createDataType<ValueType = unknown> (
39-
is: (value: unknown) => boolean,
39+
is: (value: unknown, path: Path) => boolean,
4040
Component: React.ComponentType<DataItemProps<ValueType>>,
4141
Editor: React.ComponentType<EditorProps<ValueType>>
4242
): {
43-
is: (value: unknown) => boolean
43+
is: (value: unknown, path: Path) => boolean
4444
Component: React.ComponentType<DataItemProps<ValueType>>
4545
Editor: React.ComponentType<DataItemProps<ValueType>>
4646
}
4747
// case 3: you only render with a component with pre and post.
4848
export function createDataType<ValueType = unknown> (
49-
is: (value: unknown) => boolean,
49+
is: (value: unknown, path: Path) => boolean,
5050
Component: React.ComponentType<DataItemProps<ValueType>>,
5151
Editor: undefined,
5252
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
5353
PostComponent: React.ComponentType<DataItemProps<ValueType>>
5454
): {
55-
is: (value: unknown) => boolean
55+
is: (value: unknown, path: Path) => boolean
5656
Component: React.ComponentType<DataItemProps<ValueType>>
5757
PreComponent: React.ComponentType<DataItemProps<ValueType>>
5858
PostComponent: React.ComponentType<DataItemProps<ValueType>>
5959
}
6060
// case 4: need all of these
6161
export function createDataType<ValueType = unknown> (
62-
is: (value: unknown) => boolean,
62+
is: (value: unknown, path: Path) => boolean,
6363
Component: React.ComponentType<DataItemProps<ValueType>>,
6464
Editor: React.ComponentType<EditorProps<ValueType>>,
6565
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
6666
PostComponent: React.ComponentType<DataItemProps<ValueType>>
6767
): {
68-
is: (value: unknown) => boolean
68+
is: (value: unknown, path: Path) => boolean
6969
Component: React.ComponentType<DataItemProps<ValueType>>
7070
Editor: React.ComponentType<DataItemProps<ValueType>>
7171
PreComponent: React.ComponentType<DataItemProps<ValueType>>
7272
PostComponent: React.ComponentType<DataItemProps<ValueType>>
7373
}
7474

7575
export function createDataType<ValueType = unknown> (
76-
is: (value: unknown) => boolean,
76+
is: (value: unknown, path: Path) => boolean,
7777
Component: React.ComponentType<DataItemProps<ValueType>>,
7878
Editor?: React.ComponentType<EditorProps<ValueType>> | undefined,
7979
PreComponent?: React.ComponentType<DataItemProps<ValueType>> | undefined,

0 commit comments

Comments
 (0)