Skip to content

Commit 3bafff3

Browse files
committed
feat: support progress
1 parent 469f4f2 commit 3bafff3

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

playground/src/ranges/04RangeDataList.vue

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
<script setup lang="ts">
2-
import type { RangeData } from 'vue-range-multi'
3-
import { h, ref } from 'vue'
2+
import type { RangeData, RangeProgress } from 'vue-range-multi'
3+
import { computed, h, ref } from 'vue'
44
55
const model = ref<RangeData<string>[]>([
66
{ data: '00:00', value: 10, disabled: true },
7-
{ data: '20:00', value: 40 },
7+
{ data: '30:00', value: 40 },
8+
{ data: '50:00', value: 80 },
89
{ data: '59:59', value: 90, unremovable: true },
910
])
11+
12+
const backgrounds = ['bg-cyan', 'bg-violet', 'bg-rose', 'bg-lime']
13+
const progress = computed(() => {
14+
const p: RangeProgress = []
15+
for (let i = 0; i < model.value.length - 1; i += 2) {
16+
p.push({
17+
range: [model.value[i].value, model.value[i + 1]?.value ?? 100],
18+
class: backgrounds[(i / 2) % backgrounds.length],
19+
})
20+
}
21+
return p
22+
})
1023
function handleAddData(value: number) {
1124
const date = new Date()
1225
return {
@@ -23,7 +36,7 @@ function handleAddData(value: number) {
2336
RangeData[]
2437
</h2>
2538
<span class="tag">addable</span>
26-
<span class="tag">limit:5</span>
39+
<span class="tag">progress</span>
2740
<span class="tag">thumb-type:rect</span>
2841
<span class="tag">render-top-on-active</span>
2942
</div>
@@ -35,11 +48,11 @@ function handleAddData(value: number) {
3548
v-model="model"
3649
class="w-full py8"
3750
addable
51+
:progress="progress"
3852
:add-data="handleAddData"
3953
size="large"
4054
thumb-type="rect"
4155
render-top-on-active
42-
:limit="5"
4356
:render-top="(data) => h('div', data.value)"
4457
:render-bottom="(data) => h('div', data.data)"
4558
/>

playground/src/ranges/05Vertical.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function handleAddData(value: number) {
2323
</h2>
2424
<span class="tag">vertical</span>
2525
<span class="tag">slot</span>
26+
<span class="tag">limit:5</span>
2627
</div>
2728
<Range
2829
v-model="model"
@@ -31,6 +32,7 @@ function handleAddData(value: number) {
3132
addable
3233
vertical
3334
thumb-size="large"
35+
:limit="5"
3436
:render-bottom="(data) => h('div', data.value)"
3537
:add-data="handleAddData"
3638
>

src/Range.vue

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup generic="T = any, U = RangeValueType<T>">
2-
import type { RangeData, RangeMarks, RangeRenderFn, RangeValue, RangeValueType } from './type'
2+
import type { RangeData, RangeMarks, RangeProgress, RangeRenderFn, RangeValue, RangeValueType } from './type'
33
import { computed, nextTick, provide, ref, watch } from 'vue'
44
import { RangeTrackRefKey } from './Range'
55
import RangeThumb from './RangeThumb.vue'
@@ -17,6 +17,7 @@ const props = withDefaults(defineProps<{
1717
smooth?: boolean
1818
deduplicate?: boolean
1919
rangeHighlight?: boolean
20+
progress?: RangeProgress
2021
showStops?: boolean | number
2122
size?: 'small' | 'medium' | 'large'
2223
thumbType?: 'circle' | 'square' | 'rect'
@@ -253,6 +254,24 @@ provide(RangeTrackRefKey, trackRef)
253254
: { left: `${Math.min(...Object.values(position))}%`, right: `${100 - Math.max(...Object.values(position))}%` }"
254255
/>
255256
</div>
257+
<div v-if="progress?.length" class="m-range-progress-container">
258+
<template v-for="bar, idx in progress" :key="idx">
259+
<div
260+
v-if="(Array.isArray(bar))"
261+
:class="vertical ? 'm-range-v-progress' : 'm-range-progress'"
262+
:style="vertical
263+
? { top: `${getPercentage(bar[0])}%`, bottom: `${100 - getPercentage(bar[1])}%` }
264+
: { left: `${getPercentage(bar[0])}%`, right: `${100 - getPercentage(bar[1])}%` }"
265+
/>
266+
<div
267+
v-else
268+
:class="[vertical ? 'm-range-v-progress' : 'm-range-progress', bar.class]"
269+
:style="vertical
270+
? { top: `${getPercentage(bar.range[0])}%`, bottom: `${100 - getPercentage(bar.range[1])}%`, ...bar.style }
271+
: { left: `${getPercentage(bar.range[0])}%`, right: `${100 - getPercentage(bar.range[1])}%`, ...bar.style }"
272+
/>
273+
</template>
274+
</div>
256275
<div v-if="stops > 0" class="m-range-points-area">
257276
<div :class="vertical ? 'm-range-v-points-container' : 'm-range-points-container'">
258277
<div v-for="index in stops" :key="index" class="m-range-points" />

src/type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export interface RangeData<T, U = RangeValueType<T>> {
1111
}
1212
export type RangeRenderFn<T, U = RangeValueType<T>> = (data: U) => VNode
1313
export type RangeValue<T, U = RangeValueType<T>> = U | U[]
14+
export type RangeProgress = ([number, number] | {
15+
range: [number, number]
16+
style?: CSSProperties
17+
class?: string
18+
})[]
1419
export type RangeMarks = Record<number, string | {
1520
label: string
1621
style?: CSSProperties

unocss.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export default defineConfig({
3030
['m-range-highlight-container', 'absolute h-full w-full overflow-hidden'],
3131
['m-range-highlight', 'h-full bg-primary absolute rd-1px'],
3232
['m-range-v-highlight', 'w-full bg-primary absolute rd-1px'],
33+
// progress
34+
['m-range-progress-container', 'absolute h-full w-full overflow-hidden'],
35+
['m-range-progress', 'h-full bg-primary absolute rd-1px'],
36+
['m-range-v-progress', 'w-full bg-primary absolute rd-1px'],
3337
// points
3438
['m-range-points-area', 'absolute h-full w-full rd-inherit overflow-hidden'],
3539
['m-range-points-container', 'absolute h-full left--3px right--3px flex justify-between items-center'],

0 commit comments

Comments
 (0)