欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

element-ui switch组件源码分析整理笔记(二)

程序员文章站 2022-07-05 16:06:04
源码如下: 解析: (1)组件的html结构 input标签被隐藏掉了,css部分代码如下: 如果把上面的样式代码注释掉,如图所示: 通过 javascript getMigratingConfig() { return { props: { 'on color': 'on color is ren ......

源码如下:

<template>
  <div
    class="el-switch"
    :class="{ 'is-disabled': switchdisabled, 'is-checked': checked }"
    role="switch"
    :aria-checked="checked"
    :aria-disabled="switchdisabled"
    @click="switchvalue"
  >
    <input
      class="el-switch__input"
      type="checkbox"
      @change="handlechange"
      ref="input"
      :id="id"
      :name="name"
      :true-value="activevalue"
      :false-value="inactivevalue"
      :disabled="switchdisabled"
      @keydown.enter="switchvalue"
    >

    <span
      :class="['el-switch__label', 'el-switch__label--left', !checked ? 'is-active' : '']"
      v-if="inactiveiconclass || inactivetext">
      <i :class="[inactiveiconclass]" v-if="inactiveiconclass"></i>
      <span v-if="!inactiveiconclass && inactivetext" :aria-hidden="checked">{{ inactivetext }}</span>
    </span>

    <span class="el-switch__core" ref="core" :style="{ 'width': corewidth + 'px' }"></span>

    <span
      :class="['el-switch__label', 'el-switch__label--right', checked ? 'is-active' : '']"
      v-if="activeiconclass || activetext">
      <i :class="[activeiconclass]" v-if="activeiconclass"></i>
      <span v-if="!activeiconclass && activetext" :aria-hidden="!checked">{{ activetext }}</span>
    </span>
  </div>
</template>
<script>
  import focus from 'element-ui/src/mixins/focus';
  import migrating from 'element-ui/src/mixins/migrating';

  export default {
    name: 'elswitch',
    mixins: [focus('input'), migrating],
    //  注入elform对象,防止不和el-form使用时对象不存在的问题。
    inject: {
      elform: {
        default: ''
      }
    },
    props: {
      value: {
        type: [boolean, string, number],
        default: false
      },
      disabled: { //是否禁用
        type: boolean,
        default: false
      },
      width: { //switch 的宽度(像素)
        type: number,
        default: 40
      },
      activeiconclass: { //switch 打开时所显示图标的类名,设置此项会忽略 active-text
        type: string,
        default: ''
      },
      inactiveiconclass: { //switch 关闭时所显示图标的类名,设置此项会忽略 inactive-text
        type: string,
        default: ''
      },
      activetext: string, //switch 打开时的文字描述
      inactivetext: string, //switch 关闭时的文字描述
      activecolor: { //switch 打开时的背景色
        type: string,
        default: ''
      },
      inactivecolor: { //switch 关闭时的背景色
        type: string,
        default: ''
      },
      activevalue: { //switch 打开时的值
        type: [boolean, string, number],
        default: true
      },
      inactivevalue: { //switch 关闭时的值
        type: [boolean, string, number],
        default: false
      },
      name: { //switch 对应的 name 属性
        type: string,
        default: ''
      },
      id: string
    },
    data() {
      return {
        corewidth: this.width
      };
    },
    created() {
      if (!~[this.activevalue, this.inactivevalue].indexof(this.value)) {
        this.$emit('input', this.inactivevalue);
      }
    },
    computed: {
        //当前的开关组件的状态
        checked() {
            //父组件中v-model绑定的值是否等于switch 打开时的值
            return this.value === this.activevalue;
        },
        //当前组件是否被禁用
      switchdisabled() {
        return this.disabled || (this.elform || {}).disabled;
      }
    },
    watch: {
      checked() {
        this.$refs.input.checked = this.checked;
        //在用户设置了active-color和inactive-color时,通过setbackgroundcolor设置开关的背景色
        if (this.activecolor || this.inactivecolor) {
          this.setbackgroundcolor();
        }
      }
    },
    methods: {
      handlechange(event) {
        //!this.checked为true,则表示当前是this.value === this.inactivevalue,即为关着的状态;需要切换为开着的状态,返回this.activevalue
        this.$emit('input', !this.checked ? this.activevalue : this.inactivevalue);
        this.$emit('change', !this.checked ? this.activevalue : this.inactivevalue);
        this.$nexttick(() => {
            //修改value值并不是立即生效,而且为了防止父组件未修改值,这里进行了重复赋值
          this.$refs.input.checked = this.checked;
        });
      },
      //在用户设置了active-color和inactive-color时,点击切换开关时,根据this.checked的值设置开关的背景颜色
      setbackgroundcolor() {
        //如果 this.checked为true,即当前switch是打开,开关返回打开时设置的背景色
        let newcolor = this.checked ? this.activecolor : this.inactivecolor;
        this.$refs.core.style.bordercolor = newcolor;
        this.$refs.core.style.backgroundcolor = newcolor;
      },
      switchvalue() {
        //在不禁用的状态下才能点击
        !this.switchdisabled && this.handlechange();
      },
      getmigratingconfig() {
        return {
          props: {
            'on-color': 'on-color is renamed to active-color.',
            'off-color': 'off-color is renamed to inactive-color.',
            'on-text': 'on-text is renamed to active-text.',
            'off-text': 'off-text is renamed to inactive-text.',
            'on-value': 'on-value is renamed to active-value.',
            'off-value': 'off-value is renamed to inactive-value.',
            'on-icon-class': 'on-icon-class is renamed to active-icon-class.',
            'off-icon-class': 'off-icon-class is renamed to inactive-icon-class.'
          }
        };
      }
    },
    mounted() {
      /* istanbul ignore if */
      this.corewidth = this.width || 40;
      if (this.activecolor || this.inactivecolor) {
        this.setbackgroundcolor();
      }
      this.$refs.input.checked = this.checked;
    }
  };
</script>

解析:
(1)组件的html结构

<div class="el-switch">
        <input class="el-switch__input" type="checkbox">
        <!--显示左边的标签-->
        <span class="el-switch__label el-switch__label--left">
             <i></i>
             <span></span>
        </span>
        <!--中间的开关-->
        <span class="el-switch__core"></span>
        <!--显示右边的标签-->
        <span class="el-switch__label el-switch__label--right">
            <i></i>
            <span></span>
         </span>
</div>

input标签被隐藏掉了,css部分代码如下:

.el-switch__input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    margin: 0;
}

如果把上面的样式代码注释掉,如图所示:
element-ui switch组件源码分析整理笔记(二)
通过 <input type="checkbox"> 的checked属性来控制文字显示以及开关的状态切换。最外层包裹的div是为了能够通过点击文字也能切换开关状态。

(2)混入的 mixins: [focus('input'), migrating]

主要是migrating.js,该文件主要是用于开发环境下提示一些迁移或者即将修改的属性和方法的。
示例:我用的element-ui v2.4.9,我按下面这样写,off-text属性在我当前的版本中已经被改为inactive-text

<el-switch v-model="value2" off-text="关着"></el-switch>当我运行之后在控制台输出:

element-ui switch组件源码分析整理笔记(二)

所有迁移的属性在组件的getmigratingconfig()方法中:

 getmigratingconfig() {
        return {
          props: {
            'on-color': 'on-color is renamed to active-color.',
            'off-color': 'off-color is renamed to inactive-color.',
            'on-text': 'on-text is renamed to active-text.',
            'off-text': 'off-text is renamed to inactive-text.',
            'on-value': 'on-value is renamed to active-value.',
            'off-value': 'off-value is renamed to inactive-value.',
            'on-icon-class': 'on-icon-class is renamed to active-icon-class.',
            'off-icon-class': 'off-icon-class is renamed to inactive-icon-class.'
          }
        };
      }

(3)created方法

 created() {
    //如果用户传入的v-model的值既不是activevalue也不是inactivevalue时,将inactivevalue传递出去,开关处于关状态
      if (!~[this.activevalue, this.inactivevalue].indexof(this.value)) {
        this.$emit('input', this.inactivevalue);
      }
    },

~代表按位非运算符,如果[this.activevalue, this.inactivevalue].indexof(this.value)为-1,则按位非后变为0。

参考博文:
http://www.zhuyuntao.cn/2018/10/24/element-ui-focus-js和migrating.js文件源码学习