@innertia-solutions/ui 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ <script setup>
2
+ import { ref, watch, onMounted, onBeforeUnmount, computed } from 'vue'
3
+ import { IconCalendar, IconX } from '@tabler/icons-vue'
4
+
5
+ const props = defineProps({
6
+ modelValue: {
7
+ type: [String, Date, Array],
8
+ default: null
9
+ },
10
+ mode: {
11
+ type: String,
12
+ default: 'date', // 'date' | 'datetime' | 'time' | 'range' | 'range-time'
13
+ },
14
+ placeholder: {
15
+ type: String,
16
+ default: 'Seleccionar fecha'
17
+ },
18
+ minDate: {
19
+ type: [String, Date],
20
+ default: null
21
+ },
22
+ maxDate: {
23
+ type: [String, Date],
24
+ default: null
25
+ },
26
+ disabled: {
27
+ type: Boolean,
28
+ default: false
29
+ },
30
+ clearable: {
31
+ type: Boolean,
32
+ default: true
33
+ }
34
+ })
35
+
36
+ const emit = defineEmits(['update:modelValue', 'change'])
37
+
38
+ const inputEl = ref(null)
39
+ const calendarInstance = ref(null)
40
+ const internalValue = ref('')
41
+
42
+ const isRange = computed(() => props.mode.includes('range'))
43
+ const hasTime = computed(() => props.mode.includes('time'))
44
+
45
+ const formatDateToDDMMYYYY = (isoString) => {
46
+ if (!isoString || typeof isoString !== 'string') return isoString
47
+
48
+ // isoString comes as YYYY-MM-DD or YYYY-MM-DD HH:mm
49
+ const parts = isoString.split(' ')
50
+ const datePart = parts[0].split('-')
51
+
52
+ if (datePart.length === 3) {
53
+ const formattedDate = `${datePart[2]}-${datePart[1]}-${datePart[0]}`
54
+ if (parts[1]) {
55
+ return `${formattedDate} ${parts[1]}`
56
+ }
57
+ return formattedDate
58
+ }
59
+ return isoString
60
+ }
61
+
62
+ const formatForDisplay = (val) => {
63
+ if (!val) return ''
64
+
65
+ // If it's a range, val is an array of 2 dates (YYYY-MM-DD or YYYY-MM-DD HH:mm)
66
+ if (Array.isArray(val) && val.length === 2) {
67
+ if (hasTime.value) {
68
+ return `${formatDateToDDMMYYYY(val[0])} - ${formatDateToDDMMYYYY(val[1])}`
69
+ }
70
+ return `${formatDateToDDMMYYYY(val[0].split(' ')[0])} - ${formatDateToDDMMYYYY(val[1].split(' ')[0])}`
71
+ }
72
+
73
+ // Single date/datetime string
74
+ if (typeof val === 'string') {
75
+ return formatDateToDDMMYYYY(val)
76
+ }
77
+
78
+ return val.toString()
79
+ }
80
+
81
+ // Convert prop modelValue to internal text representation
82
+ watch(() => props.modelValue, (newVal) => {
83
+ internalValue.value = formatForDisplay(newVal)
84
+ }, { immediate: true })
85
+
86
+
87
+ onMounted(async () => {
88
+ // Dynamic import to avoid SSR issues
89
+ try {
90
+ const { Calendar: VanillaCalendar } = await import('vanilla-calendar-pro')
91
+ await import('vanilla-calendar-pro/styles/index.css')
92
+
93
+ // Prepare config options based on props.mode
94
+ let type = 'default'
95
+ let time = false
96
+ let selectionDatesMode = 'single'
97
+
98
+ if (props.mode === 'datetime') {
99
+ time = true
100
+ } else if (props.mode === 'time') {
101
+ type = 'time'
102
+ } else if (props.mode === 'range') {
103
+ selectionDatesMode = 'multiple-ranged'
104
+ } else if (props.mode === 'range-time') {
105
+ selectionDatesMode = 'multiple-ranged'
106
+ time = true
107
+ }
108
+
109
+ // Set initial theme based on HTML class
110
+ const isDarkGlobal = useState('isDark')
111
+
112
+ let options = {
113
+ inputMode: true,
114
+ locale: 'es',
115
+ selectedTheme: isDarkGlobal.value ? 'dark' : 'light',
116
+ selectionDatesMode: selectionDatesMode,
117
+ selectionTimeMode: time ? 24 : false,
118
+ type: type,
119
+ onChangeToInput: (self, e) => {
120
+ let dates = self.context.selectedDates || []
121
+ let timeStr = self.context.selectedTime || ''
122
+
123
+ if (dates[0]) {
124
+ let res = dates[0]
125
+ if (dates[1]) res = [dates[0], dates[1]]
126
+
127
+ if (timeStr) {
128
+ if (dates[1]) {
129
+ res = [`${dates[0]} ${timeStr}`, `${dates[1]} ${timeStr}`]
130
+ } else {
131
+ res = `${dates[0]} ${timeStr}`
132
+ }
133
+ }
134
+ emit('update:modelValue', res)
135
+ emit('change', res)
136
+
137
+ if (!isRange.value && !hasTime.value) {
138
+ self.hide()
139
+ }
140
+ } else if (timeStr && type === 'time') {
141
+ emit('update:modelValue', timeStr)
142
+ emit('change', timeStr)
143
+ } else {
144
+ emit('update:modelValue', null)
145
+ emit('change', null)
146
+ }
147
+ }
148
+ }
149
+
150
+ if (props.mode === 'time') {
151
+ options.selectionDatesMode = false
152
+ options.type = 'time'
153
+ }
154
+
155
+ if (inputEl.value) {
156
+ calendarInstance.value = new VanillaCalendar(inputEl.value, options)
157
+ calendarInstance.value.init()
158
+
159
+ watch(() => isDarkGlobal.value, (newDark) => {
160
+ if (calendarInstance.value) {
161
+ calendarInstance.value.set({ selectedTheme: newDark ? 'dark' : 'light' })
162
+ }
163
+ })
164
+ }
165
+ } catch (e) {
166
+ // Vanilla Calendar Pro load failed — silently continue
167
+ }
168
+ })
169
+
170
+ onBeforeUnmount(() => {
171
+ // Teardown
172
+ })
173
+
174
+ const clear = () => {
175
+ emit('update:modelValue', null)
176
+ emit('change', null)
177
+ }
178
+ </script>
179
+
180
+ <template>
181
+ <div class="relative w-full">
182
+ <div class="relative group">
183
+ <div class="absolute inset-y-0 start-0 flex items-center pointer-events-none z-20 ps-3.5">
184
+ <IconCalendar class="size-4 text-gray-400 group-focus-within:text-blue-500 transition-colors" />
185
+ </div>
186
+
187
+ <input
188
+ ref="inputEl"
189
+ type="text"
190
+ :value="internalValue"
191
+ :disabled="disabled"
192
+ :placeholder="placeholder"
193
+ class="w-full py-2 ps-10 pe-10 border border-gray-300 rounded-lg text-sm dark:bg-slate-800 dark:border-slate-600 dark:text-white disabled:opacity-50 disabled:pointer-events-none cursor-pointer focus:border-blue-500 focus:ring-blue-500/20 outline-none transition-all block"
194
+ readonly
195
+ />
196
+
197
+ <!-- Clear button -->
198
+ <button
199
+ v-if="clearable && internalValue && !disabled"
200
+ @click.stop="clear"
201
+ type="button"
202
+ class="absolute inset-y-0 end-0 flex items-center z-20 pe-3 text-gray-400 hover:text-red-500 transition-colors focus:outline-none"
203
+ >
204
+ <IconX class="size-4" />
205
+ </button>
206
+ </div>
207
+ </div>
208
+ </template>
209
+
210
+ <style>
211
+ /*
212
+ Vanilla Calendar v3 compiles Tailwind classes directly into its themes.
213
+ We override the selected date and hover backgrounds for Light and Dark modes.
214
+ */
215
+
216
+ /* LIGHT MODE OVERRIDES */
217
+ [data-vc-theme=light] .vc-months__month[data-vc-months-month-selected],
218
+ [data-vc-theme=light] .vc-years__year[data-vc-years-year-selected],
219
+ [data-vc-theme=light] .vc-date[data-vc-date-selected=middle][data-vc-date-selected] .vc-date__btn,
220
+ [data-vc-theme=light] .vc-date[data-vc-date-selected] .vc-date__btn {
221
+ background-color: #2563eb !important; /* blue-600 */
222
+ color: #ffffff !important;
223
+ }
224
+
225
+ [data-vc-theme=light] .vc-months__month[data-vc-months-month-selected]:hover,
226
+ [data-vc-theme=light] .vc-years__year[data-vc-years-year-selected]:hover,
227
+ [data-vc-theme=light] .vc-date[data-vc-date-selected=middle][data-vc-date-selected] .vc-date__btn:hover,
228
+ [data-vc-theme=light] .vc-date[data-vc-date-selected] .vc-date__btn:hover {
229
+ background-color: #1d4ed8 !important; /* blue-700 */
230
+ }
231
+
232
+ [data-vc-theme=light] .vc-date[data-vc-date-today] .vc-date__btn {
233
+ color: #2563eb !important;
234
+ }
235
+
236
+ /* DARK MODE OVERRIDES */
237
+ [data-vc-theme=dark] .vc-months__month[data-vc-months-month-selected],
238
+ [data-vc-theme=dark] .vc-years__year[data-vc-years-year-selected],
239
+ [data-vc-theme=dark] .vc-date[data-vc-date-selected=middle][data-vc-date-selected] .vc-date__btn,
240
+ [data-vc-theme=dark] .vc-date[data-vc-date-selected] .vc-date__btn {
241
+ background-color: #3b82f6 !important; /* blue-500 */
242
+ color: #ffffff !important;
243
+ }
244
+
245
+ [data-vc-theme=dark] .vc-months__month[data-vc-months-month-selected]:hover,
246
+ [data-vc-theme=dark] .vc-years__year[data-vc-years-year-selected]:hover,
247
+ [data-vc-theme=dark] .vc-date[data-vc-date-selected=middle][data-vc-date-selected] .vc-date__btn:hover,
248
+ [data-vc-theme=dark] .vc-date[data-vc-date-selected] .vc-date__btn:hover {
249
+ background-color: #2563eb !important; /* blue-600 */
250
+ }
251
+
252
+ [data-vc-theme=dark] .vc-date[data-vc-date-today] .vc-date__btn {
253
+ color: #3b82f6 !important;
254
+ }
255
+ </style>
@@ -1,89 +1,100 @@
1
1
  <script setup lang="ts">
