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, getName,
numberFirstItem, numberFirstItem,
separator = "",
}: { }: {
getName?: (item: T) => string getName?: (item: T) => string
numberFirstItem?: boolean numberFirstItem?: boolean
separator?: string
} = {} } = {}
) => { ) => {
if (!prefix?.length) { if (!prefix?.length) {
return null return ""
} }
const trimmedPrefix = prefix.trim() const trimmedPrefix = prefix.trim()
const firstName = numberFirstItem ? `${prefix}1` : trimmedPrefix const firstName = numberFirstItem ? `${prefix}1` : trimmedPrefix
@ -107,5 +109,5 @@ export const getSequentialName = <T extends any>(
max = num 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, Table,
} from "@budibase/types" } from "@budibase/types"
import { utils } from "@budibase/shared-core" import { utils } from "@budibase/shared-core"
import { getSequentialName } from "@/helpers/duplicate"
interface Component extends ComponentType { interface Component extends ComponentType {
_id: string _id: string
@ -467,15 +468,14 @@ export class ComponentStore extends BudiStore<ComponentState> {
return null return null
} }
let componentName = `New ${definition.friendlyName || definition.name}` const componentName = getSequentialName(
const $screenComponents = get(screenComponents) get(screenComponents),
`New ${definition.friendlyName || definition.name}`,
const sameNameCount = $screenComponents.filter(c => {
new RegExp(`^${componentName}( \\d*)?$`).test(c._instanceName) getName: c => c._instanceName,
).length separator: " ",
if (sameNameCount) { }
componentName = `${componentName} ${sameNameCount + 1}` )
}
// Generate basic component structure // Generate basic component structure
let instance: Component = { let instance: Component = {