Validate on mount for UrlInput (#2332)

This commit is contained in:
Chenlei Hu
2025-01-23 15:09:00 -05:00
committed by GitHub
parent e8136ff0ae
commit caad27e28d
2 changed files with 21 additions and 5 deletions

View File

@@ -25,7 +25,7 @@
import IconField from 'primevue/iconfield'
import InputIcon from 'primevue/inputicon'
import InputText from 'primevue/inputtext'
import { ref, watch } from 'vue'
import { onMounted, ref, watch } from 'vue'
import { isValidUrl } from '@/utils/formatUtil'
import { checkUrlReachable } from '@/utils/networkUtil'
@@ -54,11 +54,15 @@ const internalValue = ref(props.modelValue)
// Watch for external modelValue changes
watch(
() => props.modelValue,
async (newValue) => {
async (newValue: string) => {
internalValue.value = newValue
await validateUrl()
await validateUrl(newValue)
}
)
// Validate on mount
onMounted(async () => {
await validateUrl(props.modelValue)
})
const handleInput = (value: string) => {
// Update internal value without emitting
@@ -82,8 +86,8 @@ const defaultValidateUrl = async (url: string): Promise<boolean> => {
}
}
const validateUrl = async () => {
const url = props.modelValue.trim()
const validateUrl = async (value: string) => {
const url = value.trim()
// Reset state
validationState.value = UrlValidationState.IDLE

View File

@@ -94,4 +94,16 @@ describe('UrlInput', () => {
expect(wrapper.find('.pi-times').exists()).toBe(true)
})
it('validates on mount', async () => {
const wrapper = mountComponent({
modelValue: 'https://test.com',
validateUrlFn: () => Promise.resolve(true)
})
await nextTick()
await nextTick()
expect(wrapper.find('.pi-check').exists()).toBe(true)
})
})