Skip to content

Commit

Permalink
- In travel calendar, add support for custom placeholder when text in…
Browse files Browse the repository at this point in the history
…put is not in focus.
  • Loading branch information
mattias800 committed Sep 2, 2024
1 parent cee24f7 commit 677afe8
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ export const ParseDate = () => {
</div>
);
};

export const WithBlurPlaceholders = () => {
const [value, setValue] = useState<string>("");

return (
<div style={{ display: "inline-block" }}>
<TravelDateCalendar
value={value}
onValueChange={setValue}
localeCode={"sv"}
placeholderWhenBlurred={"Gimme date"}
/>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface TravelDateCalendarProps
dateTestId?: (date: Date) => string | undefined;
previousMonthButtonTestId?: string;
nextMonthButtonTestId?: string;
placeholderWhenBlurred?: string;
}

export const TravelDateCalendar: React.FC<TravelDateCalendarProps> = ({
Expand All @@ -35,6 +36,7 @@ export const TravelDateCalendar: React.FC<TravelDateCalendarProps> = ({
initialMonthInFocus,
previousMonthButtonAriaLabel = "Previous month",
nextMonthButtonAriaLabel = "Next month",
placeholderWhenBlurred,
heading,
headingLevel,
numMonthsInMonthPicker = 12,
Expand Down Expand Up @@ -74,6 +76,7 @@ export const TravelDateCalendar: React.FC<TravelDateCalendarProps> = ({
localeCode={localeCode}
label={label}
calendarSize={size}
placeholderWhenBlurred={placeholderWhenBlurred}
/>

<MonthHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,21 @@ export const ParseDate = () => {
</div>
);
};

export const WithBlurPlaceholders = () => {
const [value, setValue] = useState<TravelDateRangeInputValue | undefined>(
undefined
);

return (
<div style={{ display: "inline-block" }}>
<TravelDateRangeCalendar
value={value}
onValueChange={setValue}
localeCode={"sv"}
placeholderWhenBlurredStartDate={"Gimme start"}
placeholderWhenBlurredEndDate={"Gimme end"}
/>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface TravelDateRangeCalendarProps
dateTestId?: (date: Date) => string | undefined;
previousMonthButtonTestId?: string;
nextMonthButtonTestId?: string;
placeholderWhenBlurredStartDate?: string;
placeholderWhenBlurredEndDate?: string;
}

export const TravelDateRangeCalendar: React.FC<
Expand All @@ -48,6 +50,8 @@ export const TravelDateRangeCalendar: React.FC<
dateTestId,
previousMonthButtonTestId,
nextMonthButtonTestId,
placeholderWhenBlurredStartDate,
placeholderWhenBlurredEndDate,
}) => {
const inputProps = useTravelDateRangeInput(
value,
Expand Down Expand Up @@ -79,6 +83,8 @@ export const TravelDateRangeCalendar: React.FC<
startDateLabel={startDateLabel}
endDateLabel={endDateLabel}
calendarSize={size}
placeholderWhenBlurredStartDate={placeholderWhenBlurredStartDate}
placeholderWhenBlurredEndDate={placeholderWhenBlurredEndDate}
/>

<MonthHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,18 @@ export const WithPresets = () => {
</div>
);
};

export const WithBlurPlaceholders = () => {
const [value, setValue] = useState<string>("");

return (
<div style={{ display: "inline-block", padding: "150px 80px" }}>
<TravelDateInput
value={value}
onValueChange={setValue}
localeCode={"sv"}
placeholderWhenBlurred={"Gimme date"}
/>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface TravelDateInputProps
dateTestId?: (date: Date) => string | undefined;
previousMonthButtonTestId?: string;
nextMonthButtonTestId?: string;
placeholderWhenBlurred?: string;
}

export const TravelDateInput: React.FC<TravelDateInputProps> = ({
Expand All @@ -59,6 +60,7 @@ export const TravelDateInput: React.FC<TravelDateInputProps> = ({
initialMonthInFocus,
previousMonthButtonAriaLabel = "Previous month",
nextMonthButtonAriaLabel = "Next month",
placeholderWhenBlurred,
heading,
headingLevel,
numMonthsInMonthPicker = 12,
Expand Down Expand Up @@ -170,6 +172,7 @@ export const TravelDateInput: React.FC<TravelDateInputProps> = ({
label={label}
onFocus={showCalendar}
calendarSize={size}
placeholderWhenBlurred={placeholderWhenBlurred}
/>
</Box>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,21 @@ export const WithPresets = () => {
</div>
);
};

export const WithBlurPlaceholders = () => {
const [value, setValue] = useState<TravelDateRangeInputValue | undefined>(
undefined
);

return (
<div style={{ display: "inline-block", padding: "150px 80px" }}>
<TravelDateRangeInput
value={value}
onValueChange={setValue}
localeCode={"sv"}
placeholderWhenBlurredStartDate={"Gimme start"}
placeholderWhenBlurredEndDate={"Gimme end"}
/>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface TravelDateRangeInputProps
dateTestId?: (date: Date) => string | undefined;
previousMonthButtonTestId?: string;
nextMonthButtonTestId?: string;
placeholderWhenBlurredStartDate?: string;
placeholderWhenBlurredEndDate?: string;
}

export const TravelDateRangeInput: React.FC<TravelDateRangeInputProps> = ({
Expand All @@ -62,6 +64,8 @@ export const TravelDateRangeInput: React.FC<TravelDateRangeInputProps> = ({
initialMonthInFocus,
previousMonthButtonAriaLabel = "Previous month",
nextMonthButtonAriaLabel = "Next month",
placeholderWhenBlurredStartDate,
placeholderWhenBlurredEndDate,
heading,
headingLevel,
numMonthsInMonthPicker = 12,
Expand Down Expand Up @@ -173,6 +177,8 @@ export const TravelDateRangeInput: React.FC<TravelDateRangeInputProps> = ({
endDateLabel={endDateLabel}
onFocus={showCalendar}
calendarSize={size}
placeholderWhenBlurredStartDate={placeholderWhenBlurredStartDate}
placeholderWhenBlurredEndDate={placeholderWhenBlurredEndDate}
/>
</Box>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface TravelDateSingleTextInputFieldProps {
label?: string;
onFocus?: () => void;
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurred: string | undefined;
}

export const TravelDateSingleTextInputField: React.FC<
Expand All @@ -25,6 +26,7 @@ export const TravelDateSingleTextInputField: React.FC<
localeCode,
onFocus,
calendarSize,
placeholderWhenBlurred,
}) => {
const { mask, placeholder } = useMemo(() => {
const dateFormatForLocaleCode = getDateFormatForLocaleCode(localeCode);
Expand All @@ -50,6 +52,7 @@ export const TravelDateSingleTextInputField: React.FC<
label={label}
placeholder={placeholder}
calendarSize={calendarSize}
placeholderWhenBlurred={placeholderWhenBlurred}
/>
</Row>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
LabelledTextInput,
LabelledTextInputProps,
} from "@stenajs-webui/forms";
import { useRef } from "react";
import { FocusEventHandler, useCallback, useRef, useState } from "react";
import {
InputMask,
InputMaskPipe,
Expand All @@ -21,6 +21,7 @@ export interface TravelDateTextInputProps extends LabelledTextInputProps {
placeholderChar?: string;
showMask?: boolean;
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurred: string | undefined;
}

export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
Expand All @@ -34,9 +35,15 @@ export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
placeholderChar,
showMask,
calendarSize,
onFocus,
onBlur,
placeholderWhenBlurred,
placeholder,
...inputProps
}) => {
const inputRef = useRef(null);
const [isFocused, setIsFocused] = useState(false);

const { onChange: maskedOnChange } = useMaskedInput(
inputRef,
onChange,
Expand All @@ -50,10 +57,33 @@ export const TravelDateTextInput: React.FC<TravelDateTextInputProps> = ({
showMask
);

const onFocusHandler = useCallback<FocusEventHandler<HTMLInputElement>>(
(ev) => {
onFocus?.(ev);
setIsFocused(true);
},
[onFocus]
);

const onBlurHandler = useCallback<FocusEventHandler<HTMLInputElement>>(
(ev) => {
onBlur?.(ev);
setIsFocused(false);
},
[onBlur]
);

const activePlaceholder = isFocused
? placeholder
: placeholderWhenBlurred ?? placeholder;

return (
<LabelledTextInput
{...inputProps}
ref={inputRef}
placeholder={activePlaceholder}
onFocus={onFocusHandler}
onBlur={onBlurHandler}
onChange={maskedOnChange}
width={getWidth(calendarSize)}
size={calendarSize === "large" ? "large" : "medium"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface TravelDateTextInputFieldsProps {
endDateLabel?: string;
onFocus?: () => void;
calendarSize: TravelCalendarSizeVariant;
placeholderWhenBlurredStartDate: string | undefined;
placeholderWhenBlurredEndDate: string | undefined;
}

export const TravelDateTextInputFields: React.FC<
Expand All @@ -30,6 +32,8 @@ export const TravelDateTextInputFields: React.FC<
endDateLabel = "To",
onFocus,
calendarSize,
placeholderWhenBlurredStartDate,
placeholderWhenBlurredEndDate,
}) => {
const { mask, placeholder } = useMemo(() => {
const dateFormatForLocaleCode = getDateFormatForLocaleCode(localeCode);
Expand Down Expand Up @@ -60,6 +64,7 @@ export const TravelDateTextInputFields: React.FC<
label={startDateLabel}
borderRadiusVariant={"onlyLeft"}
placeholder={placeholder}
placeholderWhenBlurred={placeholderWhenBlurredStartDate}
calendarSize={calendarSize}
/>
<TravelDateTextInput
Expand All @@ -79,6 +84,7 @@ export const TravelDateTextInputFields: React.FC<
label={endDateLabel}
borderRadiusVariant={"onlyRight"}
placeholder={placeholder}
placeholderWhenBlurred={placeholderWhenBlurredEndDate}
calendarSize={calendarSize}
/>
</Row>
Expand Down

0 comments on commit 677afe8

Please sign in to comment.