evan-checkbox.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view class="evan-checkbox" @click="onCheckboxChange">
  3. <slot v-if="$slots.icon" name="icon"></slot>
  4. <template v-else>
  5. <uni-icons v-if="icon" :type="icon" :size="iconSize" :color="iconColor"></uni-icons>
  6. <view v-else class="evan-checkbox__inner" :class="['evan-checkbox__inner--'+shape]" :style="{width:iconSize+'px',height:iconSize+'px',backgroundColor:innerBackgroundColor,borderColor:innerBorderColor}">
  7. <uni-icons v-if="currentValue" type="checkmarkempty" :size="iconSize" :color="isDisabled?'#c8c9cc':'#fff'"></uni-icons>
  8. </view>
  9. </template>
  10. <text v-if="$slots.default" class="evan-checkbox__label" :style="mTitleStlye">
  11. <slot></slot>
  12. </text>
  13. </view>
  14. </template>
  15. <script>
  16. import UniIcons from '@/components/uni-icons/uni-icons.vue'
  17. export default {
  18. name: 'EvanCheckbox',
  19. components: {
  20. UniIcons
  21. },
  22. props: {
  23. shape: {
  24. type: String,
  25. default: 'round'
  26. },
  27. value: {
  28. type: Boolean,
  29. default: false
  30. },
  31. label: {
  32. type: [String, Number],
  33. default: null
  34. },
  35. disabled: {
  36. type: Boolean,
  37. default: false
  38. },
  39. icon: {
  40. type: String,
  41. default: null
  42. },
  43. iconSize: {
  44. type: Number,
  45. default: 20
  46. },
  47. primaryColor: {
  48. type: String,
  49. default: '#108ee9'
  50. },
  51. titleStyle: {
  52. type: Object,
  53. default: () => {
  54. return {}
  55. }
  56. },
  57. preventClick: {
  58. type: Boolean,
  59. default: false
  60. }
  61. },
  62. computed: {
  63. isGroup() {
  64. let parent = this.getParent()
  65. if (parent) {
  66. return true
  67. }
  68. return false
  69. },
  70. isDisabled() {
  71. if (this.isGroup) {
  72. return this.getParent().disabled || this.disabled
  73. }
  74. return this.disabled
  75. },
  76. isOverLimit() {
  77. if (this.isGroup) {
  78. let parent = this.getParent()
  79. if (parent.max) {
  80. let parentValue = parent.value || []
  81. if (parentValue.length >= parent.max) {
  82. return true
  83. }
  84. }
  85. }
  86. return false
  87. },
  88. mTitleStlye() {
  89. let titleStyle = Object.assign({}, this.titleStyle || {})
  90. let arr = Object.keys(titleStyle).map((key) => {
  91. if (key === 'color' && this.disabled) {
  92. return null
  93. }
  94. return `${key}:${titleStyle[key]}`
  95. }).filter((v) => v)
  96. return arr.join(';')
  97. },
  98. innerBackgroundColor() {
  99. if (this.isDisabled) {
  100. return '#ebedf0'
  101. }
  102. if (this.currentValue) {
  103. return this.primaryColor
  104. }
  105. return '#fff'
  106. },
  107. innerBorderColor() {
  108. if (this.isDisabled) {
  109. return '#c8c9cc'
  110. }
  111. if (this.currentValue) {
  112. return this.primaryColor
  113. }
  114. return '#c8c9cc'
  115. },
  116. iconColor() {
  117. if (this.isDisabled) {
  118. return '#ebedf0'
  119. }
  120. if (this.currentValue) {
  121. return this.primaryColor
  122. }
  123. return '#c8c9cc'
  124. }
  125. },
  126. watch: {
  127. value: {
  128. immediate: true,
  129. handler(value) {
  130. this.currentValue = value
  131. }
  132. }
  133. },
  134. data() {
  135. return {
  136. currentValue: null
  137. }
  138. },
  139. methods: {
  140. // 获取EvanCheckboxGroup组件
  141. getParent() {
  142. let parent = this.$parent
  143. if (parent) {
  144. let parentName = parent.$options.name
  145. while (parentName !== 'EvanCheckboxGroup') {
  146. parent = parent.$parent
  147. if (parent) {
  148. parentName = parent.$options.name
  149. } else {
  150. return null
  151. }
  152. }
  153. return parent
  154. }
  155. return null
  156. },
  157. onCheckboxChange() {
  158. if (!this.isDisabled && !this.preventClick && (!this.isOverLimit || this.currentValue)) {
  159. this.toggleValue()
  160. }
  161. },
  162. toggle() {
  163. if (!this.isDisabled && (!this.isOverLimit || this.currentValue)) {
  164. this.toggleValue()
  165. }
  166. },
  167. toggleValue() {
  168. this.currentValue = !this.currentValue
  169. this.$emit('input', this.currentValue)
  170. this.$emit('change', this.currentValue)
  171. let parent = this.getParent()
  172. if (parent) {
  173. parent.onCheckboxChange(this.label)
  174. }
  175. },
  176. setValue(groupValue) {
  177. groupValue = groupValue || []
  178. this.currentValue = groupValue.includes(this.label)
  179. }
  180. },
  181. created() {
  182. let parent = this.getParent()
  183. if (parent) {
  184. this.setValue(parent.value)
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .evan-checkbox {
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. flex-direction: row;
  195. align-items: center;
  196. }
  197. .evan-checkbox__label {
  198. font-size: 12px;
  199. margin-left: 8px;
  200. color: #333;
  201. }
  202. .evan-checkbox__inner {
  203. border-width: 1px;
  204. border-style: solid;
  205. background-color: #fff;
  206. /* #ifndef APP-NVUE */
  207. display: flex;
  208. box-sizing: border-box;
  209. /* #endif */
  210. flex-direction: column;
  211. align-items: center;
  212. justify-content: center;
  213. }
  214. .evan-checkbox__inner--round {
  215. border-radius: 50%;
  216. }
  217. </style>