From 7c1a90a9df1fd2897440a854c5f0897db78e7322 Mon Sep 17 00:00:00 2001 From: fredfhu Date: Fri, 16 Aug 2024 16:18:10 +0800 Subject: [PATCH] feat: v0.4.0 --- README.md | 12 +++- package.json | 2 +- src/app.vue | 37 +++++++---- src/components/ElSelectAll.vue | 115 ++++++++++++++++++++++----------- 4 files changed, 114 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 9d27a87..fe6661c 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,16 @@ Vue.use(ElSelectAll) >组件用例 ``` - - + + + + +部分api:说明: +withAll: 是否默认显示全部选项 +options:选项列表 +props:自定义label和value的名称 +selectAll:是否默认选中全部 +size:指定尺寸 ``` ###### 预览地址: diff --git a/package.json b/package.json index 2df5e4f..8701354 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "el-select-all", - "version": "0.3.0", + "version": "0.4.0", "description": "带有全部选项的el-select组件", "scripts": { "serve": "vue-cli-service serve", diff --git a/src/app.vue b/src/app.vue index f29230b..91b4d22 100644 --- a/src/app.vue +++ b/src/app.vue @@ -1,27 +1,39 @@ - diff --git a/src/components/ElSelectAll.vue b/src/components/ElSelectAll.vue index e6e94de..aaaf8e1 100644 --- a/src/components/ElSelectAll.vue +++ b/src/components/ElSelectAll.vue @@ -1,6 +1,6 @@ @@ -12,18 +12,32 @@ export default { type: Array, default: () => { return [] - } + }, }, options: { type: Array, default: () => { return [] - } + }, }, - widthAll: { // Add the all prop + withAll: { type: Boolean, - default: true - } + default: true, + }, + selectAll: { + type: Boolean, + default: false, + }, + props: { + type: Object, + default: () => { + return { label: 'label', value: 'value' } + }, + }, + size: { + type: String, + default: 'small', + }, }, data() { const selected = this.value || [] @@ -31,7 +45,7 @@ export default { selected, mdoptionsValue: [], oldMdoptionsValue: [], - mdoptionsList: [] + mdoptionsList: [], } }, computed: { @@ -39,7 +53,7 @@ export default { // const val = this.$vnode.data.model && this.$vnode.data.model.value; const result = { // value: val, - ...this.$attrs + ...this.$attrs, } return result }, @@ -47,27 +61,36 @@ export default { const _this = this return Object.assign({}, this.$listeners, { change: () => { - this.$emit('change', (_this.selected || []).filter(v => { - return v !== 'all' - })) + this.$emit( + 'change', + (_this.selected || []).filter(v => { + return v !== 'all' + }) + ) }, input: () => { - this.$emit('input', (_this.selected || []).filter(v => { - return v !== 'all' - })) - } - }); - } + this.$emit( + 'input', + (_this.selected || []).filter(v => { + return v !== 'all' + }) + ) + }, + }) + }, }, watch: { selected: { immediate: true, deep: true, handler(val) { - this.$emit('input', (val || []).filter(v => { - return v !== 'all' - })) - } + this.$emit( + 'input', + (val || []).filter(v => { + return v !== 'all' + }) + ) + }, }, options: { immediate: true, @@ -76,16 +99,33 @@ export default { if (!val || val.length === 0) { this.mdoptionsList = [] } else { - this.mdoptionsList = this.widthAll ? [{ - key: 'all', - value: 'all', - label: '全部' - }, ...val] : [...val]; + this.mdoptionsList = this.withAll + ? [ + { + [this.props.value]: 'all', + [this.props.label]: '全部', + }, + ...val, + ] + : [...val] } - } - } + }, + }, }, mounted() { + if (this.selectAll) { + this.selected = [] + for (const item of this.mdoptionsList) { + this.selected.push(item[this.props.value]) + } + this.oldMdoptionsValue[1] = this.selected + this.$emit( + 'input', + (this.selected || []).filter(v => { + return v !== 'all' + }) + ) + } }, methods: { onChange(val) { @@ -93,7 +133,7 @@ export default { const allValues = [] // 保留所有值 for (const item of this.mdoptionsList) { - allValues.push(item.value) + allValues.push(item[this.props.value]) } // 用来储存上一次的值,可以进行对比 const oldVal = this.oldMdoptionsValue.length === 1 ? [] : this.oldMdoptionsValue[1] || [] @@ -110,14 +150,17 @@ export default { } // 全选未选 但是其他选项全部选上 则全选选上 上次和当前 都没有全选 if (!oldVal.includes('all') && !val.includes('all')) { - if (val.length === allValues.length - 1) this.selected = (this.widthAll ? ['all'] : []).concat(val) + if (val.length === allValues.length - 1) this.selected = (this.withAll ? ['all'] : []).concat(val) } - this.$emit('input', (this.selected || []).filter(v => { - return v !== 'all' - })) + this.$emit( + 'input', + (this.selected || []).filter(v => { + return v !== 'all' + }) + ) // 储存当前最后的结果 作为下次的老数据 this.oldMdoptionsValue[1] = this.selected - } - } + }, + }, }