evan-radio.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <view class="evan-radio" @click="onRadioClick">
  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-radio__inner" :class="['evan-radio__inner--'+shape]" :style="{width:iconSize+'px',height:iconSize+'px',backgroundColor:innerBackgroundColor,borderColor:innerBorderColor}">
  7. <uni-icons v-if="isChecked" type="checkmarkempty" :size="iconSize" :color="isDisabled?'#c8c9cc':'#fff'"></uni-icons>
  8. </view>
  9. </template>
  10. <text v-if="$slots.default" class="evan-radio__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: 'EvanRadio',
  19. components: {
  20. UniIcons
  21. },
  22. props: {
  23. shape: {
  24. type: String,
  25. default: 'round'
  26. },
  27. value: {
  28. type: [String, Number, Boolean],
  29. default: null
  30. },
  31. label: {
  32. type: [String, Number, Boolean],
  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. clearable: {
  62. type: Boolean,
  63. default: false
  64. }
  65. },
  66. computed: {
  67. isGroup() {
  68. let parent = this.getParent()
  69. if (parent) {
  70. return true
  71. }
  72. return false
  73. },
  74. isDisabled() {
  75. if (this.isGroup) {
  76. return this.getParent().disabled || this.disabled
  77. }
  78. return this.disabled
  79. },
  80. mTitleStlye() {
  81. let titleStyle = Object.assign({}, this.titleStyle || {})
  82. let arr = Object.keys(titleStyle).map((key) => {
  83. if (key === 'color' && this.disabled) {
  84. return null
  85. }
  86. return `${key}:${titleStyle[key]}`
  87. }).filter((v) => v)
  88. return arr.join(';')
  89. },
  90. isChecked() {
  91. let parent = this.getParent()
  92. if ((this.isGroup && parent.value === this.label) || (!this.isGroup && this.currentValue === this.label)) {
  93. return true
  94. }
  95. return false
  96. },
  97. innerBackgroundColor() {
  98. if (this.isDisabled) {
  99. return '#ebedf0'
  100. }
  101. let parent = this.getParent()
  102. if (this.isChecked) {
  103. return this.primaryColor
  104. }
  105. return '#fff'
  106. },
  107. innerBorderColor() {
  108. if (this.isDisabled) {
  109. return '#c8c9cc'
  110. }
  111. if (this.isChecked) {
  112. return this.primaryColor
  113. }
  114. return '#c8c9cc'
  115. },
  116. iconColor() {
  117. if (this.isDisabled) {
  118. return '#ebedf0'
  119. }
  120. if (this.isChecked) {
  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. // 获取EvanRadioGroup组件
  141. getParent() {
  142. let parent = this.$parent
  143. if (parent) {
  144. let parentName = parent.$options.name
  145. while (parentName !== 'EvanRadioGroup') {
  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. onRadioClick() {
  158. if (!this.isDisabled && !this.preventClick) {
  159. this.choose()
  160. }
  161. },
  162. select() {
  163. if (!this.isDisabled) {
  164. this.choose()
  165. }
  166. },
  167. choose() {
  168. if (this.currentValue !== this.label) {
  169. this.currentValue = this.label
  170. this.$emit('input', this.currentValue)
  171. if (this.isGroup) {
  172. let parent = this.getParent()
  173. parent.onRadioChange(this.label)
  174. }
  175. } else if (this.clearable) {
  176. this.currentValue = null
  177. this.$emit('input', this.currentValue)
  178. if (this.isGroup) {
  179. let parent = this.getParent()
  180. parent.onRadioChange(null)
  181. }
  182. }
  183. },
  184. setValue(groupValue) {
  185. this.currentValue = groupValue
  186. }
  187. },
  188. created() {
  189. let parent = this.getParent()
  190. if (parent) {
  191. this.setValue(parent.value)
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .evan-radio {
  198. /* #ifndef APP-NVUE */
  199. display: flex;
  200. /* #endif */
  201. flex-direction: row;
  202. align-items: center;
  203. }
  204. .evan-radio__label {
  205. font-size: 16px;
  206. margin-left: 8px;
  207. color: #333;
  208. }
  209. .evan-radio__inner {
  210. border-width: 1px;
  211. border-style: solid;
  212. background-color: #fff;
  213. /* #ifndef APP-NVUE */
  214. display: flex;
  215. box-sizing: border-box;
  216. /* #endif */
  217. flex-direction: column;
  218. align-items: center;
  219. justify-content: center;
  220. }
  221. .evan-radio__inner--round {
  222. border-radius: 50%;
  223. }
  224. </style>