Compare commits

...

2 Commits

Author SHA1 Message Date
qiaopengfei b86eb26f55 Merge pull request '组件' (#11) from qiaopengfei into master
Reviewed-on: #11
2024-07-03 16:34:17 +08:00
qiaopengfei 6834d27ca8 组件 2024-07-03 16:33:39 +08:00
1 changed files with 70 additions and 185 deletions

View File

@ -1,217 +1,102 @@
<template> <template>
<div <el-select
@mousedown=" class="ag_select"
(e) => { :style="{ width }"
e.preventDefault(); :value="value"
selectOpen = true; :size="size"
} v-on="inputListeners"
" v-bind="attrs"
ref="main"
> >
<a-select <el-option
class="t_select" v-for="(item, index) in options"
v-model="childSelectedValue" :key="index"
:style="{ width: width || '100%' }" :label="item.label"
:placeholder="placeholder" :value="item.value"
:open="selectOpen" ></el-option>
@select="handleSelect" <!-- <slot name="options" slot="options" /> -->
v-bind="attrs" </el-select>
v-on="$listeners"
:mode="mode"
>
<template v-for="(index, name) in $slots" v-slot:[name]>
<slot :name="name" />
</template>
<template v-for="(index, name) in $scopedSlots" v-slot:[name]="data">
<slot :name="name" v-bind="data"></slot>
</template>
<div slot="dropdownRender" slot-scope="menu">
<a-checkbox
v-if="mode && !isShowPagination"
:checked="selectChecked"
@change="selectAll"
class="all_checkbox"
>全选</a-checkbox
>
<v-nodes :vnodes="menu" />
<div class="t_select__pagination" v-if="isShowPagination">
<a-pagination
:page-size.sync="paginationOption.pageSize"
v-model="paginationOption.current"
:total="paginationOption.total"
@change="currentChange"
v-bind="{
size: 'small',
'hide-on-single-page': true,
showQuickJumper: true,
...$attrs,
...paginationOption.bind,
}"
v-on="$listeners"
/>
</div>
</div>
<a-select-option
v-for="(item, index) in optionSource"
:key="index"
:value="item[valueKey]"
>{{
customLabel ? customLabelHandler(item) : item[labelKey]
}}</a-select-option
>
</a-select>
</div>
</template> </template>
<script> <script>
import cloneDeep from "../../../src/utils/cloneDeep";
export default { export default {
name: "TAntdSelect", name: "agSelect",
components: {
VNodes: {
functional: true,
render: (h, ctx) => ctx.props.vnodes,
},
},
props: { props: {
value: {
type: [String, Number, Array, Boolean, Object],
},
// 'multiple'
mode: {
type: String,
},
placeholder: {
type: String,
default: "请选择",
},
//
width: { width: {
type: String, type: String,
default: "",
}, },
// label value: {
customLabel: {
type: String, type: String,
default: "",
}, },
// optionkey size: {
valueKey: {
type: String, type: String,
default: "key", default: "small",
},
// option
labelKey: {
type: String,
default: "label",
},
//
optionSource: {
type: Array,
},
//
isShowPagination: {
type: Boolean,
default: false,
},
//
paginationOption: {
type: Object,
default: () => {
return {
pageSize: 6, //
current: 1, //
total: 0, //
};
},
}, },
}, },
data() { data() {
return { return {
selectOpen: false, model: "",
options: [],
}; };
}, },
computed: { computed: {
childSelectedValue: {
get() {
return this.value || undefined;
},
set(val) {
this.$emit("input", val);
},
},
attrs() { attrs() {
return { if (this.$attrs["remote-method"]) {
allowClear: true, let obj = cloneDeep(this.$attrs);
showSearch: true, Reflect.deleteProperty(obj, "remote-method");
...this.$attrs, return { ...obj, clearable: true, filterable: true, remote: true };
}; }
return false;
}, },
selectChecked: { //
get() { inputListeners() {
return this.childSelectedValue?.length === this.optionSource?.length; return Object.assign(
}, {},
set(val) { //
// console.log('set', val) this.$listeners,
this.$emit("input", val); //
}, //
{
// `v-model`
input: (value) => {
this.$emit("input", this.toUpperCase ? value.toUpperCase() : value);
},
blur: (e) => {
let value = e.target.value
.trim()
.replace(/\s/g, (match) =>
match.charCodeAt(0) === 12288 ? String.fromCharCode(32) : match
);
//
this.$emit("input", value);
},
}
);
}, },
}, },
mounted() { watch: {
document.addEventListener("click", this.bodyCloseMenus); value: {
}, immediate: true,
beforeDestroy() { handler(newVal) {
document.removeEventListener("click", this.bodyCloseMenus); this.removeMethod(newVal);
this.model = newVal;
},
},
}, },
methods: { methods: {
// async removeMethod(keywords) {
bodyCloseMenus(e) { if (this.$attrs["remote-method"]) {
if (this.$refs.main && !this.$refs.main.contains(e.target)) { let opts = await this.$attrs["remote-method"](keywords);
if (this.selectOpen == true) { this.options = opts;
this.selectOpen = false;
}
} }
}, },
// handleChange(value) {
selectAll(val) { this.$emit("input", value);
const options = JSON.parse(JSON.stringify(this.optionSource));
if (val.target.checked) {
this.childSelectedValue = options?.map((item) => {
return item[this.valueKey];
});
setTimeout(() => {
this.$emit("change", this.childSelectedValue);
}, 0);
} else {
this.childSelectedValue = null;
}
this.selectOpen = false;
},
handleSelect(value, option) {
if (value) {
this.selectOpen = false;
}
this.$emit("select", value, option);
},
//
currentChange(val) {
// console.log('', val)
if (!this.mode) {
this.childSelectedValue = null;
}
setTimeout(() => {
this.selectOpen = true;
}, 0);
this.$emit("current-change", val);
},
// label
customLabelHandler() {
// eslint-disable-next-line no-eval
return eval(this.customLabel);
}, },
}, },
}; };
</script> </script>
<style >
.all_checkbox {
margin-left: 12px;
margin-top: 5px;
}
</style>
<style lang="scss" scoped></style>