Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
453 geraldo 1
import React, { useState, useEffect } from "react";
306 geraldo 2
import Option from "../option/Option";
3
 
4
const Question = (props) => {
310 geraldo 5
 
456 geraldo 6
    // get props
7
    const { question, backendVars } = props;
452 geraldo 8
 
456 geraldo 9
    // init States
10
    const [input, setInput] = useState(question.answer);
11
 
12
    /**
13
     * Update question answer
14
     * @param {*} answer
15
     */
16
    const handleAnswer = (value) => {
17
        setInput(value);
18
        question.answer = value;
19
    }
452 geraldo 20
 
456 geraldo 21
    /**
453 geraldo 22
     * componentDidMount
23
     */
456 geraldo 24
    useEffect(() => {
453 geraldo 25
        setInput(question.answer);
454 geraldo 26
        console.log(question.answer);
453 geraldo 27
    }, [question]);
28
 
456 geraldo 29
 
30
 
306 geraldo 31
    return (
313 geraldo 32
        <div className="col-md-12 col-sm-12 col-xs-12 np-padding">
310 geraldo 33
            <div className="form-group" >
333 geraldo 34
                <h6>{backendVars.LBL_QUESTION} #{question.position + 1}</h6>
310 geraldo 35
                <div
36
                    dangerouslySetInnerHTML={{ __html: question.text }}
37
                    className="title"
38
                />
39
            </div>
306 geraldo 40
            {question.type == 'open' &&
310 geraldo 41
                <div className="form-group">
306 geraldo 42
                    {question.multiline == 1 ? (
43
                        <textarea
44
                            className="form-control"
45
                            rows="5"
456 geraldo 46
                            value={input}
306 geraldo 47
                            maxLength={question.maxlength}
48
                            name={question.slug_question}
456 geraldo 49
                            onChange={e => handleAnswer(e.target.value)}
342 geraldo 50
                        />
306 geraldo 51
                    ) : (
52
                        <input
53
                            type="text"
54
                            className="form-control"
456 geraldo 55
                            value={input}
306 geraldo 56
                            maxLength={question.maxlength}
340 geraldo 57
                            name={question.slug_question}
456 geraldo 58
                            onChange={e => handleAnswer(e.target.value)}
342 geraldo 59
                        />
306 geraldo 60
                    )}
61
                </div>
62
            }
63
            {question.type == 'rating-range' &&
310 geraldo 64
                <div className="form-group">
334 geraldo 65
                    {[...Array(parseInt(question.range))].map((x, i) => {
306 geraldo 66
                        return (
333 geraldo 67
                            <div className="checkbox"
68
                                key={i}>
306 geraldo 69
                                <input
70
                                    type="radio"
456 geraldo 71
                                    checked={input == i + 1}
306 geraldo 72
                                    name={question.slug_question}
342 geraldo 73
                                    value={i + 1}
456 geraldo 74
                                    onChange={e => handleAnswer(i + 1)}
342 geraldo 75
                                />
333 geraldo 76
                                <div className="option">
336 geraldo 77
                                    {i + 1}
333 geraldo 78
                                </div>
306 geraldo 79
                            </div>
80
                        )
81
                    })}
82
                </div>
83
            }
333 geraldo 84
            {(question.type == 'simple' || question.type == 'rating-open' || question.type == 'multiple') &&
310 geraldo 85
                <div className="form-group">
323 geraldo 86
                    {question.options.length > 0 &&
456 geraldo 87
                        <Option question={question} />
306 geraldo 88
                    }
89
                </div>
90
            }
91
        </div>
92
    )
93
}
94
 
95
export default Question;