2
- import { ref, watch, nextTick } from "vue";
3
-
4
2
  const props = defineProps<{
5
- options: { value: string; label: string }[];
6
- }>();
3
+ options: { value: string | number; label: string }[]
4
+ modelValue?: string | number | null
5
+ label?: string
6
+ placeholder?: string
7
+ hint?: string
8
+ error?: string
9
+ disabled?: boolean
10
+ }>()
7
11
 
8
- const modelValue = defineModel<string | null>({ default: null });
12
+ const emit = defineEmits<{
13
+ 'update:modelValue': [value: string | number | null]
14
+ change: [value: string | number | null]
15
+ }>()
9
16
 
10
- const selectRef = ref<HTMLSelectElement | null>(null);
17
+ const selectRef = ref<HTMLSelectElement | null>(null)
11
18
 
12
19
  const reinitHsSelect = async () => {
13
- await nextTick();
14
-
15
- const el = selectRef.value;
16
- if (!el) return;
20
+ await nextTick()
21
+ const el = selectRef.value
22
+ if (!el) return
17
23
 
18
- const instance = window.HSSelect?.getInstance?.(el);
19
- if (instance?.destroy) instance.destroy();
24
+ const instance = (window as any).HSSelect?.getInstance?.(el)
25
+ if (instance?.destroy) instance.destroy()
20
26
 
21
- new window.HSSelect(el);
22
- };
27
+ new (window as any).HSSelect(el)
28
+ }
23
29
 
