feat: publish ColorField and shared color input parts
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { ColorField } from "../packages/vue/src";
|
||||
import Basic from "../apps/docs/demos/color-field-basic.vue";
|
||||
import Variants from "../apps/docs/demos/color-field-variants.vue";
|
||||
import OnSurface from "../apps/docs/demos/color-field-on-surface.vue";
|
||||
import WithDescription from "../apps/docs/demos/color-field-with-description.vue";
|
||||
import Required from "../apps/docs/demos/color-field-required.vue";
|
||||
import Disabled from "../apps/docs/demos/color-field-disabled.vue";
|
||||
import FullWidth from "../apps/docs/demos/color-field-full-width.vue";
|
||||
import Invalid from "../apps/docs/demos/color-field-invalid.vue";
|
||||
import ChannelEditing from "../apps/docs/demos/color-field-channel-editing.vue";
|
||||
import Controlled from "../apps/docs/demos/color-field-controlled.vue";
|
||||
import FormExample from "../apps/docs/demos/color-field-form-example.vue";
|
||||
import RenderFunction from "../apps/docs/demos/color-field-render-function.vue";
|
||||
import CustomStyles from "../apps/docs/demos/color-field-custom-styles.vue";
|
||||
const demos = {
|
||||
basic: Basic,
|
||||
variants: Variants,
|
||||
"on-surface": OnSurface,
|
||||
"with-description": WithDescription,
|
||||
required: Required,
|
||||
disabled: Disabled,
|
||||
"full-width": FullWidth,
|
||||
invalid: Invalid,
|
||||
"channel-editing": ChannelEditing,
|
||||
controlled: Controlled,
|
||||
"form-example": FormExample,
|
||||
"render-function": RenderFunction,
|
||||
"custom-styles": CustomStyles,
|
||||
};
|
||||
const mounted: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => {
|
||||
mounted.splice(0).forEach((w) => w.unmount());
|
||||
document.body.innerHTML = "";
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
function render(component: Parameters<typeof mount>[0]) {
|
||||
const w = mount(component, { attachTo: document.body });
|
||||
mounted.push(w);
|
||||
return w;
|
||||
}
|
||||
const inputs = (w: ReturnType<typeof mount>) =>
|
||||
w.findAll<HTMLInputElement>("input[type=text]");
|
||||
describe("ColorField documentation", () => {
|
||||
it("keeps all thirteen fixed-source previews in page order", () => {
|
||||
const doc = readFileSync("apps/docs/components/color-field.md", "utf8");
|
||||
expect(
|
||||
[...doc.matchAll(/<ComponentPreview name="color-field-([^"]+)"/g)].map(
|
||||
(m) => m[1],
|
||||
),
|
||||
).toEqual(Object.keys(demos));
|
||||
});
|
||||
for (const [name, demo] of Object.entries(demos))
|
||||
it(`renders ${name} through public parts with a label for every input`, () => {
|
||||
const w = render(demo);
|
||||
expect(inputs(w).length).toBeGreaterThan(0);
|
||||
for (const input of inputs(w))
|
||||
expect(w.find(`label[for="${input.attributes("id")}"]`).exists()).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
readFileSync(`apps/docs/demos/color-field-${name}.vue`, "utf8"),
|
||||
).not.toContain("packages/vue");
|
||||
});
|
||||
it("synchronizes basic hex editing and preview on blur", async () => {
|
||||
const w = render(Basic);
|
||||
expect(inputs(w)[0]!.element.value).toBe("#0485F7");
|
||||
await inputs(w)[0]!.setValue("22c55e");
|
||||
await inputs(w)[0]!.trigger("blur");
|
||||
expect(inputs(w)[0]!.element.value).toBe("#22C55E");
|
||||
expect(
|
||||
w.get<HTMLElement>("[data-slot=color-swatch]").element.style
|
||||
.backgroundColor,
|
||||
).toBe("rgb(34, 197, 94)");
|
||||
});
|
||||
it("renders actual secondary, full width, disabled and invalid states", () => {
|
||||
expect(
|
||||
render(Variants)
|
||||
.find("[data-slot=color-input-group].hv-color-input-group--secondary")
|
||||
.exists(),
|
||||
).toBe(true);
|
||||
expect(
|
||||
render(OnSurface)
|
||||
.find(".hv-surface .hv-color-input-group--secondary")
|
||||
.exists(),
|
||||
).toBe(true);
|
||||
expect(
|
||||
render(FullWidth).findAll(".hv-color-field--full-width"),
|
||||
).toHaveLength(2);
|
||||
expect(inputs(render(Disabled)).every((i) => i.element.disabled)).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
inputs(render(Invalid)).every(
|
||||
(i) => i.attributes("aria-invalid") === "true",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(inputs(render(Required)).every((i) => i.element.required)).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
it("updates all three HSL fields from one accepted color", async () => {
|
||||
const w = render(ChannelEditing);
|
||||
await inputs(w)[0]!.setValue("90");
|
||||
await inputs(w)[0]!.trigger("blur");
|
||||
expect(inputs(w)[0]!.element.value).toBe("90°");
|
||||
expect(inputs(w)[1]!.element.value).toBe("100%");
|
||||
expect(w.text()).toContain("当前:#407F00");
|
||||
});
|
||||
it("controlled buttons replace and clear both input and preview", async () => {
|
||||
const w = render(Controlled);
|
||||
await w.findAll("button")[0]!.trigger("click");
|
||||
expect(inputs(w)[0]!.element.value).toBe("#EF4444");
|
||||
await w.findAll("button")[1]!.trigger("click");
|
||||
expect(inputs(w)[0]!.element.value).toBe("#10B981");
|
||||
await w.findAll("button")[2]!.trigger("click");
|
||||
expect(inputs(w)[0]!.element.value).toBe("");
|
||||
expect(w.text()).toContain("(空)");
|
||||
});
|
||||
it("uses a live scoped slot in the custom root example", async () => {
|
||||
const w = render(RenderFunction);
|
||||
// Let mounted field metadata and child refs settle before exercising native focus.
|
||||
await flushPromises();
|
||||
inputs(w)[0]!.element.focus();
|
||||
await flushPromises();
|
||||
expect(w.get("[data-slot=color-field]").attributes("data-focused")).toBe(
|
||||
"true",
|
||||
);
|
||||
expect(
|
||||
w.get("[data-slot=color-input-group]").attributes("data-field-focused"),
|
||||
).toBe("true");
|
||||
});
|
||||
it("submits the accepted form color and restores empty state after saving", async () => {
|
||||
vi.useFakeTimers();
|
||||
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
const w = render(FormExample);
|
||||
expect(w.get<HTMLButtonElement>("button").element.disabled).toBe(true);
|
||||
await inputs(w)[0]!.setValue("abc");
|
||||
await inputs(w)[0]!.trigger("blur");
|
||||
expect(w.get<HTMLButtonElement>("button").element.disabled).toBe(false);
|
||||
expect(new FormData(w.get("form").element).get("brand-color")).toBe(
|
||||
"#AABBCC",
|
||||
);
|
||||
await w.get("form").trigger("submit");
|
||||
expect(w.text()).toContain("保存中…");
|
||||
await vi.advanceTimersByTimeAsync(1500);
|
||||
expect(log).toHaveBeenCalledWith("已提交颜色:", { color: "#AABBCC" });
|
||||
expect(inputs(w)[0]!.element.value).toBe("");
|
||||
expect(w.getComponent(ColorField).emitted("change")).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,13 @@
|
||||
import { afterEach, describe, it, expect, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, shallowRef } from "vue";
|
||||
import ColorField from "../packages/vue/src/components/color-field/ColorField.vue";
|
||||
import ColorInputGroup from "../packages/vue/src/components/color-field/ColorInputGroup.vue";
|
||||
import ColorInputGroupInput from "../packages/vue/src/components/color-field/ColorInputGroupInput.vue";
|
||||
import Prefix from "../packages/vue/src/components/color-field/ColorInputGroupPrefix.vue";
|
||||
import Suffix from "../packages/vue/src/components/color-field/ColorInputGroupSuffix.vue";
|
||||
import {
|
||||
ColorField,
|
||||
ColorInputGroup,
|
||||
ColorInputGroupInput,
|
||||
ColorInputGroupPrefix as Prefix,
|
||||
ColorInputGroupSuffix as Suffix,
|
||||
} from "../packages/vue/src/index";
|
||||
import {
|
||||
Label,
|
||||
Description,
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, shallowRef, h } from "vue";
|
||||
import * as api from "../packages/vue/src";
|
||||
const mounted: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => {
|
||||
mounted.splice(0).forEach((w) => w.unmount());
|
||||
document.body.innerHTML = "";
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
function view(template: string, setup = () => ({})) {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
ColorField: api.ColorField,
|
||||
ColorFieldGroup: api.ColorFieldGroup,
|
||||
ColorFieldInput: api.ColorFieldInput,
|
||||
ColorFieldPrefix: api.ColorFieldPrefix,
|
||||
ColorFieldSuffix: api.ColorFieldSuffix,
|
||||
ColorInputGroup: api.ColorInputGroup,
|
||||
ColorInputGroupInput: api.ColorInputGroupInput,
|
||||
Label: api.Label,
|
||||
Description: api.Description,
|
||||
FieldError: api.FieldError,
|
||||
},
|
||||
setup,
|
||||
template,
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
mounted.push(w);
|
||||
return w;
|
||||
}
|
||||
const control = (w: ReturnType<typeof mount>) =>
|
||||
w.get<HTMLInputElement>("[data-slot=color-input-group-input]");
|
||||
const body =
|
||||
"<Label>颜色</Label><ColorFieldGroup><ColorFieldPrefix>#</ColorFieldPrefix><ColorFieldInput/><ColorFieldSuffix>色</ColorFieldSuffix></ColorFieldGroup><Description>帮助</Description><FieldError/>";
|
||||
describe("public ColorField", () => {
|
||||
it("exports aliases without creating separate implementations", () => {
|
||||
expect(api.ColorFieldRoot).toBe(api.ColorField);
|
||||
expect(api.ColorInputGroupRoot).toBe(api.ColorFieldGroup);
|
||||
expect(api.ColorInputGroupInput).toBe(api.ColorFieldInput);
|
||||
expect(api.ColorInputGroupPrefix).toBe(api.ColorFieldPrefix);
|
||||
expect(api.ColorInputGroupSuffix).toBe(api.ColorFieldSuffix);
|
||||
expect(api.colorFieldVariants({ fullWidth: true })).toContain(
|
||||
"hv-color-field--full-width",
|
||||
);
|
||||
expect(
|
||||
api.colorInputGroupVariants({ variant: "secondary" }).base(),
|
||||
).toContain("hv-color-input-group--secondary");
|
||||
});
|
||||
it("routes every ARIA attribute to the input and updates root attrs reactively", async () => {
|
||||
const details = shallowRef("details-a");
|
||||
const w = view(
|
||||
`<ColorField aria-label="颜色" aria-labelledby="external-label" aria-describedby="external-description" :aria-details="details">${body}</ColorField>`,
|
||||
() => ({ details }),
|
||||
);
|
||||
expect(control(w).attributes("aria-details")).toBe("details-a");
|
||||
expect(control(w).attributes("aria-labelledby")).toContain(
|
||||
"external-label",
|
||||
);
|
||||
expect(control(w).attributes("aria-describedby")).toContain(
|
||||
"external-description",
|
||||
);
|
||||
expect(
|
||||
w.get("[data-slot=color-field]").attributes("aria-details"),
|
||||
).toBeUndefined();
|
||||
details.value = "details-b";
|
||||
await flushPromises();
|
||||
expect(control(w).attributes("aria-details")).toBe("details-b");
|
||||
});
|
||||
it("updates scoped slots, styles and focus states from the real input", async () => {
|
||||
const w = view(
|
||||
`<ColorField as="section" default-value="#123456" :style-fn="s => ({ opacity: s.isFocused ? '.5' : '1' })" v-slot="s"><ColorFieldGroup :style-fn="g => ({ marginTop: g.isFocusWithin ? '2px' : '0px' })"><ColorFieldInput /></ColorFieldGroup><output>{{s.isFocused}}/{{s.isFocusWithin}}</output></ColorField>`,
|
||||
);
|
||||
control(w).element.focus();
|
||||
await flushPromises();
|
||||
const root = w.get<HTMLElement>("section");
|
||||
expect(root.attributes("data-focused")).toBe("true");
|
||||
expect(root.attributes("data-focus-within")).toBe("true");
|
||||
expect(root.element.style.opacity).toBe("0.5");
|
||||
expect(
|
||||
w.get<HTMLElement>("[data-slot=color-input-group]").element.style
|
||||
.marginTop,
|
||||
).toBe("2px");
|
||||
expect(w.get("output").text()).toBe("true/true");
|
||||
control(w).element.blur();
|
||||
await flushPromises();
|
||||
expect(w.get("output").text()).toBe("false/false");
|
||||
expect(root.attributes("data-focus-within")).toBeUndefined();
|
||||
});
|
||||
it("exposes the actual input and color, with usable focus()", async () => {
|
||||
const w = view(`<ColorField default-value="#123456">${body}</ColorField>`);
|
||||
const field = w.getComponent(api.ColorField);
|
||||
field.vm.focus();
|
||||
await flushPromises();
|
||||
expect(document.activeElement).toBe(control(w).element);
|
||||
expect(field.vm.element).toBe(control(w).element);
|
||||
expect(field.vm.color?.toString("hex")).toBe("#123456");
|
||||
});
|
||||
it("retries a rejected hex, preserves the native draft until commit and resets accepted DOM", async () => {
|
||||
const w = view(
|
||||
`<form><ColorField name="color" model-value="#123456">${body}</ColorField></form>`,
|
||||
);
|
||||
for (let i = 0; i < 2; i++) {
|
||||
await control(w).setValue("abc");
|
||||
expect(new FormData(w.get("form").element).get("color")).toBe("abc");
|
||||
await control(w).trigger("keydown", { key: "Enter" });
|
||||
expect(control(w).element.value).toBe("#123456");
|
||||
}
|
||||
expect(w.getComponent(api.ColorField).emitted("change")).toHaveLength(2);
|
||||
await control(w).setValue("#12");
|
||||
w.get("form").element.reset();
|
||||
await flushPromises();
|
||||
expect(control(w).element.value).toBe("#123456");
|
||||
});
|
||||
it("modelValue including null takes precedence over value/defaultValue", async () => {
|
||||
const w = view(
|
||||
`<ColorField :model-value="null" value="#abcdef" default-value="#123456">${body}</ColorField>`,
|
||||
);
|
||||
expect(control(w).element.value).toBe("");
|
||||
await control(w).setValue("abc");
|
||||
await control(w).trigger("blur");
|
||||
expect(control(w).element.value).toBe("");
|
||||
});
|
||||
it("switches between standalone and inherited color values without leaking drafts", async () => {
|
||||
const owns = shallowRef(false);
|
||||
const w = view(
|
||||
`<ColorField default-value="hsl(200,50%,50%)"><ColorFieldGroup><ColorFieldInput /></ColorFieldGroup><ColorField v-bind="owns ? { modelValue: '#abcdef' } : {}"><ColorFieldGroup><ColorFieldInput /></ColorFieldGroup></ColorField></ColorField>`,
|
||||
() => ({ owns }),
|
||||
);
|
||||
const inputs = w.findAll<HTMLInputElement>("input[type=text]");
|
||||
expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value);
|
||||
await inputs[1]!.setValue("#12");
|
||||
owns.value = true;
|
||||
await flushPromises();
|
||||
expect(inputs[1]!.element.value).toBe("#ABCDEF");
|
||||
owns.value = false;
|
||||
await flushPromises();
|
||||
expect(inputs[1]!.element.value).toBe(inputs[0]!.element.value);
|
||||
});
|
||||
it("commits composition only after candidate completion and preserves alpha when text is untouched", async () => {
|
||||
const w = view(
|
||||
`<ColorField default-value="hsba(270,0%,50%,.123456789)">${body}</ColorField>`,
|
||||
);
|
||||
await control(w).trigger("compositionstart");
|
||||
await control(w).setValue("abc");
|
||||
await control(w).trigger("keydown", { key: "Enter" });
|
||||
expect(w.getComponent(api.ColorField).emitted("change")).toBeUndefined();
|
||||
await control(w).trigger("compositionend");
|
||||
await flushPromises();
|
||||
await control(w).trigger("keydown", { key: "Escape" });
|
||||
await control(w).trigger("blur");
|
||||
expect(
|
||||
w.getComponent(api.ColorField).vm.color?.getChannelValue("alpha"),
|
||||
).toBe(0.123456789);
|
||||
});
|
||||
it("does not run unknown wrappers to discover an input", () => {
|
||||
const setup = vi.fn(() => () => h(api.ColorFieldInput));
|
||||
const Unknown = defineComponent({ setup });
|
||||
const w = mount(api.ColorField, { slots: { default: () => h(Unknown) } });
|
||||
mounted.push(w);
|
||||
expect(setup).toHaveBeenCalledOnce();
|
||||
expect(control(w).element.tagName).toBe("INPUT");
|
||||
});
|
||||
it("supports an independent group as a plain v-model text field", async () => {
|
||||
const value = shallowRef("free text");
|
||||
const w = view(
|
||||
'<ColorInputGroup variant="secondary"><ColorInputGroupInput v-model="value" name="plain" /></ColorInputGroup>',
|
||||
() => ({ value }),
|
||||
);
|
||||
await control(w).setValue("anything");
|
||||
expect(value.value).toBe("anything");
|
||||
expect(w.get("[data-slot=color-input-group]").attributes("role")).toBe(
|
||||
"group",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -44,6 +44,26 @@ for (const id of process.argv.slice(2)) {
|
||||
assert(lib.RangeCalendar === lib.RangeCalendarRoot);
|
||||
assert(lib.rangeCalendarVariants().cell() === "hv-range-calendar__cell");
|
||||
}
|
||||
if (id === "color-field") {
|
||||
assert(lib.ColorField === lib.ColorFieldRoot);
|
||||
assert(lib.ColorFieldGroup === lib.ColorInputGroup);
|
||||
assert(lib.ColorFieldGroup === lib.ColorInputGroupRoot);
|
||||
assert(lib.ColorFieldInput === lib.ColorInputGroupInput);
|
||||
assert(lib.ColorFieldPrefix === lib.ColorInputGroupPrefix);
|
||||
assert(lib.ColorFieldSuffix === lib.ColorInputGroupSuffix);
|
||||
assert(
|
||||
lib
|
||||
.colorFieldVariants({ fullWidth: true })
|
||||
.includes("hv-color-field--full-width"),
|
||||
);
|
||||
const slots = lib.colorInputGroupVariants({
|
||||
variant: "secondary",
|
||||
fullWidth: true,
|
||||
});
|
||||
assert(slots.base().includes("hv-color-input-group--secondary"));
|
||||
assert(slots.base().includes("hv-color-input-group--full-width"));
|
||||
assert(slots.input() === "hv-color-input-group__input");
|
||||
}
|
||||
if (id === "time-field") {
|
||||
assert(lib.TimeField === lib.TimeFieldRoot);
|
||||
assert(lib.TimeFieldGroup === lib.DateInputGroup);
|
||||
|
||||
@@ -137,12 +137,12 @@
|
||||
".hv-color-input-group__prefix",
|
||||
".hv-color-input-group__suffix"
|
||||
],
|
||||
"absent": ["ColorField", "ColorInputGroup", "ColorPicker"]
|
||||
"absent": ["ColorPicker"]
|
||||
},
|
||||
"color-foundation": {
|
||||
"exports": [],
|
||||
"css": [".hv-color-area", ".hv-color-area__thumb"],
|
||||
"absent": ["ColorField", "ColorInputGroup", "ColorPicker"]
|
||||
"absent": ["ColorPicker"]
|
||||
},
|
||||
"color-picker-protocol": {
|
||||
"exports": [],
|
||||
@@ -151,7 +151,7 @@
|
||||
".hv-color-picker__trigger",
|
||||
".hv-color-picker__popover"
|
||||
],
|
||||
"absent": ["ColorField", "ColorInputGroup", "ColorPicker"]
|
||||
"absent": ["ColorPicker"]
|
||||
},
|
||||
"color-slider-foundation": {
|
||||
"exports": [],
|
||||
@@ -165,7 +165,7 @@
|
||||
"max-height:inherit",
|
||||
"overscroll-behavior:contain"
|
||||
],
|
||||
"absent": ["ColorField", "ColorInputGroup", "ColorPicker"]
|
||||
"absent": ["ColorPicker"]
|
||||
},
|
||||
"date-field": {
|
||||
"exports": [
|
||||
@@ -462,5 +462,32 @@
|
||||
"--track-start-color",
|
||||
"--track-end-color"
|
||||
]
|
||||
},
|
||||
"color-field": {
|
||||
"exports": [
|
||||
"ColorField",
|
||||
"ColorFieldRoot",
|
||||
"ColorFieldGroup",
|
||||
"ColorFieldInput",
|
||||
"ColorFieldPrefix",
|
||||
"ColorFieldSuffix",
|
||||
"ColorInputGroup",
|
||||
"ColorInputGroupRoot",
|
||||
"ColorInputGroupInput",
|
||||
"ColorInputGroupPrefix",
|
||||
"ColorInputGroupSuffix",
|
||||
"colorFieldVariants",
|
||||
"colorInputGroupVariants"
|
||||
],
|
||||
"css": [
|
||||
".hv-color-field",
|
||||
".hv-color-field--full-width",
|
||||
".hv-color-input-group",
|
||||
".hv-color-input-group--secondary",
|
||||
".hv-color-input-group__input",
|
||||
".hv-color-input-group__prefix",
|
||||
".hv-color-input-group__suffix"
|
||||
],
|
||||
"absent": ["ColorPicker", "ColorSwatchPicker"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import { h } from "vue";
|
||||
import {
|
||||
ColorField,
|
||||
ColorFieldRoot,
|
||||
ColorFieldGroup,
|
||||
ColorFieldInput,
|
||||
ColorFieldPrefix,
|
||||
ColorFieldSuffix,
|
||||
ColorInputGroup,
|
||||
ColorInputGroupRoot,
|
||||
ColorInputGroupInput,
|
||||
ColorInputGroupPrefix,
|
||||
ColorInputGroupSuffix,
|
||||
colorFieldVariants,
|
||||
colorInputGroupVariants,
|
||||
parseColor,
|
||||
type ColorFieldProps,
|
||||
type ColorFieldRootProps,
|
||||
type ColorFieldGroupProps,
|
||||
type ColorFieldInputProps,
|
||||
type ColorFieldPrefixProps,
|
||||
type ColorFieldSuffixProps,
|
||||
type ColorFieldRenderProps,
|
||||
type ColorFieldRenderState,
|
||||
type ColorFieldSlots,
|
||||
type ColorFieldExpose,
|
||||
type ColorFieldVariants,
|
||||
type ColorInputGroupVariants,
|
||||
type ColorInputGroupProps,
|
||||
type ColorInputGroupInputProps,
|
||||
type ColorInputGroupRootProps,
|
||||
type ColorInputGroupPrefixProps,
|
||||
type ColorInputGroupSuffixProps,
|
||||
type ColorInputGroupRenderProps,
|
||||
} from "@heroui-vue/components";
|
||||
const props: ColorFieldProps = {
|
||||
modelValue: parseColor("#123456"),
|
||||
value: null,
|
||||
defaultValue: "#abcdef",
|
||||
channel: "hue",
|
||||
colorSpace: "hsb",
|
||||
name: "hue",
|
||||
form: "external",
|
||||
isRequired: true,
|
||||
isReadOnly: false,
|
||||
isWheelDisabled: true,
|
||||
locale: "de-DE",
|
||||
validate: (color) => (color ? true : "必填"),
|
||||
styleFn: (s) => ({ opacity: s.isFocused ? 0.5 : 1 }),
|
||||
};
|
||||
const root: ColorFieldRootProps = props;
|
||||
const group: ColorFieldGroupProps = {
|
||||
variant: "secondary",
|
||||
styleFn: (s) => ({ opacity: s.isFocusWithin ? 1 : 0.5 }),
|
||||
};
|
||||
const ig: ColorInputGroupProps = group;
|
||||
const igr: ColorInputGroupRootProps = ig;
|
||||
const input: ColorFieldInputProps = { defaultValue: "abc" };
|
||||
const ii: ColorInputGroupInputProps = input;
|
||||
const prefix: ColorFieldPrefixProps = { as: "span" };
|
||||
const ip: ColorInputGroupPrefixProps = prefix;
|
||||
const suffix: ColorFieldSuffixProps = { as: "span" };
|
||||
const is: ColorInputGroupSuffixProps = suffix;
|
||||
const slots: ColorFieldSlots = {
|
||||
default: (s) => [h("span", s.value?.toString("hex"))],
|
||||
};
|
||||
const state: ColorFieldRenderProps = {
|
||||
value: null,
|
||||
isDisabled: false,
|
||||
isReadOnly: false,
|
||||
isRequired: false,
|
||||
isInvalid: false,
|
||||
isFocused: false,
|
||||
isFocusWithin: false,
|
||||
isFocusVisible: false,
|
||||
validationErrors: [],
|
||||
};
|
||||
const renderState: ColorFieldRenderState = state;
|
||||
const gs: ColorInputGroupRenderProps = {
|
||||
isDisabled: false,
|
||||
isInvalid: false,
|
||||
isFocusWithin: false,
|
||||
isHovered: false,
|
||||
};
|
||||
const variant: ColorFieldVariants = { fullWidth: true };
|
||||
const iv: ColorInputGroupVariants = { variant: "secondary" };
|
||||
colorFieldVariants(variant);
|
||||
colorInputGroupVariants(iv).input();
|
||||
h(ColorField, {
|
||||
...props,
|
||||
"onUpdate:modelValue": (value) => value?.toString("hex"),
|
||||
});
|
||||
h(ColorFieldRoot, root);
|
||||
h(ColorFieldGroup, group);
|
||||
h(ColorFieldInput, input);
|
||||
h(ColorFieldPrefix, prefix);
|
||||
h(ColorFieldSuffix, suffix);
|
||||
h(ColorInputGroup, ig);
|
||||
h(ColorInputGroupRoot, igr);
|
||||
h(ColorInputGroupInput, ii);
|
||||
h(ColorInputGroupPrefix, ip);
|
||||
h(ColorInputGroupSuffix, is);
|
||||
declare const field: InstanceType<typeof ColorField>;
|
||||
field.focus();
|
||||
field.element?.select();
|
||||
field.color?.toString("hex");
|
||||
const exposed: ColorFieldExpose = field;
|
||||
declare const control: InstanceType<typeof ColorFieldInput>;
|
||||
control.focus();
|
||||
control.select();
|
||||
control.element?.select();
|
||||
// @ts-expect-error invalid model
|
||||
const bad: ColorFieldProps = { modelValue: 123 };
|
||||
// @ts-expect-error invalid channel
|
||||
const badChannel: ColorFieldProps = { channel: "cyan" };
|
||||
// @ts-expect-error invalid color space
|
||||
h(ColorField, { colorSpace: "oklch" });
|
||||
// @ts-expect-error actual public component rejects invalid model
|
||||
h(ColorField, { modelValue: 123 });
|
||||
// @ts-expect-error variant is primary or secondary
|
||||
const badGroup: ColorFieldGroupProps = { variant: "tertiary" };
|
||||
// @ts-expect-error validators receive Color/null
|
||||
const badValidator: ColorFieldProps = { validate: (v: string) => true };
|
||||
// @ts-expect-error no public transaction state
|
||||
field.state.begin();
|
||||
void [slots, renderState, gs, exposed, bad, badChannel, badGroup, badValidator];
|
||||
Reference in New Issue
Block a user