Extend Built-in Data Types
The following is a list of the built-in data types that can be extended:
stringTypeintTypefloatTypebooleanTypedateTypenanTypenullTypeundefinedTypebigIntTypeobjectTypefunctionType
Example
Displaying Boolean Values as Icons
Suppose you want to display boolean values as icons (e.g., ✔️ or ❌) instead of the default true or false. There are two ways to accomplish this:
- Override the
Componentproperty of the built-in booleanType data type:
{}
"agree"
:
✔️"disagree"
:
❌"description"
:
string
"Click the ✔️ ❌ to toggle the boolean value"
import { JsonViewer, defineDataType, booleanType } from '@textea/json-viewer'
import { Button } from '@mui/material'
const toggleBoolType = defineDataType<boolean>({
...booleanType,
Component: ({ value }) => (
<span>{value ? '✔️' : '❌'}</span>
)
})
<JsonViewer
value={{
agree: true,
disagree: false,
}}
valueTypes={[toggleBoolType]}
/>[Source Code] (opens in a new tab)
- Create a new data type with
defineEasyType
{}
"agree"
:
bool
✔️
"disagree"
:
bool
❌
"description"
:
string
"Click the ✔️ ❌ to toggle the boolean value"
import { defineEasyType, booleanType } from '@textea/json-viewer'
const booleanType = defineEasyType<boolean>({
...booleanType,
type: 'bool',
colorKey: 'base0E',
Renderer: ({ value }) => (
<span>{value ? '✔️' : '❌'}</span>
)
})
<JsonViewer
value={{
agree: true,
disagree: false,
}}
valueTypes={[toggleBoolType]}
/>[Source Code] (opens in a new tab)
Did you notice the difference between the two examples?
defineEasyType is a helper function that creates data types with type labels and colors. So you only need to care about how the value should be rendered. All other details will be automatically handled.
Last updated on