evan-radio-popup.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="evan-radio-popup">
  3. <view class="evan-radio-popup__trigger" @click="openPopup">
  4. <slot name="trigger" :label="label"></slot>
  5. <slot></slot>
  6. </view>
  7. <uni-popup @change="onPopupChange" ref="popup" type="bottom" :maskClick="maskClick">
  8. <view class="evan-radio-popup__target">
  9. <view class="evan-radio-popup__target__header">
  10. <text @click="onCancel" class="evan-radio-popup__target__header__cancel">{{cancelText}}</text>
  11. <text class="evan-radio-popup__target__header__title">{{title}}</text>
  12. <text @click="onConfirm" class="evan-radio-popup__target__header__confirm" :style="{color:primaryColor}">{{confirmText}}</text>
  13. </view>
  14. <scroll-view scroll-y="true" class="evan-radio-popup__target__body">
  15. <evan-radio-group v-model="currentValue">
  16. <view @click="onRadioClick(index)" class="evan-radio-popup__target__body__listitem" v-for="(item,index) in options"
  17. :key="item[optionValue]">
  18. <text class="evan-radio-popup__target__body__listitem__label" :style="{color:currentValue===item[optionValue]?primaryColor:'#333'}">{{item[optionLabel]}}</text>
  19. <evan-radio :clearable="clearable" :primaryColor="primaryColor" ref="radio" :preventClick="true" :label="item[optionValue]">
  20. <template slot="icon">
  21. <view>
  22. <uni-icons v-if="currentValue===item[optionValue]" type="checkmarkempty" size="25" :color="primaryColor"></uni-icons>
  23. </view>
  24. </template>
  25. </evan-radio>
  26. </view>
  27. </evan-radio-group>
  28. </scroll-view>
  29. </view>
  30. </uni-popup>
  31. </view>
  32. </template>
  33. <script>
  34. import EvanRadio from '@/components/evan-radio/evan-radio.vue'
  35. import EvanRadioGroup from '@/components/evan-radio-group/evan-radio-group.vue'
  36. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  37. import uniIcons from '@/components/uni-icons/uni-icons.vue'
  38. export default {
  39. name: 'EvanRadioPopup',
  40. components: {
  41. EvanRadio,
  42. EvanRadioGroup,
  43. uniPopup,
  44. uniIcons
  45. },
  46. props: {
  47. options: {
  48. type: Array,
  49. default: () => []
  50. },
  51. value: {
  52. type: [String, Number, Boolean],
  53. default: null
  54. },
  55. primaryColor: {
  56. type: String,
  57. default: '#108ee9'
  58. },
  59. cancelText: {
  60. type: String,
  61. default: '取消'
  62. },
  63. confirmText: {
  64. type: String,
  65. default: '确定'
  66. },
  67. title: {
  68. type: String,
  69. default: '请选择'
  70. },
  71. optionLabel: {
  72. type: String,
  73. default: 'label'
  74. },
  75. optionValue: {
  76. type: String,
  77. default: 'value'
  78. },
  79. maskClick: {
  80. type: Boolean,
  81. default: true
  82. },
  83. clearable: {
  84. type: Boolean,
  85. default: false
  86. }
  87. },
  88. computed: {
  89. label() {
  90. if (this.isTrueEmpty(this.value)) {
  91. return ''
  92. }
  93. const selectedOption = this.options.find((op) => op[this.optionValue] === this.value)
  94. if (selectedOption) {
  95. return selectedOption[this.optionLabel]
  96. }
  97. return ''
  98. }
  99. },
  100. data() {
  101. return {
  102. currentValue: null,
  103. maskClose: true // 是否由点击遮罩层关闭
  104. }
  105. },
  106. methods: {
  107. openPopup() {
  108. this.currentValue = this.value
  109. this.$refs.popup.open()
  110. },
  111. closePopup() {
  112. this.maskClose = false
  113. this.$refs.popup.close()
  114. },
  115. onCancel() {
  116. this.closePopup()
  117. this.$emit('cancel', this.currentValue)
  118. },
  119. onConfirm() {
  120. this.closePopup()
  121. let obj = null
  122. if (!this.isTrueEmpty(this.currentValue)) {
  123. obj = this.options.find((op) => op[this.optionValue] === this.currentValue)
  124. }
  125. this.$emit('input', this.currentValue)
  126. this.$emit('confirm', this.currentValue)
  127. this.$emit('objConfirm', obj)
  128. },
  129. onRadioClick(index) {
  130. this.$refs.radio[index].select()
  131. },
  132. onPopupChange(e) {
  133. // 捕捉uniPopup点击遮罩层关闭事件
  134. if (!e.show) {
  135. if (this.maskClose) {
  136. this.$emit('cancel', this.currentValue)
  137. }
  138. this.maskClose = true
  139. }
  140. },
  141. isTrueEmpty(str) {
  142. if (str || str === 0 || str === false) {
  143. return false
  144. }
  145. return true
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss">
  151. .evan-radio-popup {}
  152. .evan-radio-popup__target {}
  153. .evan-radio-popup__target__header {
  154. height: 54px;
  155. background-color: #f7f7f7;
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. flex-direction: row;
  160. align-items: center;
  161. font-size: 16px;
  162. }
  163. .evan-radio-popup__target__header__cancel {
  164. color: #999;
  165. padding: 0 15px;
  166. }
  167. .evan-radio-popup__target__header__title {
  168. color: #333;
  169. flex: 1;
  170. text-align: center;
  171. }
  172. .evan-radio-popup__target__header__confirm {
  173. padding: 0 15px;
  174. }
  175. .evan-radio-popup__target__body {
  176. background-color: #fff;
  177. /* #ifndef APP-NVUE */
  178. max-height: 350px;
  179. /* #endif */
  180. /* #ifdef APP-NVUE */
  181. maxHeight: 350px;
  182. /* #endif */
  183. }
  184. .evan-radio-popup__target__body__listitem {
  185. align-items: center;
  186. height: 50px;
  187. padding: 0 15px;
  188. border-bottom-width: 1px;
  189. border-bottom-style: solid;
  190. border-bottom-color: #eee;
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. box-sizing: border-box;
  194. /* #endif */
  195. flex-direction: row;
  196. }
  197. .evan-radio-popup__target__body__listitem__label {
  198. font-size: 16px;
  199. color: #333;
  200. flex: 1;
  201. margin-right: 6px;
  202. }
  203. </style>