Skip to content

Commit 3352675

Browse files
committed
Add stringify and reviver functions to BigInt, NaN and Symbol components
1 parent 6c35cd6 commit 3352675

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

custom-component-library/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Custom components should consider the following:
5757
- `Enter` to submit
5858
- `Escape` to cancel
5959
- Provide customisation options, particularly styles
60+
- If the data contains non-JSON types, add a "stringify" and "reviver" function definition (see `BigInt`, `NaN` and `Symbol` components)
6061

6162
If your custom component is "string-like", there are two helper components exported with the package: `StringDisplay` and `StringEdit` -- these are the same components used for the actual "string" elements in the main package. See the [Hyperlink](https://github.com/CarlosNZ/json-edit-react/blob/main/custom-component-library/components/Hyperlink/component.tsx) and [BigInt](https://github.com/CarlosNZ/json-edit-react/blob/main/custom-component-library/components/BigInt/component.tsx) components for examples of how to use them.
6263

custom-component-library/components/NaN/definition.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NotANumberComponent, NaNProps } from './component'
2-
import { type CustomNodeDefinition } from '@json-edit-react'
2+
import { isCollection, type CustomNodeDefinition } from '@json-edit-react'
33

44
export const NanDefinition: CustomNodeDefinition<NaNProps> = {
55
condition: ({ value }) => Number.isNaN(value),
@@ -9,4 +9,9 @@ export const NanDefinition: CustomNodeDefinition<NaNProps> = {
99
name: 'NaN', // shown in the Type selector menu
1010
showInTypesSelector: true,
1111
defaultValue: NaN,
12+
stringifyReplacer: (value) => (Number.isNaN(value) ? { __type: 'NaN', value: 'NaN' } : value),
13+
parseReviver: (value) =>
14+
isCollection(value) && '__type' in value && 'value' in value && value.__type === 'NaN'
15+
? NaN
16+
: value,
1217
}

custom-component-library/components/Symbol/definition.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type CustomNodeDefinition } from '@json-edit-react'
1+
import { isCollection, type CustomNodeDefinition } from '@json-edit-react'
22
import { SymbolComponent, SymbolProps } from './component'
33

44
export const SymbolDefinition: CustomNodeDefinition<SymbolProps> = {
@@ -11,4 +11,10 @@ export const SymbolDefinition: CustomNodeDefinition<SymbolProps> = {
1111
name: 'Symbol', // shown in the Type selector menu
1212
showInTypesSelector: true,
1313
defaultValue: Symbol('New symbol'),
14+
stringifyReplacer: (value) =>
15+
typeof value === 'symbol' ? { __type: 'Symbol', value: value.description ?? '' } : value,
16+
parseReviver: (value) =>
17+
isCollection(value) && '__type' in value && 'value' in value && value.__type === 'Symbol'
18+
? Symbol((value.value as string) ?? null)
19+
: value,
1420
}

0 commit comments

Comments
 (0)