You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.0 KiB
196 lines
6.0 KiB
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import type { QuestionField } from "@/lib/schema-adapter";
|
|
import { cn } from "@/lib/utils";
|
|
import { useI18n } from "@/translations/provider";
|
|
import { Input } from "@/components/ui/input";
|
|
import { useQuestionAnswers } from "./question-answer-storage";
|
|
import QuestionTitle from "./question-title";
|
|
|
|
type QuestionTextProps = {
|
|
question: QuestionField;
|
|
description?: string;
|
|
disabled?: boolean;
|
|
heightClassName?: string;
|
|
};
|
|
|
|
function toEnglishDigits(str: string): string {
|
|
return str
|
|
.replace(/[٠-٩]/g, (d) => String(d.charCodeAt(0) - 1632))
|
|
.replace(/[۰-۹]/g, (d) => String(d.charCodeAt(0) - 1776));
|
|
}
|
|
|
|
export default function QuestionText({
|
|
question,
|
|
description,
|
|
disabled,
|
|
heightClassName: _heightClassName,
|
|
}: QuestionTextProps) {
|
|
const { dictionary: t, locale } = useI18n();
|
|
const { getAnswerValue, setAnswerValue } = useQuestionAnswers();
|
|
const value = getAnswerValue(question);
|
|
|
|
const isMuted = value === "-";
|
|
const [localValue, setLocalValue] = useState(
|
|
isMuted ? "" : String(value ?? ""),
|
|
);
|
|
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null);
|
|
const isFocusedRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
if (!isFocusedRef.current) {
|
|
setLocalValue(isMuted ? "" : String(value ?? ""));
|
|
}
|
|
}, [value, isMuted]);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
const isNumericQuestion = question.type === "number" || question.validation?.format === "number";
|
|
|
|
const handleChange = (val: string) => {
|
|
let nextVal = val;
|
|
if (isNumericQuestion) {
|
|
nextVal = toEnglishDigits(nextVal).replace(/[^\d]/g, "");
|
|
}
|
|
|
|
setLocalValue(nextVal);
|
|
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
|
|
debounceTimerRef.current = setTimeout(() => {
|
|
setAnswerValue(question, nextVal);
|
|
}, 300);
|
|
};
|
|
|
|
const handleFocus = () => {
|
|
isFocusedRef.current = true;
|
|
};
|
|
|
|
const handleBlur = () => {
|
|
isFocusedRef.current = false;
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
setAnswerValue(question, localValue);
|
|
};
|
|
|
|
const stringValue = localValue.trim();
|
|
const isEmailQuestion = question.type === "email" || question.validation?.format === "email";
|
|
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
const isValidEmail = !isEmailQuestion || emailRegex.test(stringValue);
|
|
const showInvalidState =
|
|
isEmailQuestion && stringValue.length > 0 && !isValidEmail;
|
|
const isAnswered =
|
|
question.required === false
|
|
? isValidEmail || stringValue.length === 0
|
|
: isValidEmail && stringValue.length > 0;
|
|
|
|
const isMarjaQuestion = question.ui_config?.showNotPriorityCheckbox === true;
|
|
|
|
if (isEmailQuestion) {
|
|
return (
|
|
<div
|
|
data-question-answered={isAnswered ? "true" : "false"}
|
|
data-question-type="email"
|
|
className={cn(
|
|
"flex w-full flex-col gap-2 transition-opacity duration-200",
|
|
disabled && "pointer-events-none opacity-30",
|
|
)}
|
|
>
|
|
<QuestionTitle question={question} />
|
|
<Input
|
|
type="text"
|
|
inputMode="email"
|
|
dir="ltr"
|
|
hasError={showInvalidState}
|
|
value={localValue}
|
|
onChange={(e) => handleChange(e.target.value)}
|
|
onFocus={handleFocus}
|
|
onBlur={handleBlur}
|
|
placeholder={question.extras.placeHolder}
|
|
disabled={disabled || isMuted}
|
|
/>
|
|
{showInvalidState ? (
|
|
<span className="block group-10 font-semibold text-[#F2465F]">
|
|
{question.validation?.errorMessage ||
|
|
(locale === "fa"
|
|
? "یک آدرس ایمیل معتبر وارد کنید."
|
|
: "Enter a valid email address.")}
|
|
</span>
|
|
) : null}
|
|
{description ? (
|
|
<span className="block group-10 font-semibold text-[#747474]">
|
|
{description}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div
|
|
data-question-answered={isAnswered ? "true" : "false"}
|
|
data-question-type={question.type}
|
|
className={cn(
|
|
"flex w-full flex-col gap-2 transition-opacity duration-200",
|
|
disabled && "pointer-events-none opacity-30",
|
|
)}
|
|
>
|
|
<QuestionTitle question={question} />
|
|
<Input
|
|
type={isNumericQuestion ? "tel" : "text"}
|
|
inputMode={isNumericQuestion ? "numeric" : "text"}
|
|
pattern={isNumericQuestion ? "[0-9]*" : undefined}
|
|
value={localValue}
|
|
onChange={(e) => handleChange(e.target.value)}
|
|
onFocus={handleFocus}
|
|
onBlur={handleBlur}
|
|
placeholder={question.extras.placeHolder}
|
|
disabled={disabled || isMuted}
|
|
className={cn(
|
|
isMuted && "opacity-50 cursor-not-allowed bg-[#F5F5F5] border-[#E2E8F0]",
|
|
)}
|
|
/>
|
|
{isMarjaQuestion ? (
|
|
<div className="mt-2.5 flex items-center">
|
|
<label className="flex cursor-pointer items-center gap-2 select-none active:scale-[0.99] transition-all">
|
|
<input
|
|
type="checkbox"
|
|
checked={isMuted}
|
|
onChange={(e) => {
|
|
if (debounceTimerRef.current) {
|
|
clearTimeout(debounceTimerRef.current);
|
|
}
|
|
if (e.target.checked) {
|
|
setAnswerValue(question, "-");
|
|
} else {
|
|
setAnswerValue(question, "");
|
|
}
|
|
}}
|
|
className="h-[18px] w-[18px] shrink-0 rounded border-[#D0D5DD] text-[#F2465F] focus:ring-[#F2465F] accent-[#F2465F] cursor-pointer"
|
|
/>
|
|
<span className="text-[13.5px] font-semibold text-[#4F4F4F]">
|
|
{t["This is not a priority for me"] ??
|
|
"This is not a priority for me"}
|
|
</span>
|
|
</label>
|
|
</div>
|
|
) : null}
|
|
{description ? (
|
|
<span className="block group-10 font-semibold text-[#747474]">
|
|
{description}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|