WMS_PDA/components/inputAndSelect/inputAndSelect.vue

325 lines
6.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view>
<u-popup :show="show" mode="bottom" :closeable="true" border-radius="20" @close="handleClose">
<view class="title">
{{title}}
<view class="clear-btn" @click="handleClear">清除</view>
</view>
<view class="popup-content">
<!-- 搜索框 -->
<view class="search-box">
<u-input
v-model="searchText"
placeholder="请输入关键词搜索"
:border="searchBorder"
prefixIcon="search"
prefixIconStyle="color: #909399; font-size: 18px"
clearable
@input="handleSearch"
></u-input>
</view>
<!-- 底部列表 -->
<scroll-view
class="list-container"
scroll-y="true"
:style="{ height: listHeight + 'rpx' }"
>
<view
v-if="showCreateAction"
class="list-item list-item--create"
@click="handleCreate"
>
<text class="item-text item-text--create">新增“{{ normalizedSearchText }}”</text>
<u-icon name="plus" color="#2979ff" size="22"></u-icon>
</view>
<view class="list-item"
v-for="item in filteredList"
:key="item.value"
:class="{ active: selectedValue === item.value }"
@click="handleSelect(item)"
>
<text class="item-text" :style="{ fontSize: itemFontSize + 'rpx' }">{{ item.label }}</text>
<u-icon
v-if="selectedValue === item.value"
name="checkbox-mark"
color="#2979ff"
size="20"
></u-icon>
</view>
<u-empty
v-if="filteredList.length === 0"
mode="data"
icon="http://cdn.uviewui.com/uview/empty/data.png"
text="暂无数据"
></u-empty>
</scroll-view>
</view>
</u-popup>
</view>
</template>
<script>
export default {
name:"inputAndSelect",
props: {
show: Boolean,
list: {
type: Array,
default: () => []
},
title:[String],
value: [String, Number],
// 列表高度,单位rpx
listHeight: {
type: Number,
default: 600
},
itemFontSize: {
type: Number,
default: 28
},
allowCreate: {
type: Boolean,
default: false
},
searchBorder: {
type: String,
default: 'surround'
}
},
computed: {
normalizedSearchText() {
return String(this.searchText || '').trim();
},
showCreateAction() {
if (!this.allowCreate || !this.normalizedSearchText) {
return false;
}
return !this.list.some(item => {
if (!item || typeof item !== 'object') {
return false;
}
const label = String(item.label || '').trim().toLowerCase();
return label === this.normalizedSearchText.toLowerCase();
});
}
},
data() {
return {
searchText: '',
selectedValue: null,
filteredList: []
};
},
watch: {
show(newVal) {
if (newVal) {
this.selectedValue = this.value;
this.filteredList = [...this.list];
this.searchText = '';
}
},
list: {
immediate: true,
handler(newVal) {
this.filteredList = [...newVal];
}
},
value(newVal) {
this.selectedValue = newVal;
}
},
methods: {
normalizeInputText(raw) {
if (typeof raw === 'string') {
return raw;
}
if (typeof raw === 'number') {
return String(raw);
}
if (raw && typeof raw === 'object') {
if (typeof raw.detail === 'string') {
return raw.detail;
}
if (raw.detail && typeof raw.detail.value === 'string') {
return raw.detail.value;
}
if (raw.target && typeof raw.target.value === 'string') {
return raw.target.value;
}
if (typeof raw.value === 'string') {
return raw.value;
}
}
return '';
},
handleClose() {
this.$emit('close');
},
handleSearch(raw) {
if (typeof raw !== 'undefined') {
const nextText = this.normalizeInputText(raw);
if (nextText !== this.searchText) {
this.searchText = nextText;
}
}
if (!this.searchText.trim()) {
this.filteredList = [...this.list];
return;
}
const searchText = this.searchText.toLowerCase();
this.filteredList = this.list.filter(item =>
item.label && item.label.toLowerCase().includes(searchText)
);
},
handleSelect(item) {
this.selectedValue = item.value;
// 回传选中的值
this.$emit('change', item);
// 关闭弹窗
this.$emit('close');
},
handleClear() {
this.selectedValue = null;
// 回传清除操作,可以传null或空对象
this.$emit('change', null);
// 关闭弹窗
this.$emit('close');
},
handleCreate() {
const value = this.normalizedSearchText;
if (!value) {
return;
}
this.$emit('create', value);
this.$emit('close');
}
}
}
</script>
<style lang="scss" scoped>
.title {
height: 96rpx;
line-height: 96rpx;
text-align: center;
background-color: #FFF;
color: #000;
margin-bottom: 20rpx;
border-bottom: 1px solid rgba(0,0,0,.3);
position: relative;
.clear-btn {
position: absolute;
left: 30rpx;
top: 0;
color: #2979ff;
font-size: 28rpx;
height: 100%;
line-height: 96rpx;
}
}
.popup-content {
padding: 30rpx;
background-color: #fff;
}
.search-box {
margin-bottom: 30rpx;
background-color: #f5f7fa;
border-radius: 10rpx;
padding: 0 20rpx;
min-height: 84rpx;
display: flex;
align-items: center;
::v-deep .u-input {
height: 76rpx;
}
::v-deep .u-input__content {
height: 76rpx;
align-items: center;
}
}
.quick-filter {
margin-bottom: 20rpx;
white-space: nowrap;
.filter-tags {
display: flex;
flex-wrap: nowrap;
padding: 10rpx 0;
}
.filter-tag {
padding: 12rpx 24rpx;
margin-right: 20rpx;
background-color: #f5f7fa;
border-radius: 50rpx;
font-size: 26rpx;
color: #606266;
flex-shrink: 0;
&.active {
background-color: #ecf5ff;
color: #2979ff;
font-weight: bold;
}
}
}
.list-container {
border-top: 2rpx solid #f0f0f0;
padding-top: 20rpx;
.list-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 20rpx;
border-bottom: 1rpx solid #f5f5f5;
.item-text {
font-size: 28rpx;
color: #333;
}
&:active {
background-color: #f5f7fa;
}
&.active {
.item-text {
color: #2979ff;
font-weight: bold;
}
}
&:last-child {
border-bottom: none;
}
}
.list-item--create {
background: #f4f8ff;
}
.item-text--create {
color: #2979ff !important;
font-weight: 600;
}
}
</style>