|
|
@ -108,158 +108,17 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) { |
|
|
question.ui_config?.max_select || |
|
|
question.ui_config?.max_select || |
|
|
(question.validation?.max ? Number(question.validation.max) : undefined); |
|
|
(question.validation?.max ? Number(question.validation.max) : undefined); |
|
|
|
|
|
|
|
|
const selectedList = useMemo(() => { |
|
|
|
|
|
if (Array.isArray(rawValue)) return rawValue; |
|
|
|
|
|
if (typeof rawValue === "string" && rawValue) return [rawValue]; |
|
|
|
|
|
return EMPTY_ARRAY; |
|
|
|
|
|
}, [rawValue]); |
|
|
|
|
|
const singleValue = typeof rawValue === "string" ? rawValue : ""; |
|
|
|
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false); |
|
|
|
|
|
const [isClosing, setIsClosing] = useState(false); |
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState(""); |
|
|
|
|
|
const listRef = useRef<HTMLDivElement>(null); |
|
|
|
|
|
const headerRef = useRef<HTMLDivElement>(null); |
|
|
|
|
|
const sheetRef = useRef<HTMLElement>(null); |
|
|
|
|
|
const sheetSizeRef = useRef(SHEET_INITIAL_SIZE); |
|
|
|
|
|
const restingStateRef = useRef<number>(SHEET_INITIAL_SIZE); |
|
|
|
|
|
const [localSelectedList, setLocalSelectedList] = useState<string[]>(selectedList); |
|
|
|
|
|
const localSelectedListRef = useRef<string[]>(selectedList); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
localSelectedListRef.current = localSelectedList; |
|
|
|
|
|
}, [localSelectedList]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (!isOpen) { |
|
|
|
|
|
setLocalSelectedList(selectedList); |
|
|
|
|
|
localSelectedListRef.current = selectedList; |
|
|
|
|
|
} |
|
|
|
|
|
}, [isOpen, selectedList]); |
|
|
|
|
|
|
|
|
|
|
|
const closeSheet = useCallback(() => { |
|
|
|
|
|
setIsClosing(true); |
|
|
|
|
|
if (isMulti) { |
|
|
|
|
|
const currentList = localSelectedListRef.current; |
|
|
|
|
|
const trimmedList = |
|
|
|
|
|
maxSelect && currentList.length > maxSelect |
|
|
|
|
|
? currentList.slice(0, maxSelect) |
|
|
|
|
|
: currentList; |
|
|
|
|
|
setAnswerValue( |
|
|
|
|
|
question, |
|
|
|
|
|
trimmedList.length > 0 ? trimmedList : null, |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
window.setTimeout(() => { |
|
|
|
|
|
setIsOpen(false); |
|
|
|
|
|
setIsClosing(false); |
|
|
|
|
|
setSearchQuery(""); |
|
|
|
|
|
}, EXIT_ANIMATION_MS); |
|
|
|
|
|
}, [isMulti, maxSelect, question, setAnswerValue]); |
|
|
|
|
|
|
|
|
|
|
|
const openSheet = useCallback(() => { |
|
|
|
|
|
if (disabled) return; |
|
|
|
|
|
sheetSizeRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
restingStateRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
if (sheetRef.current) { |
|
|
|
|
|
sheetRef.current.style.transition = ""; |
|
|
|
|
|
sheetRef.current.style.setProperty( |
|
|
|
|
|
"--sheet-size", |
|
|
|
|
|
SHEET_INITIAL_SIZE.toFixed(4), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
setLocalSelectedList(selectedList); |
|
|
|
|
|
localSelectedListRef.current = selectedList; |
|
|
|
|
|
setIsOpen(true); |
|
|
|
|
|
setIsClosing(false); |
|
|
|
|
|
}, [disabled, selectedList]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (isOpen) { |
|
|
|
|
|
sheetSizeRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
restingStateRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
if (sheetRef.current) { |
|
|
|
|
|
sheetRef.current.style.transition = ""; |
|
|
|
|
|
sheetRef.current.style.setProperty( |
|
|
|
|
|
"--sheet-size", |
|
|
|
|
|
SHEET_INITIAL_SIZE.toFixed(4), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, [isOpen]); |
|
|
|
|
|
|
|
|
|
|
|
useSheetScrollLock(isOpen, { onBack: closeSheet }); |
|
|
|
|
|
|
|
|
|
|
|
// Handle escape key
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (!isOpen) return; |
|
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => { |
|
|
|
|
|
if (e.key === "Escape") { |
|
|
|
|
|
closeSheet(); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener("keydown", handleKeyDown); |
|
|
|
|
|
return () => window.removeEventListener("keydown", handleKeyDown); |
|
|
|
|
|
}, [isOpen, closeSheet]); |
|
|
|
|
|
|
|
|
|
|
|
const isExcludedFromAutoDatasets = |
|
|
|
|
|
question.id?.toLowerCase().includes("representative") || |
|
|
|
|
|
question.id?.toLowerCase().includes("relationship") || |
|
|
|
|
|
question.id?.toLowerCase().includes("residence_status") || |
|
|
|
|
|
question.id?.toLowerCase().includes("status") || |
|
|
|
|
|
question.id?.toLowerCase().includes("responsibility") || |
|
|
|
|
|
question.id?.toLowerCase().includes("difference") || |
|
|
|
|
|
question.id?.toLowerCase().includes("impact") || |
|
|
|
|
|
question.id?.toLowerCase().includes("political") || |
|
|
|
|
|
question.id?.toLowerCase().includes("governance") || |
|
|
|
|
|
question.id?.toLowerCase().includes("stance") || |
|
|
|
|
|
question.id?.toLowerCase().includes("government"); |
|
|
|
|
|
|
|
|
|
|
|
const hasStructuredBackendId = Boolean(question.id?.includes(".")); |
|
|
|
|
|
|
|
|
|
|
|
const isLanguageQuestion = Boolean( |
|
|
const isLanguageQuestion = Boolean( |
|
|
question.ui_config?.dataset === "languages" || |
|
|
question.ui_config?.dataset === "languages" || |
|
|
question.id?.endsWith(".mother_tongue") || |
|
|
|
|
|
question.id?.endsWith(".native_language") || |
|
|
|
|
|
question.id?.endsWith(".other_languages") || |
|
|
|
|
|
question.id?.endsWith(".languages") || |
|
|
|
|
|
question.id?.endsWith(".language") || |
|
|
|
|
|
(!hasStructuredBackendId && |
|
|
|
|
|
!isExcludedFromAutoDatasets && |
|
|
|
|
|
Boolean( |
|
|
|
|
|
question.title?.toLowerCase().includes("mother tongue") || |
|
|
|
|
|
question.title?.toLowerCase().includes("native language") || |
|
|
|
|
|
question.title?.toLowerCase() === "language" || |
|
|
|
|
|
question.title?.toLowerCase() === "languages" || |
|
|
|
|
|
question.title?.includes("زبان مادری") || |
|
|
|
|
|
question.title?.includes("زبانهای مسلط") || |
|
|
|
|
|
question.title?.includes("زبان های مسلط") |
|
|
|
|
|
)) |
|
|
|
|
|
|
|
|
question.id === "personal_identity.mother_tongue" || |
|
|
|
|
|
question.id === "personal_info.mother_tongue", |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const isCountryQuestion = Boolean( |
|
|
const isCountryQuestion = Boolean( |
|
|
!isExcludedFromAutoDatasets && |
|
|
|
|
|
(question.ui_config?.dataset === "countries" || |
|
|
|
|
|
question.ui_config?.dataset === "nationalities" || |
|
|
|
|
|
question.id?.includes("nationality") || |
|
|
|
|
|
question.id?.includes("citizenship") || |
|
|
|
|
|
question.id?.includes("country_of_") || |
|
|
|
|
|
question.id?.endsWith(".birthplace") || |
|
|
|
|
|
question.id?.endsWith(".current_residence") || |
|
|
|
|
|
question.id?.endsWith(".country") || |
|
|
|
|
|
(!hasStructuredBackendId && |
|
|
|
|
|
!question.title?.includes("؟") && |
|
|
|
|
|
!question.title?.includes("?") && |
|
|
|
|
|
Boolean( |
|
|
|
|
|
question.title?.toLowerCase().includes("nationality") || |
|
|
|
|
|
question.title?.toLowerCase().includes("citizenship") || |
|
|
|
|
|
question.title?.toLowerCase().includes("country") || |
|
|
|
|
|
question.title?.includes("تابعیت") || |
|
|
|
|
|
question.title?.includes("ملیت") || |
|
|
|
|
|
question.title?.includes("کشور") |
|
|
|
|
|
))) |
|
|
|
|
|
|
|
|
question.ui_config?.dataset === "countries" || |
|
|
|
|
|
question.ui_config?.dataset === "nationalities" || |
|
|
|
|
|
question.id === "personal_identity.current_nationality_citizenship" || |
|
|
|
|
|
question.id === "personal_info.country_of_birth", |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const options = useMemo(() => { |
|
|
const options = useMemo(() => { |
|
|
@ -389,6 +248,147 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) { |
|
|
})); |
|
|
})); |
|
|
}, [question, isLanguageQuestion, isCountryQuestion, locale, t]); |
|
|
}, [question, isLanguageQuestion, isCountryQuestion, locale, t]); |
|
|
|
|
|
|
|
|
|
|
|
const findMatchingOption = useCallback( |
|
|
|
|
|
(optId: string) => { |
|
|
|
|
|
const normalized = String(optId || "").toLowerCase().trim(); |
|
|
|
|
|
if (!normalized) return undefined; |
|
|
|
|
|
return options.find( |
|
|
|
|
|
(o) => |
|
|
|
|
|
o.id === optId || |
|
|
|
|
|
o.id.toLowerCase() === normalized || |
|
|
|
|
|
String(o.value).toLowerCase() === normalized || |
|
|
|
|
|
o.label.toLowerCase().trim() === normalized || |
|
|
|
|
|
o.id.toLowerCase().endsWith(`.${normalized}`), |
|
|
|
|
|
); |
|
|
|
|
|
}, |
|
|
|
|
|
[options], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const isOptionSelectionValid = useCallback( |
|
|
|
|
|
(optId: string) => { |
|
|
|
|
|
if (!optId || typeof optId !== "string" || !optId.trim()) return false; |
|
|
|
|
|
if (options.length === 0) return true; |
|
|
|
|
|
if (findMatchingOption(optId)) return true; |
|
|
|
|
|
if (isLanguageQuestion) { |
|
|
|
|
|
const resolved = resolveLanguageName(optId, locale); |
|
|
|
|
|
return Boolean(resolved && resolved !== optId); |
|
|
|
|
|
} |
|
|
|
|
|
if (isCountryQuestion) { |
|
|
|
|
|
const resolved = resolveCountryName(optId, locale); |
|
|
|
|
|
return Boolean(resolved && resolved !== optId); |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
}, |
|
|
|
|
|
[options.length, findMatchingOption, isLanguageQuestion, isCountryQuestion, locale], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const selectedList = useMemo(() => { |
|
|
|
|
|
const rawList = Array.isArray(rawValue) |
|
|
|
|
|
? rawValue |
|
|
|
|
|
: typeof rawValue === "string" && rawValue |
|
|
|
|
|
? [rawValue] |
|
|
|
|
|
: EMPTY_ARRAY; |
|
|
|
|
|
if (rawList.length === 0) return EMPTY_ARRAY; |
|
|
|
|
|
const validList = rawList.filter((item) => |
|
|
|
|
|
isOptionSelectionValid(String(item)), |
|
|
|
|
|
); |
|
|
|
|
|
return validList.length > 0 ? validList : EMPTY_ARRAY; |
|
|
|
|
|
}, [rawValue, isOptionSelectionValid]); |
|
|
|
|
|
|
|
|
|
|
|
const singleValue = |
|
|
|
|
|
typeof rawValue === "string" && isOptionSelectionValid(rawValue) |
|
|
|
|
|
? rawValue |
|
|
|
|
|
: ""; |
|
|
|
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false); |
|
|
|
|
|
const [isClosing, setIsClosing] = useState(false); |
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState(""); |
|
|
|
|
|
const listRef = useRef<HTMLDivElement>(null); |
|
|
|
|
|
const headerRef = useRef<HTMLDivElement>(null); |
|
|
|
|
|
const sheetRef = useRef<HTMLElement>(null); |
|
|
|
|
|
const sheetSizeRef = useRef(SHEET_INITIAL_SIZE); |
|
|
|
|
|
const restingStateRef = useRef<number>(SHEET_INITIAL_SIZE); |
|
|
|
|
|
const [localSelectedList, setLocalSelectedList] = useState<string[]>(selectedList); |
|
|
|
|
|
const localSelectedListRef = useRef<string[]>(selectedList); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
localSelectedListRef.current = localSelectedList; |
|
|
|
|
|
}, [localSelectedList]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (!isOpen) { |
|
|
|
|
|
setLocalSelectedList(selectedList); |
|
|
|
|
|
localSelectedListRef.current = selectedList; |
|
|
|
|
|
} |
|
|
|
|
|
}, [isOpen, selectedList]); |
|
|
|
|
|
|
|
|
|
|
|
const closeSheet = useCallback(() => { |
|
|
|
|
|
setIsClosing(true); |
|
|
|
|
|
if (isMulti) { |
|
|
|
|
|
const currentList = localSelectedListRef.current; |
|
|
|
|
|
const trimmedList = |
|
|
|
|
|
maxSelect && currentList.length > maxSelect |
|
|
|
|
|
? currentList.slice(0, maxSelect) |
|
|
|
|
|
: currentList; |
|
|
|
|
|
setAnswerValue( |
|
|
|
|
|
question, |
|
|
|
|
|
trimmedList.length > 0 ? trimmedList : null, |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
window.setTimeout(() => { |
|
|
|
|
|
setIsOpen(false); |
|
|
|
|
|
setIsClosing(false); |
|
|
|
|
|
setSearchQuery(""); |
|
|
|
|
|
}, EXIT_ANIMATION_MS); |
|
|
|
|
|
}, [isMulti, maxSelect, question, setAnswerValue]); |
|
|
|
|
|
|
|
|
|
|
|
const openSheet = useCallback(() => { |
|
|
|
|
|
if (disabled) return; |
|
|
|
|
|
sheetSizeRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
restingStateRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
if (sheetRef.current) { |
|
|
|
|
|
sheetRef.current.style.transition = ""; |
|
|
|
|
|
sheetRef.current.style.setProperty( |
|
|
|
|
|
"--sheet-size", |
|
|
|
|
|
SHEET_INITIAL_SIZE.toFixed(4), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
setLocalSelectedList(selectedList); |
|
|
|
|
|
localSelectedListRef.current = selectedList; |
|
|
|
|
|
setIsOpen(true); |
|
|
|
|
|
setIsClosing(false); |
|
|
|
|
|
}, [disabled, selectedList]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (isOpen) { |
|
|
|
|
|
sheetSizeRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
restingStateRef.current = SHEET_INITIAL_SIZE; |
|
|
|
|
|
if (sheetRef.current) { |
|
|
|
|
|
sheetRef.current.style.transition = ""; |
|
|
|
|
|
sheetRef.current.style.setProperty( |
|
|
|
|
|
"--sheet-size", |
|
|
|
|
|
SHEET_INITIAL_SIZE.toFixed(4), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, [isOpen]); |
|
|
|
|
|
|
|
|
|
|
|
useSheetScrollLock(isOpen, { onBack: closeSheet }); |
|
|
|
|
|
|
|
|
|
|
|
// Handle escape key
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (!isOpen) return; |
|
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => { |
|
|
|
|
|
if (e.key === "Escape") { |
|
|
|
|
|
closeSheet(); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener("keydown", handleKeyDown); |
|
|
|
|
|
return () => window.removeEventListener("keydown", handleKeyDown); |
|
|
|
|
|
}, [isOpen, closeSheet]); |
|
|
|
|
|
|
|
|
const noSearch = Boolean( |
|
|
const noSearch = Boolean( |
|
|
!isLanguageQuestion && |
|
|
!isLanguageQuestion && |
|
|
!isCountryQuestion && |
|
|
!isCountryQuestion && |
|
|
@ -687,20 +687,16 @@ export function QuestionSheet({ question, disabled }: QuestionSheetProps) { |
|
|
}, [options, searchQuery, isLanguageQuestion, isCountryQuestion, locale]); |
|
|
}, [options, searchQuery, isLanguageQuestion, isCountryQuestion, locale]); |
|
|
|
|
|
|
|
|
const getCleanLabel = (optId: string) => { |
|
|
const getCleanLabel = (optId: string) => { |
|
|
const normalized = String(optId || "").toLowerCase().trim(); |
|
|
|
|
|
const opt = options.find( |
|
|
|
|
|
(o) => |
|
|
|
|
|
o.id === optId || |
|
|
|
|
|
o.id.toLowerCase() === normalized || |
|
|
|
|
|
String(o.value).toLowerCase() === normalized || |
|
|
|
|
|
o.label.toLowerCase().trim() === normalized || |
|
|
|
|
|
o.id.endsWith(`.${normalized}`), |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
const opt = findMatchingOption(optId); |
|
|
if (!opt) { |
|
|
if (!opt) { |
|
|
if (isLanguageQuestion) { |
|
|
if (isLanguageQuestion) { |
|
|
const resolved = resolveLanguageName(optId, locale); |
|
|
const resolved = resolveLanguageName(optId, locale); |
|
|
if (resolved && resolved !== optId) return resolved; |
|
|
if (resolved && resolved !== optId) return resolved; |
|
|
} |
|
|
} |
|
|
|
|
|
if (isCountryQuestion) { |
|
|
|
|
|
const resolved = resolveCountryName(optId, locale); |
|
|
|
|
|
if (resolved && resolved !== optId) return resolved; |
|
|
|
|
|
} |
|
|
return optId; |
|
|
return optId; |
|
|
} |
|
|
} |
|
|
return opt.label.split(" - ")[0]; |
|
|
return opt.label.split(" - ")[0]; |
|
|
|