Skip to content

Commit 0a73eb2

Browse files
committed
Allow null values in most places where undefined values are allowed
1 parent b564ba9 commit 0a73eb2

12 files changed

+47
-47
lines changed

src/Calendar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CalendarGrid } from './CalendarGrid'
88
import { CommonProps, DateChangeCallBack } from './types'
99

1010
export interface CalendarProps extends CommonProps {
11-
month?: Date
11+
month?: Date | null
1212
onMonthChange?: DateChangeCallBack<Date>
1313
onDayHover?: DateChangeCallBack
1414
onDayClick?: DateChangeCallBack<Date>
@@ -17,11 +17,11 @@ export interface CalendarProps extends CommonProps {
1717

1818
export function Calendar({
1919
locale,
20-
month: receivedMonth,
20+
month: receivedMonth = null,
2121
modifiers: receivedModifiers,
2222
modifiersClassNames,
23-
minimumDate,
24-
maximumDate,
23+
minimumDate = null,
24+
maximumDate = null,
2525
onMonthChange,
2626
onDayHover,
2727
onDayClick,
@@ -30,7 +30,7 @@ export function Calendar({
3030
}: CalendarProps): React.JSX.Element {
3131
const [month, setMonth] = useControllableState(
3232
receivedMonth,
33-
onMonthChange as (month: Date) => void,
33+
onMonthChange,
3434
startOfMonth(new Date())
3535
)
3636

src/CalendarNavigation.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { addMonths, getYear, startOfMonth, subMonths, format, isSameMonth, Local
55
export interface CalendarNavigationProps {
66
locale: Locale
77
month: Date
8-
minimumDate?: Date
9-
maximumDate?: Date
8+
minimumDate?: Date | null
9+
maximumDate?: Date | null
1010
onMonthChange: (month: Date) => void
1111
}
1212

1313
export function CalendarNavigation({
1414
locale,
1515
month,
16-
minimumDate,
17-
maximumDate,
16+
minimumDate = null,
17+
maximumDate = null,
1818
onMonthChange
1919
}: CalendarNavigationProps): React.JSX.Element {
2020
const handlePrevious = (event: MouseEvent<HTMLAnchorElement> | TouchEvent<HTMLAnchorElement>) => {

src/DatePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface DatePickerChildrenProps {
1313

1414
export interface DatePickerProps extends CommonProps {
1515
children: (props: DatePickerChildrenProps) => React.ReactNode
16-
date?: Date
16+
date?: Date | null
1717
onDateChange?: DateChangeCallBack
1818
format?: string
1919
touchDragEnabled?: boolean
@@ -24,7 +24,7 @@ const defaultOnDateChange = () => {}
2424
export function DatePicker({
2525
children,
2626
locale,
27-
date,
27+
date = null,
2828
onDateChange = defaultOnDateChange,
2929
format,
3030
minimumDate,

src/DatePickerCalendar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import { Calendar } from './Calendar'
66
import { CommonProps, DateChangeCallBack } from './types'
77

88
export interface DatePickerCalendarProps extends CommonProps {
9-
date?: Date
10-
month?: Date
9+
date?: Date | null
10+
month?: Date | null
1111
onDateChange?: DateChangeCallBack<Date>
1212
onMonthChange?: DateChangeCallBack<Date>
1313
touchDragEnabled?: boolean
1414
}
1515

1616
export function DatePickerCalendar({
1717
locale,
18-
date: selectedDate,
19-
month: receivedMonth,
18+
date: selectedDate = null,
19+
month: receivedMonth = null,
2020
onDateChange,
2121
onMonthChange,
2222
minimumDate,

src/DateRangePicker.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import { CommonProps, DateChangeCallBack, DateRangeFocus, InputProps } from './t
1111
export interface DateRangePickerChildrenProps {
1212
startDateInputProps: InputProps
1313
endDateInputProps: InputProps
14-
focus?: DateRangeFocus
14+
focus?: DateRangeFocus | null
1515
}
1616

1717
export interface DateRangePickerProps extends CommonProps {
1818
children: (props: DateRangePickerChildrenProps) => React.ReactNode
19-
startDate?: Date
20-
endDate?: Date
19+
startDate?: Date | null
20+
endDate?: Date | null
2121
minimumLength?: number
2222
maximumLength?: number
2323
onStartDateChange?: DateChangeCallBack
@@ -31,8 +31,8 @@ const defaultListener = () => {}
3131
export function DateRangePicker({
3232
children,
3333
locale,
34-
startDate,
35-
endDate,
34+
startDate = null,
35+
endDate = null,
3636
onStartDateChange = defaultListener,
3737
onEndDateChange = defaultListener,
3838
format,
@@ -45,15 +45,15 @@ export function DateRangePicker({
4545
weekdayFormat,
4646
touchDragEnabled
4747
}: DateRangePickerProps): React.JSX.Element {
48-
const [focus, setFocus] = useState<DateRangeFocus | undefined>()
49-
const [month, setMonth] = useState<Date | undefined>(() => startDate || endDate || new Date())
48+
const [focus, setFocus] = useState<DateRangeFocus | null>(null)
49+
const [month, setMonth] = useState<Date | null>(() => startDate || endDate || new Date())
5050
const isTouch = useDetectTouch()
5151

5252
const [startDateInputRef, endDateInputRef, popoverRef] = useOutsideClickHandler<
5353
HTMLInputElement,
5454
HTMLInputElement,
5555
HTMLDivElement
56-
>(() => setFocus(undefined))
56+
>(() => setFocus(null))
5757

5858
const startDateInputProps = useDateInput({
5959
date: startDate,
@@ -130,7 +130,7 @@ export function DateRangePicker({
130130
onStartDateChange={onStartDateChange}
131131
onEndDateChange={onEndDateChange}
132132
onFocusChange={setFocus}
133-
onMonthChange={setMonth as (date: Date | null) => void}
133+
onMonthChange={setMonth}
134134
minimumDate={minimumDate}
135135
maximumDate={maximumDate}
136136
minimumLength={minimumLength}

src/DateRangePickerCalendar.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Calendar } from './Calendar'
77
import { CommonProps, DateChangeCallBack, DateRangeFocus } from './types'
88

99
export interface DateRangePickerCalendarProps extends CommonProps {
10-
startDate?: Date
11-
endDate?: Date
12-
focus?: DateRangeFocus
13-
month?: Date
10+
startDate?: Date | null
11+
endDate?: Date | null
12+
focus?: DateRangeFocus | null
13+
month?: Date | null
1414
minimumLength?: number
1515
maximumLength?: number
16-
onFocusChange?: (focus: DateRangeFocus | undefined) => void
16+
onFocusChange?: (focus: DateRangeFocus | null) => void
1717
onStartDateChange?: DateChangeCallBack
1818
onEndDateChange?: DateChangeCallBack
1919
onMonthChange?: DateChangeCallBack
@@ -24,10 +24,10 @@ const defaultListener = () => {}
2424

2525
export function DateRangePickerCalendar({
2626
locale,
27-
startDate,
28-
endDate,
29-
focus,
30-
month: receivedMonth,
27+
startDate = null,
28+
endDate = null,
29+
focus = null,
30+
month: receivedMonth = null,
3131
onStartDateChange = defaultListener,
3232
onEndDateChange = defaultListener,
3333
onFocusChange = defaultListener,
@@ -141,7 +141,7 @@ export function DateRangePickerCalendar({
141141
}
142142

143143
onEndDateChange(endDate ? setTime(date, endDate) : date)
144-
onFocusChange(invalidStartDate || !startDate ? START_DATE : undefined)
144+
onFocusChange(invalidStartDate || !startDate ? START_DATE : null)
145145
}
146146
}
147147

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export type ModifiersClassNames = { [key in DefaultModifiers | string]: string }
2222

2323
export interface CommonProps {
2424
locale: Locale
25-
minimumDate?: Date
26-
maximumDate?: Date
25+
minimumDate?: Date | null
26+
maximumDate?: Date | null
2727
modifiers?: Modifiers
2828
modifiersClassNames?: ModifiersClassNames
2929
weekdayFormat?: string

src/useControllableState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function useControllableState<T>(
55
onChange: ((value: T) => void) | null | undefined
66
): [T, (value: T) => void]
77
export function useControllableState<T>(
8-
value: T | undefined,
8+
value: T | null,
99
onChange: ((value: T) => void) | null | undefined,
1010
initialValue: T
1111
): [T, (value: T) => void]

src/useDateInput.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export interface UseDateInputProps {
66
date?: Date | null
77
format?: string
88
locale: Locale
9-
minimumDate?: Date
10-
maximumDate?: Date
9+
minimumDate?: Date | null
10+
maximumDate?: Date | null
1111
onDateChange: (date: Date | null) => void
1212
validate?: (date: Date) => boolean
1313
}
@@ -22,11 +22,11 @@ export interface UseDateInputReturn {
2222
}
2323

2424
export function useDateInput({
25-
date: selectedDate,
25+
date: selectedDate = null,
2626
format: receivedFormatString,
2727
locale,
28-
minimumDate,
29-
maximumDate,
28+
minimumDate = null,
29+
maximumDate = null,
3030
onDateChange,
3131
validate
3232
}: UseDateInputProps): UseDateInputReturn {
@@ -36,7 +36,7 @@ export function useDateInput({
3636
const formatDate = (date: Date) => format(date, formatString, { locale })
3737
const parseDate = (dateString: string) =>
3838
parse(dateString, formatString, selectedDate || new Date())
39-
const isValidAndSelectable = (date: Date | null | undefined): date is Date =>
39+
const isValidAndSelectable = (date: Date | null): date is Date =>
4040
(isValid as (date: unknown) => date is Date)(date) &&
4141
isSelectable(date, { minimumDate, maximumDate }) &&
4242
(!validate || validate(date))

src/useGrid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const createInitialState = (currentMonth: Date, locale: Locale): GridState => {
5252
}
5353
}
5454

55-
export type GridAction =
55+
type GridAction =
5656
| { type: 'setStartDate'; value: Date }
5757
| { type: 'setEndDate'; value: Date }
5858
| { type: 'setRange'; startDate: Date; endDate: Date }

0 commit comments

Comments
 (0)