uniapp的小程序历史搜索组件 开箱即用

javascript 复制代码
<template>
    <view v-if="isPopupVisible" class="history-down-popup-mask" :style="maskStyle" @click="startHideAnimation">
        <view class="history-down-popup-content" :class="{ 'history-slide-down': value && !isClosing, 'history-slide-up': isClosing }"
            @click.stop>
            <view class="history-history-keyword">
                <view class="history-history-title space-between" @click="startHideAnimation">
                        <text>历史搜索</text>
                        <text class="qh-rt-single qh-rt-a-zu4416 close-popup-icon" style="font-size: 22rpx;" ></text>
                </view>
                <view class="history-history-tag-list">
                    <view v-for="(item, index) in historyList" :key="index" @tap="clickTaghandler(item)">{{ item }}
                    </view>
                </view>
                <view class="history-bottom" v-if="historyList.length > 0" >
                    <text @tap="clearHistory" class="feiIconfont icon-shanchu" ></text>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
//本地的key
const HISTORY_KEY = 'history_list'

export default {
    name: 'HistoryPopup',
    props: {
        value: {
            type: Boolean,
            default: false
        },
        topDistance: {
            type: [Number, String],
            default: 0
        }
    },
    computed: {
        maskStyle() {
            return {
                top: `${this.topDistance}rpx`,
                height: `calc(100vh - ${this.topDistance}rpx)`
            }
        }
    },
    data() {
        return {
            historyList: [],
            isPopupVisible: false,
            isClosing: false
        }
    },
    watch: {
        value: {
            immediate: true,
            handler(newVal) {
                if (newVal) {
                    this.isPopupVisible = true
                    this.isClosing = false
                } else {
                    this.startHideAnimation()
                }
            }
        }
    },
    created() {
        this.loadHistory()
    },
    methods: {
        startHideAnimation() {
            if (!this.isPopupVisible) return
            this.isClosing = true
            setTimeout(() => {
                this.isPopupVisible = false
                this.isClosing = false
                this.$emit('input', false)
            }, 300)
        },
        loadHistory() {
            try {
                const history = uni.getStorageSync(HISTORY_KEY)
                if (history) {
                    this.historyList = history
                }
            } catch (e) {
                console.error('获取搜索历史失败:', e)
            }
        },

        saveSearchHistory(keyword) {
            if (!keyword.trim())return
            try {
                let history = this.historyList.filter(item => item !== keyword)
                history.unshift(keyword)
                history = history.slice(0, 10)

                this.historyList = history
                uni.setStorageSync(HISTORY_KEY, history)
            } catch (e) {
                console.error('保存搜索历史失败:', e)
            }
        },

        clearHistory() {
            try {
                this.historyList = []
                uni.removeStorageSync(HISTORY_KEY)
                uni.showToast({
                    title: '已清空历史记录',
                    icon: 'none'
                })
                this.startHideAnimation()
                // this.$parent.checkHistory()
            } catch (e) {
                console.error('清除搜索历史失败:', e)
                uni.showToast({
                    title: '清除历史失败',
                    icon: 'none'
                })
            }
        },

        clickTaghandler(content) {
            this.$emit('doSearch', {
                value: content
            })
        }
    }
}
</script>

<style lang="scss">
.history-bottom {
    display: flex;  
    justify-content: flex-end;
    text:last-child {
        font-size: 22px;
    }
}
.history-down-popup-mask {
    position: fixed;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 99;
}

.history-down-popup-content {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: white;
    z-index: 1000;
    border-radius: 0 0 30rpx 30rpx;
    transform: translateY(-100%);
}

.history-slide-down {
    animation: slide-down 0.3s ease-out forwards;
}

.history-slide-up {
    animation: slide-up 0.3s ease-in forwards;
}

@keyframes slide-down {
    0% {
        transform: translateY(-100%);
    }

    100% {
        transform: translateY(0);
    }
}

@keyframes slide-up {
    0% {
        transform: translateY(0);
    }

    100% {
        transform: translateY(-100%);
    }
}

.history-history-keyword {
    padding: 30rpx;

    .history-history-title {
        font-size: 30rpx;
        color: #222222;
        display: flex;
        justify-content: space-between;
        align-items: center;

        text:last-child {
            color: #999;
        }
    }

    .history-history-tag-list {
        display: flex;
        flex-wrap: wrap;
        margin-top: 30rpx;
        max-height: 300rpx;
        overflow-y: auto;
        view {
            font-size: 25rpx;
            color: #999;
            border: 1rpx solid #999;
            padding: 6rpx 15rpx;
            margin: 15rpx;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            border-radius: 12rpx;
        }
    }
}
</style>

父组件的调用

相关推荐
微订外卖系统7 小时前
门店小程序从零上线:单店实体建模与订单状态机梳理
小程序·外卖系统·本地生活·微订
西泽XIZ920 小时前
微信小程序网络与数据库交互实践:从后端环境搭建到数据渲染
网络·数据库·微信小程序·小程序
知了学历孙老师1 天前
考研报名档案所在地填错,调档和录取会出什么岔子
考研·小程序
夜-未央1 天前
微信小程序多端插件获取OAID实践
微信小程序·小程序
AI源码1 天前
体育场馆预约小程序
小程序
普通人61 天前
uniapp鸿蒙开发完整指南
华为·uni-app·harmonyos
Johny_Zhao2 天前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
千寻技术帮2 天前
基于微信小程序的校园美食评鉴系统(附完整源码+数据库+部署文档)50049
mysql·微信小程序·小程序·毕业设计·美食
FX1317887KP_3 天前
FCE口语 Part 1 问答:B2 First 从个人信息到抽象问题的回答层次
小程序·ket口语·pet口语·fce备考·剑桥英语
码间留白3 天前
鸿蒙 Web 播放器断流不报错:黑屏提示从 70 秒优化到 2 秒
uni-app·鸿蒙·直播·arkweb·鸿蒙os开发