Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 456 | Rev 458 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
456 geraldo 1
import React, { useState, useEffect } from "react";
306 geraldo 2
 
3
const Option = (props) => {
315 geraldo 4
 
306 geraldo 5
    // get props
456 geraldo 6
    const { question } = props;
306 geraldo 7
 
456 geraldo 8
    // init States
9
    const [input, setInput] = useState(question.answer);
10
 
11
    /**
12
     * Update question answer
13
     * @param {*} answer
14
     */
15
    const handleAnswerSimple = (value) => {
16
        setInput(value);
17
        question.answer = value;
18
    }
19
 
20
    /**
21
     * Update question answer
22
     * @param {*} answer
23
     */
24
    const handleAnswerMultiple = (value) => {
25
 
26
        question.answer.includes(value) ?
27
            question.answer = removeOptionMultiple(question.answer, value) :
457 geraldo 28
            question.answer.push(value);
29
 
30
            console.log(question.answer);
456 geraldo 31
        setInput(question.answer);
32
    }
33
 
34
    /**
35
     * componentDidMount
36
     */
37
    useEffect(() => {
38
        setInput(question.answer);
39
    }, [question]);
40
 
41
    /**
42
     * Delete existing option
43
     * @param {*} arr
44
     * @param {*} item
45
     * @returns
46
     */
47
    const removeOptionMultiple = (arr, item) => arr.splice(arr.indexOf(item), 1);
48
 
306 geraldo 49
    return (
50
        <div>
319 geraldo 51
            {question.options.map((opt, key) => {
52
                return (
320 geraldo 53
                    <div className="checkbox"
54
                        key={key}>
321 geraldo 55
 
56
                        {question.type == 'multiple' &&
57
 
58
                            <input
59
                                type="checkbox"
60
                                name={`${opt.slug_option}[]`}
456 geraldo 61
                                value={input}
62
                                checked={input.includes(opt.slug_option)}
63
                                onChange={() => handleAnswerMultiple(opt.slug_option)}
344 geraldo 64
                            />
321 geraldo 65
                        }
66
                        {question.type != 'multiple' &&
67
                            <input
68
                                type="radio"
328 geraldo 69
                                name={`${opt.slug_question}`}
342 geraldo 70
                                value={opt.slug_option}
456 geraldo 71
                                checked={input == opt.slug_option}
72
                                onChange={() => handleAnswerSimple(opt.slug_option)}
342 geraldo 73
                            />
321 geraldo 74
                        }
319 geraldo 75
                        <div
76
                            dangerouslySetInnerHTML={{ __html: opt.text }}
77
                            className="option"
78
                        />
79
                    </div>
80
                )
81
            })}
306 geraldo 82
        </div>
83
 
84
    )
85
}
86
 
87
export default Option;