@ -3,6 +3,7 @@ import type { ColumnReqType, ColumnType, TableType } from 'nocodb-sdk'
import { UITypes , isLinksOrLTAR } from 'nocodb-sdk'
import { UITypes , isLinksOrLTAR } from 'nocodb-sdk'
import type { Ref } from 'vue'
import type { Ref } from 'vue'
import type { RuleObject } from 'ant-design-vue/es/form'
import type { RuleObject } from 'ant-design-vue/es/form'
import { generateUniqueColumnName } from '~/helpers/parsers/parserHelpers'
const clone = rfdc ( )
const clone = rfdc ( )
@ -20,6 +21,7 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
column : Ref < ColumnType | undefined > ,
column : Ref < ColumnType | undefined > ,
tableExplorerColumns? : Ref < ColumnType [ ] | undefined > ,
tableExplorerColumns? : Ref < ColumnType [ ] | undefined > ,
fromTableExplorer? : Ref < boolean | undefined > ,
fromTableExplorer? : Ref < boolean | undefined > ,
isColumnValid? : Ref < ( ( value : Partial < ColumnType > ) = > boolean ) | undefined > ,
) = > {
) = > {
const baseStore = useBase ( )
const baseStore = useBase ( )
@ -69,33 +71,21 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
}
}
const formState = ref < Record < string , any > > ( {
const formState = ref < Record < string , any > > ( {
title : 'title ' ,
title : '' ,
uidt : fromTableExplorer?.value ? UITypes.SingleLineText : null ,
uidt : fromTableExplorer?.value ? UITypes.SingleLineText : null ,
. . . clone ( column . value || { } ) ,
. . . clone ( column . value || { } ) ,
} )
} )
const generateUniqueColumnSuffix = ( ) = > {
let suffix = ( meta . value ? . columns ? . length || 0 ) + 1
let columnName = ` title ${ suffix } `
while (
( tableExplorerColumns ? . value || meta . value ? . columns ) ? . some (
( c ) = > ( c . column_name || '' ) . toLowerCase ( ) === columnName . toLowerCase ( ) ,
)
) {
suffix ++
columnName = ` title ${ suffix } `
}
return suffix
}
// actions
// actions
const generateNewColumnMeta = ( ignoreUidt = false ) = > {
const generateNewColumnMeta = ( ignoreUidt = false ) = > {
setAdditionalValidations ( { } )
setAdditionalValidations ( { } )
formState . value = {
formState . value = {
meta : { } ,
meta : { } ,
. . . sqlUi . value . getNewColumn ( generateUniqueColumnSuffix ( ) ) ,
. . . sqlUi . value . getNewColumn ( 1 ) ,
}
}
formState . value . title = formState . value . column_name
formState . value . title = ''
formState . value . column_name = ''
if ( ignoreUidt && ! fromTableExplorer ? . value ) {
if ( ignoreUidt && ! fromTableExplorer ? . value ) {
formState . value . uidt = null
formState . value . uidt = null
}
}
@ -104,10 +94,14 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
const validators = computed ( ( ) = > {
const validators = computed ( ( ) = > {
return {
return {
title : [
title : [
. . . ( isEdit . value
? [
{
{
required : true ,
required : true ,
message : t ( 'msg.error.columnNameRequired' ) ,
message : t ( 'msg.error.columnNameRequired' ) ,
} ,
} ,
]
: [ ] ) ,
// validation for unique column name
// validation for unique column name
{
{
validator : ( rule : any , value : any ) = > {
validator : ( rule : any , value : any ) = > {
@ -158,16 +152,19 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
const onUidtOrIdTypeChange = ( ) = > {
const onUidtOrIdTypeChange = ( ) = > {
disableSubmitBtn . value = false
disableSubmitBtn . value = false
const newTitle = updateFieldName ( false )
const colProp = sqlUi . value . getDataTypeForUiType ( formState . value as { uidt : UITypes } , idType ? ? undefined )
const colProp = sqlUi . value . getDataTypeForUiType ( formState . value as { uidt : UITypes } , idType ? ? undefined )
formState . value = {
formState . value = {
. . . ( ! isEdit . value && {
. . . ( ! isEdit . value && {
// only take title, column_name and uidt when creating a column
// only take title, column_name and uidt when creating a column
// to avoid the extra props from being taken (e.g. SingleLineText -> LTAR -> SingleLineText)
// to avoid the extra props from being taken (e.g. SingleLineText -> LTAR -> SingleLineText)
// to mess up the column creation
// to mess up the column creation
title : formState.value.title ,
title : newTitle || formState . value . title ,
column_name : formState.value.column_name ,
column_name : newTitle || formState . value . column_name ,
uidt : formState.value.uidt ,
uidt : formState.value.uidt ,
temp_id : formState.value.temp_id ,
temp_id : formState.value.temp_id ,
userHasChangedTitle : ! ! formState . value ? . userHasChangedTitle ,
} ) ,
} ) ,
. . . ( isEdit . value && {
. . . ( isEdit . value && {
// take the existing formState.value when editing a column
// take the existing formState.value when editing a column
@ -285,6 +282,16 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
// Column updated
// Column updated
// message.success(t('msg.success.columnUpdated'))
// message.success(t('msg.success.columnUpdated'))
} else {
} else {
// set default field title
if ( ! formState . value . title . trim ( ) ) {
const columnName = generateUniqueColumnName ( {
formState : formState.value ,
tableExplorerColumns : tableExplorerColumns?.value ,
metaColumns : meta.value?.columns || [ ] ,
} )
formState . value . title = columnName
formState . value . column_name = columnName
}
// todo : set additional meta for auto generated string id
// todo : set additional meta for auto generated string id
if ( formState . value . uidt === UITypes . ID ) {
if ( formState . value . uidt === UITypes . ID ) {
// based on id column type set autogenerated meta prop
// based on id column type set autogenerated meta prop
@ -323,6 +330,30 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
}
}
}
}
function updateFieldName ( updateFormState : boolean = true ) {
if (
isEdit . value ||
! fromTableExplorer ? . value ||
formState . value ? . userHasChangedTitle ||
! isColumnValid ? . value ? . ( formState . value )
) {
return
}
const defaultColumnName = generateUniqueColumnName ( {
formState : formState.value ,
tableExplorerColumns : tableExplorerColumns?.value || [ ] ,
metaColumns : meta.value?.columns || [ ] ,
} )
if ( updateFormState ) {
formState . value . title = defaultColumnName
formState . value . column_name = defaultColumnName
} else {
return defaultColumnName
}
}
/** set column name same as title which is actual name in db */
/** set column name same as title which is actual name in db */
watch (
watch (
( ) = > formState . value ? . title ,
( ) = > formState . value ? . title ,
@ -349,6 +380,7 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
isXcdbBase ,
isXcdbBase ,
disableSubmitBtn ,
disableSubmitBtn ,
setPostSaveOrUpdateCbk ,
setPostSaveOrUpdateCbk ,
updateFieldName ,
}
}
} ,
} ,
)
)