Skip to content

Commit d06541e

Browse files
committed
feat: enhance TypeScript support in examples
- Updated tsconfig.json for examples to include new compiler options and exclude specific files. - Introduced a new live-provider-globals.d.ts file to declare global variables and types that we configured for react-live. - Refactored examples to use globally declared DateFNS methods. - Add the DATEFNS directly to the react-live scope instead of destructuring it, as it avoid declaring all utilities of date-fns - Fixed the type related issues in the examples.
1 parent bde4eac commit d06541e

36 files changed

+111
-37
lines changed

docs-site/src/components/Example/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default class CodeExampleComponent extends React.Component<
165165
useState,
166166
DatePicker,
167167
CalendarContainer,
168-
...DateFNS,
168+
DateFNS,
169169
range,
170170
fi,
171171
forwardRef,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
import * as DateFNS from "date-fns";
3+
import type { ReactDatePickerCustomHeaderProps as ReactDatePickerCustomHeaderPropsType } from "react-datepicker";
4+
import type { MiddlewareState as MiddlewareStateType } from "@floating-ui/react";
5+
declare global {
6+
const useState: typeof React.useState;
7+
const DatePicker: any;
8+
const CalendarContainer: any;
9+
const range: any;
10+
const fi: any;
11+
const forwardRef: typeof React.forwardRef;
12+
const render: (component: React.ComponentType) => void;
13+
14+
// DateFNS object is available globally
15+
const DateFNS: typeof DateFNS;
16+
17+
// Declare types
18+
type ReactNode = React.ReactNode;
19+
type ReactDatePickerCustomHeaderProps = ReactDatePickerCustomHeaderPropsType;
20+
type MiddlewareState = MiddlewareStateType;
21+
}
22+
23+
export {};

docs-site/src/examples/ts/customDayClassName.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const CustomDayClassName = () => {
66
selected={startDate}
77
onChange={(date: Date | null) => setStartDate(date)}
88
dayClassName={(date: Date) =>
9-
getDate(date) < Math.random() * 31 ? "random" : undefined
9+
DateFNS.getDate(date) < Math.random() * 31 ? "random" : undefined
1010
}
1111
/>
1212
);

docs-site/src/examples/ts/customInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type ExampleCustomInputProps = {
55
};
66

77
const CustomInput = () => {
8-
const [startDate, setStartDate] = useState(new Date());
8+
const [startDate, setStartDate] = useState<Date | null>(new Date());
99

1010
const ExampleCustomInput = forwardRef<
1111
HTMLButtonElement,
@@ -19,7 +19,7 @@ const CustomInput = () => {
1919
return (
2020
<DatePicker
2121
selected={startDate}
22-
onChange={(date) => setStartDate(date)}
22+
onChange={(date: Date | null) => setStartDate(date)}
2323
customInput={<ExampleCustomInput className="example-custom-input" />}
2424
/>
2525
);

docs-site/src/examples/ts/customTimeInput.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interface ExampleCustomTimeInputProps {
22
date?: Date;
3-
value: string;
4-
onChange: (time: string) => void;
3+
value?: string;
4+
onChange?: (time: string) => void;
55
}
66

77
const CustomTimeInput = () => {
@@ -13,8 +13,10 @@ const CustomTimeInput = () => {
1313
}: ExampleCustomTimeInputProps) => (
1414
<input
1515
value={value}
16-
onChange={(e) => onChange(e.target.value)}
17-
onClick={(e) => e.target?.focus()}
16+
onChange={(e) => onChange?.(e.target.value)}
17+
onClick={(e: React.MouseEvent<HTMLInputElement>) =>
18+
(e.target as HTMLInputElement).focus()
19+
}
1820
style={{ border: "solid 1px pink" }}
1921
/>
2022
);

docs-site/src/examples/ts/dateRangeWithShowDisabledNavigation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const DateRangeWithShowDisabledNavigation = () => {
1313
selected={startDate}
1414
onChange={onChange}
1515
minDate={new Date()}
16-
maxDate={addMonths(new Date(), 5)}
16+
maxDate={DateFNS.addMonths(new Date(), 5)}
1717
startDate={startDate}
1818
endDate={endDate}
1919
selectsRange

docs-site/src/examples/ts/default.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ const Default = () => {
22
const [startDate, setStartDate] = useState<Date | null>(new Date());
33

44
return (
5-
<DatePicker selected={startDate} onChange={(date) => setStartDate(date)} />
5+
<DatePicker
6+
selected={startDate}
7+
onChange={(date: Date | null) => setStartDate(date)}
8+
/>
69
);
710
};
811

docs-site/src/examples/ts/excludeDateIntervals.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const ExcludeDateIntervals = () => {
88

99
const excludeDateIntervals: TExcludeDateIntervals = [
1010
{
11-
start: subDays(new Date(), 5),
12-
end: addDays(new Date(), 5),
11+
start: DateFNS.subDays(new Date(), 5),
12+
end: DateFNS.addDays(new Date(), 5),
1313
},
1414
];
1515

docs-site/src/examples/ts/excludeDates.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ type TExcludeDates =
88
const ExcludeDates = () => {
99
const [startDate, setStartDate] = useState<Date | null>(new Date());
1010

11-
const excludeDates: TExcludeDates = [new Date(), subDays(new Date(), 1)];
11+
const excludeDates: TExcludeDates = [
12+
new Date(),
13+
DateFNS.subDays(new Date(), 1),
14+
];
1215

1316
return (
1417
<DatePicker

docs-site/src/examples/ts/excludeDatesWithMessage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ExcludeDatesWithMessage = () => {
1010

1111
const excludeDates: TExcludeDates = [
1212
{ date: new Date(), message: "Today is excluded" },
13-
{ date: subDays(new Date(), 1), message: "This day is excluded" },
13+
{ date: DateFNS.subDays(new Date(), 1), message: "This day is excluded" },
1414
];
1515

1616
return (

0 commit comments

Comments
 (0)