Skip to content

Commit

Permalink
fix: props loss reactivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuki committed Feb 16, 2023
1 parent 3105f0f commit 6dac403
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/hoci/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type HookComponent<
> = (
props: MaybeFunction<Partial<Defaults> & Omit<D, keyof Defaults>>,
context: SetupContext<E>
) => R & { $props: D };
) => R;

export function defineHookProps<
P extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
Expand Down Expand Up @@ -65,15 +65,26 @@ export function defineHookComponent<
isFunction(props) ? reactiveComputed(() => props()) : props,
options.props!
);
const rs = options.setup(p, context);
return { ...rs, $props: p } as R & { $props: D };
return options.setup(p, context);
};
}

type Constructor<T> =
| {
new (...args: any[]): T & {}
}
| {
(): T
};

function isFunction<F extends Function>(value: unknown): value is F {
return typeof value === "function";
}

function isConstructor<T>(value: unknown): value is Constructor<T> {
return isFunction(value) && value.prototype !== undefined;
}

function withDefaults<
P = ComponentPropsOptions,
D = ExtractPropTypes<P>,
Expand All @@ -82,33 +93,37 @@ function withDefaults<
if (Array.isArray(propsOptions)) {
return props as D;
}
const rs = {} as D;
const newProps = props as D;
const options = propsOptions as ComponentObjectPropsOptions<D>;
for (const key in options) {
const k = key as keyof D;
const opt = options[k];
if (newProps[k] !== undefined) {
continue;
}
if (opt === null) {
continue;
}
if (typeof opt === "function") {
if (isConstructor(opt)) {
if (isExtends(opt, Boolean)) {
rs[key] = false as D[typeof key];
newProps[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];
newProps[key] = false as D[typeof key];
}
continue;
}
if (isFunction(opt.default)) {
rs[key] = opt.default(props) as D[typeof key];
newProps[key] = opt.default(props) as D[typeof key];
} else if (opt.default !== undefined) {
rs[key] = opt.default as D[typeof key];
newProps[key] = opt.default as D[typeof key];
}
}
return { ...rs, ...props } as D;

return newProps as D;
}

export function isExtends(types: PropType<any>, value: PropType<any>): boolean {
Expand Down

0 comments on commit 6dac403

Please sign in to comment.