Skip to content

feat: copy #10

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

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const App = () => {
{ value: 'shanghai', label: 'Shanghai' },
],
}}
copy
/>
</div>
<div
Expand Down
5,591 changes: 0 additions & 5,591 deletions example/yarn.lock

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"size-limit": [
{
"path": "dist/react-json-editor-ui.cjs.production.min.js",
"limit": "10 KB"
"limit": "100 KB"
},
{
"path": "dist/react-json-editor-ui.esm.js",
"limit": "10 KB"
"limit": "100 KB"
}
],
"devDependencies": {
Expand Down
20 changes: 19 additions & 1 deletion src/components/JsonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ConfigContext } from '../store'
import ArrayView from './ArrayView'
import ToolsView from './Tools'
import CollapsePart from './Collapse'
import cloneDeep from 'lodash.clonedeep'

export type JsonViewProps = {
setEditObject: any
Expand All @@ -23,10 +24,11 @@ export type JsonViewProps = {
label?: string
}>
>
copy?: boolean
}

function JsonView(props: JsonViewProps) {
const { editObject, setEditObject, optionsMap } = props
const { editObject, setEditObject, optionsMap, copy } = props
const [allowMap, setAllowMap] = useState<Record<string, boolean>>({})

const syncData = (data: Record<string, any>) => {
Expand All @@ -42,6 +44,20 @@ function JsonView(props: JsonViewProps) {
syncData(editObject)
}

const onClickCopy = (key: string, value: string, sourceData: any) => {
if (Array.isArray(sourceData)) {
sourceData.splice(+key + 1, 0, typeof value === 'object' ? cloneDeep(value) : value);
} else {
const keys = Object.keys(sourceData);
const filterEqual = keys.filter(i => i.includes(key + '-'));
const sp = filterEqual.at(-1)?.split('-');
const lastNumber = sp && sp.at(-1);
const index = +lastNumber! + 1 || 1;
Reflect.set(sourceData, `${key}-${index}`, cloneDeep(Reflect.get(sourceData, key)));
}
syncData(editObject)
}

const onChangeType = (type: DataType, fieldValue: any) => {
const newEditObject = getQuoteAddress(fieldValue, typeMap[type], editObject)
syncData(newEditObject)
Expand Down Expand Up @@ -237,9 +253,11 @@ function JsonView(props: JsonViewProps) {
editObject,
setEditObject,
optionsMap,
copy,

onChangeType,
onClickDelete,
onClickCopy,
onChangeAllow,
allowMap,
}}
Expand Down
12 changes: 10 additions & 2 deletions src/components/Tools.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MinusSquareOutlined } from '@ant-design/icons'
import { MinusSquareOutlined, CopyOutlined } from '@ant-design/icons'
import { Select } from 'antd'
import React from 'react'
import { ConfigContext } from '../store'
Expand All @@ -11,7 +11,7 @@ function ToolsView(props: {
}) {
return (
<ConfigContext.Consumer>
{({ onChangeType, onClickDelete }) => (
{({ onChangeType, onClickDelete, onClickCopy, copy }) => (
<span className="tools">
<span>
<Select
Expand All @@ -33,6 +33,14 @@ function ToolsView(props: {
onClick={() => onClickDelete(props.fieldKey, props.sourceData)}
/>
</span>
{copy && (
<span className="iconCopy">
<CopyOutlined
style={props.fieldKey.includes('-') ? {visibility: 'hidden'} : {color: '#3078f6'}}
onClick={() => onClickCopy(props.fieldKey, props.fieldValue, props.sourceData)}
/>
</span>
)}
</span>
)}
</ConfigContext.Consumer>
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type JsonEditorProps = {
label?: string
}>
>
copy?: boolean
onChange: (data: any) => void
}

Expand All @@ -30,6 +31,7 @@ function JsonEditor(props: JsonEditorProps) {
editObject,
setEditObject,
optionsMap: props.optionsMap,
copy: props.copy,
}}
/>
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/styles/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
.iconSubtraction {
margin-left: 10px;
}

.iconCopy {
margin-left: 10px;
}
}

.addItem {
Expand Down
Loading