Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add displayArrayKey Option for Rendering Array Keys in JSONViewer #19

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The viewMode property seems a bit wasteful as it is only used to determine whether to display commas.😆
Let me think twice...🤔

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import 'react18-json-view/src/style.css'
| `onDelete` | `function` | - | `(params:{ value: any,indexOrName: string \| number,depth: number,src: any,parentType: 'object' \| 'array'}) => void` |
| `onEdit` | `function` | - | `(params: { newValue: any, oldValue: any, depth: number, src: any, indexOrName: string \| number, parentType: 'object' \| 'array'}) => void` |
| `customizeNode` | `ReactElement`\|`ReactComponent`\|`Options` | - | Highly customize every node. |
| `displayArrayKey` | `boolean` | `true` | Whether to display the index of `Array` or not. |
| `viewMode` | `'viewer'` \| `'json'` | `'viewer'` | When set to json, commas are append to each line. |

### Collapsed function
```ts
Expand Down
47 changes: 19 additions & 28 deletions src/components/json-view.tsx
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting here is not that friendly.😅

Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import { createContext, useCallback, useState } from 'react'
import JsonNode from './json-node'
import type { Collapsed, CustomizeNode, DisplaySize, Editable } from '../types'
import type { Collapsed, CustomizeNode, DisplaySize, Editable, ViewMode } from '../types'

type OnEdit = (params: {
newValue: any
oldValue: any
depth: number
src: any
indexOrName: string | number
parentType: 'object' | 'array'
}) => void
type OnDelete = (params: {
value: any
indexOrName: string | number
depth: number
src: any
parentType: 'object' | 'array'
}) => void
type OnEdit = (params: { newValue: any; oldValue: any; depth: number; src: any; indexOrName: string | number; parentType: 'object' | 'array' }) => void
type OnDelete = (params: { value: any; indexOrName: string | number; depth: number; src: any; parentType: 'object' | 'array' }) => void
type OnAdd = (params: { indexOrName: string | number; depth: number; src: any; parentType: 'object' | 'array' }) => void
type OnChange = (params: {
indexOrName: string | number
depth: number
src: any
parentType: 'object' | 'array'
type: 'add' | 'edit' | 'delete'
}) => void
type OnChange = (params: { indexOrName: string | number; depth: number; src: any; parentType: 'object' | 'array'; type: 'add' | 'edit' | 'delete' }) => void

export const JsonViewContext = createContext({
src: undefined as any,
Expand All @@ -47,7 +28,10 @@ export const JsonViewContext = createContext({

customizeNode: undefined as CustomizeNode | undefined,

displaySize: undefined as DisplaySize
displaySize: undefined as DisplaySize,

displayArrayKey: true,
viewMode: 'viewer' as ViewMode
})

interface Props {
Expand All @@ -73,6 +57,8 @@ interface Props {
theme?: 'default' | 'a11y' | 'github' | 'vscode' | 'atom' | 'winter-is-coming'

displaySize?: DisplaySize
displayArrayKey?: boolean
viewMode?: ViewMode
}

export default function JsonView({
Expand All @@ -97,7 +83,10 @@ export default function JsonView({

customizeNode,

displaySize
displaySize,

displayArrayKey = true,
viewMode = 'viewer'
}: Props) {
const [_, update] = useState(0)
const forceUpdate = useCallback(() => update(state => ++state), [])
Expand Down Expand Up @@ -125,10 +114,12 @@ export default function JsonView({

customizeNode,

displaySize
displaySize,

displayArrayKey,
viewMode
}}>
<code
className={'json-view' + (dark ? ' dark' : '') + (theme && theme !== 'default' ? ' json-view_' + theme : '')}>
<code className={'json-view' + (dark ? ' dark' : '') + (theme && theme !== 'default' ? ' json-view_' + theme : '')}>
<JsonNode node={src} depth={1} />
</code>
</JsonViewContext.Provider>
Expand Down
27 changes: 15 additions & 12 deletions src/components/name-value.tsx
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The important point is to use the displayArrayKey from useContext instead of props. Also, the conditional operator is enough.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useContext } from 'react'
import { JsonViewContext } from './json-view'
import JsonNode from './json-node'

interface Props {
Expand All @@ -10,20 +12,21 @@ interface Props {
}

export default function NameValue({ indexOrName, value, depth, parent, deleteHandle, editHandle }: Props) {
const { displayArrayKey, viewMode } = useContext(JsonViewContext)
const isArray = Array.isArray(parent)

return (
<div className='json-view--pair'>
<span className={typeof indexOrName === 'number' ? 'json-view--index' : 'json-view--property'}>
{indexOrName}
</span>
:{' '}
<JsonNode
node={value}
depth={depth + 1}
deleteHandle={deleteHandle}
editHandle={editHandle}
parent={parent}
indexOrName={indexOrName}
/>
{!isArray || displayArrayKey ? (
<span>
<span className={typeof indexOrName === 'number' ? 'json-view--index' : 'json-view--property'}>{indexOrName}</span>
<span>: </span>
</span>
) : null}

<JsonNode node={value} depth={depth + 1} deleteHandle={deleteHandle} editHandle={editHandle} parent={parent} indexOrName={indexOrName} />

{viewMode === 'json' && <span>,</span>}
</div>
)
}
18 changes: 16 additions & 2 deletions src/stories/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export const argTypes = {
table: {
defaultValue: { summary: false }
},
description:
'When set to true, you can add, edit, or delete the property, and the actions will trigger onAdd, onEdit, or onDelete. Options is available.'
description: 'When set to true, you can add, edit, or delete the property, and the actions will trigger onAdd, onEdit, or onDelete. Options is available.'
},
onAdd: {
description: `(params: { indexOrName: string | number, depth: number, src: any; parentType: 'object' | 'array' }) => void`
Expand All @@ -83,5 +82,20 @@ export const argTypes = {
},
customizeNode: {
description: 'Highly customize every node.'
},
displayArrayKey: {
control: 'boolean',
description: 'Sets whether to display the array key or not.',
table: {
defaultValue: { summary: true }
}
},
viewMode: {
control: 'select',
options: ['viewer', 'json'],
description: 'Sets the viewer mode to viewer or json. With json mode, commas are displayed.',
table: {
defaultValue: { summary: 'viewer' }
}
}
}
9 changes: 3 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ export declare type Collapsed =
| undefined
| number
| boolean
| ((params: {
node: Record<string, any> | Array<any>
indexOrName: number | string | undefined
depth: number
size: number
}) => boolean | void)
| ((params: { node: Record<string, any> | Array<any>; indexOrName: number | string | undefined; depth: number; size: number }) => boolean | void)

export type DisplaySize = undefined | number | boolean | 'collapsed' | 'expanded'

Expand All @@ -32,3 +27,5 @@ export declare type CustomizeNode = (params: {
indexOrName: number | string | undefined
depth: number
}) => CustomizeOptions | React.FC | React.Component | React.ReactElement<any, any> | undefined

export declare type ViewMode = 'viewer' | 'json'