-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
204 additions
and
16 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
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,74 @@ | ||
import type { | ||
DateFieldProps as AriaDateFieldProps, | ||
DateValue, | ||
} from "react-aria-components"; | ||
import { | ||
DateField as AriaDateField, | ||
DateInput, | ||
DateSegment, | ||
Label, | ||
Text, | ||
} from "react-aria-components"; | ||
import { Control, FieldValues, Path, useController } from "react-hook-form"; | ||
import { tv } from "tailwind-variants"; | ||
|
||
import { currentTimezone } from "~/utils"; | ||
|
||
const datefield = tv({ | ||
slots: { | ||
dateInput: | ||
"flex w-full items-center space-x-1 rounded-lg border p-2 md:w-fit", | ||
dateSegment: "font-medium text-gray-800", | ||
error: "text-xs text-red-500", | ||
desc: "text-xs", | ||
}, | ||
}); | ||
|
||
const { dateInput, dateSegment, error, desc } = datefield(); | ||
|
||
interface DateFieldProps<T extends DateValue, U extends FieldValues> | ||
extends AriaDateFieldProps<T> { | ||
name: Path<U>; | ||
control: Control<U>; | ||
label?: string; | ||
description?: string; | ||
errorMessage?: string; | ||
} | ||
|
||
export function DateField<T extends DateValue, U extends FieldValues>({ | ||
name, | ||
control, | ||
label, | ||
description, | ||
errorMessage, | ||
...props | ||
}: DateFieldProps<T, U>) { | ||
const { field } = useController({ name, control }); | ||
return ( | ||
<AriaDateField | ||
{...props} | ||
onChange={(newDate) => { | ||
field.onChange(newDate.toDate(currentTimezone).toISOString()); | ||
}} | ||
onBlur={field.onBlur} | ||
ref={field.ref} | ||
> | ||
<Label>{label}</Label> | ||
<DateInput className={dateInput()}> | ||
{(segment) => ( | ||
<DateSegment className={dateSegment()} segment={segment} /> | ||
)} | ||
</DateInput> | ||
{errorMessage && ( | ||
<Text className={error()} slot='errorMessage'> | ||
{errorMessage} | ||
</Text> | ||
)} | ||
{description && !errorMessage && ( | ||
<Text className={desc()} slot='description'> | ||
{description} | ||
</Text> | ||
)} | ||
</AriaDateField> | ||
); | ||
} |
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 @@ | ||
export * from "./DateField"; |
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,79 @@ | ||
import type { | ||
RangeCalendarProps as AriaRangeCalendarProps, | ||
DateValue, | ||
} from "react-aria-components"; | ||
import { | ||
RangeCalendar as AriaRangeCalendar, | ||
Label, | ||
Text, | ||
} from "react-aria-components"; | ||
import { Control, FieldValues, Path, useController } from "react-hook-form"; | ||
import { tv } from "tailwind-variants"; | ||
|
||
import { currentTimezone } from "~/utils"; | ||
import { CalendarHeader } from "../Calendar/CalendarHeader"; | ||
import { CalendarTable } from "../Calendar/CalendarTable"; | ||
|
||
const rangecalendar = tv({ | ||
slots: { | ||
container: "w-full rounded-lg border bg-white p-5 md:w-fit", | ||
error: "text-xs text-red-500", | ||
desc: "text-xs", | ||
}, | ||
}); | ||
|
||
const { container, error, desc } = rangecalendar(); | ||
|
||
interface RangeCalendarProps<T extends DateValue, U extends FieldValues> | ||
extends AriaRangeCalendarProps<T> { | ||
name: Path<U>; | ||
control: Control<U>; | ||
errorMessage?: string; | ||
label?: string; | ||
description?: string; | ||
} | ||
|
||
export function RangeCalendar<T extends DateValue, U extends FieldValues>({ | ||
errorMessage, | ||
description, | ||
label, | ||
name, | ||
control, | ||
...props | ||
}: RangeCalendarProps<T, U>) { | ||
const { field } = useController({ name, control }); | ||
return ( | ||
<div> | ||
{label && <Label id={label}>{label}</Label>} | ||
<AriaRangeCalendar | ||
{...props} | ||
visibleDuration={{ months: 2 }} | ||
aria-labelledby={label} | ||
onChange={(newDate) => { | ||
field.onChange([ | ||
newDate?.start.toDate(currentTimezone).toISOString(), | ||
newDate?.end.toDate(currentTimezone).toISOString(), | ||
]); | ||
}} | ||
onFocusChange={field.onBlur} | ||
className={container()} | ||
> | ||
<CalendarHeader /> | ||
<div className='flex items-center space-x-6'> | ||
<CalendarTable /> | ||
<CalendarTable offset={{ months: 1 }} /> | ||
</div> | ||
</AriaRangeCalendar> | ||
{errorMessage && ( | ||
<Text className={error()} slot='errorMessage'> | ||
{errorMessage} | ||
</Text> | ||
)} | ||
{description && !errorMessage && ( | ||
<Text className={desc()} slot='description'> | ||
{description} | ||
</Text> | ||
)} | ||
</div> | ||
); | ||
} |
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 @@ | ||
export * from "./RangeCalendar"; |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from "./Button"; | ||
export * from "./Calendar"; | ||
export * from "./DateField"; | ||
export * from "./DatePicker"; | ||
export * from "./IconWrapper"; | ||
export * from "./Modal"; | ||
export * from "./RangeCalendar"; |