Skip to content

Commit d727adb

Browse files
committed
feat: expose defineEasyType for easier customization
1 parent 7bcbb64 commit d727adb

File tree

9 files changed

+40
-19
lines changed

9 files changed

+40
-19
lines changed

src/components/DataTypes/Boolean.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
import { createEasyType } from './createEasyType'
2+
import { defineEasyType } from './defineEasyType'
33

4-
export const booleanType = createEasyType<boolean>({
4+
export const booleanType = defineEasyType<boolean>({
55
is: (value) => typeof value === 'boolean',
66
type: 'bool',
77
colorKey: 'base0E',

src/components/DataTypes/Date.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { createEasyType } from './createEasyType'
2+
import { defineEasyType } from './defineEasyType'
33

44
const displayOptions: Intl.DateTimeFormatOptions = {
55
weekday: 'short',
@@ -10,7 +10,7 @@ const displayOptions: Intl.DateTimeFormatOptions = {
1010
minute: '2-digit'
1111
}
1212

13-
export const dateType = createEasyType<Date>({
13+
export const dateType = defineEasyType<Date>({
1414
is: (value) => value instanceof Date,
1515
type: 'date',
1616
colorKey: 'base0D',

src/components/DataTypes/Null.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Box } from '@mui/material'
22

33
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
4-
import { createEasyType } from './createEasyType'
4+
import { defineEasyType } from './defineEasyType'
55

6-
export const nullType = createEasyType<null>({
6+
export const nullType = defineEasyType<null>({
77
is: (value) => value === null,
88
type: 'null',
99
colorKey: 'base08',

src/components/DataTypes/Number.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Box } from '@mui/material'
22

33
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
4-
import { createEasyType } from './createEasyType'
4+
import { defineEasyType } from './defineEasyType'
55

66
const isInt = (n: number) => n % 1 === 0
77

8-
export const nanType = createEasyType<number>({
8+
export const nanType = defineEasyType<number>({
99
is: (value) => typeof value === 'number' && isNaN(value),
1010
type: 'NaN',
1111
colorKey: 'base08',
@@ -31,7 +31,7 @@ export const nanType = createEasyType<number>({
3131
}
3232
})
3333

34-
export const floatType = createEasyType<number>({
34+
export const floatType = defineEasyType<number>({
3535
is: (value) => typeof value === 'number' && !isInt(value) && !isNaN(value),
3636
type: 'float',
3737
colorKey: 'base0B',
@@ -40,7 +40,7 @@ export const floatType = createEasyType<number>({
4040
Renderer: ({ value }) => <>{value}</>
4141
})
4242

43-
export const intType = createEasyType<number>({
43+
export const intType = defineEasyType<number>({
4444
is: (value) => typeof value === 'number' && isInt(value),
4545
type: 'int',
4646
colorKey: 'base0F',
@@ -50,7 +50,7 @@ export const intType = createEasyType<number>({
5050
Renderer: ({ value }) => <>{value}</>
5151
})
5252

53-
export const bigIntType = createEasyType<bigint>({
53+
export const bigIntType = defineEasyType<bigint>({
5454
is: (value) => typeof value === 'bigint',
5555
type: 'bigint',
5656
colorKey: 'base0F',

src/components/DataTypes/String.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Box } from '@mui/material'
22
import { useState } from 'react'
33

44
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
5-
import { createEasyType } from './createEasyType'
5+
import { defineEasyType } from './defineEasyType'
66

7-
export const stringType = createEasyType<string>({
7+
export const stringType = defineEasyType<string>({
88
is: (value) => typeof value === 'string',
99
type: 'string',
1010
colorKey: 'base09',

src/components/DataTypes/Undefined.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Box } from '@mui/material'
22

33
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
4-
import { createEasyType } from './createEasyType'
4+
import { defineEasyType } from './defineEasyType'
55

6-
export const undefinedType = createEasyType<undefined>({
6+
export const undefinedType = defineEasyType<undefined>({
77
is: (value) => value === undefined,
88
type: 'undefined',
99
colorKey: 'base05',

src/components/DataTypes/createEasyType.tsx renamed to src/components/DataTypes/defineEasyType.tsx

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@ import type { DataItemProps, DataType, EditorProps } from '../../type'
88
import { DataTypeLabel } from '../DataTypeLabel'
99
import { DataBox } from '../mui/DataBox'
1010

11-
type EasyTypeConfig<Value> = Pick<DataType<Value>, 'is' | 'serialize' | 'deserialize'> & {
11+
export type EasyTypeConfig<Value> = Pick<DataType<Value>, 'is' | 'serialize' | 'deserialize'> & {
1212
type: string
1313
colorKey: keyof Colorspace
1414
displayTypeLabel?: boolean
15-
Renderer: ComponentType<Pick<DataItemProps<Value>, 'value'>>
15+
Renderer: ComponentType<DataItemProps<Value>>
1616
}
17-
export function createEasyType<Value> ({
17+
/**
18+
* Enhanced version of `defineDataType` that creates a `DataType` with editor and a optional type label.
19+
* It will take care of the color and all the necessary props.
20+
*
21+
* *All of the built-in data types are defined with this function.*
22+
*
23+
* @param config.type The type name.
24+
* @param config.colorKey The color key in the colorspace. ('base00' - 'base0F')
25+
* @param config.displayTypeLabel Whether to display the type label.
26+
* @param config.Renderer The component to render the value.
27+
*/
28+
export function defineEasyType<Value> ({
1829
is,
1930
serialize,
2031
deserialize,
@@ -33,7 +44,13 @@ export function createEasyType<Value> ({
3344
<DataBox onClick={() => onSelect?.(props.path, props.value)} sx={{ color }}>
3445
{(displayTypeLabel && storeDisplayDataTypes) && <DataTypeLabel dataType={type} />}
3546
<DataBox className={`${type}-value`}>
36-
<Render value={props.value} />
47+
<Render
48+
path={props.path}
49+
inspect={props.inspect}
50+
setInspect={props.setInspect}
51+
value={props.value}
52+
prevValue={props.prevValue}
53+
/>
3754
</DataBox>
3855
</DataBox>
3956
)

src/components/DataTypes/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { booleanType } from './Boolean'
22
export { dateType } from './Date'
3+
export { defineEasyType, type EasyTypeConfig } from './defineEasyType'
34
export { functionType } from './Function'
45
export { nullType } from './Null'
56
export { bigIntType, floatType, intType, nanType } from './Number'

src/utils/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export function createDataType<ValueType = unknown> (
142142
}
143143
}
144144

145+
/**
146+
* Define custom data types for any data structure
147+
*/
145148
export function defineDataType<ValueType = unknown> ({
146149
is,
147150
serialize,

0 commit comments

Comments
 (0)