emis-app/components/tpx-select/tpx-select.vue

275 lines
6.6 KiB
Vue
Raw Normal View History

2023-12-18 16:30:33 +00:00
<template>
<view @click.stop="handleRootClick" class="" style="position: relative;">
<view @click="handleSelectSwitch">
2024-04-29 07:07:45 +00:00
<slot v-if="$slots.default"></slot>
2024-05-22 16:32:47 +00:00
<template v-else>
<default-select-input v-if="selectTextType == 'input'" :placeholder="placeholder" :show="show"
2024-05-22 17:48:52 +00:00
:value="value" :list="filDataList" :checkedList="filDatacheckedList"
2024-05-22 16:32:47 +00:00
:isWhite="isWhite" :text="text" :customStyle="customStyle"
></default-select-input>
<DefaultSelectText v-if="selectTextType == 'text'"></DefaultSelectText>
</template>
2023-12-18 16:30:33 +00:00
</view>
<view v-if="mode == modeEnum.sheet">
2024-01-29 10:05:13 +00:00
<tpx-dialog :show="show" :title="placeholder" @onClose="handleSelectSwitch">
2024-04-26 10:32:18 +00:00
<view style="height: calc(60vh);">
2024-05-24 04:40:41 +00:00
<tpx-scroll-view :resObject="filResObject" :searchParam="filDataSearchParam" :getListFun="getListFun" size="sm" :showLoadedAll="false" @onListChange="onListChange">
<view v-if="showKeyWord" class="mb-10">
<tpx-input v-model:value="keyword" placeholder="请输入关键字" @change="handleKeyWordChange"></tpx-input>
</view>
2024-05-19 05:56:23 +00:00
<content-items :value="value" :list="filDataList" :checkedList="filDatacheckedList" :minChecked="minChecked" :type="type"
2024-05-22 16:32:47 +00:00
:text="text" :vdataType="vdataType" :transKeyVal="transKeyVal"
2024-04-26 10:32:18 +00:00
@onChange="handleDataItemChange"
></content-items>
</tpx-scroll-view>
</view>
2023-12-18 16:30:33 +00:00
</tpx-dialog>
</view>
<view v-if="mode == modeEnum.drop && show" class="md-drop-cont">
2024-05-24 04:40:41 +00:00
<tpx-scroll-view :maxHeight="dropMaxHeight" :resObject="filResObject" :searchParam="filDataSearchParam" :getListFun="getListFun" size="sm" :showLoadedAll="false" @onListChange="onListChange">
<view v-if="showKeyWord" class="mb-10">
<tpx-input v-model:value="keyword" placeholder="请输入关键字" @change="handleKeyWordChange"></tpx-input>
</view>
2024-04-26 06:37:16 +00:00
<content-items :value="value" :list="filDataList" :minChecked="minChecked" :type="type"
2024-05-22 16:32:47 +00:00
:text="text" :vdataType="vdataType" :transKeyVal="transKeyVal"
2024-04-26 06:37:16 +00:00
@onChange="handleDataItemChange"
></content-items>
</tpx-scroll-view>
2023-12-18 16:30:33 +00:00
</view>
</view>
</template>
<script>
import ContentItems from "./content-items"
import DefaultSelectInput from "./default-select-input"
2024-05-22 16:32:47 +00:00
import DefaultSelectText from "./default-select-text"
2024-04-26 06:37:16 +00:00
import { transListKeyTitle } from "@/common/util"
2024-04-29 06:51:51 +00:00
import { pageEventNames } from "@/common/enum"
2023-12-18 16:30:33 +00:00
const modeEnum = {
sheet: 'sheet',
drop: 'drop'
}
const selectDropEventName = "selectDropEventName"
2023-12-18 16:30:33 +00:00
export default {
2024-04-26 06:37:16 +00:00
computed: {
filDataList() {
let list = (this.getListFun ? this.resObject.list : this.list) || []
if(this.transKeyVal) {
list = transListKeyTitle({ ...this.transKeyVal, list: list })
}
2024-05-24 04:40:41 +00:00
// if(!this.getListFun && this.keyword) {
// list = list.filter(t=> t.title?.indexOf?.(this.keyword) > -1)
// }
2024-04-26 06:37:16 +00:00
return list
},
2024-05-24 04:40:41 +00:00
filDataSearchParam() {
let filedName = this.searchFiledName
return {
...this.searchParam,
[filedName]: this.keyword,
}
},
2024-05-19 05:56:23 +00:00
filDatacheckedList() {
2024-05-23 15:57:57 +00:00
let list = (this.getListFun ? this.resObject.checkedList : this.checkedList)
if(Array.isArray(list) && this.transKeyVal) {
2024-05-19 05:56:23 +00:00
list = transListKeyTitle({ ...this.transKeyVal, list: list })
}
return list
},
2024-04-26 06:37:16 +00:00
filResObject() {
let res = this.getListFun ? this.resObject : { list: this.list, total: this.list.length }
return res
2024-05-24 04:40:41 +00:00
},
showKeyWord() {
// 查接口的列表才有搜索
let bl = !!this.getListFun
return bl
2024-04-26 06:37:16 +00:00
}
},
2023-12-18 16:30:33 +00:00
components: {
2024-05-22 16:32:47 +00:00
ContentItems, DefaultSelectInput, DefaultSelectText
2023-12-18 16:30:33 +00:00
},
props: {
2024-01-29 10:05:13 +00:00
isWhite: {
default () {
return false
}
},
customStyle: {
default () {
return ''
}
},
2023-12-18 16:30:33 +00:00
minChecked: {
default () {
return 1
}
},
type: {
default () {
2024-06-01 16:21:36 +00:00
return "single" // single | multiple
2023-12-18 16:30:33 +00:00
}
},
placeholder: {
default () {
2024-01-29 10:05:13 +00:00
return "请选择"
2023-12-18 16:30:33 +00:00
}
},
mode: {
default () {
return modeEnum.sheet
}
},
value: {
default () {
return ""
}
},
2024-04-29 02:37:22 +00:00
dropMaxHeight: {
default () {
2024-04-29 02:47:58 +00:00
return 400
2024-04-29 02:37:22 +00:00
}
},
2023-12-18 16:30:33 +00:00
list: {
default () {
return []
}
2024-04-26 06:37:16 +00:00
},
2024-05-19 05:56:23 +00:00
checkedList: {
},
2024-04-26 06:37:16 +00:00
searchParam: {
default () {
return {}
}
},
getListFun: {
default () {
return null
}
},
resObject: {
default () {
return {
list: []
}
}
},
transKeyVal: {
default () {
return null
// {
// // valKey: '',
// // titleKey: ''
// }
}
2024-05-21 18:19:58 +00:00
},
2024-05-22 16:32:47 +00:00
text: {
default () {
return ''
}
},
vdataType: {
default () {
return 'string' // string | array
}
},
2024-05-21 18:19:58 +00:00
disabled: {
default () {
return false
}
2024-05-22 16:32:47 +00:00
},
selectTextType: {
default () {
return 'input' // 选择框样式 input | text
}
},
2023-12-18 16:30:33 +00:00
},
data() {
return {
componentId: uni.$u.guid(20),
2023-12-18 16:30:33 +00:00
show: false,
2024-05-24 04:40:41 +00:00
modeEnum: Object.assign({}, modeEnum),
keyword: '',
// searchFiledName: this.transKeyVal?.['searchKey'] || '_searchValue',
searchFiledName: this.transKeyVal?.['titleKey'] || '_searchValue'
2023-12-18 16:30:33 +00:00
}
},
created() {
2024-04-29 06:51:51 +00:00
uni.$on(pageEventNames.globalClick, this.handleDocumentClick);
uni.$on(selectDropEventName, this.listenSelectDrop);
2023-12-18 16:30:33 +00:00
},
2024-01-30 03:09:14 +00:00
unmounted() {
2024-04-29 06:51:51 +00:00
uni.$off(pageEventNames.globalClick, this.handleDocumentClick);
uni.$off(selectDropEventName, this.listenSelectDrop);
2023-12-18 16:30:33 +00:00
},
methods: {
2024-01-30 03:09:14 +00:00
handleDocumentClick(){
this.show = false
},
listenSelectDrop(e) {
// 忽略当前组件发出的事件消息
if(e != this.componentId) {
this.show = false
}
},
2023-12-18 16:30:33 +00:00
handleSelectSwitch() {
2024-05-21 18:19:58 +00:00
if(this.disabled) {
return
}
2023-12-18 16:30:33 +00:00
// this.$emit('update:show', !this.show)
this.show = !this.show
},
2024-05-22 16:32:47 +00:00
handleDataItemChange(val, text, checkedList) {
this.$emit('onChange', val, text, checkedList)
2023-12-18 16:30:33 +00:00
this.$emit('update:value', val)
2024-05-22 16:32:47 +00:00
this.$emit('update:text', text)
2024-05-19 05:56:23 +00:00
this.$emit('update:checkedList', checkedList)
if(this.resObject) {
this.$emit('update:resObject', {
...this.resObject,
checkedList
})
}
2023-12-18 16:30:33 +00:00
if(this.type == "single") {
this.handleSelectSwitch()
}
2024-04-26 06:37:16 +00:00
},
onListChange(val){
this.$emit('update:resObject', val)
},
handleRootClick() {
uni.$emit(selectDropEventName, this.componentId)
2024-05-24 04:40:41 +00:00
},
handleKeyWordChange(val) {
2023-12-18 16:30:33 +00:00
}
}
}
</script>
<style scoped lang="scss">
.md-drop-cont{
position: absolute;
background-color: #fff;
border-radius: 10rpx;
border: 2rpx solid #ebeef5;;
top: calc(100% + 8px);
left: 0;
right: 0;
z-index: 10;
padding: 20rpx;
box-sizing: border-box;
border-radius: 6px;
box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}
</style>