Rev 467 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useEffect } from "react";
const InputMultiple = (props) => {
// get props
const { option, question } = props;
// init States
const [checked, setChecked] = useState(option.checked);
/**
* Update question answer
*/
const handleAnswer = () => {
question.answer.includes(option.slug_option) ?
question.answer = removeOption(question.answer, option.slug_option) :
question.answer.push(option.slug_option);
option.checked = !option.checked;
setChecked(option.checked);
}
/**
* Delete existing option
* @param {*} arr
* @param {*} item
* @returns
*/
const removeOption = (arr, item) => arr.splice(arr.indexOf(item), 1);
/**
* componentDidMount
*/
useEffect(() => {
setChecked(option.checked);
}, [option]);
return (
<div>
<input
type="checkbox"
name={`${option.slug_option}[]`}
value={option.slug_option}
checked={checked}
onChange={() => handleAnswer()}
/>
</div>)
}
export default InputMultiple;