Skip to content
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
76 changes: 39 additions & 37 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ function App() {
customNodeDefinitions={demoData[selectedData]?.customNodeDefinitions}
customText={demoData[selectedData]?.customTextDefinitions}
onChange={demoData[selectedData]?.onChange ?? undefined}
useJSON5Editor
/>
</Box>
<VStack w="100%" align="flex-end" gap={4}>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-edit-react",
"version": "1.12.0-rc1",
"version": "1.12.0-rc2",
"description": "React component for editing or viewing JSON/object data",
"main": "build/index.cjs.js",
"module": "build/index.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default [
bundleSize(),
sizes(),
],
external: [],
external: ['json5', 'just-clone', 'object-property-assigner', 'object-property-extractor'],
},
{
input: './build/dts/index.d.ts',
Expand Down
17 changes: 13 additions & 4 deletions src/CollectionNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
type ErrorString,
type NodeData,
type JerError,
type CollectionData,
ERROR_DISPLAY_TIME,
CollectionData,
} from './types'
import { Icon } from './Icons'
import { filterNode, isCollection } from './filterHelpers'
Expand Down Expand Up @@ -55,8 +55,17 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
defaultValue,
translate,
customNodeDefinitions,
useJSON5Editor,
} = props
const [stringifiedValue, setStringifiedValue] = useState(JSON.stringify(data, null, 2))
const stringifyJson = useMemo(() => {
if (!useJSON5Editor) return (data: object) => JSON.stringify(data, null, 2)
if (useJSON5Editor instanceof Object) {
return (data: object) => JSON5.stringify(data, useJSON5Editor)
}
return (data: object) => JSON5.stringify(data, { space: 2 })
}, [useJSON5Editor])

const [stringifiedValue, setStringifiedValue] = useState(stringifyJson(data))
const [error, setError] = useState<string | null>(null)

const startCollapsed = collapseFilter(incomingNodeData)
Expand All @@ -77,7 +86,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
const [isAnimating, setIsAnimating] = useState(false)

useEffect(() => {
setStringifiedValue(JSON.stringify(data, null, 2))
setStringifiedValue(stringifyJson(data))
}, [data])

useEffect(() => {
Expand Down Expand Up @@ -249,7 +258,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
const handleCancel = () => {
setCurrentlyEditingElement(null)
setError(null)
setStringifiedValue(JSON.stringify(data, null, 2))
setStringifiedValue(stringifyJson(data))
}

// DERIVED VALUES (this makes the JSX logic less messy)
Expand Down
2 changes: 2 additions & 0 deletions src/JsonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const Editor: React.FC<JsonEditorProps> = ({
id,
customText = {},
customNodeDefinitions = [],
useJSON5Editor = false,
}) => {
const { getStyles, setTheme, setIcons } = useTheme()
const { setCollapseState } = useTreeState()
Expand Down Expand Up @@ -196,6 +197,7 @@ const Editor: React.FC<JsonEditorProps> = ({
translate,
customNodeDefinitions,
parentData: null,
useJSON5Editor,
}

const mainContainerStyles = { ...getStyles('container', nodeData), minWidth, maxWidth }
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface JsonEditorProps {
translations?: Partial<LocalisedStrings>
customNodeDefinitions?: CustomNodeDefinition[]
customText?: CustomTextDefinitions
useJSON5Editor?: boolean | JSON5StringifyOptions
}

const ValueDataTypes = ['string', 'number', 'boolean', 'null'] as const
Expand Down Expand Up @@ -175,6 +176,7 @@ export interface CollectionNodeProps extends BaseNodeProps {
showCollectionCount: boolean | 'when-closed'
showStringQuotes: boolean
defaultValue: unknown
useJSON5Editor: boolean | JSON5StringifyOptions
}

export type ValueData = string | number | boolean
Expand Down Expand Up @@ -305,3 +307,14 @@ export type ThemeInput =
| Theme
| Partial<ThemeStyles>
| Array<ThemeName | Theme | Partial<ThemeStyles>>

// JSON5 options (https://json5.org/)
export interface JSON5StringifyOptions {
space?: number
quote?: string
replacer?:
| Array<string | number>
| ((this: any, key: string, value: any) => any)
| null
| undefined
}