Skip to content

Commit

Permalink
- When travel calendar input is not in focus, show date description i…
Browse files Browse the repository at this point in the history
…f a valid date has been entered.
  • Loading branch information
mattias800 committed Sep 2, 2024
1 parent 4ff2f31 commit 34cc0d3
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 4 deletions.
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
Expand Up @@ -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 : undefined}
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, locale)
: undefined,
[locale, selectedDate]
);

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, locale)
: undefined,
[locale, selectedStartDate]
);

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

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,7 @@
import { format, Locale } from "date-fns";

export const formatDateDescription = (date: Date, locale: Locale) =>
format(date, "eee MMM do", { locale });

export const formatDateDescriptionLong = (date: Date, locale: Locale) =>
format(date, "eeee MMMM do", { 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

0 comments on commit 34cc0d3

Please sign in to comment.