Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
175 lines
4.8 KiB
TypeScript
175 lines
4.8 KiB
TypeScript
"use client"
|
|
|
|
import { da, de, fi, nb, sv } from "date-fns/locale"
|
|
import { useCallback, useEffect, useRef, useState } from "react"
|
|
import { useFormContext, useWatch } from "react-hook-form"
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
import { dt } from "@/lib/dt"
|
|
|
|
import Body from "@/components/TempDesignSystem/Text/Body"
|
|
import useLang from "@/hooks/useLang"
|
|
|
|
import DatePickerDesktop from "./Screen/Desktop"
|
|
import DatePickerMobile from "./Screen/Mobile"
|
|
|
|
import styles from "./date-picker.module.css"
|
|
|
|
import type { DatePickerFormProps } from "@/types/components/datepicker"
|
|
|
|
const locales = {
|
|
[Lang.da]: da,
|
|
[Lang.de]: de,
|
|
[Lang.fi]: fi,
|
|
[Lang.no]: nb,
|
|
[Lang.sv]: sv,
|
|
}
|
|
|
|
export default function DatePickerForm({ name = "date" }: DatePickerFormProps) {
|
|
const lang = useLang()
|
|
const intl = useIntl()
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const selectedDate = useWatch({ name })
|
|
const { register, setValue } = useFormContext()
|
|
const ref = useRef<HTMLDivElement | null>(null)
|
|
|
|
const [isSelectingFrom, setIsSelectingFrom] = useState(true)
|
|
|
|
const close = useCallback(() => {
|
|
if (!selectedDate.toDate) {
|
|
setValue(name, {
|
|
fromDate: selectedDate.fromDate,
|
|
toDate: dt(selectedDate.fromDate).add(1, "day").format("YYYY-MM-DD"),
|
|
})
|
|
|
|
setIsSelectingFrom(true)
|
|
}
|
|
|
|
setIsOpen(false)
|
|
}, [name, setValue, selectedDate])
|
|
|
|
function showOnFocus() {
|
|
setIsOpen(true)
|
|
}
|
|
|
|
function handleSelectDate(selected: Date) {
|
|
/* check if selected date is not before todays date,
|
|
which happens when "Enter" key is pressed in any other input field of the form */
|
|
if (!dt(selected).isBefore(dt(), "day")) {
|
|
if (isSelectingFrom) {
|
|
setValue(name, {
|
|
fromDate: dt(selected).format("YYYY-MM-DD"),
|
|
toDate: undefined,
|
|
})
|
|
setIsSelectingFrom(false)
|
|
} else if (!dt(selectedDate.fromDate).isSame(dt(selected))) {
|
|
const fromDate = dt(selectedDate.fromDate)
|
|
const toDate = dt(selected)
|
|
if (toDate.isAfter(fromDate)) {
|
|
setValue(name, {
|
|
fromDate: selectedDate.fromDate,
|
|
toDate: toDate.format("YYYY-MM-DD"),
|
|
})
|
|
} else {
|
|
setValue(name, {
|
|
fromDate: toDate.format("YYYY-MM-DD"),
|
|
toDate: selectedDate.fromDate,
|
|
})
|
|
}
|
|
setIsSelectingFrom(true)
|
|
}
|
|
}
|
|
}
|
|
const closeIfOutside = useCallback(
|
|
(target: HTMLElement) => {
|
|
if (ref.current && target && !ref.current.contains(target)) {
|
|
close()
|
|
}
|
|
},
|
|
[close, ref]
|
|
)
|
|
|
|
function closeOnBlur(evt: FocusEvent) {
|
|
if (isOpen) {
|
|
const target = evt.relatedTarget as HTMLElement
|
|
closeIfOutside(target)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
function handleClickOutside(evt: Event) {
|
|
if (isOpen) {
|
|
const target = evt.target as HTMLElement
|
|
closeIfOutside(target)
|
|
}
|
|
}
|
|
document.body.addEventListener("click", handleClickOutside)
|
|
return () => {
|
|
document.body.removeEventListener("click", handleClickOutside)
|
|
}
|
|
}, [closeIfOutside, isOpen])
|
|
|
|
const selectedFromDate = dt(selectedDate.fromDate)
|
|
.locale(lang)
|
|
.format("ddd D MMM")
|
|
const selectedToDate = !!selectedDate.toDate
|
|
? dt(selectedDate.toDate).locale(lang).format("ddd D MMM")
|
|
: ""
|
|
|
|
return (
|
|
<div
|
|
className={styles.container}
|
|
onBlur={(e) => {
|
|
closeOnBlur(e.nativeEvent)
|
|
}}
|
|
data-isopen={isOpen}
|
|
ref={ref}
|
|
>
|
|
<button
|
|
className={styles.btn}
|
|
onFocus={showOnFocus}
|
|
onClick={() => setIsOpen(true)}
|
|
type="button"
|
|
>
|
|
<Body className={styles.body} asChild color="uiTextHighContrast">
|
|
<span>
|
|
{intl.formatMessage(
|
|
{ id: "{selectedFromDate} - {selectedToDate}" },
|
|
{
|
|
selectedFromDate,
|
|
selectedToDate,
|
|
}
|
|
)}
|
|
</span>
|
|
</Body>
|
|
</button>
|
|
<input {...register("date.fromDate")} type="hidden" />
|
|
<input {...register("date.toDate")} type="hidden" />
|
|
<div aria-modal className={styles.hideWrapper} role="dialog">
|
|
<DatePickerDesktop
|
|
close={close}
|
|
handleOnSelect={handleSelectDate}
|
|
locales={locales}
|
|
// DayPicker lib needs Daterange in form as below to show appropriate UI
|
|
selectedDate={{
|
|
from: selectedDate.fromDate,
|
|
to: selectedDate.toDate,
|
|
}}
|
|
/>
|
|
<DatePickerMobile
|
|
close={close}
|
|
handleOnSelect={handleSelectDate}
|
|
locales={locales}
|
|
// DayPicker lib needs Daterange in form as below to show appropriate UI
|
|
selectedDate={{
|
|
from: selectedDate.fromDate,
|
|
to: selectedDate.toDate,
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|