You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `defineDataType` function allows you to define custom data types for any data structure. This is useful for:
9
+
10
+
- Adding support for data types not supported by the library.
11
+
- Customizing or extending the appearance and functionality of existing data types.
12
+
13
+
## Introduction
14
+
15
+
To define a data type using `defineDataType`, you provide an object with various properties, such as `is`, `Component`, `PreComponent`, `PostComponent`, and `Editor`. These properties enable you to specify how to match values of the data type, how to render them, and how to edit them.
16
+
17
+
Before we dive into the details of defining a data type, let's take a closer look at the object that you'll need to provide to `defineDataType`.
46
18
47
19
#### `is` - The Type Matcher
48
20
49
-
`is`isafunctionthat takes a value and a path and returns `true` if the value is the type you want to define.
21
+
The `is` function takes a value and a path and returns true if the value belongs to the data type being defined.
50
22
51
23
#### `Component` - The Renderer
52
24
53
-
`Component`isaReactcomponentthatrendersthevalue.\
54
-
Itreceivesa`DataItemProps`objectasa`prop`.
25
+
The `Component` prop is a React component that renders the value of the data type. It receives a `DataItemProps` object as a `prop`, which includes the following:
For advanced use cases, you can provide `PreComponent` and `PostComponent` to render content before and after the value, respectively. This is useful if you want to add a button or implement an expandable view.
To enable editing for a data type, you need to provide `serialize` and `deserialize`functions to convert the value to and from a string representation. You can then use the `Editor` component to provide a custom editor for the stringified value. When the user edits the value, it will be parsed using `deserialize`, and the result will be passed to the`onChange` callback.
67
38
68
39
## Examples
69
40
70
41
### Adding support for image
71
42
72
-
Let's say you have an object that looks like this:
43
+
Suppose you have a JSON object that includes an image URL:
73
44
74
45
```json
75
46
{
76
47
"image": "https://i.imgur.com/1bX5QH6.jpg"
77
48
}
78
49
```
79
50
80
-
YoumightwanttopreviewimagesinyourJSON.\
81
-
Wecandefineadatatypefor it like this:
51
+
If you want to preview images directly in your JSON, you can define a data type for images:
* A data type definition, including methods for checking, serializing, and deserializing values, as well as components for rendering and editing values.
55
+
*
56
+
* @template ValueType The type of the value represented by this data type
57
+
*/
53
58
exporttypeDataType<ValueType=unknown>={
54
59
/**
55
-
* Type matcher - Whether the value belongs to the data type
60
+
* Determines whether a given value belongs to this data type.
61
+
*
62
+
* @param value The value to check
63
+
* @param path The path to the value within the input data structure
64
+
* @returns `true` if the value belongs to this data type, `false` otherwise
56
65
*/
57
66
is: (value: unknown,path: Path)=>boolean
58
67
/**
59
-
* transform the value to a string for editing
68
+
* Convert the value of this data type to a string for editing
60
69
*/
61
70
serialize?: (value: ValueType)=>string
62
71
/**
63
-
* parse the string back to a value
64
-
* throw an error if the input is invalid
65
-
* and the editor will ignore the change
72
+
* Converts a string representation of a value back to a value of this data type.
73
+
*
74
+
* Throws an error if the input is invalid, in which case the editor will ignore the change.
66
75
*/
67
76
deserialize?: (value: string)=>ValueType
77
+
/**
78
+
* The main component used to render a value of this data type.
0 commit comments