Browse Source

feat: add focus-based read-only toggle to QuestionText and update mock type casting in QuestionPhone test

master
mortezaei 6 days ago
parent
commit
f7cdb77556
  1. 4
      src/components/Componentes/question-phone.test.tsx
  2. 18
      src/components/Componentes/question-text.tsx

4
src/components/Componentes/question-phone.test.tsx

@ -27,14 +27,14 @@ vi.mock("./question-answer-storage", () => ({
})); }));
describe("QuestionPhone Component", () => { describe("QuestionPhone Component", () => {
const mockQuestion: QuestionField = {
const mockQuestion = {
id: "personal_contact_number", id: "personal_contact_number",
title: "شماره تماس شخصی", title: "شماره تماس شخصی",
type: "phone", type: "phone",
extras: { extras: {
placeHolder: "+98 9123456789", placeHolder: "+98 9123456789",
}, },
};
} as QuestionField;
it("renders phone input and opens sheet on country code click", async () => { it("renders phone input and opens sheet on country code click", async () => {
render(<QuestionPhone question={mockQuestion} countryCode="+98" />); render(<QuestionPhone question={mockQuestion} countryCode="+98" />);

18
src/components/Componentes/question-text.tsx

@ -33,6 +33,7 @@ export default function QuestionText({
const [localValue, setLocalValue] = useState( const [localValue, setLocalValue] = useState(
isMuted ? "" : String(value ?? ""), isMuted ? "" : String(value ?? ""),
); );
const [isReadOnly, setIsReadOnly] = useState(true);
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null); const debounceTimerRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => { useEffect(() => {
@ -59,12 +60,17 @@ export default function QuestionText({
}; };
const handleBlur = () => { const handleBlur = () => {
setIsReadOnly(true);
if (debounceTimerRef.current) { if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current); clearTimeout(debounceTimerRef.current);
} }
setAnswerValue(question, localValue); setAnswerValue(question, localValue);
}; };
const handleFocus = () => {
setIsReadOnly(false);
};
const stringValue = localValue.trim(); const stringValue = localValue.trim();
const isEmailQuestion = question.type === "email" || question.validation?.format === "email"; const isEmailQuestion = question.type === "email" || question.validation?.format === "email";
@ -101,8 +107,11 @@ export default function QuestionText({
> >
<input <input
type="text" type="text"
inputMode="email"
autoComplete="off"
name="off_email"
readOnly={isReadOnly || disabled || isMuted}
onFocus={handleFocus}
onTouchStart={handleFocus}
autoComplete="one-time-code"
autoCorrect="off" autoCorrect="off"
autoCapitalize="none" autoCapitalize="none"
spellCheck="false" spellCheck="false"
@ -147,7 +156,10 @@ export default function QuestionText({
type={isNumericQuestion ? "tel" : "text"} type={isNumericQuestion ? "tel" : "text"}
inputMode={isNumericQuestion ? "numeric" : undefined} inputMode={isNumericQuestion ? "numeric" : undefined}
pattern={isNumericQuestion ? "[0-9]*" : undefined} pattern={isNumericQuestion ? "[0-9]*" : undefined}
autoComplete="off"
readOnly={isReadOnly || disabled || isMuted}
onFocus={handleFocus}
onTouchStart={handleFocus}
autoComplete="one-time-code"
autoCorrect="off" autoCorrect="off"
autoCapitalize="none" autoCapitalize="none"
spellCheck="false" spellCheck="false"

Loading…
Cancel
Save