Questions.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view
  3. class="question-item"
  4. :class="{
  5. 'question-item-correct': mode === 'practice' && isSelected && isCorrect,
  6. 'question-item-wrong': mode === 'practice' && isSelected && !isCorrect,
  7. 'question-item-selected': mode === 'exam' && isSelected
  8. }"
  9. @tap="handleClick"
  10. >
  11. <view
  12. class="option-label"
  13. :class="{
  14. 'option-label-correct': mode === 'practice' && isSelected && isCorrect,
  15. 'option-label-wrong': mode === 'practice' && isSelected && !isCorrect,
  16. 'option-label-selected': mode === 'exam' && isSelected
  17. }"
  18. >
  19. {{ TopicMapList[index] }}
  20. </view>
  21. <view class="option-text">{{ question.label }}</view>
  22. </view>
  23. </template>
  24. <script setup>
  25. import { computed } from "vue";
  26. const TopicMapList = ["A", "B", "C", "D", "E"];
  27. // Props 定义
  28. const props = defineProps({
  29. answerList: {
  30. type: Array,
  31. default: () => [],
  32. },
  33. index: {
  34. type: Number,
  35. required: true,
  36. },
  37. selectCount: {
  38. type: Array,
  39. default: () => [],
  40. },
  41. question: {
  42. type: Object,
  43. required: true,
  44. default: () => ({
  45. label: "",
  46. value: 0,
  47. }),
  48. },
  49. parindex: {
  50. type: Number,
  51. required: true,
  52. },
  53. mode: {
  54. type: String,
  55. default: "exam", // practice: 练习模式, exam: 考试模式
  56. },
  57. });
  58. // Emits 定义
  59. const emit = defineEmits(["select", "showAnswer"]);
  60. // 计算属性
  61. const isSelected = computed(() =>
  62. props.selectCount.includes(props.question.value)
  63. );
  64. const isCorrect = computed(() =>
  65. props.answerList.includes(props.question.value)
  66. );
  67. // 方法
  68. const handleClick = () => {
  69. // 如果已经选中,则取消选择
  70. if (isSelected.value) {
  71. emit("select", props.question.value, props.parindex);
  72. return;
  73. }
  74. // 如果是练习模式,且已经达到答案数量,则不允许继续选择
  75. if (props.mode === 'practice' && props.selectCount.length === props.answerList.length) {
  76. return;
  77. }
  78. // 如果是练习模式,且即将完成所有选择,则显示答案
  79. if (props.mode === 'practice' && props.answerList.length - 1 === props.selectCount.length) {
  80. emit("showAnswer", true);
  81. }
  82. emit("select", props.question.value, props.parindex);
  83. };
  84. </script>
  85. <style lang="scss" scoped>
  86. @import "@/uni.scss";
  87. .question-item {
  88. display: flex;
  89. padding: 16px;
  90. border: 1px solid #ccc;
  91. border-radius: 8px;
  92. background-color: #f5f5f5;
  93. gap: 12px;
  94. align-items: center;
  95. color: #333;
  96. }
  97. .question-item-correct {
  98. border-color: $success;
  99. background-color: #e8f5e9;
  100. }
  101. .question-item-wrong {
  102. border-color: $error;
  103. background-color: #ffebee;
  104. }
  105. .question-item-selected {
  106. border-color: $uni-primary;
  107. background-color: #e3f2fd;
  108. }
  109. .option-label {
  110. border: 1px solid #ccc;
  111. border-radius: 50%;
  112. padding: 0 4px;
  113. }
  114. .option-label-correct {
  115. border-color: $success;
  116. background-color: $success;
  117. color: #fff;
  118. }
  119. .option-label-wrong {
  120. border-color: $error;
  121. background-color: $error;
  122. color: #fff;
  123. }
  124. .option-label-selected {
  125. border-color: $uni-primary;
  126. background-color: $uni-primary;
  127. color: #fff;
  128. }
  129. .option-text {
  130. flex: 1;
  131. }
  132. </style>