Checkbox-Group 組件實現思路

基於 組件進行二次封裝的 CheckboxGroup 組件javascript

CheckboxGroup 組件與 RadioGroup 組件相似,可是也存在不一樣點,在於 CheckboxGroup 爲多選組,而且須要 Checkbox 能夠單獨使用。

1. 實例

最終效果

代碼html

<!-- 基礎用法 -->
<fat-checkbox-group v-model="anotherValue">
    <fat-checkbox value="あらがき ゆい">
        <fat-hover-tip type="right-center">
            <template slot="hover-part">あらがき ゆい</template>
            <template slot="tip-part">
                <img src="/static/img/gakki.jpg" style="width: 100px" alt="示意圖">
            </template>
        </fat-hover-tip>
    </fat-checkbox>
    <fat-checkbox value="石原さとみ">
        <fat-hover-tip type="right-center">
            <template slot="hover-part">石原さとみ</template>
            <template slot="tip-part">
                <img src="/static/img/u=4046980169,286278015&fm=26&gp=0.jpg" style="width: 100px" alt="示意圖" >
            </template>
        </fat-hover-tip>
    </fat-checkbox>
</fat-checkbox-group>
複製代碼

實例地址:CheckboxGroup 實例java

代碼地址:Github UI-Librarygit

2. 原理

Checkboxgithub

首先單獨實現 Checkbox 組件,它是由 labelinput 兩部分構成,主要區分兩個狀態,checked 以及 disabledapi

<template>
  <label
    :class="[ 'checkbox-item', { 'is-checked': isChecked }, { 'is-disabled': isDisabled } ]"
    @click.stop="handleClick"
  >
    <span class="checkbox-input">
      <fat-icon name="check"/>
    </span>
    
    <input
      v-if="false"
      type="checkbox"
      v-bind="$attrs"
      :value="model"
      @click.stop
    >
    
    <slot></slot>
  </label>
</template>

<script>
export default {
    props: {
        value: {
            type: [String, Number],
            required: true
        },
        disabled: {
            type: [Boolean],
            default: false
        },
        propValue: {
            type: [String, Number, Boolean]
        }
    },
    model: {
        prop: "propValue",
        event: "select"
    },
    computed: {
        isGroup() {
            return this.$parent.$options._componentTag === "fat-checkbox-group";
        },
        isDisabled() {
            return this.$parent.disabled || this.disabled;
        },
        isChecked() {
            const { isGroup, model } = this;
            if (!isGroup) return model;

            const {
                value,
                $parent: { value: selectItems }
            } = this;

            return selectItems.some(item => item === value);
        },
        model: {
            get() {
                return this.isGroup ? this.$parent.value : this.propValue;
            },
            set(newValue) {
                const { isGroup, isChecked } = this;

                if (isGroup) {
                    isChecked
                    ? this.$parent.deleteItem(newValue)
                    : this.$parent.selectItem(newValue);
                } else {
                    this.$emit("select", newValue);
                }
            }
        }
    },
    methods: {
        handleClick(event) {
            const { isDisabled, isGroup, model, value } = this;
            if (!isDisabled) {
                this.model = isGroup ? value : !model;
            }
        }
    }
};
</script>
複製代碼

Radio 同樣,因爲須要實現 Checkout 能夠單獨使用的,因此不採用 provide / inject api,而是利用 this.$parent.$options._componentTag 判斷,當前組件是否爲 Groupbash

同時,依據當前組件是否爲 Group 來定義 isDisabledisChecked 等狀態的判斷條件,例如app

isChecked() {
    const { isGroup, model } = this;
    if (!isGroup) return model;
    
    const {
        value,
        $parent: { value: selectItems }
    } = this;
    
    return selectItems.some(item => item === value);
}
複製代碼

若是當前爲多選組,就會利用 some 來判斷當前 Checkout 的 value 是否在其中,而後若是單獨使用,則直接返回 modelide

一樣,model的值也是依據 isGroup 進行區分工具

model: {
    get() {
        return this.isGroup ? this.$parent.value : this.propValue;
    },
    set(newValue) {
        const { isGroup, isChecked } = this;

        if (isGroup) {
            isChecked ? this.$parent.deleteItem(newValue)
            : this.$parent.selectItem(newValue);
        } else {
            this.$emit("select", newValue);
        }
    }
}
複製代碼

CheckboxGroup

則是在 Checkbox 的外部包裹一層 Group 用於,實現組的概念

<template>
    <div class="checkbox-group-wrapper" name="checkbox-group">
        <slot></slot>
    </div>
</template>
<script> export default { name: "checkbox-group", props: { value: { type: Array, required: true }, disabled: { type: Boolean } }, model: { prop: "value", event: "select" }, watch: { value(newValue) { this.$emit("change", newValue); } }, methods: { selectItem(item) { const { value } = this; this.$emit("select", [...value, item]); }, deleteItem(item) { const { value: selectItems } = this; this.$emit( "select", selectItems.filter(selectitem => selectitem !== item) ); } } }; </script>
複製代碼

Checkbox-Group 內維護了

methods: {
    selectItem(item) {
        const { value } = this;
        this.$emit("select", [...value, item]);
    },
    deleteItem(item) {
        const { value: selectItems } = this;
        this.$emit(
            "select",
            selectItems.filter(selectitem => selectitem !== item)
        );
    }
}
複製代碼

用於和 Checkbox 通訊,若是當前是多選組的形式,則須要利用 selectItem 以及 deleteItem 來對其進行增減。

3. 總結

Checkbox-Group 組件的開發,主要是父子組件之間的數據傳遞。

往期文章:

原創聲明: 該文章爲原創文章,轉載請註明出處。

相關文章
相關標籤/搜索