Skip to content

Commit

Permalink
feat: support onSelect callback (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfklauberg authored Feb 23, 2023
1 parent 5718165 commit 81ef6ed
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const Component = () => (<JsonViewer value={object}/>)
| `valueTypes` | `ValueTypes` | - | Customize value types. |
| `onChange` | `(path, oldVal, newVal) => void` | - | Callback when value changed. |
| `onCopy` | `(path, value) => void` | - | Callback when value copied. |
| `onSelect` | `(path, value) => void` | - | Callback when value selected. |
| `enableClipboard` | `boolean` | `true` | Whether enable clipboard feature. |
| `editable` | `boolean` \|<br />`(path, currentValue) => boolean` | `false` | Whether enable edit feature. You can pass a function to customize the result. |
| `defaultInspectDepth` | `number` | 5 | Default inspect depth for nested objects.<br /><br />_\* If the number is set too large, it could result in performance issues._ |
Expand Down
4 changes: 3 additions & 1 deletion src/components/DataTypes/createEasyType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function createEasyType<Value> (
const EasyType: React.FC<DataItemProps<Value>> = (props) => {
const storeDisplayDataTypes = useJsonViewerStore(store => store.displayDataTypes)
const color = useJsonViewerStore(store => store.colorspace[colorKey])
const onSelect = useJsonViewerStore(store => store.onSelect)

return (
<DataBox sx={{ color }} >
<DataBox onClick={() => onSelect?.(props.path, props.value)} sx={{ color }} >
{(displayTypeLabel && storeDisplayDataTypes) && <DataTypeLabel dataType={type}/>}
<DataBox className={`${type}-value`}>
<Render value={props.value}/>
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const JsonViewerInner: React.FC<JsonViewerProps> = (props) => {
useSetIfNotUndefinedEffect('displayDataTypes', props.displayDataTypes)
useSetIfNotUndefinedEffect('displayObjectSize', props.displayObjectSize)
useSetIfNotUndefinedEffect('onCopy', props.onCopy)
useSetIfNotUndefinedEffect('onSelect', props.onSelect)
useEffect(() => {
if (props.theme === 'light') {
api.setState({
Expand Down
3 changes: 3 additions & 0 deletions src/stores/JsonViewerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { combine } from 'zustand/middleware'
import type {
JsonViewerOnChange,
JsonViewerOnCopy,
JsonViewerOnSelect,
JsonViewerProps,
Path
} from '..'
Expand Down Expand Up @@ -34,6 +35,7 @@ export type JsonViewerState<T = unknown> = {
value: T
onChange: JsonViewerOnChange
onCopy: JsonViewerOnCopy | undefined
onSelect: JsonViewerOnSelect | undefined
keyRenderer: JsonViewerKeyRenderer
displayObjectSize: boolean
}
Expand Down Expand Up @@ -61,6 +63,7 @@ export const createJsonViewerStore = <T = unknown> (props: JsonViewerProps<T>) =
rootName: props.rootName ?? 'root',
onChange: props.onChange ?? (() => {}),
onCopy: props.onCopy ?? undefined,
onSelect: props.onSelect ?? undefined,
keyRenderer: props.keyRenderer ?? DefaultKeyRenderer,
editable: props.editable ?? false,
defaultInspectDepth: props.defaultInspectDepth ?? 5,
Expand Down
10 changes: 10 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export type JsonViewerOnCopy = <U = unknown>(
value: U
) => unknown | Promise<unknown>

/**
* @param path path to the target value
* @param value
*/
export type JsonViewerOnSelect = <U = unknown>(
path: Path,
value: U,
) => void

export interface DataItemProps<ValueType = unknown> {
inspect: boolean
setInspect: Dispatch<SetStateAction<boolean>>
Expand Down Expand Up @@ -75,6 +84,7 @@ export type JsonViewerProps<T = unknown> = {
valueTypes?: DataType<any>[]
onChange?: JsonViewerOnChange
onCopy?: JsonViewerOnCopy
onSelect?: JsonViewerOnSelect
/**
* Whether enable clipboard feature.
*
Expand Down

0 comments on commit 81ef6ed

Please sign in to comment.