168 lines
4.6 KiB
TypeScript
168 lines
4.6 KiB
TypeScript
import { h } from "vue";
|
|
import {
|
|
ColorPicker,
|
|
ColorPickerRoot,
|
|
ColorPickerTrigger,
|
|
ColorPickerPopover,
|
|
colorPickerVariants,
|
|
parseColor,
|
|
type Color,
|
|
type ColorPickerExpose,
|
|
type ColorPickerPopoverProps,
|
|
type ColorPickerPopoverRenderProps,
|
|
type ColorPickerPopoverSlots,
|
|
type ColorPickerProps,
|
|
type ColorPickerRenderProps,
|
|
type ColorPickerRootProps,
|
|
type ColorPickerSlots,
|
|
type ColorPickerTriggerProps,
|
|
type ColorPickerTriggerRenderProps,
|
|
type ColorPickerTriggerSlots,
|
|
type ColorPickerVariants,
|
|
} from "@heroui-vue/components";
|
|
|
|
const props: ColorPickerProps = {
|
|
modelValue: parseColor("#123456"),
|
|
value: "#abcdef",
|
|
defaultValue: "#0485F7",
|
|
open: false,
|
|
isOpen: true,
|
|
defaultOpen: true,
|
|
isDisabled: false,
|
|
disabled: false,
|
|
isReadOnly: true,
|
|
name: "color",
|
|
form: "theme",
|
|
as: "section",
|
|
};
|
|
const root: ColorPickerRootProps = props;
|
|
const trigger: ColorPickerTriggerProps = {
|
|
as: "button",
|
|
asChild: false,
|
|
isDisabled: true,
|
|
};
|
|
const popover: ColorPickerPopoverProps = {
|
|
placement: "bottom left",
|
|
offset: 8,
|
|
crossOffset: 2,
|
|
shouldFlip: true,
|
|
containerPadding: 12,
|
|
isNonModal: true,
|
|
isDismissable: false,
|
|
isKeyboardDismissDisabled: true,
|
|
};
|
|
const slots: ColorPickerSlots = {
|
|
default: (state: ColorPickerRenderProps) => [
|
|
h("span", {}, [
|
|
state.color.toString("hex"),
|
|
String(state.isOpen),
|
|
String(state.isDisabled),
|
|
String(state.isReadOnly),
|
|
state.close(),
|
|
]),
|
|
],
|
|
};
|
|
const triggerSlots: ColorPickerTriggerSlots = {
|
|
default: (state: ColorPickerTriggerRenderProps) => [
|
|
h("span", {}, [state.color?.toString("hex"), String(state.isDisabled)]),
|
|
],
|
|
};
|
|
const popoverSlots: ColorPickerPopoverSlots = {
|
|
default: (state: ColorPickerPopoverRenderProps) => [
|
|
h("span", {}, [String(state.isOpen), state.close()]),
|
|
],
|
|
};
|
|
const state: ColorPickerRenderProps = {
|
|
color: parseColor("#8B5CF6"),
|
|
isOpen: true,
|
|
isDisabled: false,
|
|
isReadOnly: false,
|
|
close: () => {},
|
|
};
|
|
const triggerState: ColorPickerTriggerRenderProps = {
|
|
color: parseColor("#8B5CF6"),
|
|
isDisabled: false,
|
|
};
|
|
const popoverState: ColorPickerPopoverRenderProps = {
|
|
isOpen: false,
|
|
close: () => {},
|
|
};
|
|
const variants: ColorPickerVariants = {};
|
|
colorPickerVariants(variants).base();
|
|
colorPickerVariants(variants).trigger();
|
|
colorPickerVariants(variants).popover();
|
|
|
|
h(
|
|
ColorPicker,
|
|
{
|
|
...props,
|
|
"onUpdate:modelValue": (value: Color) => value.toString("hex"),
|
|
onChange: (value: Color) => value.toString("hexa"),
|
|
onChangeEnd: (value: Color) => value.getChannelValue("alpha"),
|
|
"onUpdate:open": (value: boolean) => !value,
|
|
"onUpdate:isOpen": (value: boolean) => !value,
|
|
onOpenChange: (value: boolean) => !value,
|
|
},
|
|
{
|
|
default: (state: ColorPickerRenderProps) => [
|
|
h("span", {}, state.color.toString("hex")),
|
|
h("span", {}, String(state.isOpen)),
|
|
],
|
|
},
|
|
);
|
|
h(ColorPickerRoot, root, () => [h("span", "内容")]);
|
|
h(ColorPickerTrigger, trigger, {
|
|
default: (state: ColorPickerTriggerRenderProps) => [
|
|
h("span", {}, state.color?.toString("hex")),
|
|
],
|
|
});
|
|
h(ColorPickerPopover, popover, {
|
|
default: (state: ColorPickerPopoverRenderProps) => [
|
|
h("span", {}, String(state.isOpen)),
|
|
],
|
|
});
|
|
declare const picker: InstanceType<typeof ColorPicker>;
|
|
declare const rootInstance: InstanceType<typeof ColorPickerRoot>;
|
|
picker.element?.focus();
|
|
picker.color?.toString("hex");
|
|
rootInstance.element?.focus();
|
|
rootInstance.color?.toString("hex");
|
|
const exposed: ColorPickerExpose = picker;
|
|
type ColorPickerInstanceProps = InstanceType<typeof ColorPicker>["$props"];
|
|
const instanceProps: ColorPickerInstanceProps = { as: "div" };
|
|
|
|
// @ts-expect-error invalid model
|
|
const bad: ColorPickerProps = { modelValue: 123 };
|
|
// @ts-expect-error picker is non-null even when a child field is cleared
|
|
const empty: ColorPickerProps = { modelValue: null };
|
|
// @ts-expect-error open accepts booleans only
|
|
const badOpen: ColorPickerProps = { open: "yes" };
|
|
// @ts-expect-error actual public component rejects invalid value
|
|
h(ColorPicker, { value: 123 });
|
|
// @ts-expect-error the component's own props reject numeric values
|
|
const badInstance: ColorPickerInstanceProps = { value: 123 };
|
|
// @ts-expect-error no public transaction or owner state
|
|
picker.state.begin();
|
|
// @ts-expect-error popover placement is an overlay placement
|
|
const badPlacement: ColorPickerPopoverProps = { placement: "center" };
|
|
// @ts-expect-error trigger renders a real button type only through `as`
|
|
const badTrigger: ColorPickerTriggerProps = { asChild: "yes" };
|
|
|
|
void [
|
|
slots,
|
|
triggerSlots,
|
|
popoverSlots,
|
|
state,
|
|
triggerState,
|
|
popoverState,
|
|
exposed,
|
|
variants,
|
|
instanceProps,
|
|
bad,
|
|
empty,
|
|
badOpen,
|
|
badInstance,
|
|
badPlacement,
|
|
badTrigger,
|
|
];
|