Handle prevention of duplicate keys

This commit is contained in:
josdejong 2020-05-22 09:35:18 +02:00
parent dfe7c06ec7
commit d88120d41c
1 changed files with 25 additions and 19 deletions

View File

@ -5,6 +5,7 @@
import { SEARCH_PROPERTY, SEARCH_VALUE } from './search' import { SEARCH_PROPERTY, SEARCH_VALUE } from './search'
import classnames from 'classnames' import classnames from 'classnames'
import debounce from 'lodash/debounce' import debounce from 'lodash/debounce'
import { findUniqueName } from './utils/stringUtils.js'
import { isUrl, stringConvert, valueType } from './utils/typeUtils' import { isUrl, stringConvert, valueType } from './utils/typeUtils'
import { updateProps } from './utils/updateProps.js' import { updateProps } from './utils/updateProps.js'
import { compileJSONPointer } from './utils/jsonPointer' import { compileJSONPointer } from './utils/jsonPointer'
@ -185,6 +186,10 @@
function handleChangeKey (newChildKey, oldChildKey) { function handleChangeKey (newChildKey, oldChildKey) {
if (type === 'object') { if (type === 'object') {
// make sure the key is not a duplicate of an other property
const uniqueNewChildKey = findUniqueName(newChildKey, value)
if (uniqueNewChildKey !== oldChildKey) {
// we need to make sure that the renamed property will keep the same id // we need to make sure that the renamed property will keep the same id
const index = props.findIndex(item => item.key === oldChildKey) const index = props.findIndex(item => item.key === oldChildKey)
if (index !== -1) { if (index !== -1) {
@ -195,7 +200,7 @@
// TODO: is there a better way to do this? // TODO: is there a better way to do this?
props.splice(index, 1, { props.splice(index, 1, {
id: props[index].id, id: props[index].id,
key: newChildKey key: uniqueNewChildKey
}) })
} }
@ -203,10 +208,11 @@
onChange([{ onChange([{
op: 'move', op: 'move',
from: compileJSONPointer(path.concat(oldChildKey)), from: compileJSONPointer(path.concat(oldChildKey)),
path: compileJSONPointer(path.concat(newChildKey)) path: compileJSONPointer(path.concat(uniqueNewChildKey))
}]) }])
} }
} }
}
function handleShowAll () { function handleShowAll () {
limit = Infinity limit = Infinity