feat: add ProgressBar ProgressCircle and Meter
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, useAttrs } from "vue";
|
||||
import { inject, onUnmounted, useAttrs, useId, watch } from "vue";
|
||||
import { useFieldPart } from "../field/useFieldPart";
|
||||
import { useInheritedDisabled } from "../field/disabled";
|
||||
import { fieldContextKey } from "../field/context";
|
||||
import { injectIndicatorLabel } from "../progress/value";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const field = inject(fieldContextKey, undefined);
|
||||
const inheritedField = inject(fieldContextKey, undefined);
|
||||
const indicatorCandidate = injectIndicatorLabel();
|
||||
// The nearest owner wins: an indicator overrides its surrounding field,
|
||||
// while a field nested inside that indicator keeps its own labels.
|
||||
const indicatorLabel =
|
||||
indicatorCandidate?.fieldScope === inheritedField
|
||||
? indicatorCandidate
|
||||
: undefined;
|
||||
const field = indicatorLabel ? undefined : inheritedField;
|
||||
const attrs = useAttrs();
|
||||
const props = defineProps<{
|
||||
isRequired?: boolean;
|
||||
@@ -16,16 +26,45 @@ const labelPart =
|
||||
const disabled = useInheritedDisabled(
|
||||
() => props.isDisabled || field?.disabled.value,
|
||||
);
|
||||
|
||||
/** Unique fallback so multiple labels under one indicator never share an id. */
|
||||
const autoId = useId();
|
||||
|
||||
function resolveId(): string {
|
||||
const explicit = attrs.id;
|
||||
if (typeof explicit === "string" && explicit) return explicit;
|
||||
return labelPart?.id.value ?? autoId;
|
||||
}
|
||||
|
||||
let unregister: (() => void) | undefined;
|
||||
watch(
|
||||
resolveId,
|
||||
(id, prevId) => {
|
||||
if (!indicatorLabel || id === prevId) return;
|
||||
unregister?.();
|
||||
unregister = indicatorLabel.registerLabel(id);
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onUnmounted(() => {
|
||||
unregister?.();
|
||||
unregister = undefined;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component
|
||||
:is="
|
||||
(field?.kind === 'toggle' || field?.kind === 'group') && !attrs.for
|
||||
? 'span'
|
||||
: 'label'
|
||||
attrs.for
|
||||
? 'label'
|
||||
: field?.kind === 'toggle' || field?.kind === 'group'
|
||||
? 'span'
|
||||
: indicatorLabel && !field
|
||||
? 'span'
|
||||
: 'label'
|
||||
"
|
||||
v-bind="attrs"
|
||||
:id="labelPart?.id.value ?? attrs.id"
|
||||
:id="resolveId()"
|
||||
:for="
|
||||
attrs.for ?? (field?.kind === 'text' ? field.controlId.value : undefined)
|
||||
"
|
||||
|
||||
Reference in New Issue
Block a user