Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add return types to forwardRef for IntelliJ completion #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ComponentType, type ForwardedRef, forwardRef, type ReactElement } from 'react';
import { type ComponentType, type ForwardedRef, forwardRef, type ReactElement, type RefAttributes } from 'react';
import {
ContextMenu as _ContextMenu,
type ContextMenuRendererContext,
Expand Down Expand Up @@ -30,6 +30,8 @@ function ContextMenu(props: ContextMenuProps, ref: ForwardedRef<ContextMenuEleme
);
}

const ForwardedContextMenu = forwardRef(ContextMenu);
const ForwardedContextMenu = forwardRef(ContextMenu) as (
props: ContextMenuProps & RefAttributes<ContextMenuElement>,
) => ReactElement | null;

export { ForwardedContextMenu as ContextMenu };
14 changes: 10 additions & 4 deletions src/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { forwardRef } from 'react';
import { forwardRef, type ReactElement, type RefAttributes } from 'react';
import {
DateTimePicker as _DateTimePicker,
type DateTimePickerElement,
type DateTimePickerProps,
type DateTimePickerProps as _DateTimePickerProps,
} from './generated/DateTimePicker.js';
import createComponentWithOrderedProps from './utils/createComponentWithOrderedProps.js';

export * from './generated/DateTimePicker.js';

export const DateTimePicker = forwardRef(
type KeysStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? Set : never;

type DateTimePickerProps = Omit<_DateTimePickerProps, KeysStartingWith<keyof _DateTimePickerProps, '_'>>;

const ForwardedDateTimePicker = forwardRef(
createComponentWithOrderedProps<DateTimePickerProps, DateTimePickerElement>(_DateTimePicker, 'value'),
);
) as (props: DateTimePickerProps & RefAttributes<DateTimePickerElement>) => ReactElement | null;

export { ForwardedDateTimePicker as DateTimePicker };
13 changes: 11 additions & 2 deletions src/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { type ComponentType, type ForwardedRef, forwardRef, type ReactElement, type ReactNode } from 'react';
import {
type ComponentType,
type ForwardedRef,
forwardRef,
type ReactElement,
type ReactNode,
type RefAttributes,
} from 'react';
import { Dialog as _Dialog, type DialogElement, type DialogProps as _DialogProps } from './generated/Dialog.js';
import { useSimpleOrChildrenRenderer } from './renderers/useSimpleOrChildrenRenderer.js';
import type { ReactSimpleRendererProps } from './renderers/useSimpleRenderer.js';
Expand Down Expand Up @@ -34,6 +41,8 @@ function Dialog(
);
}

const ForwardedDialog = forwardRef(Dialog);
const ForwardedDialog = forwardRef(Dialog) as (
props: DialogProps & RefAttributes<DialogElement>,
) => ReactElement | null;

export { ForwardedDialog as Dialog };
11 changes: 9 additions & 2 deletions src/Select.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I still see a very limited list of options for these components on IntelliJ:

Screenshot 2023-12-08 at 16 14 32

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you test it? I was copying the .d.ts file and for me it worked:

Screenshot 2023-12-08 at 16 43 27

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used npm link @hilla/react-components on docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using npm link for me makes code completion popup not show at all (IntelliJ 2022.2.5). Will try latest IDEA.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
forwardRef,
type ReactElement,
type ReactNode,
type RefAttributes,
useEffect,
useRef,
} from 'react';
Expand All @@ -16,7 +17,11 @@ export * from './generated/Select.js';

export type SelectReactRendererProps = ReactSimpleRendererProps<SelectElement>;

export type SelectProps = Partial<Omit<_SelectProps, 'children' | 'renderer'>> &
type KeysStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? Set : never;

export type SelectProps = Partial<
Omit<_SelectProps, 'children' | 'renderer' | KeysStartingWith<keyof _SelectProps, '_'>>
> &
Readonly<{
children?: ReactNode | ComponentType<SelectReactRendererProps>;
renderer?: ComponentType<SelectReactRendererProps> | null;
Expand All @@ -40,6 +45,8 @@ function Select(props: SelectProps, ref: ForwardedRef<SelectElement>): ReactElem
);
}

const ForwardedSelect = forwardRef(Select);
const ForwardedSelect = forwardRef(Select) as (
props: SelectProps & RefAttributes<SelectElement>,
) => ReactElement | null;

export { ForwardedSelect as Select };
18 changes: 14 additions & 4 deletions src/TimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { forwardRef } from 'react';
import { TimePicker as _TimePicker, type TimePickerElement, type TimePickerProps } from './generated/TimePicker.js';
import { forwardRef, type ReactElement, type RefAttributes } from 'react';
import {
TimePicker as _TimePicker,
type TimePickerElement,
type TimePickerProps as _TimePickerProps,
} from './generated/TimePicker.js';
import createComponentWithOrderedProps from './utils/createComponentWithOrderedProps.js';

export * from './generated/TimePicker.js';

export const TimePicker = forwardRef(
type KeysStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? Set : never;

type TimePickerProps = Omit<_TimePickerProps, KeysStartingWith<keyof _TimePickerProps, '_'>>;

const ForwardedTimePicker = forwardRef(
createComponentWithOrderedProps<TimePickerProps, TimePickerElement>(_TimePicker, 'value'),
);
) as (props: TimePickerProps & RefAttributes<TimePickerElement>) => ReactElement | null;

export { ForwardedTimePicker as TimePicker };