mirror of https://github.com/nocodb/nocodb
102 lines
3.1 KiB
102 lines
3.1 KiB
3 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import type { VNodeRef } from '@vue/runtime-core'
|
||
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import { ColumnInj, EditModeInj, computed, inject, isValidURL } from '#imports'
|
||
2 years ago
|
import MiCircleWarning from '~icons/mi/circle-warning'
|
||
3 years ago
|
|
||
2 years ago
|
const { modelValue: value } = defineProps<Props>()
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
const { t } = useI18n()
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue?: string | null
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const column = inject(ColumnInj)!
|
||
2 years ago
|
|
||
2 years ago
|
const editEnabled = inject(EditModeInj)!
|
||
3 years ago
|
|
||
2 years ago
|
// Used in the logic of when to display error since we are not storing the url if its not valid
|
||
|
const localState = ref(value)
|
||
|
|
||
2 years ago
|
const vModel = computed({
|
||
3 years ago
|
get: () => value,
|
||
|
set: (val) => {
|
||
2 years ago
|
localState.value = val
|
||
2 years ago
|
if (!column.value.meta?.validate || (val && isValidURL(val)) || !val) {
|
||
3 years ago
|
emit('update:modelValue', val)
|
||
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
})
|
||
3 years ago
|
|
||
2 years ago
|
const isValid = computed(() => value && isValidURL(value))
|
||
3 years ago
|
|
||
2 years ago
|
const url = computed(() => {
|
||
|
if (!value || !isValidURL(value)) return ''
|
||
|
|
||
2 years ago
|
/** add url scheme if missing */
|
||
|
if (/^https?:\/\//.test(value)) return value
|
||
2 years ago
|
|
||
2 years ago
|
return `https://${value}`
|
||
|
})
|
||
|
|
||
2 years ago
|
const focus: VNodeRef = (el) => (el as HTMLInputElement)?.focus()
|
||
2 years ago
|
|
||
|
watch(
|
||
|
() => editEnabled.value,
|
||
|
() => {
|
||
2 years ago
|
if (column.value.meta?.validate && !editEnabled.value && localState.value && !isValidURL(localState.value)) {
|
||
2 years ago
|
message.error(t('msg.error.invalidURL'))
|
||
2 years ago
|
localState.value = undefined
|
||
|
return
|
||
2 years ago
|
}
|
||
|
localState.value = value
|
||
|
},
|
||
|
)
|
||
3 years ago
|
</script>
|
||
|
|
||
3 years ago
|
<template>
|
||
2 years ago
|
<div class="flex flex-row items-center justify-between">
|
||
|
<input v-if="editEnabled" :ref="focus" v-model="vModel" class="outline-none text-sm w-full" @blur="editEnabled = false" />
|
||
|
|
||
|
<nuxt-link v-else-if="isValid" class="text-sm underline hover:opacity-75" :to="url" target="_blank">{{ value }} </nuxt-link>
|
||
|
|
||
|
<span v-else class="w-9/10 overflow-ellipsis overflow-hidden">{{ value }}</span>
|
||
|
|
||
2 years ago
|
<div v-if="column.meta?.validate && !isValid && value?.length && !editEnabled" class="mr-1 w-1/10">
|
||
2 years ago
|
<a-tooltip placement="top">
|
||
|
<template #title> Invalid URL </template>
|
||
|
<div class="flex flex-row items-center">
|
||
2 years ago
|
<MiCircleWarning class="text-red-400 h-4" />
|
||
2 years ago
|
</div>
|
||
|
</a-tooltip>
|
||
|
</div>
|
||
|
</div>
|
||
3 years ago
|
</template>
|
||
3 years ago
|
|
||
3 years ago
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
3 years ago
|
* @author Wing-Kam Wong <wingkwong.code@gmail.com>
|
||
3 years ago
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|