budibase/packages/builder/src/builderStore/getNewComponentName.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-08-07 13:01:16 +02:00
import { walkProps } from "./storeUtils"
import { get_capitalised_name } from "../helpers"
export default function(component, state) {
const capitalised = get_capitalised_name(component)
const matchingComponents = []
const findMatches = props => {
walkProps(props, c => {
2020-10-07 23:30:00 +02:00
const thisInstanceName = get_capitalised_name(c._instanceName)
if ((thisInstanceName || "").startsWith(capitalised)) {
matchingComponents.push(thisInstanceName)
2020-08-07 13:01:16 +02:00
}
})
}
// check page first
findMatches(state.pages[state.currentPageName].props)
2020-08-07 13:01:16 +02:00
// if viewing screen, check current screen for duplicate
if (state.currentFrontEndType === "screen") {
findMatches(state.currentPreviewItem.props)
} else {
// viewing master page - need to find against all screens
for (let screen of state.screens) {
findMatches(screen.props)
2020-08-07 13:01:16 +02:00
}
}
2020-08-07 13:01:16 +02:00
2020-08-07 13:09:48 +02:00
let index = 1
2020-08-07 13:01:16 +02:00
let name
while (!name) {
const tryName = `${capitalised} ${index}`
if (!matchingComponents.includes(tryName)) name = tryName
index++
}
return name
}