@coreui/vue-pro 5.7.0 → 5.8.0
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.
- package/README.md +1 -1
- package/dist/cjs/components/date-picker/CDatePicker.d.ts +19 -0
- package/dist/cjs/components/date-picker/CDatePicker.js +9 -0
- package/dist/cjs/components/date-picker/CDatePicker.js.map +1 -1
- package/dist/cjs/components/date-range-picker/CDateRangePicker.d.ts +19 -0
- package/dist/cjs/components/date-range-picker/CDateRangePicker.js +26 -1
- package/dist/cjs/components/date-range-picker/CDateRangePicker.js.map +1 -1
- package/dist/cjs/components/index.d.ts +1 -0
- package/dist/cjs/components/index.js +28 -24
- package/dist/cjs/components/index.js.map +1 -1
- package/dist/cjs/components/picker/CPicker.d.ts +19 -0
- package/dist/cjs/components/picker/CPicker.js +32 -7
- package/dist/cjs/components/picker/CPicker.js.map +1 -1
- package/dist/cjs/components/range-slider/CRangeSlider.d.ts +241 -0
- package/dist/cjs/components/range-slider/CRangeSlider.js +311 -0
- package/dist/cjs/components/range-slider/CRangeSlider.js.map +1 -0
- package/dist/cjs/components/range-slider/index.d.ts +6 -0
- package/dist/cjs/components/range-slider/index.js +13 -0
- package/dist/cjs/components/range-slider/index.js.map +1 -0
- package/dist/cjs/components/range-slider/types.d.ts +11 -0
- package/dist/cjs/components/range-slider/utils.d.ts +38 -0
- package/dist/cjs/components/range-slider/utils.js +172 -0
- package/dist/cjs/components/range-slider/utils.js.map +1 -0
- package/dist/cjs/components/sidebar/CSidebar.js +0 -1
- package/dist/cjs/components/sidebar/CSidebar.js.map +1 -1
- package/dist/cjs/components/time-picker/CTimePicker.d.ts +19 -0
- package/dist/cjs/components/time-picker/CTimePicker.js +26 -1
- package/dist/cjs/components/time-picker/CTimePicker.js.map +1 -1
- package/dist/cjs/index.js +34 -30
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/components/date-picker/CDatePicker.d.ts +19 -0
- package/dist/esm/components/date-picker/CDatePicker.js +9 -0
- package/dist/esm/components/date-picker/CDatePicker.js.map +1 -1
- package/dist/esm/components/date-range-picker/CDateRangePicker.d.ts +19 -0
- package/dist/esm/components/date-range-picker/CDateRangePicker.js +26 -1
- package/dist/esm/components/date-range-picker/CDateRangePicker.js.map +1 -1
- package/dist/esm/components/index.d.ts +1 -0
- package/dist/esm/components/index.js +2 -0
- package/dist/esm/components/index.js.map +1 -1
- package/dist/esm/components/picker/CPicker.d.ts +19 -0
- package/dist/esm/components/picker/CPicker.js +32 -7
- package/dist/esm/components/picker/CPicker.js.map +1 -1
- package/dist/esm/components/range-slider/CRangeSlider.d.ts +241 -0
- package/dist/esm/components/range-slider/CRangeSlider.js +309 -0
- package/dist/esm/components/range-slider/CRangeSlider.js.map +1 -0
- package/dist/esm/components/range-slider/index.d.ts +6 -0
- package/dist/esm/components/range-slider/index.js +10 -0
- package/dist/esm/components/range-slider/index.js.map +1 -0
- package/dist/esm/components/range-slider/types.d.ts +11 -0
- package/dist/esm/components/range-slider/utils.d.ts +38 -0
- package/dist/esm/components/range-slider/utils.js +157 -0
- package/dist/esm/components/range-slider/utils.js.map +1 -0
- package/dist/esm/components/sidebar/CSidebar.js +0 -1
- package/dist/esm/components/sidebar/CSidebar.js.map +1 -1
- package/dist/esm/components/time-picker/CTimePicker.d.ts +19 -0
- package/dist/esm/components/time-picker/CTimePicker.js +26 -1
- package/dist/esm/components/time-picker/CTimePicker.js.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/date-picker/CDatePicker.ts +9 -0
- package/src/components/date-range-picker/CDateRangePicker.ts +26 -2
- package/src/components/index.ts +1 -0
- package/src/components/picker/CPicker.ts +43 -7
- package/src/components/range-slider/CRangeSlider.ts +420 -0
- package/src/components/range-slider/index.ts +10 -0
- package/src/components/range-slider/types.ts +16 -0
- package/src/components/range-slider/utils.ts +241 -0
- package/src/components/sidebar/CSidebar.ts +0 -1
- package/src/components/time-picker/CTimePicker.ts +26 -1
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { defineComponent, ref, watch, onMounted, h, PropType, VNode } from 'vue'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
calculateClickValue,
|
|
5
|
+
calculateLabelPosition,
|
|
6
|
+
calculateMoveValue,
|
|
7
|
+
calculateTooltipPosition,
|
|
8
|
+
getLabelValue,
|
|
9
|
+
getNearestValueIndex,
|
|
10
|
+
getThumbSize,
|
|
11
|
+
updateGradient,
|
|
12
|
+
updateValue,
|
|
13
|
+
validateValue,
|
|
14
|
+
} from './utils'
|
|
15
|
+
|
|
16
|
+
import type { Label, ThumbSize } from './types'
|
|
17
|
+
|
|
18
|
+
import { isRTL } from '../../utils'
|
|
19
|
+
|
|
20
|
+
const CRangeSlider = defineComponent({
|
|
21
|
+
name: 'CRangeSlider',
|
|
22
|
+
props: {
|
|
23
|
+
/**
|
|
24
|
+
* Enable or disable clickable labels in the Vue Range Slider.
|
|
25
|
+
* When set to `true`, users can click on labels to adjust the slider's value directly, enhancing interactivity and user experience.
|
|
26
|
+
*/
|
|
27
|
+
clickableLabels: {
|
|
28
|
+
type: Boolean,
|
|
29
|
+
default: true,
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* Control the interactive state of the Vue Range Slider with the `disabled` prop.
|
|
33
|
+
* Setting it to `true` will disable all slider functionalities, preventing user interaction and visually indicating a non-interactive state.
|
|
34
|
+
*/
|
|
35
|
+
disabled: {
|
|
36
|
+
type: Boolean,
|
|
37
|
+
default: false,
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Define the minimum distance between slider handles using the `distance` prop in the Vue Range Slider.
|
|
41
|
+
* This ensures that the handles maintain a specified separation, preventing overlap and maintaining clear value distinctions.
|
|
42
|
+
*/
|
|
43
|
+
distance: {
|
|
44
|
+
type: Number,
|
|
45
|
+
default: 0,
|
|
46
|
+
},
|
|
47
|
+
/**
|
|
48
|
+
* Add descriptive labels to your Vue Range Slider by providing an array of `labels`.
|
|
49
|
+
* These labels enhance the slider's usability by clearly indicating key values and providing contextual information to users.
|
|
50
|
+
*/
|
|
51
|
+
labels: {
|
|
52
|
+
type: Array as PropType<Label[]>,
|
|
53
|
+
default: () => [],
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Specify the maximum value for the Vue Range Slider with the `max` prop.
|
|
57
|
+
* This determines the upper limit of the slider's range, enabling precise control over the highest selectable value.
|
|
58
|
+
*/
|
|
59
|
+
max: {
|
|
60
|
+
type: Number,
|
|
61
|
+
default: 100,
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* Set the minimum value for the Vue Range Slider using the `min` prop.
|
|
65
|
+
* This defines the lower bound of the slider's range, allowing you to control the starting point of user selection.
|
|
66
|
+
*/
|
|
67
|
+
min: {
|
|
68
|
+
type: Number,
|
|
69
|
+
default: 0,
|
|
70
|
+
},
|
|
71
|
+
/**
|
|
72
|
+
* The default name for a value passed using v-model.
|
|
73
|
+
*/
|
|
74
|
+
modelValue: [Number, Array] as PropType<number | number[]>,
|
|
75
|
+
/**
|
|
76
|
+
* Assign a `name` to the Vue Range Slider for form integration.
|
|
77
|
+
* Whether using a single string or an array of strings, this prop ensures that the slider's values are correctly identified when submitting forms.
|
|
78
|
+
*/
|
|
79
|
+
name: {
|
|
80
|
+
type: [String, Array] as PropType<string | string[]>,
|
|
81
|
+
default: '',
|
|
82
|
+
},
|
|
83
|
+
/**
|
|
84
|
+
* Control the granularity of the Vue Range Slider by setting the `step` prop.
|
|
85
|
+
* This defines the increment intervals between selectable values, allowing for precise adjustments based on your application's requirements.
|
|
86
|
+
*/
|
|
87
|
+
step: {
|
|
88
|
+
type: Number,
|
|
89
|
+
default: 1,
|
|
90
|
+
},
|
|
91
|
+
/**
|
|
92
|
+
* Toggle the visibility of tooltips in the Vue Range Slider with the `tooltips` prop.
|
|
93
|
+
* When enabled, tooltips display the current value of the slider handles, providing real-time feedback to users.
|
|
94
|
+
*/
|
|
95
|
+
tooltips: {
|
|
96
|
+
type: Boolean,
|
|
97
|
+
default: true,
|
|
98
|
+
},
|
|
99
|
+
/**
|
|
100
|
+
* Customize the display format of tooltips in the Vue Range Slider using the `tooltipsFormat` function.
|
|
101
|
+
* This allows you to format the tooltip values according to your specific requirements, enhancing the clarity and presentation of information.
|
|
102
|
+
*/
|
|
103
|
+
tooltipsFormat: {
|
|
104
|
+
type: Function as PropType<(value: number) => string | VNode>,
|
|
105
|
+
default: null,
|
|
106
|
+
},
|
|
107
|
+
/**
|
|
108
|
+
* Controls the visual representation of the slider's track. When set to `'fill'`, the track is dynamically filled based on the slider's value(s). Setting it to `false` disables the filled track.
|
|
109
|
+
*/
|
|
110
|
+
track: {
|
|
111
|
+
type: [Boolean, String],
|
|
112
|
+
default: 'fill',
|
|
113
|
+
validator: (value: boolean | string) => {
|
|
114
|
+
return typeof value === 'boolean' || value === 'fill'
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* Set the current value(s) of the Vue Range Slider using the `value` prop.
|
|
119
|
+
* Whether you're using a single value or an array for multi-handle sliders, this prop controls the slider's position and ensures it reflects the desired state.
|
|
120
|
+
*/
|
|
121
|
+
value: {
|
|
122
|
+
type: [Number, Array] as PropType<number | number[]>,
|
|
123
|
+
default: () => [0],
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* Orient the Vue Range Slider vertically by setting the `vertical` prop to `true`.
|
|
127
|
+
* This changes the slider's layout from horizontal to vertical, providing a different aesthetic and fitting various UI designs.
|
|
128
|
+
*/
|
|
129
|
+
vertical: {
|
|
130
|
+
type: Boolean,
|
|
131
|
+
default: false,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
emits: [
|
|
135
|
+
'change',
|
|
136
|
+
/**
|
|
137
|
+
* Emit the new value whenever there’s a change event.
|
|
138
|
+
*/
|
|
139
|
+
'update:modelValue',
|
|
140
|
+
],
|
|
141
|
+
setup(props, { emit }) {
|
|
142
|
+
const rangeSliderRef = ref<HTMLDivElement | null>(null)
|
|
143
|
+
const inputsRef = ref<HTMLInputElement[]>([])
|
|
144
|
+
const labelsContainerRef = ref<HTMLDivElement | null>(null)
|
|
145
|
+
const labelsRef = ref<HTMLDivElement[]>([])
|
|
146
|
+
const trackRef = ref<HTMLDivElement | null>(null)
|
|
147
|
+
|
|
148
|
+
const currentValue = ref<number[]>(
|
|
149
|
+
props.modelValue
|
|
150
|
+
? Array.isArray(props.modelValue)
|
|
151
|
+
? props.modelValue
|
|
152
|
+
: [props.modelValue]
|
|
153
|
+
: Array.isArray(props.value)
|
|
154
|
+
? props.value
|
|
155
|
+
: [props.value],
|
|
156
|
+
)
|
|
157
|
+
const isDragging = ref(false)
|
|
158
|
+
const dragIndex = ref(0)
|
|
159
|
+
const thumbSize = ref<ThumbSize | null>()
|
|
160
|
+
|
|
161
|
+
watch(
|
|
162
|
+
() => props.value,
|
|
163
|
+
(newVal) => {
|
|
164
|
+
currentValue.value = Array.isArray(newVal) ? newVal : [newVal]
|
|
165
|
+
},
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
watch(
|
|
169
|
+
() => props.modelValue,
|
|
170
|
+
(newVal) => {
|
|
171
|
+
if (newVal !== undefined) {
|
|
172
|
+
currentValue.value = Array.isArray(newVal) ? newVal : [newVal]
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
// Adjust labels container size based on labels
|
|
178
|
+
onMounted(() => {
|
|
179
|
+
const maxSize = Math.max(
|
|
180
|
+
...labelsRef.value.map((label) =>
|
|
181
|
+
props.vertical ? label.offsetWidth : label.offsetHeight,
|
|
182
|
+
),
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
if (labelsContainerRef.value) {
|
|
186
|
+
labelsContainerRef.value.style[props.vertical ? 'width' : 'height'] = `${maxSize}px`
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (rangeSliderRef.value) {
|
|
190
|
+
thumbSize.value = getThumbSize(rangeSliderRef.value, props.vertical)
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
watch(isDragging, (newVal) => {
|
|
195
|
+
if (newVal) {
|
|
196
|
+
window.addEventListener('mousemove', handleMouseMove)
|
|
197
|
+
window.addEventListener('mouseup', handleMouseUp)
|
|
198
|
+
} else {
|
|
199
|
+
window.removeEventListener('mousemove', handleMouseMove)
|
|
200
|
+
window.removeEventListener('mouseup', handleMouseUp)
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
const updateNearestValue = (value: number) => {
|
|
205
|
+
const nearestIndex = getNearestValueIndex(value, currentValue.value)
|
|
206
|
+
const newCurrentValue = [...currentValue.value]
|
|
207
|
+
newCurrentValue[nearestIndex] = validateValue(
|
|
208
|
+
value,
|
|
209
|
+
currentValue.value,
|
|
210
|
+
props.distance,
|
|
211
|
+
nearestIndex,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
setTimeout(() => {
|
|
215
|
+
if (inputsRef.value[nearestIndex]) {
|
|
216
|
+
inputsRef.value[nearestIndex].focus()
|
|
217
|
+
}
|
|
218
|
+
}, 0)
|
|
219
|
+
|
|
220
|
+
currentValue.value = newCurrentValue
|
|
221
|
+
|
|
222
|
+
emit('change', newCurrentValue)
|
|
223
|
+
emit('update:modelValue', newCurrentValue)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const handleInputChange = (event: Event, index: number) => {
|
|
227
|
+
if (props.disabled) return
|
|
228
|
+
|
|
229
|
+
const target = event.target as HTMLInputElement
|
|
230
|
+
const value = Number(target.value)
|
|
231
|
+
|
|
232
|
+
const newCurrentValue = updateValue(value, currentValue.value, props.distance, index)
|
|
233
|
+
|
|
234
|
+
currentValue.value = newCurrentValue
|
|
235
|
+
|
|
236
|
+
emit('change', newCurrentValue)
|
|
237
|
+
emit('update:modelValue', newCurrentValue)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const handleInputsContainerMouseDown = (event: MouseEvent) => {
|
|
241
|
+
if (!trackRef.value) return
|
|
242
|
+
|
|
243
|
+
const clickValue = calculateClickValue(
|
|
244
|
+
event,
|
|
245
|
+
trackRef.value,
|
|
246
|
+
props.min,
|
|
247
|
+
props.max,
|
|
248
|
+
props.step,
|
|
249
|
+
props.vertical,
|
|
250
|
+
isRTL(rangeSliderRef.value),
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
const index = getNearestValueIndex(clickValue, currentValue.value)
|
|
254
|
+
|
|
255
|
+
isDragging.value = true
|
|
256
|
+
dragIndex.value = index
|
|
257
|
+
updateNearestValue(clickValue)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const handleLabelClick = (value: number) => {
|
|
261
|
+
if (!props.clickableLabels || props.disabled) return
|
|
262
|
+
|
|
263
|
+
updateNearestValue(value)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const handleMouseMove = (event: MouseEvent) => {
|
|
267
|
+
if (!isDragging.value || !trackRef.value) return
|
|
268
|
+
|
|
269
|
+
const moveValue = calculateMoveValue(
|
|
270
|
+
event,
|
|
271
|
+
trackRef.value,
|
|
272
|
+
props.min,
|
|
273
|
+
props.max,
|
|
274
|
+
props.step,
|
|
275
|
+
props.vertical,
|
|
276
|
+
isRTL(rangeSliderRef.value),
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
const newCurrentValue = updateValue(
|
|
280
|
+
moveValue,
|
|
281
|
+
currentValue.value,
|
|
282
|
+
props.distance,
|
|
283
|
+
dragIndex.value,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
currentValue.value = newCurrentValue
|
|
287
|
+
|
|
288
|
+
emit('change', newCurrentValue)
|
|
289
|
+
emit('update:modelValue', newCurrentValue)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const handleMouseUp = () => {
|
|
293
|
+
isDragging.value = false
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return () =>
|
|
297
|
+
h(
|
|
298
|
+
'div',
|
|
299
|
+
{
|
|
300
|
+
class: [
|
|
301
|
+
'range-slider',
|
|
302
|
+
{
|
|
303
|
+
'range-slider-vertical': props.vertical,
|
|
304
|
+
disabled: props.disabled,
|
|
305
|
+
},
|
|
306
|
+
],
|
|
307
|
+
ref: rangeSliderRef,
|
|
308
|
+
},
|
|
309
|
+
[
|
|
310
|
+
h(
|
|
311
|
+
'div',
|
|
312
|
+
{
|
|
313
|
+
class: 'range-slider-inputs-container',
|
|
314
|
+
onMousedown: handleInputsContainerMouseDown,
|
|
315
|
+
},
|
|
316
|
+
[
|
|
317
|
+
currentValue.value.map((value, index) => [
|
|
318
|
+
h('input', {
|
|
319
|
+
class: 'range-slider-input',
|
|
320
|
+
type: 'range',
|
|
321
|
+
min: props.min,
|
|
322
|
+
max: props.max,
|
|
323
|
+
step: props.step,
|
|
324
|
+
value: value,
|
|
325
|
+
name: Array.isArray(props.name)
|
|
326
|
+
? props.name[index]
|
|
327
|
+
: `${props.name || ''}-${index}`,
|
|
328
|
+
role: 'slider',
|
|
329
|
+
'aria-valuemin': props.min,
|
|
330
|
+
'aria-valuemax': props.max,
|
|
331
|
+
'aria-valuenow': value,
|
|
332
|
+
'aria-orientation': props.vertical ? 'vertical' : 'horizontal',
|
|
333
|
+
disabled: props.disabled,
|
|
334
|
+
onInput: (e: Event) => handleInputChange(e, index),
|
|
335
|
+
ref: (el) => {
|
|
336
|
+
inputsRef.value[index] = el as HTMLInputElement
|
|
337
|
+
},
|
|
338
|
+
}),
|
|
339
|
+
props.tooltips &&
|
|
340
|
+
h(
|
|
341
|
+
'div',
|
|
342
|
+
{
|
|
343
|
+
class: 'range-slider-tooltip',
|
|
344
|
+
...(thumbSize.value && {
|
|
345
|
+
style: calculateTooltipPosition(
|
|
346
|
+
props.min,
|
|
347
|
+
props.max,
|
|
348
|
+
value,
|
|
349
|
+
thumbSize.value,
|
|
350
|
+
props.vertical,
|
|
351
|
+
isRTL(rangeSliderRef.value),
|
|
352
|
+
),
|
|
353
|
+
}),
|
|
354
|
+
},
|
|
355
|
+
[
|
|
356
|
+
h('div', { class: 'range-slider-tooltip-inner' }, [
|
|
357
|
+
props.tooltipsFormat ? props.tooltipsFormat(value) : value,
|
|
358
|
+
]),
|
|
359
|
+
h('div', { class: 'range-slider-tooltip-arrow' }),
|
|
360
|
+
],
|
|
361
|
+
),
|
|
362
|
+
]),
|
|
363
|
+
h('div', {
|
|
364
|
+
class: 'range-slider-track',
|
|
365
|
+
...(props.track && {
|
|
366
|
+
style: updateGradient(props.min, props.max, currentValue.value, props.vertical),
|
|
367
|
+
}),
|
|
368
|
+
ref: trackRef,
|
|
369
|
+
}),
|
|
370
|
+
],
|
|
371
|
+
),
|
|
372
|
+
Array.isArray(props.labels) &&
|
|
373
|
+
props.labels.length > 0 &&
|
|
374
|
+
h(
|
|
375
|
+
'div',
|
|
376
|
+
{
|
|
377
|
+
class: 'range-slider-labels-container',
|
|
378
|
+
ref: labelsContainerRef,
|
|
379
|
+
},
|
|
380
|
+
props.labels.map((label, index) => {
|
|
381
|
+
const labelPosition = calculateLabelPosition(
|
|
382
|
+
props.min,
|
|
383
|
+
props.max,
|
|
384
|
+
props.labels,
|
|
385
|
+
label,
|
|
386
|
+
index,
|
|
387
|
+
)
|
|
388
|
+
const labelValue = getLabelValue(props.min, props.max, props.labels, label, index)
|
|
389
|
+
const labelStyle = {
|
|
390
|
+
...(props.vertical ? { bottom: labelPosition } : { left: labelPosition }),
|
|
391
|
+
...(typeof label === 'object' && 'style' in label ? label.style : {}),
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return h(
|
|
395
|
+
'div',
|
|
396
|
+
{
|
|
397
|
+
class: [
|
|
398
|
+
'range-slider-label',
|
|
399
|
+
{
|
|
400
|
+
clickable: props.clickableLabels,
|
|
401
|
+
},
|
|
402
|
+
typeof label === 'object' && 'className' in label ? label.className : '',
|
|
403
|
+
],
|
|
404
|
+
style: labelStyle,
|
|
405
|
+
onMousedown: () => handleLabelClick(labelValue),
|
|
406
|
+
key: index,
|
|
407
|
+
ref: (el) => {
|
|
408
|
+
labelsRef.value[index] = el as HTMLDivElement
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
typeof label === 'object' && 'label' in label ? label.label : label,
|
|
412
|
+
)
|
|
413
|
+
}),
|
|
414
|
+
),
|
|
415
|
+
],
|
|
416
|
+
)
|
|
417
|
+
},
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
export { CRangeSlider }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { VNode } from 'vue'
|
|
2
|
+
|
|
3
|
+
export type Label =
|
|
4
|
+
| {
|
|
5
|
+
label: number | string | VNode
|
|
6
|
+
value: number
|
|
7
|
+
className?: string | string[] | Record<string, boolean>
|
|
8
|
+
style?: Record<string, any> // Alternatively, you can use `CSSProperties` from Vue
|
|
9
|
+
}
|
|
10
|
+
| VNode
|
|
11
|
+
| string
|
|
12
|
+
|
|
13
|
+
export type ThumbSize = {
|
|
14
|
+
value: number
|
|
15
|
+
unit: string | null
|
|
16
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { Label, ThumbSize } from './types'
|
|
2
|
+
|
|
3
|
+
export const calculateClickValue = (
|
|
4
|
+
event: MouseEvent,
|
|
5
|
+
container: HTMLDivElement,
|
|
6
|
+
min: number,
|
|
7
|
+
max: number,
|
|
8
|
+
step: number,
|
|
9
|
+
vertical: boolean,
|
|
10
|
+
rtl: boolean,
|
|
11
|
+
) => {
|
|
12
|
+
const clickPosition = getClickPosition(event, container, vertical, rtl)
|
|
13
|
+
const value = min + clickPosition * (max - min)
|
|
14
|
+
return roundToStep(value, step)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const calculateMoveValue = (
|
|
18
|
+
event: MouseEvent,
|
|
19
|
+
container: HTMLDivElement,
|
|
20
|
+
min: number,
|
|
21
|
+
max: number,
|
|
22
|
+
step: number,
|
|
23
|
+
vertical: boolean,
|
|
24
|
+
rtl: boolean,
|
|
25
|
+
) => {
|
|
26
|
+
const rect = container.getBoundingClientRect()
|
|
27
|
+
const position = vertical
|
|
28
|
+
? calculateVerticalPosition(event.clientY, rect)
|
|
29
|
+
: calculateHorizontalPosition(event.clientX, rect, rtl)
|
|
30
|
+
|
|
31
|
+
if (typeof position === 'string') {
|
|
32
|
+
return position === 'max' ? max : min
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const value = min + position * (max - min)
|
|
36
|
+
|
|
37
|
+
return roundToStep(value, step)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const calculateVerticalPosition = (mouseY: number, rect: DOMRect) => {
|
|
41
|
+
if (mouseY < rect.top) {
|
|
42
|
+
return 'max'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (mouseY > rect.bottom) {
|
|
46
|
+
return 'min'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return Math.min(Math.max((rect.bottom - mouseY) / rect.height, 0), 1)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const calculateHorizontalPosition = (mouseX: number, rect: DOMRect, rtl: boolean) => {
|
|
53
|
+
if (mouseX < rect.left) {
|
|
54
|
+
return rtl ? 'max' : 'min'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (mouseX > rect.right) {
|
|
58
|
+
return rtl ? 'min' : 'max'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const relativeX = rtl ? rect.right - mouseX : mouseX - rect.left
|
|
62
|
+
return Math.min(Math.max(relativeX / rect.width, 0), 1)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const calculateLabelPosition = (
|
|
66
|
+
min: number,
|
|
67
|
+
max: number,
|
|
68
|
+
labels: Label[],
|
|
69
|
+
label: Label,
|
|
70
|
+
index: number,
|
|
71
|
+
) => {
|
|
72
|
+
if (typeof label === 'object' && 'value' in label) {
|
|
73
|
+
return `${((label.value - min) / (max - min)) * 100}%`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return `${(index / (labels.length - 1)) * 100}%`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const calculateTooltipPosition = (
|
|
80
|
+
min: number,
|
|
81
|
+
max: number,
|
|
82
|
+
value: number,
|
|
83
|
+
thumbSize: ThumbSize,
|
|
84
|
+
vertical: boolean,
|
|
85
|
+
rtl: boolean,
|
|
86
|
+
) => {
|
|
87
|
+
const percent = (value - min) / (max - min)
|
|
88
|
+
const margin =
|
|
89
|
+
percent > 0.5
|
|
90
|
+
? `-${(percent - 0.5) * thumbSize.value}${thumbSize.unit}`
|
|
91
|
+
: `${(0.5 - percent) * thumbSize.value}${thumbSize.unit}`
|
|
92
|
+
|
|
93
|
+
if (vertical) {
|
|
94
|
+
return {
|
|
95
|
+
bottom: `${percent * 100}%`,
|
|
96
|
+
marginBottom: margin,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return rtl
|
|
101
|
+
? { right: `${percent * 100}%`, marginRight: margin }
|
|
102
|
+
: { left: `${percent * 100}%`, marginLeft: margin }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const getClickPosition = (
|
|
106
|
+
event: MouseEvent,
|
|
107
|
+
container: HTMLDivElement,
|
|
108
|
+
vertical: boolean,
|
|
109
|
+
rtl: boolean,
|
|
110
|
+
) => {
|
|
111
|
+
const { offsetX, offsetY } = event
|
|
112
|
+
const { offsetWidth, offsetHeight } = container
|
|
113
|
+
|
|
114
|
+
if (vertical) {
|
|
115
|
+
return 1 - offsetY / offsetHeight
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return rtl ? 1 - offsetX / offsetWidth : offsetX / offsetWidth
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const getLabelValue = (
|
|
122
|
+
min: number,
|
|
123
|
+
max: number,
|
|
124
|
+
labels: Label[],
|
|
125
|
+
label: Label,
|
|
126
|
+
index: number,
|
|
127
|
+
) =>
|
|
128
|
+
typeof label === 'object' && 'value' in label
|
|
129
|
+
? label.value
|
|
130
|
+
: min + (index / (labels.length - 1)) * (max - min)
|
|
131
|
+
|
|
132
|
+
export const getNearestValueIndex = (value: number, values: number[]) => {
|
|
133
|
+
const valuesLength = values.length
|
|
134
|
+
|
|
135
|
+
if (value < values[0]) {
|
|
136
|
+
return 0
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (value > values[valuesLength - 1]) {
|
|
140
|
+
return valuesLength - 1
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const distances = values.map((v) => Math.abs(v - value))
|
|
144
|
+
const min = Math.min(...distances)
|
|
145
|
+
const firstIndex = distances.indexOf(min)
|
|
146
|
+
|
|
147
|
+
return value < values[firstIndex] ? firstIndex : distances.lastIndexOf(min)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export const getThumbSize = (element: HTMLDivElement, vertical: boolean): ThumbSize | null => {
|
|
151
|
+
const value = globalThis
|
|
152
|
+
.getComputedStyle(element, null)
|
|
153
|
+
.getPropertyValue(
|
|
154
|
+
vertical ? '--cui-range-slider-thumb-height' : '--cui-range-slider-thumb-width',
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
const regex = /^(\d+\.?\d*)([%a-z]*)$/i
|
|
158
|
+
const match = value.match(regex)
|
|
159
|
+
|
|
160
|
+
if (match) {
|
|
161
|
+
return {
|
|
162
|
+
value: Number.parseFloat(match[1]),
|
|
163
|
+
unit: match[2] || null,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return null
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const roundToStep = (number: number, step: number) => {
|
|
171
|
+
const _step = step === 0 ? 1 : step
|
|
172
|
+
return Math.round(number / _step) * _step
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export const updateGradient = (min: number, max: number, values: number[], vertical: boolean) => {
|
|
176
|
+
const minVal = Math.min(...values)
|
|
177
|
+
const maxVal = Math.max(...values)
|
|
178
|
+
|
|
179
|
+
const from = ((minVal - min) / (max - min)) * 100
|
|
180
|
+
const to = ((maxVal - min) / (max - min)) * 100
|
|
181
|
+
|
|
182
|
+
const direction = vertical ? 'to top' : 'to right'
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
backgroundImage:
|
|
186
|
+
values.length === 1
|
|
187
|
+
? `linear-gradient(
|
|
188
|
+
${direction},
|
|
189
|
+
var(--cui-range-slider-track-in-range-bg) 0%,
|
|
190
|
+
var(--cui-range-slider-track-in-range-bg) ${to}%,
|
|
191
|
+
transparent ${to}%,
|
|
192
|
+
transparent 100%
|
|
193
|
+
)`
|
|
194
|
+
: `linear-gradient(
|
|
195
|
+
${direction},
|
|
196
|
+
transparent 0%,
|
|
197
|
+
transparent ${from}%,
|
|
198
|
+
var(--cui-range-slider-track-in-range-bg) ${from}%,
|
|
199
|
+
var(--cui-range-slider-track-in-range-bg) ${to}%,
|
|
200
|
+
transparent ${to}%,
|
|
201
|
+
transparent 100%
|
|
202
|
+
)`,
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export const updateValue = (value: number, values: number[], distance: number, index: number) => {
|
|
207
|
+
const newValue = [...values]
|
|
208
|
+
newValue[index] = validateValue(value, values, distance, index)
|
|
209
|
+
return newValue
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export const validateValue = (value: number, values: number[], distance: number, index: number) => {
|
|
213
|
+
// If there's only one value, return it as is
|
|
214
|
+
if (values.length === 1) {
|
|
215
|
+
return value
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Determine previous and next values if they exist
|
|
219
|
+
const prevValue: number | undefined = index > 0 ? values[index - 1] : undefined
|
|
220
|
+
const nextValue: number | undefined = index < values.length - 1 ? values[index + 1] : undefined
|
|
221
|
+
|
|
222
|
+
// If it's the first element, ensure it's not too close to the next value
|
|
223
|
+
if (index === 0 && nextValue !== undefined) {
|
|
224
|
+
return Math.min(value, nextValue - distance)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// If it's the last element, ensure it's not too close to the previous value
|
|
228
|
+
if (index === values.length - 1 && prevValue !== undefined) {
|
|
229
|
+
return Math.max(value, prevValue + distance)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// For middle elements, ensure the value is within the allowed distance from both neighbors
|
|
233
|
+
if (prevValue !== undefined && nextValue !== undefined) {
|
|
234
|
+
const minVal = prevValue + distance
|
|
235
|
+
const maxVal = nextValue - distance
|
|
236
|
+
return Math.min(Math.max(value, minVal), maxVal)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Fallback: If for some reason prevValue or nextValue is undefined, return the original value
|
|
240
|
+
return value
|
|
241
|
+
}
|