-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split range modes into a separate useTempocalRange hook
- Loading branch information
Showing
20 changed files
with
840 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Temporal } from "@js-temporal/polyfill"; | ||
|
||
export function getNow(calendar: Temporal.CalendarLike = "iso8601") { | ||
return Temporal.Now.plainDateTime(calendar); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Temporal } from "@js-temporal/polyfill"; | ||
import { expect, test } from "vitest"; | ||
import { getNow } from "../src/getNow"; | ||
|
||
test("getNow (PlainDate, unbounded)", () => { | ||
const result = getNow(); | ||
|
||
expect(result instanceof Temporal.PlainDate).toBe(true); | ||
|
||
expect( | ||
result.equals( | ||
Temporal.PlainDate.from({ | ||
year: new Date().getFullYear(), | ||
month: new Date().getMonth() + 1, | ||
day: new Date().getDate(), | ||
}) | ||
) | ||
).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ClampMode = "always" | "value-change" | "never"; | ||
|
||
export type Locale = Intl.LocalesArgument; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Temporal } from "@js-temporal/polyfill"; | ||
import { clamp, getNow } from "@tempocal/core"; | ||
import { useCallback } from "react"; | ||
|
||
export function useCalendarValue({ | ||
clampCalendarValue, | ||
maxValue, | ||
minValue, | ||
setCalendarValue, | ||
calendarValue, | ||
}: { | ||
clampCalendarValue: boolean; | ||
maxValue: Temporal.PlainDate | Temporal.PlainDateTime | undefined; | ||
minValue: Temporal.PlainDate | Temporal.PlainDateTime | undefined; | ||
setCalendarValue: (value: Temporal.PlainDate) => void; | ||
calendarValue: Temporal.PlainDate; | ||
}) { | ||
const updateCalendarValue = useCallback( | ||
(nextCalendarValue: Temporal.PlainDate) => { | ||
if (!clampCalendarValue) { | ||
setCalendarValue(nextCalendarValue); | ||
|
||
return nextCalendarValue; | ||
} | ||
|
||
const clampedValue = clamp( | ||
nextCalendarValue, | ||
minValue instanceof Temporal.PlainDate | ||
? minValue | ||
: minValue?.toPlainDate(), | ||
maxValue instanceof Temporal.PlainDate | ||
? maxValue | ||
: maxValue?.toPlainDate() | ||
); | ||
|
||
setCalendarValue(clampedValue); | ||
|
||
return clampedValue; | ||
}, | ||
[clampCalendarValue, maxValue, minValue, setCalendarValue] | ||
); | ||
|
||
const onChangeCalendarValue = useCallback( | ||
(params?: Temporal.PlainDate | Temporal.PlainDateLike) => { | ||
if (!params) { | ||
return updateCalendarValue(getNow().toPlainDate()); | ||
} | ||
|
||
if (params instanceof Temporal.PlainDate) { | ||
return updateCalendarValue(params); | ||
} | ||
|
||
return updateCalendarValue(calendarValue.with(params)); | ||
}, | ||
[calendarValue, updateCalendarValue] | ||
); | ||
|
||
return { | ||
onChangeCalendarValue, | ||
}; | ||
} |
Oops, something went wrong.