Line data Source code
1 16 : <template>
2 0 : <InputGroup>
3 : <InputGroupInput
4 : :model-value="internalValue"
5 : v-bind="$attrs"
6 : :disabled
7 : :aria-invalid="validationState === ValidationState.INVALID"
8 : @update:model-value="handleInput"
9 : @blur="handleBlur"
10 : />
11 : <InputGroupAddon
12 : v-show="validationState !== ValidationState.IDLE"
13 : align="inline-end"
14 : >
15 : <InputGroupButton
16 : size="icon-sm"
17 : :aria-label="$t('g.validate')"
18 : :disabled="disabled || validationState === ValidationState.LOADING"
19 : :data-validation-state="validationState"
20 : @click="validateUrl(modelValue)"
21 : >
22 : <i :class="validationIcon" />
23 : </InputGroupButton>
24 : </InputGroupAddon>
25 : </InputGroup>
26 : </template>
27 :
28 : <script setup lang="ts">
29 : import { computed, onMounted, ref, watch } from 'vue'
30 :
31 : import InputGroup from '@comfyorg/design-system/input-group/InputGroup'
32 : import InputGroupAddon from '@comfyorg/design-system/input-group/InputGroupAddon'
33 : import InputGroupButton from '@comfyorg/design-system/input-group/InputGroupButton'
34 : import InputGroupInput from '@comfyorg/design-system/input-group/InputGroupInput'
35 : import { isValidUrl } from '@/utils/formatUtil'
36 : import { checkUrlReachable } from '@/utils/networkUtil'
37 : import { ValidationState } from '@/utils/validationUtil'
38 :
39 : const {
40 : modelValue,
41 : validateUrlFn,
42 : disabled = false
43 : } = defineProps<{
44 : modelValue: string
45 : validateUrlFn?: (url: string) => Promise<boolean>
46 : disabled?: boolean
47 : }>()
48 :
49 0 : const emit = defineEmits<{
50 : 'update:modelValue': [value: string]
51 : 'state-change': [state: ValidationState]
52 : }>()
53 :
54 0 : const validationState = ref<ValidationState>(ValidationState.IDLE)
55 0 : const validationIcon = computed(() => {
56 0 : switch (validationState.value) {
57 : case ValidationState.LOADING:
58 0 : return 'icon-[lucide--loader-circle] animate-spin text-muted-foreground'
59 : case ValidationState.VALID:
60 0 : return 'icon-[lucide--check] text-success'
61 : case ValidationState.INVALID:
62 0 : return 'icon-[lucide--x] text-destructive'
63 : default:
64 0 : return undefined
65 : }
66 : })
67 :
68 0 : const cleanInput = (value: string): string =>
69 : value ? value.replace(/\s+/g, '') : ''
70 :
71 : // Add internal value state
72 0 : const internalValue = ref(cleanInput(modelValue))
73 :
74 : // Watch for external modelValue changes
75 0 : watch(
76 : () => modelValue,
77 : async (newValue: string) => {
78 0 : internalValue.value = cleanInput(newValue)
79 0 : await validateUrl(newValue)
80 : }
81 : )
82 :
83 0 : watch(validationState, (newState) => {
84 0 : emit('state-change', newState)
85 : })
86 :
87 : // Validate on mount
88 0 : onMounted(async () => {
89 0 : await validateUrl(modelValue)
90 : })
91 :
92 0 : const handleInput = (value: string | number | undefined) => {
93 0 : const cleaned = cleanInput(String(value ?? ''))
94 0 : internalValue.value = cleaned
95 0 : validationState.value = ValidationState.IDLE
96 : }
97 :
98 0 : const handleBlur = async () => {
99 0 : const input = cleanInput(internalValue.value)
100 :
101 0 : let normalizedUrl = input
102 0 : try {
103 : const url = new URL(input)
104 0 : normalizedUrl = url.toString()
105 : } catch {
106 : // If URL parsing fails, just use the cleaned input
107 : }
108 :
109 : // Emit the update only on blur
110 0 : emit('update:modelValue', normalizedUrl)
111 : }
112 :
113 : // Default validation implementation
114 0 : const defaultValidateUrl = async (url: string): Promise<boolean> => {
115 0 : if (!isValidUrl(url)) return false
116 0 : try {
117 0 : return await checkUrlReachable(url)
118 : } catch {
119 0 : return false
120 : }
121 : }
122 :
123 0 : const validateUrl = async (value: string) => {
124 0 : if (validationState.value === ValidationState.LOADING) return
125 :
126 0 : const url = cleanInput(value)
127 :
128 : // Reset state
129 0 : validationState.value = ValidationState.IDLE
130 :
131 : // Skip validation if empty
132 0 : if (!url) return
133 :
134 0 : validationState.value = ValidationState.LOADING
135 0 : try {
136 0 : const isValid = await (validateUrlFn ?? defaultValidateUrl)(url)
137 0 : validationState.value = isValid
138 : ? ValidationState.VALID
139 : : ValidationState.INVALID
140 : } catch {
141 0 : validationState.value = ValidationState.INVALID
142 : }
143 : }
144 :
145 : // Add inheritAttrs option to prevent attrs from being applied to root element
146 : defineOptions({
147 : inheritAttrs: false
148 : })
149 : </script>
|