danxuan.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view v-if="question" class="ezy-yingyu-danxuan-box">
  3. <!-- 标题区域 -->
  4. <view class="yingyu-danxuan-title"></view>
  5. <!-- 题干区域 -->
  6. <textReplaceIconVue :question="question" :text="question.name" :placeholders="placeholders">
  7. </textReplaceIconVue>
  8. <!-- 选项区域 -->
  9. <view v-for="(item,index) in data.contents" class="yingyu-danxuan-option-box" :class="formatClass(index)" :key="index" @click="onSelect(index)">
  10. <text class="option-change">{{item.number}}</text>
  11. <textReplaceIconVue :question="question" :text="item.label" :placeholders="placeholders" @itemclick="onSelect(index)">
  12. </textReplaceIconVue>
  13. </view>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. ref,
  19. reactive,
  20. watch
  21. } from 'vue';
  22. import {
  23. useQuestionTools
  24. } from "../useQuestionTools"
  25. import textReplaceIconVue from './textReplaceIcon.vue'
  26. const {
  27. getLetterByIndex
  28. } = useQuestionTools();
  29. const props = defineProps({
  30. question: {
  31. type: Object,
  32. },
  33. showError: {
  34. type: Boolean,
  35. default: false
  36. },
  37. placeholders: { // 占位符
  38. type: Array,
  39. required: true
  40. },
  41. })
  42. const data = reactive({
  43. name: '', //题干数据
  44. contents: [], // 选项数据
  45. })
  46. watch(() => props.question, (val) => formatData(val), {
  47. immediate: true
  48. })
  49. function formatClass(index) {
  50. if (props.showError) {
  51. return {
  52. active_right: props.question.result == index,
  53. showError: props.question.reply == index && props.question.result != index
  54. }
  55. } else {
  56. return {
  57. active: props.question.reply == index
  58. }
  59. }
  60. }
  61. function formatData(val) {
  62. if (val) {
  63. data.name = val.name;
  64. data.contents = val.optList.map((item, index) => {
  65. return {
  66. label: item,
  67. number: getLetterByIndex(index)
  68. }
  69. })
  70. }
  71. }
  72. function onSelect(index) {
  73. console.log('onSelect',index)
  74. if (props.showError) {
  75. return;
  76. }
  77. props.question.reply = index;
  78. }
  79. </script>