Skip to content

feat: support path in dataType #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/DataKeyPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (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
Expand Down
10 changes: 5 additions & 5 deletions src/stores/typeRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -59,10 +59,10 @@ const objectType: DataType<object> = {
}

export function matchTypeComponents<Value> (
value: Value, registry: TypeRegistryState['registry']): DataType<Value> {
value: Value, path: Path, registry: TypeRegistryState['registry']): DataType<Value> {
let potential: DataType<Value> | 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`
Expand All @@ -79,9 +79,9 @@ export function matchTypeComponents<Value> (
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<any>[] {
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type DataType<ValueType = unknown> = {
/**
* Whether the value belongs to the data type
*/
is: (value: unknown) => boolean
is: (value: unknown, path: Path) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor?: React.ComponentType<EditorProps<ValueType>>
PreComponent?: React.ComponentType<DataItemProps<ValueType>>
Expand Down
20 changes: 10 additions & 10 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -28,52 +28,52 @@ export const applyValue = (obj: any, path: (string | number)[], value: any) => {

// case 1: you only render with a single component
export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
is: (value: unknown, path: Path) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => boolean
is: (value: unknown, path: Path) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
}
// case 2: you only render with a single component with editor
export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
is: (value: unknown, path: Path) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>,
Editor: React.ComponentType<EditorProps<ValueType>>
): {
is: (value: unknown) => boolean
is: (value: unknown, path: Path) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor: React.ComponentType<DataItemProps<ValueType>>
}
// case 3: you only render with a component with pre and post.
export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
is: (value: unknown, path: Path) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>,
Editor: undefined,
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
PostComponent: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => boolean
is: (value: unknown, path: Path) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
PreComponent: React.ComponentType<DataItemProps<ValueType>>
PostComponent: React.ComponentType<DataItemProps<ValueType>>
}
// case 4: need all of these
export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
is: (value: unknown, path: Path) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>,
Editor: React.ComponentType<EditorProps<ValueType>>,
PreComponent: React.ComponentType<DataItemProps<ValueType>>,
PostComponent: React.ComponentType<DataItemProps<ValueType>>
): {
is: (value: unknown) => boolean
is: (value: unknown, path: Path) => boolean
Component: React.ComponentType<DataItemProps<ValueType>>
Editor: React.ComponentType<DataItemProps<ValueType>>
PreComponent: React.ComponentType<DataItemProps<ValueType>>
PostComponent: React.ComponentType<DataItemProps<ValueType>>
}

export function createDataType<ValueType = unknown> (
is: (value: unknown) => boolean,
is: (value: unknown, path: Path) => boolean,
Component: React.ComponentType<DataItemProps<ValueType>>,
Editor?: React.ComponentType<EditorProps<ValueType>> | undefined,
PreComponent?: React.ComponentType<DataItemProps<ValueType>> | undefined,
Expand Down