Skip to content

Commit

Permalink
feat #300 - Refac
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Apr 22, 2021
1 parent a905d31 commit 345cc8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
27 changes: 3 additions & 24 deletions src/components/JSONPathPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import noop from 'lodash/noop'
import { recursiveRender } from './utils'
import { useStyles } from './styles'
import React, { FC } from 'react'
Expand All @@ -19,39 +20,17 @@ export interface JSONPathPickerProps {
export const JSONPathPicker: FC<JSONPathPickerProps> = ({
json,
path,
onChange
onChange = noop
}: JSONPathPickerProps) => {
const classes = useStyles()

const onClick = (e: any) => {
const target = e.target

if (target.hasAttribute('data-json-path')) {
const jsonPath = target.getAttribute('data-json-path')
// let choosenPath

// if (target.hasAttribute('data-choosearr')) {
// const tmp = getPathArr(path)

// const idx = getPathArr(pathKey).length
// tmp[idx] = '[*]'
// choosenPath = tmp.join('')
// } else {
// choosenPath = pathKey
// }

// onChange && onChange(choosenPath)
onChange && onChange(jsonPath)
}
}

return (
<div className={classes.container}>
{recursiveRender({
classes,
currPath: '$',
isLastItem: true,
onClick,
onChange,
pickedPath: path || '',
remainingJSON: json
})}
Expand Down
21 changes: 8 additions & 13 deletions src/components/JSONPathPicker/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Classes } from './styles'
import isNull from 'lodash/isNull'
import isUndefined from 'lodash/isUndefined'
import { JSONValue } from '.'
import React, { ReactNode } from 'react'

Expand All @@ -20,13 +19,10 @@ interface RenderParams {
pickedPath: string
isLastItem?: boolean
currPath?: string
onClick: (e: any) => void
onChange: (pickedPath: string) => void
remainingJSON: JSONValue | Record<string, JSONValue>
}

const getPathArr = (path = ''): string[] =>
path.match(/(\..+?)|(\[.+?\])/g) || []

const renderComma = ({
classes,
isLastItem
Expand All @@ -36,7 +32,7 @@ const renderComma = ({
const renderArray = ({
classes,
isLastItem,
onClick,
onChange,
pickedPath,
currPath,
remainingJSON
Expand All @@ -60,7 +56,7 @@ const renderArray = ({
classes,
currPath: `${currPath}[${i}]`,
isLastItem: arr.length - 1 === i,
onClick,
onChange,
pickedPath,
remainingJSON: item
})}
Expand Down Expand Up @@ -115,7 +111,7 @@ const renderString = ({
const renderObject = ({
classes,
isLastItem,
onClick,
onChange,
pickedPath,
remainingJSON,
currPath
Expand All @@ -131,15 +127,14 @@ const renderObject = ({
<li key={i}>
<span
className={classes.property}
data-json-path={`${currPath}.${key}`}
onClick={onClick}
onClick={() => onChange(`${currPath}.${key}`)}
>{`"${key}"`}</span>
<span className={classes.operator}>:</span>
{recursiveRender({
classes,
currPath: `${currPath}.${key}`,
isLastItem: remainingKeys.length - 1 === i,
onClick,
onChange,
pickedPath,
remainingJSON: json[key]
})}
Expand Down Expand Up @@ -182,14 +177,14 @@ export const recursiveRender = ({
pickedPath,
isLastItem,
currPath,
onClick,
onChange,
remainingJSON
}: RenderParams): ReactNode =>
mappedTypesToRenderFns[getRemainingJSONType(remainingJSON)]({
classes,
currPath,
isLastItem,
onClick,
onChange,
pickedPath,
remainingJSON
})

0 comments on commit 345cc8e

Please sign in to comment.