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

Calendar: Improve type declaration using generic types with default parameters #7555

Closed
iamkyrylo opened this issue Jan 3, 2025 · 1 comment · Fixed by #7556
Closed

Calendar: Improve type declaration using generic types with default parameters #7555

iamkyrylo opened this issue Jan 3, 2025 · 1 comment · Fixed by #7556
Assignees
Labels
Typescript Issue or pull request is *only* related to TypeScript definition
Milestone

Comments

@iamkyrylo
Copy link
Contributor

Describe the bug

I've encountered a problem while working with the Calendar component and creating an uncontrolled wrapper around it. The issue lies in the current type overloading approach used in the type definition:

export type CalendarProps = CalendarPropsRange | CalendarPropsMultiple | CalendarPropsSingle;

This implementation makes it difficult to dynamically manage the selectionMode without introducing additional boilerplate.

Reproducer

https://stackblitz.com/edit/vitejs-vite-7rwuaddq?file=src%2FApp.tsx

System Information

"primereact": "^10.9.1"
"react": "^18.3.1"

Steps to reproduce the behavior

  1. Check typescript errors in the provided codesandbox

Expected behavior

I propose replacing the type overloading approach with generic types that include default parameters. This change will enhance the flexibility and usability of the CalendarProps type.

Example:

export interface CalendarProps<
    TMode extends CalendarSelectionMode = 'single',
    TValue = TMode extends 'multiple' ? Date[] : TMode extends 'range' ? (Date | null)[] : Date
> extends CalendarBaseProps {
    selectionMode?: TMode;
    value?: Nullable<TValue>;
    onChange?(event: FormEvent<TValue>): void;
}
@iamkyrylo iamkyrylo added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 3, 2025
iamkyrylo added a commit to iamkyrylo/primereact that referenced this issue Jan 3, 2025
…ault params to dynamically manage the `selectionMode` (fixes primefaces#7555)
@iamkyrylo
Copy link
Contributor Author

iamkyrylo commented Jan 3, 2025

I have prepared a PR to demonstrate how this approach could be implemented effectively.

iamkyrylo added a commit to iamkyrylo/primereact that referenced this issue Jan 3, 2025
…ault params to dynamically manage the `selectionMode` (fixes primefaces#7555)
@melloware melloware added Typescript Issue or pull request is *only* related to TypeScript definition and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Jan 3, 2025
@melloware melloware added this to the 10.9.2 milestone Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typescript Issue or pull request is *only* related to TypeScript definition
Projects
None yet
2 participants