table-checkbox.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view class="uni-table-checkbox" @click="selected">
  3. <view v-if="!indeterminate" class="checkbox__inner"
  4. :class="{ 'is-checked': isChecked, 'is-disable': isDisabled }">
  5. <view class="checkbox__inner-icon"></view>
  6. </view>
  7. <view v-else class="checkbox__inner checkbox--indeterminate">
  8. <view class="checkbox__inner-icon"></view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'TableCheckbox',
  15. emits: ['checkboxSelected'],
  16. props: {
  17. indeterminate: {
  18. type: Boolean,
  19. default: false
  20. },
  21. checked: {
  22. type: [Boolean, String],
  23. default: false
  24. },
  25. disabled: {
  26. type: Boolean,
  27. default: false
  28. },
  29. index: {
  30. type: Number,
  31. default: -1
  32. },
  33. cellData: {
  34. type: Object,
  35. default() {
  36. return {}
  37. }
  38. }
  39. },
  40. watch: {
  41. checked(newVal) {
  42. if (typeof this.checked === 'boolean') {
  43. this.isChecked = newVal
  44. } else {
  45. this.isChecked = true
  46. }
  47. },
  48. indeterminate(newVal) {
  49. this.isIndeterminate = newVal
  50. }
  51. },
  52. data() {
  53. return {
  54. isChecked: false,
  55. isDisabled: false,
  56. isIndeterminate: false
  57. }
  58. },
  59. created() {
  60. if (typeof this.checked === 'boolean') {
  61. this.isChecked = this.checked
  62. }
  63. this.isDisabled = this.disabled
  64. },
  65. methods: {
  66. selected() {
  67. if (this.isDisabled) return
  68. this.isIndeterminate = false
  69. this.isChecked = !this.isChecked
  70. this.$emit('checkboxSelected', {
  71. checked: this.isChecked,
  72. data: this.cellData
  73. })
  74. }
  75. }
  76. }
  77. </script>
  78. <style lang="scss">
  79. $uni-primary: #007aff !default;
  80. $border-color: #DCDFE6;
  81. $disable: 0.4;
  82. .uni-table-checkbox {
  83. display: flex;
  84. flex-direction: row;
  85. align-items: center;
  86. justify-content: center;
  87. position: relative;
  88. margin: 5px 0;
  89. cursor: pointer;
  90. // 多选样式
  91. .checkbox__inner {
  92. /* #ifndef APP-NVUE */
  93. flex-shrink: 0;
  94. box-sizing: border-box;
  95. /* #endif */
  96. position: relative;
  97. width: 16px;
  98. height: 16px;
  99. border: 1px solid $border-color;
  100. border-radius: 2px;
  101. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  102. z-index: 1;
  103. .checkbox__inner-icon {
  104. position: absolute;
  105. /* #ifdef APP-NVUE */
  106. top: 2px;
  107. /* #endif */
  108. /* #ifndef APP-NVUE */
  109. top: 2px;
  110. /* #endif */
  111. left: 5px;
  112. height: 7px;
  113. width: 3px;
  114. border: 1px solid #fff;
  115. border-left: 0;
  116. border-top: 0;
  117. opacity: 0;
  118. transform-origin: center;
  119. transform: rotate(45deg);
  120. box-sizing: content-box;
  121. }
  122. &.checkbox--indeterminate {
  123. border-color: $uni-primary;
  124. background-color: $uni-primary;
  125. .checkbox__inner-icon {
  126. position: absolute;
  127. opacity: 1;
  128. transform: rotate(0deg);
  129. height: 2px;
  130. top: 0;
  131. bottom: 0;
  132. margin: auto;
  133. left: 0px;
  134. right: 0px;
  135. bottom: 0;
  136. width: auto;
  137. border: none;
  138. border-radius: 2px;
  139. transform: scale(0.5);
  140. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  141. }
  142. }
  143. &:hover {
  144. border-color: $uni-primary;
  145. }
  146. // 禁用
  147. &.is-disable {
  148. /* #ifdef H5 */
  149. cursor: not-allowed;
  150. /* #endif */
  151. background-color: #F2F6FC;
  152. border-color: $border-color;
  153. }
  154. // 选中
  155. &.is-checked {
  156. border-color: $uni-primary;
  157. background-color: $uni-primary;
  158. .checkbox__inner-icon {
  159. opacity: 1;
  160. transform: rotate(45deg);
  161. }
  162. // 选中禁用
  163. &.is-disable {
  164. opacity: $disable;
  165. }
  166. }
  167. }
  168. }
  169. </style>