24
- watch(
25
- () => props.options,
26
- async () => {
27
- await reinitHsSelect();
28
- },
29
- { deep: true }
30
- );
30
+ // Re-initialize when options change (async load)
31
+ watch(() => props.options, async () => {
32
+ await reinitHsSelect()
33
+ }, { deep: true })
31
34
 
32
35
  onMounted(() => {
33
- reinitHsSelect();
34
- });
36
+ reinitHsSelect()
37
+ })
38
+
39
+ const handleChange = (e: Event) => {
40
+ const val = (e.target as HTMLSelectElement).value
41
+ emit('update:modelValue', val || null)
42
+ emit('change', val || null)
43
+ }
35
44
  </script>
36
45
 
37
46
  <template>
38
- <ClientOnly>
39
- <!-- Fallback mientras se monta en el cliente -->
40
- <template #fallback>
41
- <div
42
- class="h-12 bg-slate-100 dark:bg-slate-800 animate-pulse rounded-lg"
43
- ></div>
44
- </template>
45
- <div class="relative">
46
- <select
47
- ref="selectRef"
48
- class="hs-select w-full"
49
- :value="modelValue"
50
- @change="(e) => modelValue?.value ? modelValue.value = (e.target as HTMLSelectElement).value : null"
51
- data-hs-select='{
52
- "placeholder": "Select option...",
53
- "toggleTag": "<button type=\"button\" aria-expanded=\"false\"></button>",
54
- "toggleClasses": "hs-select-disabled:pointer-events-none hs-select-disabled:opacity-50 relative py-2 ps-4 pe-9 flex gap-x-2 text-nowrap w-full cursor-pointer bg-white border border-slate-200 rounded-lg text-start text-sm focus:outline-hidden focus:ring-2 focus:ring-blue-500 dark:bg-slate-900 dark:border-slate-700 dark:text-slate-400 dark:focus:outline-hidden dark:focus:ring-1 dark:focus:ring-slate-600",
55
- "dropdownClasses": "mt-1 z-50 w-full max-h-72 p-1 space-y-0.5 bg-white border border-slate-200 rounded-lg overflow-hidden overflow-y-auto dark:bg-slate-900 dark:border-slate-700",
56
- "optionClasses": "py-2 px-4 w-full text-sm text-slate-800 cursor-pointer hover:bg-slate-100 rounded-lg focus:outline-hidden focus:bg-slate-100 dark:bg-slate-900 dark:hover:bg-slate-800 dark:text-slate-200 dark:focus:bg-slate-800",
57
- "optionTemplate": "<div class=\"flex justify-between items-center w-full\"><span data-title></span><span class=\"hidden hs-selected:block\"><svg class=\"shrink-0 size-3.5 text-blue-600 dark:text-blue-500 \" xmlns=\"http:.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><polyline points=\"20 6 9 17 4 12\"/></svg></span></div>"
58
- }'
59
- >
60
- <option disabled value="">Selecciona</option>
61
- <option
62
- v-for="option in options"
63
- :key="option.value"
64
- :value="option.value"
65
- >
66
- {{ option.label }}
67
- </option>
68
- </select>
47
+ <div class="space-y-1.5">
48
+ <!-- Label -->
49
+ <label v-if="label" class="block text-sm font-medium text-slate-700 dark:text-slate-300">
50
+ {{ label }}
51
+ </label>
69
52
 
