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: Prop typings are not working correctly #6583

Open
JosuerBague opened this issue May 7, 2024 · 5 comments
Open

Calendar: Prop typings are not working correctly #6583

JosuerBague opened this issue May 7, 2024 · 5 comments
Labels
Typescript Issue or pull request is *only* related to TypeScript definition

Comments

@JosuerBague
Copy link

JosuerBague commented May 7, 2024

Describe the bug

The selectionMode prop shows a TS error regardless of value passed: 'single' | 'multiple' | 'range'.

Reproducer

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

PrimeReact version

10.6.5

React version

18.x

Language

TypeScript

Build / Runtime

Vite

Browser(s)

No response

Steps to reproduce the behavior

I'm creating a custom wrapper around the Calendar component, like so:

import { Calendar as PRCalendar, CalendarProps } from 'primereact/calendar';

type Omissions = 'disabled' | 'invalid' | 'required' | 'visible';

type Properties = {
  isDisabled?: boolean;
  isInvalid?: boolean;
  isRequired?: boolean;
  isVisible?:boolean;
} & Omit<CalendarProps, Omissions>

const Calendar: React.FC<Properties> = ({
  isDisabled,
  isInvalid,
  isRequired,
  isVisible,
 ...props,
}) = (
   <PRCalendar
      disabled={isDisabled}
      invalid={isInvalid}
      required={isRequired}
      visible={isVisible}
      {...props}
   />
)

I get the error:
image

Even when I just use the Component without creating a wrapper, passing in selectionMode value shows an error:

const Calendar: React.FC<CalendarProps> = ({ ...props }) => (
    <PRCalendar
        selectionMode="multiple"
        {...props}
    />
);
```

### Expected behavior

Calendar should accept values for selectionMode prop.
@JosuerBague JosuerBague added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label May 7, 2024
@Rekl0w
Copy link
Contributor

Rekl0w commented May 7, 2024

Can you provide a stackblitz link please ?

@JosuerBague
Copy link
Author

Can you provide a stackblitz link please ?

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

@melloware
Copy link
Member

OK so it works for me in TypeScript here: https://stackblitz.com/edit/kd3h15?file=src%2FApp.tsx

i think the issue is the Calendar detects the value and sets the SelectionMode based on whether you pass it a single Date, a Date[] array, etc.

See here:

/**
* Defines valid properties in single Calendar component.
* @group Properties
*/
interface CalendarPropsSingle extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode?: 'single' | undefined;
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<Date>;
/**
* Callback to invoke when value changes.
* @param { FormEvent<Date>} event - Custom change event
*/
onChange?(event: FormEvent<Date>): void;
}
/**
* Defines valid properties in range Calendar component.
* @group Properties
*/
interface CalendarPropsRange extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode: 'range';
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<(Date | null)[]>;
/**
* Callback to invoke when value changes.
* @param { FormEvent<(Date | null)[]>} event - Custom change event
*/
onChange?(event: FormEvent<(Date | null)[]>): void;
}
/**
* Defines valid properties in multiple Calendar component.
* @group Properties
*/
interface CalendarPropsMultiple extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode: 'multiple';
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<Date[]>;
/**
* Callback to invoke when value changes.
* @param {FormEvent<Date[]>} event - Custom change event
*/
onChange?(event: FormEvent<Date[]>): void;
}
/**
* Defines valid properties in Calendar component.
* @group Properties
*/
export type CalendarProps = CalendarPropsRange | CalendarPropsMultiple | CalendarPropsSingle;

@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 May 7, 2024
@JosuerBague
Copy link
Author

OK so it works for me in TypeScript here: https://stackblitz.com/edit/kd3h15?file=src%2FApp.tsx

i think the issue is the Calendar detects the value and sets the SelectionMode based on whether you pass it a single Date, a Date[] array, etc.

See here:

/**
* Defines valid properties in single Calendar component.
* @group Properties
*/
interface CalendarPropsSingle extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode?: 'single' | undefined;
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<Date>;
/**
* Callback to invoke when value changes.
* @param { FormEvent<Date>} event - Custom change event
*/
onChange?(event: FormEvent<Date>): void;
}
/**
* Defines valid properties in range Calendar component.
* @group Properties
*/
interface CalendarPropsRange extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode: 'range';
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<(Date | null)[]>;
/**
* Callback to invoke when value changes.
* @param { FormEvent<(Date | null)[]>} event - Custom change event
*/
onChange?(event: FormEvent<(Date | null)[]>): void;
}
/**
* Defines valid properties in multiple Calendar component.
* @group Properties
*/
interface CalendarPropsMultiple extends CalendarBaseProps {
/**
* Specifies the selection mode either "single", "range", or "multiple";
* @defaultValue single
*/
selectionMode: 'multiple';
/**
* Value of the component.
* @defaultValue null
*/
value?: Nullable<Date[]>;
/**
* Callback to invoke when value changes.
* @param {FormEvent<Date[]>} event - Custom change event
*/
onChange?(event: FormEvent<Date[]>): void;
}
/**
* Defines valid properties in Calendar component.
* @group Properties
*/
export type CalendarProps = CalendarPropsRange | CalendarPropsMultiple | CalendarPropsSingle;

The issue starts when I try to wrap the Calendar with a wrapping component.
https://stackblitz.com/edit/kd3h15-yfszte?file=src%2FApp.tsx

I tried, passing it a value and an onChange to see if it works, but the typing error still persists. Strange behavour, in theory it should work the same whether the component gets wrapper or not, right?

@melloware
Copy link
Member

It should it might be some TypeScript weirdness or magic that I don't know.

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
Development

No branches or pull requests

3 participants