import React, { useEffect, useState } from 'react' import EditorComponent from '../../components/RichEditor' import { Button, Checkbox, Radio, RadioChangeEvent, Space } from 'antd' import error from '../../assets/error.svg' import questionSuccess from '../../assets/questionSuccess.svg' import Icon_Warning from '../../assets/Icon_Warning.svg' import arrowIcon from '../../assets/arrowIcon.svg' type QuestionProps = { data: any report: any updateActivity?: (activityId: string, topicConfig: string, topicResult: string, topicStatus: '0' | '1') => Promise preActive?: (data: any) => boolean | void } const Question: React.FC = (props) => { const { data = {}, report, updateActivity, preActive } = props const { category, title = '', options = [], buttonName = '', auxiliary = [], selected } = data // 选项校验 const [tip, setTip] = useState<'' | 'success' | 'error'>('') // 校验失败显示提示 const [showAuxiliary, setShowAuxiliary] = useState(true) // 是否提交 const [submit, setSubmit] = useState(false) // 用来判断是否点击过按钮 const [hasClick, setHasClick] = useState(0) // 单选 const [value, setValue] = useState() const onChange = (e: RadioChangeEvent) => { setValue(e.target.value) reset() } // 多选 const [check, setCheck] = useState([]) const onCheckChange = (checkedValues: any) => { setCheck(checkedValues) reset() } // 提交后重新选择需要重置状态 const reset = () => { if (tip) { setTip('') } if (!showAuxiliary) { setShowAuxiliary(true) } if (submit) { setSubmit(false) } } // 提交校验 const verify = () => { if (preActive && preActive(data)) return setHasClick((pre) => ++pre) report() // 单选 同时执行数据更改 if (category === 'radio') { setTip(value === selected ? 'success' : 'error') updateActivity?.(data.id, JSON.stringify(data), value!, value === selected ? '1' : '0') } else { setTip(check.join(',') === selected ? 'success' : 'error') updateActivity?.(data.id, JSON.stringify(data), check.join(','), check.join(',') === selected ? '1' : '0') } setSubmit(true) } useEffect(() => { if (data?.topicResult) { // 设置成功的状态 setTip(data?.topicStatus === '1' ? 'success' : 'error') setShowAuxiliary(data?.topicStatus === '0') setSubmit(true) // 设置选项 if (data?.category === 'radio') { setValue(data?.topicResult) } else { try { const sel = data.topicResult.split(',') setCheck(sel) } catch (error) { } } setHasClick(1) } else { setTip('') } }, [data]) return (
选择题
{category === 'radio' ? '单选' : '多选'}
{ category === 'radio' ? { options.map((item: any) => { return {item?.value} }) } : { options.map((item: any) => { return {item?.value} }) } }
{ tip ? tip === 'success' ?
回答正确
:
回答错误,请重新作答!
: <> }
{ auxiliary?.length > 0 && tip === 'error' &&
提示 { setShowAuxiliary(!showAuxiliary) }} style={{transition: '.3s', transform: showAuxiliary ? '' : 'rotate(180deg)'}}/>
{ showAuxiliary &&
}
}
) } export default Question