文章目錄
- 1.最終效果預覽
- 2.快速選擇組件封裝
- 3.彈框組件封裝
- 4.組件邏輯實現
- 5.組件樣式
- 6.頁面引入
1.最終效果預覽
2.快速選擇組件封裝
<uv-cell :border="isShowBorder"><template v-slot:title><text class="title-key">{{ title }}</text></template><template v-slot:label><text style="font-size: 28rpx;color: #999;">{{content}}</text></template><template v-slot:value><view class="content"><view class="content-tag" v-for="(item, index) in showList" :key="index"><uv-tags :text="item.name" :plain="!item.checked" :name="index" shape="circle"@click="handleTagClick(index)"></uv-tags></view></view></template><template v-slot:right-icon><view class="content-tag" v-if="showList.length < list.length"><uv-icon size="30rpx" name="arrow-right" @click="showMoreOptions"></uv-icon></view></template></uv-cell>
基于 uv-ui 的行組件實現的快速選擇,默認展示前三個值
3.彈框組件封裝
<uv-popup ref="showPopup" mode="top"><view class="popup-content"><view class="popup-title">請選擇</view><uv-radio-group v-model="selectedIndex" @change="handleRadioChange"><view v-for="(item, index) in list" :key="index" style="margin-right: 10px;"><uv-radio :name="index">{{ item.name }}</uv-radio></view></uv-radio-group><view class="popup-actions"><uv-button type="primary" @click="confirmSelection">確定</uv-button><uv-button @click="closePopup">取消</uv-button></view></view></uv-popup>
這兩個放一個頁面了,沒必要再單獨封裝一個彈框組件了
4.組件邏輯實現
import {defineProps,defineEmits,computed,ref} from 'vue'const props = defineProps({isShowBorder: {type: Boolean,required: true},title: {type: String,required: true},content: {type: String,required: true},list: {type: Array,required: true},maxShow: {type: Number,default: 3}})const emit = defineEmits(['tagClick', 'tagMoreClick', 'selectionConfirmed'])const showList = computed(() => {return props.list.slice(0, props.maxShow)})const allList = computed(() => {return props.list})const handleTagClick = (index) => {emit('tagClick', index)}const handleMoreClick = () => {emit('tagMoreClick')}const showPopup = ref()const selectedIndex = ref({})const showMoreOptions = () => {showPopup.value.open()}const handleRadioChange = (index) => {selectedIndex.value = index}const confirmSelection = () => {emit('selectionConfirmed', selectedIndex.value)closePopup()}const closePopup = () => {showPopup.value.close()}
5.組件樣式
.title-key {width: 150rpx;}.content {display: flex;flex-wrap: wrap;}.content-tag {margin: 0 0 10rpx 10rpx;}.popup-content {padding: 20rpx;}.popup-title {font-size: 32rpx;font-weight: bold;margin-bottom: 20rpx;}.popup-actions {display: flex;justify-content: space-evenly;margin-top: 20rpx;}
6.頁面引入
import RowSel from '@/components/rowSel.vue'<RowSel :isShowBorder="true" :title="'維修大類'" :content="largeContent" :list="wxLargeAllList"@tagClick="radio1Click" @selectionConfirmed="tagMore1Click">
</RowSel>
<RowSel :title="'維修小類'" :content="smallContent" :list="wxSmallAllList" @tagClick="radio2Click"@selectionConfirmed="tagMore2Click">
</RowSel>const radio1Click = (index) => {}const radio2Click = (index) => {}const tagMore1Click = (index) => {radio1Click(index)}const tagMore2Click = (index) => {radio2Click(index)}
在點擊事件中實現自己的邏輯選擇即可