Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import React from 'react'; |
| 6 | import {makeStyles} from '@material-ui/styles'; |
| 7 | import SelectMenu from '../SelectMenu.tsx'; |
| 8 | import {CustomQuestion, CustomQuestionType} from '../IssueWizardTypes.tsx'; |
| 9 | import CustomQuestionInput from './CustomQuestionInput.tsx'; |
| 10 | import CustomQuestionTextarea from './CustomQuestionTextarea.tsx'; |
| 11 | import {GetSelectMenuOptions} from '../IssueWizardUtils.tsx'; |
| 12 | |
| 13 | const userStyles = makeStyles({ |
| 14 | head: { |
| 15 | marginTop: '1.5rem', |
| 16 | fontSize: '1rem' |
| 17 | }, |
| 18 | inputArea: { |
| 19 | width: '100%', |
| 20 | }, |
| 21 | tip: { |
| 22 | margin: '0.5rem 0', |
| 23 | }, |
| 24 | }); |
| 25 | |
| 26 | type Props = { |
| 27 | question: string, |
| 28 | tip?: string, |
| 29 | options: string[], |
| 30 | subQuestions: CustomQuestion[] | null, |
| 31 | updateAnswers: Function, |
| 32 | } |
| 33 | |
| 34 | export default function CustomQuestionSelector(props: Props): React.ReactElement { |
| 35 | |
| 36 | const classes = userStyles(); |
| 37 | |
| 38 | const {question, updateAnswers, options, subQuestions, tip} = props; |
| 39 | const [selectedOption, setSelectedOption] = React.useState(options[0]); |
| 40 | |
| 41 | const [subQuestion, setSubQuestion] = React.useState(subQuestions? subQuestions[0] : null); |
| 42 | |
| 43 | React.useEffect(() => { |
| 44 | updateAnswers(options[0]); |
| 45 | },[]); |
| 46 | |
| 47 | const handleOptionChange = (option: string) => { |
| 48 | setSelectedOption(option); |
| 49 | updateAnswers(option); |
| 50 | const index = options.indexOf(option); |
| 51 | if (subQuestions !== null) { |
| 52 | setSubQuestion(subQuestions[index]); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | const updateSubQuestionAnswer = (answer:string) => { |
| 57 | const updatedAnswer = selectedOption + ' ' + answer; |
| 58 | updateAnswers(updatedAnswer); |
| 59 | } |
| 60 | const optionList = GetSelectMenuOptions(options); |
| 61 | |
| 62 | let renderSubQuestion = null; |
| 63 | |
| 64 | if (subQuestion != null) { |
| 65 | switch(subQuestion.type) { |
| 66 | case CustomQuestionType.Input: |
| 67 | renderSubQuestion = |
| 68 | <CustomQuestionInput |
| 69 | question={subQuestion.question} |
| 70 | updateAnswers={updateSubQuestionAnswer} |
| 71 | /> |
| 72 | break; |
| 73 | case CustomQuestionType.Text: |
| 74 | renderSubQuestion = |
| 75 | <CustomQuestionTextarea |
| 76 | question={subQuestion.question} |
| 77 | tip={subQuestion.tip} |
| 78 | updateAnswers={updateSubQuestionAnswer} |
| 79 | />; |
| 80 | break; |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const getQuestionInnerHtml = () => { |
| 87 | return {__html: question}; |
| 88 | } |
| 89 | |
| 90 | const getTipInnerHtml = () => { |
| 91 | return {__html: tip}; |
| 92 | } |
| 93 | return ( |
| 94 | <> |
| 95 | <h3 dangerouslySetInnerHTML={getQuestionInnerHtml()} className={classes.head}/> |
| 96 | {tip? <div dangerouslySetInnerHTML={getTipInnerHtml()} className={classes.tip}/> : null} |
| 97 | <SelectMenu |
| 98 | optionsList={optionList} |
| 99 | selectedOption={selectedOption} |
| 100 | setOption={handleOptionChange} |
| 101 | /> |
| 102 | {renderSubQuestion} |
| 103 | </> |
| 104 | ); |
| 105 | } |