Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfocused calendar inputs show date description #776

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const TravelDateCalendar: React.FC<TravelDateCalendarProps> = ({
</Heading>
)}
<TravelDateSingleTextInputField
{...inputProps}
value={value}
onValueChange={onValueChangeByInputs}
localeCode={localeCode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const TravelDateRangeCalendar: React.FC<
</Heading>
)}
<TravelDateTextInputFields
{...inputProps}
value={value}
onValueChange={onValueChangeByInputs}
localeCode={localeCode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const TravelDateInput: React.FC<TravelDateInputProps> = ({
zIndex={calendarInDom ? zIndex : zIndexWhenClosed}
>
<TravelDateSingleTextInputField
{...inputProps}
value={value}
onValueChange={onValueChangeByInputs}
localeCode={localeCode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const TravelDateRangeInput: React.FC<TravelDateRangeInputProps> = ({
zIndex={calendarInDom ? zIndex : zIndexWhenClosed}
>
<TravelDateTextInputFields
{...inputProps}
value={value}
onValueChange={onValueChangeByInputs}
localeCode={localeCode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface TravelDateSingleTextInputFieldProps {
onFocus?: () => void;
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurred: string | undefined;
valueWhenBlurred: string | undefined;
}

export const TravelDateSingleTextInputField: React.FC<
Expand All @@ -27,6 +28,7 @@ export const TravelDateSingleTextInputField: React.FC<
onFocus,
calendarSize,
placeholderWhenBlurred,
valueWhenBlurred,
}) => {
const { mask, placeholder } = useMemo(() => {
const dateFormatForLocaleCode = getDateFormatForLocaleCode(localeCode);
Expand All @@ -53,6 +55,7 @@ export const TravelDateSingleTextInputField: React.FC<
placeholder={placeholder}
calendarSize={calendarSize}
placeholderWhenBlurred={placeholderWhenBlurred}
valueWhenBlurred={valueWhenBlurred}
/>
</Row>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from "react";
import { FocusEventHandler, useCallback, useRef, useState } from "react";
import {
LabelledTextInput,
LabelledTextInputProps,
} from "@stenajs-webui/forms";
import { FocusEventHandler, useCallback, useRef, useState } from "react";
import {
InputMask,
InputMaskPipe,
Expand All @@ -22,6 +22,7 @@ export interface TravelDateTextInputProps extends LabelledTextInputProps {
showMask?: boolean;
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurred: string | undefined;
valueWhenBlurred: string | undefined;
}

export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
Expand All @@ -39,6 +40,7 @@ export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
onBlur,
placeholderWhenBlurred,
placeholder,
valueWhenBlurred,
...inputProps
}) => {
const inputRef = useRef(null);
Expand All @@ -54,7 +56,8 @@ export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
guide,
keepCharPositions,
placeholderChar,
showMask
showMask,
isFocused
);

const onFocusHandler = useCallback<FocusEventHandler<HTMLInputElement>>(
Expand All @@ -80,6 +83,8 @@ export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
return (
<LabelledTextInput
{...inputProps}
aria-live={"polite"}
value={(!isFocused ? valueWhenBlurred : value) ?? ""}
ref={inputRef}
placeholder={activePlaceholder}
onFocus={onFocusHandler}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface TravelDateTextInputFieldsProps {
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurredStartDate: string | undefined;
placeholderWhenBlurredEndDate: string | undefined;
valueWhenBlurredStartDate: string | undefined;
valueWhenBlurredEndDate: string | undefined;
}

export const TravelDateTextInputFields: React.FC<
Expand All @@ -34,6 +36,8 @@ export const TravelDateTextInputFields: React.FC<
calendarSize,
placeholderWhenBlurredStartDate,
placeholderWhenBlurredEndDate,
valueWhenBlurredStartDate,
valueWhenBlurredEndDate,
}) => {
const { mask, placeholder } = useMemo(() => {
const dateFormatForLocaleCode = getDateFormatForLocaleCode(localeCode);
Expand Down Expand Up @@ -65,6 +69,7 @@ export const TravelDateTextInputFields: React.FC<
borderRadiusVariant={"onlyLeft"}
placeholder={placeholder}
placeholderWhenBlurred={placeholderWhenBlurredStartDate}
valueWhenBlurred={valueWhenBlurredStartDate}
calendarSize={calendarSize}
/>
<TravelDateTextInput
Expand All @@ -85,6 +90,7 @@ export const TravelDateTextInputFields: React.FC<
borderRadiusVariant={"onlyRight"}
placeholder={placeholder}
placeholderWhenBlurred={placeholderWhenBlurredEndDate}
valueWhenBlurred={valueWhenBlurredEndDate}
calendarSize={calendarSize}
/>
</Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getMonthInYear } from "../../../util/calendar/CalendarDataFactory";
import { startCase } from "lodash-es";
import { formatLocalizedDate } from "../../localize-date-format/LocalizedDateFormatter";
import { VisiblePanel } from "../types";
import { formatDateDescription } from "../util/DateDescriptionFormatter";

export const useTravelDateInput = (
value: string | undefined,
Expand Down Expand Up @@ -39,6 +40,14 @@ export const useTravelDateInput = (
[dateFormat.length, localeCode, value]
);

const valueWhenBlurred = useMemo(
() =>
selectedDate != null
? formatDateDescription(selectedDate, today, locale)
: undefined,
[locale, selectedDate, today]
);

const [visibleMonth, setVisibleMonth] = useState<Date>(
initialMonthInFocus ?? selectedDate ?? new Date()
);
Expand Down Expand Up @@ -124,5 +133,6 @@ export const useTravelDateInput = (
selectedDate,
today,
visibleMonth,
valueWhenBlurred,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getMonthInYear } from "../../../util/calendar/CalendarDataFactory";
import { startCase } from "lodash-es";
import { formatLocalizedDate } from "../../localize-date-format/LocalizedDateFormatter";
import { TravelDateRangeInputValue, VisiblePanel } from "../types";
import { formatDateDescription } from "../util/DateDescriptionFormatter";

export const useTravelDateRangeInput = (
value: TravelDateRangeInputValue | undefined,
Expand Down Expand Up @@ -47,6 +48,22 @@ export const useTravelDateRangeInput = (
[dateFormat.length, localeCode, value?.endDate]
);

const valueWhenBlurredStartDate = useMemo(
() =>
selectedStartDate != null
? formatDateDescription(selectedStartDate, today, locale)
: undefined,
[locale, selectedStartDate, today]
);

const valueWhenBlurredEndDate = useMemo(
() =>
selectedEndDate != null
? formatDateDescription(selectedEndDate, today, locale)
: undefined,
[locale, selectedEndDate, today]
);

const [visibleMonth, setVisibleMonth] = useState<Date>(
initialMonthInFocus ?? selectedStartDate ?? new Date()
);
Expand Down Expand Up @@ -185,5 +202,7 @@ export const useTravelDateRangeInput = (
selectedEndDate,
today,
visibleMonth,
valueWhenBlurredStartDate,
valueWhenBlurredEndDate,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { format, isSameYear, Locale } from "date-fns";

export const formatDateDescription = (
date: Date,
today: Date,
locale: Locale
) => {
const year = isSameYear(date, today) ? "" : " y";
if (locale.code.startsWith("en")) {
return format(date, "eee MMM d" + year, { locale }).replace(".", "");
} else {
return format(date, "eee d MMM" + year, { locale }).replace(".", "");
}
};

export const formatDateDescriptionLong = (
date: Date,
today: Date,
locale: Locale
) => {
const year = isSameYear(date, today) ? "" : " y";
if (locale.code.startsWith("en")) {
return format(date, "eeee MMMM d" + year, { locale });
} else {
return format(date, "eeee d MMMM" + year, { locale });
}
};
10 changes: 7 additions & 3 deletions packages/input-mask/src/hooks/UseInputMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const useMaskedInput = (
guide: boolean = false,
keepCharPositions: boolean = false,
placeholderChar: string = "\u2000",
showMask: boolean = true
showMask: boolean = true,
enabled: boolean = true
) => {
const textMask = useRef(null);

Expand All @@ -33,8 +34,10 @@ export const useMaskedInput = (
showMask,
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(textMask.current as any).update(initialValue);
if (enabled) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(textMask.current as any).update(initialValue);
}
}, [
inputRef,
guide,
Expand All @@ -44,6 +47,7 @@ export const useMaskedInput = (
placeholderChar,
showMask,
initialValue,
enabled,
]);

return {
Expand Down
Loading