Use existing utils

This commit is contained in:
Adria Navarro 2025-01-30 16:47:04 +01:00
parent b7ce306e6a
commit 6f2d279f0e
2 changed files with 13 additions and 11 deletions

View File

@ -76,13 +76,15 @@ export const getSequentialName = <T extends any>(
{
getName,
numberFirstItem,
separator = "",
}: {
getName?: (item: T) => string
numberFirstItem?: boolean
separator?: string
} = {}
) => {
if (!prefix?.length) {
return null
return ""
}
const trimmedPrefix = prefix.trim()
const firstName = numberFirstItem ? `${prefix}1` : trimmedPrefix
@ -107,5 +109,5 @@ export const getSequentialName = <T extends any>(
max = num
}
})
return max === 0 ? firstName : `${prefix}${max + 1}`
return max === 0 ? firstName : `${prefix}${separator}${max + 1}`
}

View File

@ -38,6 +38,7 @@ import {
Table,
} from "@budibase/types"
import { utils } from "@budibase/shared-core"
import { getSequentialName } from "@/helpers/duplicate"
interface Component extends ComponentType {
_id: string
@ -467,15 +468,14 @@ export class ComponentStore extends BudiStore<ComponentState> {
return null
}
let componentName = `New ${definition.friendlyName || definition.name}`
const $screenComponents = get(screenComponents)
const sameNameCount = $screenComponents.filter(c =>
new RegExp(`^${componentName}( \\d*)?$`).test(c._instanceName)
).length
if (sameNameCount) {
componentName = `${componentName} ${sameNameCount + 1}`
}
const componentName = getSequentialName(
get(screenComponents),
`New ${definition.friendlyName || definition.name}`,
{
getName: c => c._instanceName,
separator: " ",
}
)
// Generate basic component structure
let instance: Component = {