Skip to content

Commit 0141018

Browse files
Cleanup hooks
1 parent 3de2e9b commit 0141018

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

public-types/reflect.d.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ type UseUnitConfig = Parameters<typeof useUnit>[1];
88
type UnbindableProps = 'key' | 'ref';
99

1010
type Hooks<Props> = {
11-
mounted?: EventCallable<Props> | EventCallable<void> | ((props: Props) => unknown);
12-
unmounted?: EventCallable<void> | (() => unknown);
11+
mounted?:
12+
| EventCallable<Props extends infer HookArg ? HookArg : never>
13+
| EventCallable<void>
14+
| ((props: Props) => unknown);
15+
unmounted?:
16+
| EventCallable<Props extends infer HookArg ? HookArg : never>
17+
| EventCallable<void>
18+
| ((props: Props) => unknown);
1319
};
1420

1521
/**

src/core/reflect.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ export function reflectFactory(context: Context) {
3434
const eventsProps = context.useUnit(events as any, config.useUnitConfig);
3535
const functionProps = useBoundFunctions(functions);
3636

37+
const finalProps: any = {};
38+
39+
if (ref) {
40+
finalProps.ref = ref;
41+
}
42+
3743
const elementProps: Props = Object.assign(
38-
{ ref },
44+
finalProps,
3945
storeProps,
4046
eventsProps,
4147
data,
@@ -50,12 +56,12 @@ export function reflectFactory(context: Context) {
5056
const hooks: Hooks<Props> = Object.assign({}, functionsHooks, eventsHooks);
5157

5258
if (hooks.mounted) {
53-
hooks.mounted(props);
59+
hooks.mounted(elementProps);
5460
}
5561

5662
return () => {
5763
if (hooks.unmounted) {
58-
hooks.unmounted();
64+
hooks.unmounted(elementProps);
5965
}
6066
};
6167
}, [eventsHooks, functionsHooks]);

src/core/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type Hook<Props> =
2727

2828
export type Hooks<Props> = {
2929
mounted?: Hook<Props>;
30-
unmounted?: Hook<void>;
30+
unmounted?: Hook<Props>;
3131
};
3232

3333
export type UseUnitConifg = Parameters<typeof useUnit>[1];

src/no-ssr/reflect.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ describe('hooks', () => {
401401
);
402402

403403
expect($lastProps.getState()).toBeNull();
404-
expect(scope.getState($lastProps)).toStrictEqual({
404+
expect(scope.getState($lastProps)).toEqual({
405405
value: 'test',
406406
'data-testid': 'name',
407407
});
@@ -475,7 +475,7 @@ describe('hooks', () => {
475475
);
476476

477477
expect($lastProps.getState()).toBeNull();
478-
expect(scope.getState($lastProps)).toStrictEqual({
478+
expect(scope.getState($lastProps)).toEqual({
479479
value: 'test',
480480
'data-testid': 'name',
481481
});

type-tests/types-reflect.tsx

+40-3
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ function localize(value: string): unknown {
380380
type Props = { loading: boolean };
381381

382382
const mounted = createEvent();
383+
const unmounted = createEvent();
383384

384385
const Foo: FC<Props> = (props) => <></>;
385386

@@ -390,7 +391,7 @@ function localize(value: string): unknown {
390391
bind: {
391392
loading: $loading,
392393
},
393-
hooks: { mounted },
394+
hooks: { mounted, unmounted },
394395
});
395396
}
396397

@@ -399,6 +400,7 @@ function localize(value: string): unknown {
399400
type Props = { loading: boolean };
400401

401402
const mounted = createEvent<Props>();
403+
const unmounted = createEvent<Props>();
402404

403405
const Foo: FC<Props> = (props) => <></>;
404406

@@ -409,19 +411,54 @@ function localize(value: string): unknown {
409411
bind: {
410412
loading: $loading,
411413
},
412-
hooks: { mounted },
414+
hooks: { mounted, unmounted },
413415
});
414416
}
415417

416418
// should error if mounted event doesn't satisfy component props
417419
{
418420
const mounted = createEvent<{ foo: string }>();
421+
const unmounted = createEvent<{ foo: string }>();
419422

420423
const Foo: FC<{ bar: number }> = () => null;
421424

422425
const Bar = reflect({
423426
view: Foo,
424427
// @ts-expect-error
425-
hooks: { mounted },
428+
hooks: { mounted, unmounted },
429+
});
430+
}
431+
432+
// reflect supports partial match of mounted event and component props
433+
{
434+
const mounted = createEvent<{ foo: string }>();
435+
const unmounted = createEvent<{ foo: string }>();
436+
437+
const Foo: FC<{ foo: string; bar: number }> = () => null;
438+
439+
const Bar = reflect({
440+
view: Foo,
441+
bind: {
442+
foo: 'foo',
443+
bar: 42,
444+
},
445+
hooks: { mounted, unmounted },
446+
});
447+
}
448+
449+
// reflect supports partial match of mounted callback and component props
450+
{
451+
const mounted = (args: { foo: string }) => {};
452+
const unmounted = (args: { foo: string }) => {};
453+
454+
const Foo: FC<{ foo: string; bar: number }> = () => null;
455+
456+
const Bar = reflect({
457+
view: Foo,
458+
bind: {
459+
foo: 'foo',
460+
bar: 42,
461+
},
462+
hooks: { mounted, unmounted },
426463
});
427464
}

0 commit comments

Comments
 (0)