-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chizuki
committed
Feb 16, 2023
1 parent
30f84c7
commit 29185d2
Showing
10 changed files
with
244 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,6 @@ export const HiItem = defineComponent({ | |
props, | ||
context | ||
); | ||
|
||
return () => | ||
h( | ||
"div", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import type { PropType } from "vue"; | ||
|
||
export const valuePropType = [String, Number, Object, Boolean, null] as PropType<any>; | ||
export const valuePropType = [ | ||
String, | ||
Number, | ||
Object, | ||
Boolean, | ||
null | ||
] as PropType<any>; | ||
|
||
export const classPropType = [String, Array] as PropType<string | string[]>; | ||
export const classPropType = [String, Array, Object] as PropType< | ||
string | string[] | Record<string, boolean> | ||
>; | ||
|
||
export const labelPropType = [String, Function] as PropType<string | ((val?: any) => string) | null>; | ||
export const labelPropType = [String, Function] as PropType< | ||
string | ((val?: any) => string) | null | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,131 @@ | ||
import type { ComponentPropsOptions, EmitsOptions, ExtractPropTypes, SetupContext } from "vue"; | ||
import type { MaybeFunction } from "maybe-types"; | ||
import type { | ||
ComponentObjectPropsOptions, | ||
ComponentPropsOptions, | ||
EmitsOptions, | ||
ExtractDefaultPropTypes, | ||
ExtractPropTypes, | ||
PropType, | ||
SetupContext | ||
} from "vue"; | ||
import { reactiveComputed } from "@vueuse/core"; | ||
import type { ClassType } from "./types"; | ||
|
||
export interface HookComponentOptions<R, E = EmitsOptions, EE extends string = string, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>> { | ||
export interface HookComponentOptions< | ||
R, | ||
E = EmitsOptions, | ||
EE extends string = string, | ||
P = ComponentPropsOptions, | ||
D = ExtractPropTypes<P> | ||
> { | ||
props?: P | ||
emits?: E | EE[] | ||
setup: (props: D, context: SetupContext<E>) => R | ||
} | ||
|
||
export type HookComponent<R, E = EmitsOptions, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>> = (props: MaybeFunction<D>, context: SetupContext<E>) => R; | ||
export type HookComponent< | ||
R, | ||
E = EmitsOptions, | ||
P = ComponentPropsOptions, | ||
D = ExtractPropTypes<P>, | ||
Defaults = ExtractDefaultPropTypes<P> | ||
> = ( | ||
props: MaybeFunction<Partial<Defaults> & Omit<D, keyof Defaults>>, | ||
context: SetupContext<E> | ||
) => R & { $props: D }; | ||
|
||
export function defineHookProps<P = ComponentPropsOptions>(props: P) { | ||
export function defineHookProps< | ||
P extends ComponentObjectPropsOptions = ComponentObjectPropsOptions | ||
>(props: P) { | ||
return props; | ||
} | ||
|
||
export function defineHookEmits<E, EE extends string = string>(emits: E | EE[]) { | ||
export function defineHookEmits< | ||
E extends EmitsOptions = EmitsOptions, | ||
EE extends string = string | ||
>(emits: E | EE[]) { | ||
return emits; | ||
} | ||
|
||
export function defineHookComponent<R, E = EmitsOptions, EE extends string = string, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>>(options: HookComponentOptions<R, E, EE, P, D>): HookComponent<R, E, P, D> { | ||
return (props: MaybeFunction<D>, context: SetupContext<E>) => { | ||
const p = props instanceof Function ? reactiveComputed(() => props()) : props; | ||
return options.setup(p, context); | ||
export function defineHookComponent< | ||
R, | ||
E = EmitsOptions, | ||
EE extends string = string, | ||
P = ComponentPropsOptions, | ||
D = ExtractPropTypes<P>, | ||
Defaults = ExtractDefaultPropTypes<P> | ||
>( | ||
options: HookComponentOptions<R, E, EE, P, D> | ||
): HookComponent<R, E, P, D, Defaults> { | ||
return ( | ||
props: MaybeFunction<Partial<Defaults> & Omit<D, keyof Defaults>>, | ||
context: SetupContext<E> | ||
) => { | ||
const p = withDefaults<P, D, Defaults>( | ||
isFunction(props) ? reactiveComputed(() => props()) : props, | ||
options.props! | ||
); | ||
const rs = options.setup(p, context); | ||
return { ...rs, $props: p } as R & { $props: D }; | ||
}; | ||
} | ||
|
||
function isFunction<F extends Function>(value: unknown): value is F { | ||
return typeof value === "function"; | ||
} | ||
|
||
function withDefaults< | ||
P = ComponentPropsOptions, | ||
D = ExtractPropTypes<P>, | ||
Defaults = ExtractDefaultPropTypes<P> | ||
>(props: Partial<Defaults> & Omit<D, keyof Defaults>, propsOptions: P): D { | ||
if (Array.isArray(propsOptions)) { | ||
return props as D; | ||
} | ||
const rs = {} as D; | ||
const options = propsOptions as ComponentObjectPropsOptions<D>; | ||
for (const key in options) { | ||
const k = key as keyof D; | ||
const opt = options[k]; | ||
if (opt === null) { | ||
continue; | ||
} | ||
if (typeof opt === "function") { | ||
if (isExtends(opt, Boolean)) { | ||
rs[key] = false as D[typeof key]; | ||
} | ||
continue; | ||
} | ||
if (Array.isArray(opt)) { | ||
if (opt.some((e) => isExtends(e, Boolean))) { | ||
rs[key] = false as D[typeof key]; | ||
} | ||
continue; | ||
} | ||
if (isFunction(opt.default)) { | ||
rs[key] = opt.default(props) as D[typeof key]; | ||
} else if (opt.default !== undefined) { | ||
rs[key] = opt.default as D[typeof key]; | ||
} | ||
} | ||
return { ...rs, ...props } as D; | ||
} | ||
|
||
export function isExtends(types: PropType<any>, value: PropType<any>): boolean { | ||
if (Array.isArray(types)) { | ||
return types.some((e) => isExtends(e, value)); | ||
} | ||
return value === types; | ||
} | ||
|
||
export function normalizeClass(value: ClassType): string { | ||
if (Array.isArray(value)) { | ||
return value.join(" "); | ||
} | ||
if (typeof value === "string") { | ||
return value; | ||
} | ||
return Object.keys(value) | ||
.filter((e) => !!value[e]) | ||
.join(" "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
export type ClassType = string | string[]; | ||
export type ClassType = string | string[] | Record<string, any>; | ||
|
||
export type ActivateEvent = "click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu" | "touchstart" | "touchend"; | ||
export type ActivateEvent = | ||
| "click" | ||
| "mouseenter" | ||
| "mousedown" | ||
| "mouseup" | ||
| "dblclick" | ||
| "contextmenu" | ||
| "touchstart" | ||
| "touchend"; | ||
|
||
export type ElementLike = JSX.Element | string | ElementLike[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.