- {/* Solo permitir condicionales para boolean y single_choice */}
- {(() => {
- const currentType = formData.options?.type || formData.type
- const allowConditional = ['boolean', 'single_choice'].includes(currentType)
-
- if (!allowConditional) {
+
+
+ {/* Selector de pregunta padre - SIEMPRE disponible */}
+
+
+ Pregunta padre (opcional)
+
+
setFormData({
+ ...formData,
+ parent_question_id: e.target.value ? parseInt(e.target.value) : null,
+ show_if_answer: '' // Reset al cambiar padre
+ })}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 bg-white"
+ >
+ Ninguna (pregunta principal)
+ {questions
+ .filter(q => {
+ // Permitir cualquier pregunta que no sea esta misma
+ // y que tenga depth_level < 5 (para no exceder límite)
+ const depth = q.depth_level || 0
+ return depth < 5 && (!editingQuestion || q.id !== editingQuestion.id)
+ })
+ .map(q => {
+ const depth = q.depth_level || 0
+ const indent = ' '.repeat(depth)
+ const levelLabel = depth > 0 ? ` [Nivel ${depth}]` : ''
+ return (
+
+ {indent}#{q.id} - {q.text.substring(0, 40)}{q.text.length > 40 ? '...' : ''}{levelLabel}
+
+ )
+ })
+ }
+
+
+ Si seleccionas una pregunta padre, esta pregunta se mostrará como subpregunta debajo de ella
+
+
+
+ {/* Condición - Solo si hay padre y el padre es boolean/single_choice */}
+ {formData.parent_question_id && (() => {
+ const parentQ = questions.find(q => q.id === formData.parent_question_id)
+ if (!parentQ) return null
+
+ const config = parentQ.options || {}
+ const parentType = config.type || parentQ.type
+ const isConditionalParent = ['boolean', 'single_choice'].includes(parentType)
+
+ if (!isConditionalParent) {
+ return (
+
+
+ ✓ Esta subpregunta aparecerá SIEMPRE debajo de la pregunta padre seleccionada
+
+
+ )
+ }
+
return (
-
-
- ℹ️ Las preguntas condicionales solo están disponibles para tipos Boolean y Opción Única
+
+
+ ⚡ Condición (opcional) - Mostrar si la respuesta es:
+
+
setFormData({ ...formData, show_if_answer: e.target.value || null })}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 bg-white"
+ >
+ Siempre visible (sin condición)
+ {config.choices ? (
+ config.choices.map((choice, idx) => (
+
+ Solo si es: {choice.label}
+
+ ))
+ ) : (
+ // Compatibilidad con tipos antiguos
+ parentType === 'pass_fail' ? [
+ Solo si es: ✓ Pasa ,
+ Solo si es: ✗ Falla
+ ] : [
+ Solo si es: ✓ Bueno ,
+ Solo si es: ✗ Malo
+ ]
+ )}
+
+
+ {formData.show_if_answer
+ ? '⚡ Se mostrará SOLO cuando la respuesta del padre coincida'
+ : '✓ Se mostrará SIEMPRE debajo de la pregunta padre'}
)
- }
-
- return (
- <>
-
-
-
- Pregunta padre
-
-
setFormData({
- ...formData,
- parent_question_id: e.target.value ? parseInt(e.target.value) : null,
- show_if_answer: '' // Reset al cambiar padre
- })}
- className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 bg-white"
- >
- Ninguna (pregunta principal)
- {questions
- .filter(q => {
- // Permitir cualquier pregunta que no sea esta misma
- // y que tenga depth_level < 5 (para no exceder límite)
- const depth = q.depth_level || 0
- return depth < 5
- })
- .map(q => {
- const depth = q.depth_level || 0
- const indent = ' '.repeat(depth)
- const levelLabel = depth > 0 ? ` [Nivel ${depth}]` : ''
- return (
-
- {indent}#{q.id} - {q.text.substring(0, 40)}{q.text.length > 40 ? '...' : ''}{levelLabel}
-
- )
- })
- }
-
-
- Esta pregunta aparecerá solo si se cumple la condición de la pregunta padre
-
-
-
-
- Mostrar si la respuesta es:
-
-
setFormData({ ...formData, show_if_answer: e.target.value })}
- className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 bg-white"
- disabled={!formData.parent_question_id}
- >
- Seleccione...
- {formData.parent_question_id && (() => {
- const parentQ = questions.find(q => q.id === formData.parent_question_id)
- if (!parentQ) return null
-
- // Leer opciones del nuevo formato
- const config = parentQ.options || {}
- const parentType = config.type || parentQ.type
-
- // Para boolean o single_choice, mostrar las opciones configuradas
- if ((parentType === 'boolean' || parentType === 'single_choice') && config.choices) {
- return config.choices.map((choice, idx) => (
-
- {choice.label}
-
- ))
- }
-
- // Compatibilidad con tipos antiguos
- if (parentType === 'pass_fail') {
- return [
- ✓ Pasa ,
- ✗ Falla
- ]
- } else if (parentType === 'good_bad') {
- return [
- ✓ Bueno ,
- ✗ Malo
- ]
- }
-
- return Tipo de pregunta no compatible
- })()}
-
-
- La pregunta solo se mostrará con esta respuesta específica
-
-
+ })()}
+
+ {/* Indicador de profundidad */}
+ {formData.parent_question_id && (() => {
+ const parentQ = questions.find(q => q.id === formData.parent_question_id)
+ const parentDepth = parentQ?.depth_level || 0
+ const newDepth = parentDepth + 1
+
+ return (
+
= 5 ? 'bg-red-50 border border-red-200' : 'bg-blue-100'}`}>
+
+ 📊 Profundidad: Nivel {newDepth} de 5 máximo
+ {newDepth >= 5 && ' ⚠️ Máximo alcanzado'}
+
-
- {/* Indicador de profundidad */}
- {formData.parent_question_id && (() => {
- const parentQ = questions.find(q => q.id === formData.parent_question_id)
- const parentDepth = parentQ?.depth_level || 0
- const newDepth = parentDepth + 1
-
- return (
-
= 5 ? 'bg-red-50 border border-red-200' : 'bg-blue-100'}`}>
-
- 📊 Profundidad: Nivel {newDepth} de 5 máximo
- {newDepth >= 5 && ' ⚠️ Máximo alcanzado'}
-
-
- )
- })()}
- >
- )
- })()}
+ )
+ })()}
+
{/* AI Prompt - Solo visible si el checklist tiene IA habilitada */}
@@ -1979,15 +1969,22 @@ function QuestionsManagerModal({ checklist, onClose }) {
{isSubQuestion && (
-
- ⚡ Condicional
+
+ {question.show_if_answer ? '⚡ Condicional' : '📎 Subpregunta'}
)}
{question.text}
{isSubQuestion && parentQuestion && (
-
- → Aparece si #{question.parent_question_id} es {question.show_if_answer}
+
+ {question.show_if_answer
+ ? `→ Aparece si #${question.parent_question_id} es ${question.show_if_answer}`
+ : `→ Siempre visible debajo de #${question.parent_question_id}`
+ }
)}
@@ -4416,11 +4413,16 @@ function InspectionModal({ checklist, existingInspection, user, onClose, onCompl
// If no parent, always visible
if (!q.parent_question_id) return true
- // Check parent answer
+ // If has parent but NO condition (show_if_answer is null/empty), always show if parent was answered
+ if (!q.show_if_answer) {
+ const parentAnswer = answers[q.parent_question_id]
+ return !!parentAnswer // Show if parent has any answer
+ }
+
+ // If has parent AND condition, check if parent answer matches
const parentAnswer = answers[q.parent_question_id]
if (!parentAnswer) return false
- // Show if parent answer matches trigger
return parentAnswer.value === q.show_if_answer
})
.sort((a, b) => a.order - b.order) // Mantener orden del backend