mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-19 19:21:15 -05:00
add autofocus to date question
This commit is contained in:
@@ -8,7 +8,7 @@ import { getMonthName, getOrdinalDate } from "@/lib/date-time";
|
||||
import { getLocalizedValue } from "@/lib/i18n";
|
||||
import { getUpdatedTtc, useTtc } from "@/lib/ttc";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useEffect, useMemo, useState } from "preact/hooks";
|
||||
import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
|
||||
import DatePicker, { DatePickerProps } from "react-date-picker";
|
||||
import { type TResponseData, type TResponseTtc } from "@formbricks/types/responses";
|
||||
import type { TSurveyDateQuestion, TSurveyQuestionId } from "@formbricks/types/surveys/types";
|
||||
@@ -27,6 +27,7 @@ interface DateQuestionProps {
|
||||
setTtc: (ttc: TResponseTtc) => void;
|
||||
currentQuestionId: TSurveyQuestionId;
|
||||
isBackButtonHidden: boolean;
|
||||
autoFocusEnabled?: boolean;
|
||||
}
|
||||
|
||||
function CalendarIcon() {
|
||||
@@ -91,6 +92,7 @@ export function DateQuestion({
|
||||
ttc,
|
||||
currentQuestionId,
|
||||
isBackButtonHidden,
|
||||
autoFocusEnabled,
|
||||
}: Readonly<DateQuestionProps>) {
|
||||
const [startTime, setStartTime] = useState(performance.now());
|
||||
const [errorMessage, setErrorMessage] = useState("");
|
||||
@@ -101,6 +103,15 @@ export function DateQuestion({
|
||||
const [selectedDate, setSelectedDate] = useState<Date | undefined>(value ? new Date(value) : undefined);
|
||||
const [hideInvalid, setHideInvalid] = useState(!selectedDate);
|
||||
|
||||
const datePickerRef = useCallback(
|
||||
(currentElement: HTMLButtonElement | null) => {
|
||||
if (currentElement && autoFocusEnabled && isCurrent) {
|
||||
currentElement.focus();
|
||||
}
|
||||
},
|
||||
[autoFocusEnabled, isCurrent]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (datePickerOpen) {
|
||||
if (!selectedDate) setSelectedDate(new Date());
|
||||
@@ -167,6 +178,7 @@ export function DateQuestion({
|
||||
<div className="fb-relative">
|
||||
{!datePickerOpen && (
|
||||
<button
|
||||
ref={datePickerRef}
|
||||
onClick={() => {
|
||||
setDatePickerOpen(true);
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user