Files
heroui-vue/apps/docs/demos/progress-bar-custom-value.vue
T
2026-10-22 14:33:15 +08:00

126 lines
3.4 KiB
Vue

<script setup lang="ts">
import { computed, ref } from "vue";
import {
Label,
ProgressBar,
ProgressBarFill,
ProgressBarOutput,
ProgressBarTrack,
Separator,
} from "@heroui-vue/components";
const value = ref(750);
const minValue = ref(0);
const maxValue = ref(1000);
const format = ref("percent");
const formatStyleOptions = [
{ label: "货币", value: "currency" },
{ label: "百分比", value: "percent" },
{ label: "小数", value: "decimal" },
{ label: "单位", value: "unit" },
] as const;
const formatOptionsMap: Record<string, Intl.NumberFormatOptions> = {
currency: { currency: "USD", style: "currency" },
decimal: { style: "decimal" },
percent: { style: "percent" },
unit: { style: "unit", unit: "mile" },
};
const formatOptions = computed(() => formatOptionsMap[format.value]);
function onValueChange(event: Event) {
const next = Number((event.target as HTMLInputElement).value);
value.value = Math.min(Math.max(next, minValue.value), maxValue.value);
}
function onMinChange(event: Event) {
const next = Number((event.target as HTMLInputElement).value);
minValue.value = Math.min(next, maxValue.value - 1);
if (value.value < minValue.value) value.value = minValue.value;
}
function onMaxChange(event: Event) {
const next = Number((event.target as HTMLInputElement).value);
maxValue.value = Math.max(next, minValue.value + 1);
if (value.value > maxValue.value) value.value = maxValue.value;
}
</script>
<template>
<div class="flex w-full flex-col gap-6 md:flex-row md:items-center md:gap-10">
<div class="flex w-full max-w-md flex-1 justify-center">
<ProgressBar
aria-label="收入"
class="w-full max-w-52"
:format-options="formatOptions"
:max-value="maxValue"
:min-value="minValue"
:value="value"
>
<Label>进度</Label>
<ProgressBarOutput />
<ProgressBarTrack>
<ProgressBarFill />
</ProgressBarTrack>
</ProgressBar>
</div>
<Separator class="md:hidden" />
<Separator class="hidden md:block" orientation="vertical" />
<div class="flex max-w-52 flex-col gap-3">
<Label class="text-xs font-medium text-muted">选项</Label>
<Label for="pb-cv-value">值</Label>
<input
id="pb-cv-value"
class="hv-input hv-input--secondary hv-input--full-width"
type="number"
:min="minValue"
:max="maxValue"
:value="value"
@change="onValueChange"
/>
<Label for="pb-cv-min">最小值</Label>
<input
id="pb-cv-min"
class="hv-input hv-input--secondary hv-input--full-width"
type="number"
min="0"
:max="maxValue - 1"
:value="minValue"
@change="onMinChange"
/>
<Label for="pb-cv-max">最大值</Label>
<input
id="pb-cv-max"
class="hv-input hv-input--secondary hv-input--full-width"
type="number"
:min="minValue + 1"
max="2000"
:value="maxValue"
@change="onMaxChange"
/>
<Label for="pb-cv-format">格式</Label>
<select
id="pb-cv-format"
v-model="format"
class="hv-input hv-input--secondary hv-input--full-width"
>
<option
v-for="option in formatStyleOptions"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</option>
</select>
</div>
</div>
</template>