Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Sep 17, 2022
1 parent bd43388 commit c966281
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/components/DataTypes/Array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const ArrayType: React.FC<DataItemProps<unknown[]>> = (props) => {
}
return array
}, [])
console.log('value', value)

return value.map((list, index) => {
const path = [...props.path]
Expand Down
25 changes: 16 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useCallback, useEffect, useMemo } from 'react'
import { DataKeyPair } from './components/DataKeyPair'
import {
createJsonViewerStore,
JsonViewerProvider, useJsonViewerStore,
JsonViewerProvider, JsonViewerState, useJsonViewerStore,
useJsonViewerStoreApi
} from './stores/JsonViewerStore'
import type { JsonViewerProps } from './type'
Expand All @@ -21,15 +21,22 @@ export type JsonViewerOnChange = <U = unknown>(path: (string | number)[], oldVal

const JsonViewerInner: React.FC<JsonViewerProps> = (props) => {
const api = useJsonViewerStoreApi()
const setIfNotUndefined = useCallback(function setIfNotUndefined <Key extends keyof JsonViewerState> (key: Key, value: JsonViewerState[Key] | undefined) {
if (value !== undefined) {
api.setState({
[key]: value
})
}
}, [api])
useEffect(() => {
api.setState(() => ({
value: props.value,
indentWidth: props.indentWidth,
defaultCollapsed: props.defaultCollapsed,
onChange: props.onChange,
groupArraysAfterLength: props.groupArraysAfterLength
}))
}, [api, props.defaultCollapsed, props.groupArraysAfterLength, props.indentWidth, props.onChange, props.value])
setIfNotUndefined('value', props.value)
}, [props.value, setIfNotUndefined])
useEffect(() => {
// setIfNotUndefined('indentWidth', props.indentWidth)
setIfNotUndefined('defaultCollapsed', props.defaultCollapsed)
setIfNotUndefined('onChange', props.onChange)
setIfNotUndefined('groupArraysAfterLength', props.groupArraysAfterLength)
}, [api, props.defaultCollapsed, props.groupArraysAfterLength, props.onChange, props.value, setIfNotUndefined])

const value = useJsonViewerStore(store => store.value)
const setHover = useJsonViewerStore(store => store.setHover)
Expand Down
1 change: 0 additions & 1 deletion src/stores/JsonViewerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export type JsonViewerActions = {
setHover: (path: (string | number)[] | null) => void
}

// todo
export const createJsonViewerStore = () =>
create(
combine<JsonViewerState, JsonViewerActions>(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const applyValue = (obj: any, path: string[], value: any) => {
export const applyValue = (obj: any, path: (string | number)[], value: any) => {
if (typeof obj !== 'object' || obj === null) {
return value
}
const arr: string[] = [...path]
const arr: (string | number)[] = [...path]
let key
if (path.length > 0) {
key = arr[0]
Expand Down

0 comments on commit c966281

Please sign in to comment.