Skip to content

Commit

Permalink
fix(Calendart): improve type declaration using generic types with def…
Browse files Browse the repository at this point in the history
…ault params to dynamically manage the `selectionMode` (fixes primefaces#7555) (primefaces#7556)
  • Loading branch information
iamkyrylo authored Jan 3, 2025
1 parent 7ed3085 commit 4fad1ca
Showing 1 changed file with 15 additions and 61 deletions.
76 changes: 15 additions & 61 deletions components/lib/calendar/calendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,77 +965,31 @@ interface CalendarBaseProps {
*/
unstyled?: boolean;
}
/**
* 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;
}

export type CalendarSelectionMode = 'single' | 'range' | 'multiple';

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

/**
* Defines valid properties in Calendar component.
* @group Properties
*/
export type CalendarProps = CalendarPropsRange | CalendarPropsMultiple | CalendarPropsSingle;

/**
* **PrimeReact - Calendar**
*
Expand All @@ -1047,7 +1001,7 @@ export type CalendarProps = CalendarPropsRange | CalendarPropsMultiple | Calenda
*
* @group Component
*/
export declare class Calendar extends React.Component<CalendarProps, any> {
export declare class Calendar<TMode extends CalendarSelectionMode = 'single', TValue = TMode extends 'multiple' ? Date[] : TMode extends 'range' ? (Date | null)[] : Date> extends React.Component<CalendarProps<TMode>, any> {
/**
* Used to show the overlay.
*/
Expand All @@ -1062,14 +1016,14 @@ export declare class Calendar extends React.Component<CalendarProps, any> {
public focus(): void;
/**
* Used to get the current date.
* @return {Date | Date[]} Current Date
* @return {TValue} Current Date
*/
public getCurrentDateTime(): Date | Date[];
public getCurrentDateTime(): TValue;
/**
* Used to get the view date.
* @return {Date | Date[]} View Date
* @return {TValue} View Date
*/
public getViewDate(): Date | Date[];
public getViewDate(): TValue;
/**
* Used to get container element.
* @return {HTMLSpanElement} Container element
Expand All @@ -1088,7 +1042,7 @@ export declare class Calendar extends React.Component<CalendarProps, any> {
/**
* Used to update the current view date.
* @param {React.SyntheticEvent | null} event - Browser event.
* @param {Date | Date[] | null} value - New date.
* @param {TValue | null} value - New date.
*/
public updateViewDate(event: React.SyntheticEvent | null, value: Nullable<Date | Date[]>): void;
public updateViewDate(event: React.SyntheticEvent | null, value: Nullable<TValue>): void;
}

0 comments on commit 4fad1ca

Please sign in to comment.