Questions.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view
  3. class="question-item"
  4. :class="{
  5. 'question-item-selected':
  6. question.checked && !props.showResult && styleCount !== 6,
  7. 'question-item-correct':
  8. question.checked && props.showResult && question.isRight && styleCount !== 6,
  9. 'question-item-wrong':
  10. question.checked && props.showResult && !question.isRight && styleCount !== 6,
  11. }"
  12. :style="{
  13. border: styleCount !== 6 ? '1px solid #ccc' : 0,
  14. padding: styleCount !== 6 ? '28rpx;' : 0,
  15. }"
  16. @tap="handleClick"
  17. >
  18. <view
  19. class="option-label"
  20. :class="{
  21. 'option-label-selected': question.checked && !props.showResult,
  22. 'option-label-correct':
  23. question.checked && props.showResult && question.isRight,
  24. 'option-label-wrong':
  25. question.checked && props.showResult && !question.isRight,
  26. }"
  27. >
  28. {{ question.value }}
  29. </view>
  30. <uvParse
  31. :content="replaceImageDimensions(question.label)"
  32. class="option-text"
  33. ></uvParse>
  34. <view
  35. class="option-icon"
  36. v-if="
  37. question.checked && props.showResult && !question.isRight && styleCount !== 6
  38. "
  39. >
  40. <uni-icons type="closeempty" color="#f00"></uni-icons>
  41. </view>
  42. <view
  43. class="option-icon"
  44. v-if="
  45. question.checked && props.showResult && question.isRight && styleCount !== 6
  46. "
  47. >
  48. <uni-icons type="checkmarkempty" color="#00be00"></uni-icons>
  49. </view>
  50. </view>
  51. </template>
  52. <script setup>
  53. import { computed } from "vue";
  54. import uvParse from "../../uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  55. // Props 定义
  56. const props = defineProps({
  57. answerList: {
  58. type: Array,
  59. default: () => [],
  60. },
  61. selectCount: {
  62. type: Array,
  63. default: () => [],
  64. },
  65. question: {
  66. type: Object,
  67. required: true,
  68. default: () => ({
  69. label: "",
  70. value: 0,
  71. checked: false,
  72. }),
  73. },
  74. parindex: {
  75. type: Number,
  76. required: true,
  77. },
  78. showResult: {
  79. type: Boolean,
  80. default: false,
  81. },
  82. styleCount: Number,
  83. });
  84. // Emits 定义
  85. const emit = defineEmits(["select"]);
  86. const replaceImageDimensions = (str) =>
  87. str
  88. .replace(
  89. /<img([^>]*?)style="([^"]*?)width:\s*[^;]+;\s*height:\s*[^;]+;([^"]*?)"([^>]*?)\/?>/g,
  90. '<img$1style="$2width:100%;height:100%;$3"$4/>'
  91. )
  92. .replace(
  93. /<img([^>]*?)width="[^"]*"([^>]*?)height="[^"]*"([^>]*?)\/?>/g,
  94. '<img$1width="100%"$2height="100%"$3/>'
  95. );
  96. // 方法
  97. const handleClick = () => {
  98. if (props.showResult) return;
  99. emit("select", {
  100. ...props.question,
  101. pid: props.parindex,
  102. style: props.styleCount,
  103. });
  104. };
  105. </script>
  106. <style lang="scss" scoped>
  107. @import "@/uni.scss";
  108. .question-item {
  109. display: flex;
  110. border-radius: 8px;
  111. background-color: #fafbfd;
  112. gap: 12px;
  113. align-items: center;
  114. color: #333;
  115. }
  116. .question-item-correct {
  117. border-color: $success;
  118. background-color: #e8f5e9;
  119. }
  120. .question-item-wrong {
  121. border-color: $error;
  122. background-color: #ffebee;
  123. }
  124. .question-item-selected {
  125. border-color: $uni-primary;
  126. background-color: #e3f2fd;
  127. }
  128. .option-label {
  129. border: 1px solid #ccc;
  130. border-radius: 50%;
  131. padding: 0 5px;
  132. }
  133. .option-label-correct {
  134. border-color: $success;
  135. background-color: $success;
  136. color: #fff;
  137. }
  138. .option-label-wrong {
  139. border-color: $error;
  140. background-color: $error;
  141. color: #fff;
  142. }
  143. .option-label-selected {
  144. border-color: $uni-primary;
  145. background-color: $uni-primary;
  146. color: #fff;
  147. }
  148. .option-text {
  149. flex: 1;
  150. white-space: normal;
  151. }
  152. </style>