bug
jiangping
2023-11-07 64b432916af9c9218ab3f3eca614e26c542142ae
minipro_standard/uni_modules/uview-ui/components/u-radio-group/u-radio-group.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,108 @@
<template>
   <view
       class="u-radio-group"
       :class="bemClass"
   >
      <slot></slot>
   </view>
</template>
<script>
   import props from './props.js';
   /**
    * radioRroup å•选框父组件
    * @description å•选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
    * @tutorial https://www.uviewui.com/components/radio.html
    * @property {String | Number | Boolean}   value          ç»‘定的值
    * @property {Boolean}                  disabled      æ˜¯å¦ç¦ç”¨æ‰€æœ‰radio(默认 false ï¼‰
    * @property {String}                  shape         å¤–观形状,shape-方形,circle-圆形(默认 circle )
    * @property {String}                  activeColor      é€‰ä¸­æ—¶çš„颜色,应用到所有子Radio组件(默认 '#2979ff' ï¼‰
    * @property {String}                  inactiveColor   æœªé€‰ä¸­çš„颜色 (默认 '#c8c9cc' )
    * @property {String}                  name         æ ‡è¯†ç¬¦
    * @property {String | Number}            size         ç»„件整体的大小,单位px(默认 18 ï¼‰
    * @property {String}                  placement      å¸ƒå±€æ–¹å¼ï¼Œrow-横向,column-纵向 ï¼ˆé»˜è®¤ 'row' ï¼‰
    * @property {String}                  label         æ–‡æœ¬
    * @property {String}                  labelColor      label的颜色 ï¼ˆé»˜è®¤ '#303133' ï¼‰
    * @property {String | Number}            labelSize      label的字体大小,px单位 ï¼ˆé»˜è®¤ 14 ï¼‰
    * @property {Boolean}                  labelDisabled   æ˜¯å¦ç¦æ­¢ç‚¹å‡»æ–‡æœ¬æ“ä½œcheckbox(默认 false )
    * @property {String}                  iconColor      å›¾æ ‡é¢œè‰² ï¼ˆé»˜è®¤ '#ffffff' ï¼‰
    * @property {String | Number}            iconSize      å›¾æ ‡çš„大小,单位px ï¼ˆé»˜è®¤ 12 ï¼‰
    * @property {Boolean}                  borderBottom   placement为row时,是否显示下边框 ï¼ˆé»˜è®¤ false ï¼‰
    * @property {String}                  iconPlacement   å›¾æ ‡ä¸Žæ–‡å­—的对齐方式 ï¼ˆé»˜è®¤ 'left' ï¼‰
     * @property {Object}                  customStyle      ç»„件的样式,对象形式
    * @event {Function} change ä»»ä¸€ä¸ªradio状态发生变化时触发
    * @example <u-radio-group v-model="value"></u-radio-group>
    */
   export default {
      name: 'u-radio-group',
      mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
      computed: {
         // è¿™é‡Œcomputed的变量,都是子组件u-radio需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
         // æ‰€ä»¥éœ€è¦æ‰‹åŠ¨é€šçŸ¥å­ç»„ä»¶ï¼Œè¿™é‡Œè¿”å›žä¸€ä¸ªparentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-radio-group)
         // æ‹‰å–父组件新的变化后的参数
         parentData() {
            return [this.value, this.disabled, this.inactiveColor, this.activeColor, this.size, this.labelDisabled, this.shape,
               this.iconSize, this.borderBottom, this.placement
            ]
         },
         bemClass() {
            // this.bem为一个computed变量,在mixin中
            return this.bem('radio-group', ['placement'])
         },
      },
      watch: {
         // å½“父组件需要子组件需要共享的参数发生了变化,手动通知子组件
         parentData() {
            if (this.children.length) {
               this.children.map(child => {
                  // åˆ¤æ–­å­ç»„ä»¶(u-radio)如果有init方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
                  typeof(child.init) === 'function' && child.init()
               })
            }
         },
      },
      data() {
         return {
         }
      },
      created() {
         this.children = []
      },
      methods: {
         // å°†å…¶ä»–çš„radio设置为未选中的状态
         unCheckedOther(childInstance) {
            this.children.map(child => {
               // æ‰€æœ‰å­radio中,被操作组件实例的checked的值无需修改
               if (childInstance !== child) {
                  child.checked = false
               }
            })
            const {
               name
            } = childInstance
            // é€šè¿‡emit事件,设置父组件通过v-model双向绑定的值
            this.$emit('input', name)
            // å‘出事件
            this.$emit('change', name)
         },
      }
   }
</script>
<style lang="scss" scoped>
   @import "../../libs/css/components.scss";
   .u-radio-group {
      flex: 1;
      &--row {
         @include flex;
      }
      &--column {
         @include flex(column);
      }
   }
</style>