Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 466 | Ir a la última revisión | | Comparar con el anterior | 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
     */
467 geraldo 15
    const handleAnswer = () => {
466 geraldo 16
        question.answer.includes(option.slug_option) ?
17
            question.answer = removeOption(question.answer, option.slug_option) :
18
            question.answer.push(option.slug_option);
467 geraldo 19
        option.checked = !option.checked;
466 geraldo 20
        setChecked(option.checked);
21
    }
22
 
23
    /**
24
     * Delete existing option
25
     * @param {*} arr
26
     * @param {*} item
27
     * @returns
28
     */
29
    const removeOption = (arr, item) => arr.splice(arr.indexOf(item), 1);
30
 
31
    /**
32
     * componentDidMount
33
     */
34
    useEffect(() => {
35
        setChecked(question.checked);
36
    }, [option]);
37
 
38
 
39
    return (
40
        <div>
41
            <input
42
                type="checkbox"
43
                name={`${option.slug_option}[]`}
44
                value={option.slug_option}
45
                checked={checked}
467 geraldo 46
                onChange={() => handleAnswer()}
466 geraldo 47
            />
467 geraldo 48
        </div>)
466 geraldo 49
}
50
 
51
export default InputMultiple;