fix: enforce ColorSlider union props and clean part refs

This commit is contained in:
maoyingjie
2026-10-27 10:23:10 +08:00
parent 92eb59e389
commit ea881cbcf8
9 changed files with 221 additions and 62 deletions
+60
View File
@@ -579,6 +579,66 @@ describe("ColorSlider public contract", () => {
expect(component.emitted("changeEnd")).toBeUndefined();
});
it("clears thumb-owned refs when only the thumb unmounts and allows remount", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack><ColorSliderThumb v-if="show" /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const first = input(w).element;
const focus = vi.spyOn(first, "focus");
const bar = geometry(w);
await pointer(bar, "pointerdown", { clientX: 65 });
expect(bar.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(bar.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
expect(input(w).element).not.toBe(first);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(document.activeElement).toBe(input(w).element);
const remounted = geometry(w);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("clears track-owned refs when the track unmounts and remounts", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack v-if="show"><ColorSliderThumb /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const oldTrack = geometry(w);
const oldInput = input(w).element;
const focus = vi.spyOn(oldInput, "focus");
await pointer(oldTrack, "pointerdown", { clientX: 65 });
expect(oldTrack.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(oldTrack.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
const remounted = geometry(w);
expect(remounted).not.toBe(oldTrack);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("aborts stale drags on external replacement but accepts synchronous echoes", async () => {
const value = shallowRef(parseColor("hsb(240,50%,50%)"));
const w = slide(
+29
View File
@@ -1,3 +1,4 @@
import { h } from "vue";
import {
ColorSlider,
ColorSliderOutput,
@@ -140,6 +141,30 @@ const exposed: ColorSliderExpose = {
focus: () => {},
};
const typedColor: Color = color;
type ColorSliderInstanceProps = InstanceType<typeof ColorSlider>["$props"];
const instanceProps: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "hsb",
};
// @ts-expect-error the component's own props reject red + hsl
const badInstanceRed: ColorSliderInstanceProps = {
channel: "red",
colorSpace: "hsl",
};
// @ts-expect-error the component's own props reject hue + rgb
const badInstanceHue: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "rgb",
};
const sliderNode = h(ColorSlider, {
channel: "hue",
colorSpace: "hsb",
defaultValue: "hsb(240, 50%, 50%)",
});
// @ts-expect-error h() rejects red + hsl on the real component
h(ColorSlider, { channel: "red", colorSpace: "hsl" });
// @ts-expect-error h() rejects hue + rgb on the real component
h(ColorSlider, { channel: "hue", colorSpace: "rgb" });
// @ts-expect-error red requires the rgb color space
const badRed: ColorSliderProps = { channel: "red", colorSpace: "hsl" };
@@ -219,6 +244,10 @@ void [
variants,
exposed,
typedColor,
instanceProps,
badInstanceRed,
badInstanceHue,
sliderNode,
badRed,
badHue,
badBrightness,