70
- <div class="absolute top-1/2 end-2.5 -translate-y-1/2">
71
- <svg
72
- class="shrink-0 size-4 text-slate-500 dark:text-slate-500"
73
- xmlns="http://www.w3.org/2000/svg"
74
- width="24"
75
- height="24"
76
- viewBox="0 0 24 24"
77
- fill="none"
78
- stroke="currentColor"
79
- stroke-width="2"
80
- stroke-linecap="round"
81
- stroke-linejoin="round"
53
+ <!-- Select (HSSelect) -->
54
+ <ClientOnly>
55
+ <template #fallback>
56
+ <div class="h-[38px] bg-slate-100 dark:bg-slate-800 animate-pulse rounded-lg" />
57
+ </template>
58
+
59
+ <div :class="['relative', error ? 'select-error' : '']">
60
+ <select
61
+ ref="selectRef"
62
+ class="hs-select w-full"
63
+ :value="modelValue ?? ''"
64
+ :disabled="disabled"
65
+ @change="handleChange"
66
+ data-hs-select='{
67
+ "placeholder": "Seleccionar...",
68
+ "toggleTag": "<button type=\"button\" aria-expanded=\"false\"></button>",
69
+ "toggleClasses": "hs-select-disabled:pointer-events-none hs-select-disabled:opacity-50 relative py-2 ps-4 pe-9 flex gap-x-2 text-nowrap w-full cursor-pointer bg-white border border-slate-200 rounded-lg text-start text-sm focus:outline-hidden focus:ring-2 focus:ring-blue-500 dark:bg-slate-800 dark:border-slate-700 dark:text-slate-300 dark:focus:outline-hidden dark:focus:ring-1 dark:focus:ring-blue-600",
70
+ "dropdownClasses": "mt-1 z-50 w-full max-h-72 p-1 space-y-0.5 bg-white border border-slate-200 rounded-lg overflow-hidden overflow-y-auto shadow-lg dark:bg-slate-800 dark:border-slate-700",
71
+ "optionClasses": "py-2 px-4 w-full text-sm text-slate-800 cursor-pointer hover:bg-slate-100 rounded-lg focus:outline-hidden focus:bg-slate-100 dark:bg-slate-800 dark:hover:bg-slate-700 dark:text-slate-200 dark:focus:bg-slate-700",
72
+ "optionTemplate": "<div class=\"flex justify-between items-center w-full\"><span data-title></span><span class=\"hidden hs-selected:block\"><svg class=\"shrink-0 size-3.5 text-blue-600 dark:text-blue-500\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><polyline points=\"20 6 9 17 4 12\"/></svg></span></div>"
73
+ }'
82
74
  >
83
- <path d="m7 15 5 5 5-5"></path>
84
- <path d="m7 9 5-5 5 5"></path>
85
- </svg>
75
+ <option value="">{{ placeholder ?? 'Seleccionar...' }}</option>
76
+ <option
77
+ v-for="option in options"
78
+ :key="option.value"
79
+ :value="option.value"
80
+ >
81
+ {{ option.label }}
82
+ </option>
83
+ </select>
86
84
  </div>
87
- </div>
88
- </ClientOnly>
85
+ </ClientOnly>
86
+
87
+ <!-- Error -->
88
+ <p v-if="error" class="text-xs text-red-500 dark:text-red-400">{{ error }}</p>
89
+
90
+ <!-- Hint -->
91
+ <p v-else-if="hint" class="text-xs text-slate-400 dark:text-slate-500">{{ hint }}</p>
92
+ </div>
89
93
  </template>
94
+
95
+ <style scoped>
96
+ /* Apply error border to the HSSelect toggle button when in error state */
97
+ .select-error :deep(button[aria-expanded]) {
98
+ border-color: rgb(248 113 113) !important; /* red-400 */
99
+ }
100
+ </style>