Skip to content

Commit 6dc2806

Browse files
author
Lionel Bertrand
committed
fix typescript error hoc
1 parent 13ee864 commit 6dc2806

27 files changed

+205
-198
lines changed

Diff for: packages/core/src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export type State<P> = {
3131
export type StyledComponent<P extends Record<string, any>> =
3232
ForwardRefExoticComponent<P & RefAttributes<any>>;
3333

34+
export type InferRef<T> =
35+
T extends ForwardRefExoticComponent<infer Ref>
36+
? Ref extends RefAttributes<infer RefElement>
37+
? RefElement
38+
: never
39+
: never;
40+
3441
export type GetProps<A extends StylableComponent<any>> =
3542
A extends StyledComponent<infer P>
3643
? P

Diff for: packages/core/src/withDefaultProps.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77

88
import { forwardRef, type ComponentType } from 'react';
99
import { withStaticProperties } from './withStaticProperties';
10+
import type { InferRef } from './types';
1011

11-
export function withDefaultProps<P extends Record<string, any>>(
12+
export function withDefaultProps<P>(
1213
Comp: ComponentType<P>,
1314
defaultProps: Partial<P>
1415
) {
15-
const { id, styleSheet, displayName } = Comp as any;
16+
const { id, displayName } = Comp as any;
1617
return withStaticProperties(
17-
forwardRef(function WithDefaultPropsRender(
18-
props: Omit<P, keyof typeof defaultProps> &
19-
Partial<Pick<P, keyof typeof defaultProps>>,
20-
ref: any
21-
) {
18+
forwardRef<
19+
InferRef<typeof Comp>,
20+
Omit<P, keyof typeof defaultProps> &
21+
Partial<Pick<P, keyof typeof defaultProps>>
22+
>(function WithDefaultPropsRender(props, ref) {
2223
return <Comp {...defaultProps} {...(props as any)} ref={ref} />;
2324
}),
24-
{ id, styleSheet, displayName } as {
25+
{ id, displayName } as {
2526
id?: string;
26-
styleSheet: () => unknown;
2727
displayName?: string;
2828
}
2929
);

Diff for: packages/primitive/src/Badge/BadgeText.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8+
import type { InferRef } from '@crossed/core';
89
import { ComponentType, forwardRef } from 'react';
910

1011
export const createBadgeText = <T,>(StyledText: ComponentType<T>) =>
11-
forwardRef<any, T>((props, ref) => {
12-
return <StyledText {...props} ref={ref} />;
12+
forwardRef<InferRef<typeof StyledText>, T>((props, ref) => {
13+
return <StyledText {...(props as any)} ref={ref} />;
1314
});

Diff for: packages/primitive/src/Button/ButtonElement.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { ComponentType, forwardRef } from 'react';
8+
import type { InferRef } from '@crossed/core';
9+
import { type ComponentType, forwardRef } from 'react';
910

1011
export const createButtonElement = <T,>(StyledElement: ComponentType<T>) =>
11-
forwardRef<any, T>((props, ref) => {
12-
return <StyledElement {...props} ref={ref} />;
12+
forwardRef<InferRef<typeof StyledElement>, T>((props, ref) => {
13+
return <StyledElement {...(props as any)} ref={ref} />;
1314
});

Diff for: packages/primitive/src/Button/ButtonGroup.tsx

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { ComponentType, forwardRef } from 'react';
8+
import { type ComponentType, forwardRef } from 'react';
99
import { ProviderGroup } from './contextGroup';
1010
import { Orientation, RovingFocusGroup } from '../utils/RovingFocus';
1111
import { ButtonGroupCollection } from './contextCollection';
12+
import type { InferRef } from '@crossed/core';
1213

1314
export const createButtonGroup = <T,>(StyledGroup: ComponentType<T>) =>
14-
forwardRef<any, T & { orientation?: Orientation }>((props, ref) => {
15-
return (
16-
<ProviderGroup grouped orientation={props.orientation ?? 'horizontal'}>
17-
<RovingFocusGroup orientation={props.orientation ?? 'horizontal'}>
18-
<ButtonGroupCollection.Provider>
19-
<ButtonGroupCollection.Slot>
20-
<StyledGroup {...props} ref={ref} />
21-
</ButtonGroupCollection.Slot>
22-
</ButtonGroupCollection.Provider>
23-
</RovingFocusGroup>
24-
</ProviderGroup>
25-
);
26-
});
15+
forwardRef<InferRef<typeof StyledGroup>, T & { orientation?: Orientation }>(
16+
(props, ref) => {
17+
return (
18+
<ProviderGroup grouped orientation={props.orientation ?? 'horizontal'}>
19+
<RovingFocusGroup orientation={props.orientation ?? 'horizontal'}>
20+
<ButtonGroupCollection.Provider>
21+
<ButtonGroupCollection.Slot>
22+
<StyledGroup {...(props as any)} ref={ref} />
23+
</ButtonGroupCollection.Slot>
24+
</ButtonGroupCollection.Provider>
25+
</RovingFocusGroup>
26+
</ProviderGroup>
27+
);
28+
}
29+
);

Diff for: packages/primitive/src/Dropdown/Dropdown.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8+
import type { InferRef } from '@crossed/core';
89
import { forwardRef, type ComponentType } from 'react';
910

1011
export const createDropdownMain = <P,>(StyledRoot: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot {...props} ref={ref} />;
12+
forwardRef<InferRef<typeof StyledRoot>, P>((props, ref) => {
13+
return <StyledRoot {...(props as any)} ref={ref} />;
1314
});

Diff for: packages/primitive/src/Dropdown/DropdownContent.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import { forwardRef, type ComponentType } from 'react';
99
import { useContext } from './context';
1010
import { RovingFocus } from '../utils/RovingFocus';
1111
import { VisibilityHidden } from '../utils/VisibilityHidden';
12+
import type { InferRef } from '@crossed/core';
1213

1314
export const createDropdownContent = <P,>(StyledRoot: ComponentType<P>) =>
14-
forwardRef<any, P>((props, ref) => {
15+
forwardRef<InferRef<typeof StyledRoot>, P>((props, ref) => {
1516
const { id, open } = useContext();
1617
return (
1718
<RovingFocus>
1819
<VisibilityHidden hidden={!open}>
1920
<StyledRoot
2021
role="menu"
21-
{...props}
22+
{...(props as any)}
2223
ref={ref}
2324
id={id}
2425
aria-labelledby={`label-${id}`}

Diff for: packages/primitive/src/Dropdown/DropdownDivider.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { forwardRef, type ComponentType } from 'react';
99

1010
export const createDropdownDivider = <P,>(StyledRoot: ComponentType<P>) =>
1111
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot role="separator" {...props} ref={ref} />;
12+
return <StyledRoot role="separator" {...(props as any)} ref={ref} />;
1313
});

Diff for: packages/primitive/src/Dropdown/DropdownItem.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { ComponentType, forwardRef } from 'react';
99
import { useContext } from './context';
10-
import { composeEventHandlers } from '@crossed/core';
10+
import { composeEventHandlers, InferRef } from '@crossed/core';
1111
import { RovingFocus } from '../utils/RovingFocus';
1212
import type { RequiredAccessibilityProps } from '../types';
1313

@@ -17,18 +17,16 @@ export type DropdownItemProps = {
1717
export const createDropdownItem = <P extends Record<string, any>>(
1818
Styled: ComponentType<P>
1919
) =>
20-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
21-
//@ts-ignore
2220
forwardRef<
23-
any,
21+
InferRef<typeof Styled>,
2422
DropdownItemProps & RequiredAccessibilityProps<P, 'aria-label'>
2523
>((props, ref) => {
2624
const { setOpen } = useContext();
2725
return (
28-
<RovingFocus.Item ref={ref} focusable={!props.disabled}>
26+
<RovingFocus.Item ref={ref} focusable={!(props as any).disabled}>
2927
<Styled
30-
tabIndex={props.disabled ? -1 : 0}
31-
aria-disabled={(props.disabled || false).toString()}
28+
tabIndex={(props as any).disabled ? -1 : 0}
29+
aria-disabled={((props as any).disabled || false).toString()}
3230
role="menuitem"
3331
{...(props as any)}
3432
onPointerUp={composeEventHandlers((props as any).onPress, () => {

Diff for: packages/primitive/src/Dropdown/DropdownLabel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { forwardRef, type ComponentType } from 'react';
99

1010
export const createDropdownLabel = <P,>(StyledRoot: ComponentType<P>) =>
1111
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot role="separator" {...props} ref={ref} />;
12+
return <StyledRoot role="separator" {...(props as any)} ref={ref} />;
1313
});

Diff for: packages/primitive/src/Label/Label.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
import { useId, type ComponentType, useRef, forwardRef } from 'react';
99
import { Provider } from './context';
10+
import type { InferRef } from '@crossed/core';
1011

1112
export const createLabelMain = <P,>(StyledRoot: ComponentType<P>) =>
12-
forwardRef<any, P>((props, ref) => {
13+
forwardRef<InferRef<typeof StyledRoot>, P>((props, ref) => {
1314
const id = useId();
1415
const inputRef = useRef();
1516
return (
1617
<Provider id={id} inputRef={inputRef}>
17-
<StyledRoot {...props} ref={ref} />
18+
<StyledRoot {...(props as any)} ref={ref} />
1819
</Provider>
1920
);
2021
});

Diff for: packages/primitive/src/List/List.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { ComponentType, forwardRef } from 'react';
8+
import { withDefaultProps } from '@crossed/core';
9+
import type { ComponentType } from 'react';
910

10-
export const createListMain = <P,>(StyledRoot: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => (
12-
<StyledRoot role="list" {...props} ref={ref} />
13-
));
11+
export const createListMain = <P extends Record<string, any>>(
12+
StyledRoot: ComponentType<P>
13+
) => withDefaultProps(StyledRoot, { role: 'list' } as any);

Diff for: packages/primitive/src/List/ListDivider.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { forwardRef, type ComponentType } from 'react';
8+
import type { ComponentType } from 'react';
99

10-
export const createListDivider = <P,>(Styled: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => <Styled {...props} ref={ref} />);
10+
export const createListDivider = <P,>(Styled: ComponentType<P>) => Styled;

Diff for: packages/primitive/src/List/ListItem.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8+
import type { InferRef } from '@crossed/core';
89
import { ComponentType, forwardRef } from 'react';
910

1011
export const createListItem = <P,>(StyledItem: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => (
12-
<StyledItem role="listitem" {...props} ref={ref} />
12+
forwardRef<InferRef<typeof StyledItem>, P>((props, ref) => (
13+
<StyledItem role="listitem" {...(props as any)} ref={ref} />
1314
));

Diff for: packages/primitive/src/List/ListLabel.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8+
import type { InferRef } from '@crossed/core';
89
import { forwardRef, type ComponentType } from 'react';
910

1011
export const createListLabel = <P,>(Styled: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => <Styled {...props} ref={ref} />);
12+
forwardRef<InferRef<typeof Styled>, P>((props, ref) => (
13+
<Styled {...(props as any)} ref={ref} />
14+
));

Diff for: packages/primitive/src/List/ListSubTitle.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
import { forwardRef, type ComponentType } from 'react';
99

1010
export const createListSubTitle = <P,>(StyledSubTitle: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => <StyledSubTitle {...props} ref={ref} />);
11+
forwardRef<any, P>((props, ref) => (
12+
<StyledSubTitle {...(props as any)} ref={ref} />
13+
));

Diff for: packages/primitive/src/List/ListTitle.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { ComponentType, forwardRef } from 'react';
8+
import { type ComponentType, forwardRef } from 'react';
99

1010
export const createListTitle = <P,>(StyledTitle: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => <StyledTitle {...props} ref={ref} />);
11+
forwardRef<any, P>((props, ref) => (
12+
<StyledTitle {...(props as any)} ref={ref} />
13+
));

Diff for: packages/primitive/src/Select/Select.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { forwardRef, type ComponentType } from 'react';
8+
import type { ComponentType } from 'react';
99

1010
export const createSelectMain = <P,>(StyledRoot: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot {...props} ref={ref} />;
13-
});
11+
StyledRoot;

Diff for: packages/primitive/src/Select/SelectContent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const createSelectContent = <P,>(StyledRoot: ComponentType<P>) =>
1818
<VisibilityHidden hidden={!open}>
1919
<StyledRoot
2020
role="menu"
21-
{...props}
21+
{...(props as any)}
2222
ref={ref}
2323
id={id}
2424
aria-labelledby={`label-${id}`}

Diff for: packages/primitive/src/Select/SelectDivider.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8+
import type { InferRef } from '@crossed/core';
89
import { forwardRef, type ComponentType } from 'react';
910

1011
export const createSelectDivider = <P,>(StyledRoot: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot role="separator" {...props} ref={ref} />;
12+
forwardRef<InferRef<typeof StyledRoot>, P>((props, ref) => {
13+
return <StyledRoot role="separator" {...(props as any)} ref={ref} />;
1314
});

Diff for: packages/primitive/src/Select/SelectItem.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { ComponentType, forwardRef } from 'react';
99
import { useContext } from './context';
10-
import { composeEventHandlers } from '@crossed/core';
10+
import { composeEventHandlers, InferRef } from '@crossed/core';
1111
import { RovingFocus } from '../utils/RovingFocus';
1212
import type { RequiredAccessibilityProps } from '../types';
1313

@@ -21,14 +21,15 @@ export const createSelectItem = <P extends Record<string, any>>(
2121
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2222
//@ts-ignore
2323
forwardRef<
24-
any,
24+
InferRef<typeof Styled>,
2525
SelectItemProps & RequiredAccessibilityProps<P, 'aria-label'>
26-
>(({ value, ...props }, ref) => {
26+
>((originalProps, ref) => {
27+
const { value, ...props } = originalProps as any;
2728
const { setOpen, setValue } = useContext();
2829
return (
29-
<RovingFocus.Item ref={ref} focusable={!props.disabled}>
30+
<RovingFocus.Item ref={ref} focusable={!(props as any).disabled}>
3031
<Styled
31-
tabIndex={props.disabled ? -1 : 0}
32+
tabIndex={(props as any).disabled ? -1 : 0}
3233
aria-disabled={((props as any).disabled || false).toString()}
3334
role="menuitem"
3435
{...(props as any)}

Diff for: packages/primitive/src/Select/SelectLabel.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import { forwardRef, type ComponentType } from 'react';
8+
import { withDefaultProps } from '@crossed/core';
9+
import type { ComponentType } from 'react';
910

1011
export const createSelectLabel = <P,>(StyledRoot: ComponentType<P>) =>
11-
forwardRef<any, P>((props, ref) => {
12-
return <StyledRoot role="separator" {...props} ref={ref} />;
13-
});
12+
withDefaultProps(StyledRoot, { role: 'separator' } as any);

Diff for: packages/primitive/src/Sheet/SheetOverlay.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
import { ComponentType, forwardRef } from 'react';
99
import { useContext } from './context';
10-
import { composeEventHandlers } from '@crossed/core';
10+
import { composeEventHandlers, InferRef } from '@crossed/core';
1111

1212
export const createSheetOverlay = <P,>(Styled: ComponentType<P>) =>
13-
forwardRef<any, P>((props, ref) => {
13+
forwardRef<InferRef<typeof Styled>, P>((props, ref) => {
1414
const { setOpen } = useContext();
1515
return (
1616
<Styled
17-
{...props}
17+
{...(props as any)}
1818
onPress={composeEventHandlers((props as any).onPress, () => {
1919
setOpen(false);
2020
})}

Diff for: packages/styled/src/withReactive/withReactive.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* LICENSE file in the root of this projects source tree.
66
*/
77

8-
import React, { ComponentType, forwardRef } from 'react';
8+
import React, { forwardRef, ForwardRefRenderFunction } from 'react';
99
import { useTheme } from '../useTheme';
1010

11-
export const withReactive = <P extends Record<string, any>>(
12-
Comp: ComponentType<P>
11+
export const withReactive = <Ref, P = {}>(
12+
Comp: ForwardRefRenderFunction<Ref, P>
1313
) =>
14-
forwardRef<P['ref'], P>((props, ref) => {
14+
forwardRef<Ref, P>((props, ref) => {
1515
useTheme();
16-
return <Comp {...props} ref={ref} />;
16+
return <Comp {...(props as any)} ref={ref} />;
1717
});

0 commit comments

Comments
 (0)