Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 467 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
466 geraldo 1
import React, { useState, useEffect } from "react";
2
 
3
const InputMultiple = (props) => {
4
 
5
    // get props
6
    const { option, question } = props;
7
 
8
    // init States
9
    const [checked, setChecked] = useState(question.checked);
10
 
11
    /**
12
     * Update question answer
13
     * @param {*} option
14
     */
15
    const handleAnswer = (option) => {
16
        console.log(option);
17
        question.answer.includes(option.slug_option) ?
18
            question.answer = removeOption(question.answer, option.slug_option) :
19
            question.answer.push(option.slug_option);
20
        option.checked = true;
21
        console.log(option);
22
        setChecked(option.checked);
23
    }
24
 
25
    /**
26
     * Delete existing option
27
     * @param {*} arr
28
     * @param {*} item
29
     * @returns
30
     */
31
    const removeOption = (arr, item) => arr.splice(arr.indexOf(item), 1);
32
 
33
    /**
34
     * componentDidMount
35
     */
36
    useEffect(() => {
37
        setChecked(question.checked);
38
    }, [option]);
39
 
40
 
41
    return (
42
        <div>
43
 
44
            <input
45
                type="checkbox"
46
                name={`${option.slug_option}[]`}
47
                value={option.slug_option}
48
                checked={checked}
49
                onChange={() => handleAnswer(option)}
50
            />
51
 
52
        </div>
53
 
54
    )
55
}
56
 
57
export default InputMultiple;