专业的高密网站建设,上海网站建站建设服务,百度关键字怎么搜到公司网站,wordpress付费主题破解视频演示地址#xff1a;
https://www.bilibili.com/video/BV1jomdBBE4H/
#x1f4cb; 目录
概述特性快速开始API 参考使用示例主题配置最佳实践常见问题总结 概述
SearchInput 是控件库中专用于搜索场景的输入框组件#xff0c;支持清除按钮、历史记录、搜索按钮等功…视频演示地址https://www.bilibili.com/video/BV1jomdBBE4H/ 目录概述特性快速开始API 参考使用示例主题配置最佳实践常见问题总结概述SearchInput是控件库中专用于搜索场景的输入框组件支持清除按钮、历史记录、搜索按钮等功能适用于搜索框、筛选输入、快速查找等场景。设计理念搜索输入框采用便捷高效设计具有以下特点搜索图标左侧自动显示搜索图标直观明了清除按钮支持一键清除输入内容历史记录支持显示搜索历史快速复用搜索按钮支持显示搜索按钮明确操作品牌标识左下角自动包含品牌标识圆圈红字PC主题统一所有样式配置都在代码中方便定制适用场景搜索功能全局搜索、内容搜索筛选输入数据筛选、条件输入快速查找列表查找、表格搜索历史复用搜索历史、常用关键词特性✨ 核心特性✅搜索图标左侧自动显示搜索图标✅清除按钮支持一键清除输入内容✅搜索按钮支持显示搜索按钮可选✅历史记录支持显示搜索历史列表✅历史过滤根据输入内容过滤历史记录✅多种尺寸支持 small、medium、large 三种尺寸✅状态管理支持禁用、只读等状态✅品牌标识自动包含左下角品牌标识✅主题配置所有样式都可通过代码配置 视觉特点正常状态白色背景 灰色边框 搜索图标聚焦状态主色边框高亮禁用状态灰色背景 灰色文字 灰色边框只读状态正常样式但不可编辑历史记录下拉列表显示支持点击选择快速开始基础用法import{SearchInput}from../components/baseEntry Component struct MyPage{State searchValue:stringbuild(){Column({space:20}){// 基础搜索输入框SearchInput({value:$searchValue,placeholder:请输入搜索关键词})// 带搜索按钮的搜索输入框SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showSearchButton:true})// 带历史记录的搜索输入框SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showHistory:true,historyItems:[{text:鸿蒙开发},{text:UI控件库},{text:ArkTS}]})}.width(100%).height(100%).padding(20).justifyContent(FlexAlign.Center)}}关于双向绑定SearchInput使用Link实现双向绑定需要使用$variableName语法State searchValue:stringSearchInput({value:$searchValue// 使用 $ 创建双向绑定})API 参考Props属性名类型默认值说明valueLink string-搜索值必需双向绑定placeholderstring请输入搜索关键词占位符文本inputSizesmall | medium | largemedium输入框尺寸disabledbooleanfalse是否禁用readonlybooleanfalse是否只读showClearButtonbooleantrue是否显示清除按钮showSearchButtonbooleanfalse是否显示搜索按钮showHistorybooleanfalse是否显示历史记录historyItemsSearchHistoryItem[][]历史记录列表maxHistoryCountnumber10最大历史记录数showBrandbooleantrue是否显示品牌标识inputWidthstring | number100%输入框宽度SearchHistoryItem 接口属性名类型说明textstring历史记录文本必需timestampnumber?时间戳可选尺寸规格尺寸高度字体大小图标大小内边距左右small48vp14vp18vp12vpmedium60vp16vp20vp14vplarge72vp18vp24vp16vp使用示例1. 基础搜索输入框Entry Component struct SearchExample1{State searchValue:stringbuild(){Column({space:15}){SearchInput({value:$searchValue,placeholder:请输入搜索关键词})Text(当前搜索${this.searchValue||(空)}).fontSize(14).fontColor(#666)}.width(100%).padding(20)}}2. 带搜索按钮Entry Component struct SearchExample2{State searchValue:stringbuild(){Column({space:15}){SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showSearchButton:true})Text(提示点击搜索按钮或按回车键执行搜索).fontSize(14).fontColor(#666)}.width(100%).padding(20)}}3. 历史记录Entry Component struct SearchExample3{State searchValue:stringState historyItems:SearchHistoryItem[][{text:鸿蒙开发,timestamp:Date.now()-3600000},{text:UI控件库,timestamp:Date.now()-7200000},{text:ArkTS,timestamp:Date.now()-10800000}]build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})Text(提示输入关键词时会显示匹配的历史记录).fontSize(14).fontColor(#666)}.width(100%).padding(20)}}4. 不同尺寸Entry Component struct SearchExample4{State search1:stringState search2:stringState search3:stringbuild(){Column({space:15}){SearchInput({value:$search1,placeholder:小尺寸搜索,inputSize:small})SearchInput({value:$search2,placeholder:中等尺寸搜索默认,inputSize:medium})SearchInput({value:$search3,placeholder:大尺寸搜索,inputSize:large})}.width(100%).padding(20)}}5. 状态管理Entry Component struct SearchExample5{State search1:stringState search2:string禁用搜索State search3:string只读搜索build(){Column({space:15}){SearchInput({value:$search1,placeholder:正常状态})SearchInput({value:$search2,placeholder:请输入搜索关键词,disabled:true})SearchInput({value:$search3,placeholder:请输入搜索关键词,readonly:true})}.width(100%).padding(20)}}6. 隐藏清除按钮Entry Component struct SearchExample6{State searchValue:stringbuild(){Column({space:15}){SearchInput({value:$searchValue,placeholder:不显示清除按钮,showClearButton:false})Text(提示隐藏清除按钮后需要手动删除输入内容).fontSize(14).fontColor(#666)}.width(100%).padding(20)}}7. 完整功能示例Entry Component struct SearchExample7{State searchValue:stringState historyItems:SearchHistoryItem[][{text:鸿蒙开发,timestamp:Date.now()-3600000},{text:UI控件库,timestamp:Date.now()-7200000},{text:ArkTS,timestamp:Date.now()-10800000},{text:HarmonyOS,timestamp:Date.now()-14400000},{text:组件开发,timestamp:Date.now()-18000000}]build(){Column({space:15}){SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showSearchButton:true,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})Text(提示支持搜索按钮和历史记录功能).fontSize(14).fontColor(#666)}.width(100%).padding(20)}}8. 搜索页面示例Entry Component struct SearchPage{State searchValue:stringState searchResults:string[][]State historyItems:SearchHistoryItem[][]privateperformSearch(){if(this.searchValue.trim().length0){// 执行搜索逻辑this.searchResults[结果1,结果2,结果3]// 添加到历史记录constnewItem:SearchHistoryItem{text:this.searchValue,timestamp:Date.now()}this.historyItems[newItem,...this.historyItems].slice(0,10)}}build(){Column({space:20}){Text(搜索).fontSize(24).fontWeight(FontWeight.Bold)SearchInput({value:$searchValue,placeholder:请输入搜索关键词,showSearchButton:true,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5})if(this.searchResults.length0){Column({space:10}){Text(搜索结果).fontSize(18).fontWeight(FontWeight.Bold)ForEach(this.searchResults,(result:string){Text(result).fontSize(16).fontColor(#333).padding(10).width(100%).backgroundColor(#F5F5F5).borderRadius(8)})}.width(100%)}}.width(100%).padding(30)}}主题配置SearchInput 的所有样式都通过ComponentTheme配置所有配置都在代码中不依赖JSON文件。修改默认颜色import{ComponentTheme}from../theme/ComponentTheme// 修改主色影响聚焦状态的边框颜色ComponentTheme.PRIMARY_COLOR#007AFF// 修改错误色影响错误状态的边框和提示颜色ComponentTheme.ERROR_COLOR#FF3B30// 修改边框颜色ComponentTheme.BORDER_COLOR#E5E5E5// 修改圆角ComponentTheme.BORDER_RADIUS8批量配置import{ComponentTheme}from../theme/ComponentTheme// 使用 setTheme 方法批量配置ComponentTheme.setTheme({primaryColor:#007AFF,errorColor:#FF3B30,borderRadius:8,spacing:16})最佳实践1. 尺寸选择推荐根据使用场景选择尺寸small用于紧凑空间、导航栏搜索medium默认尺寸适用于大多数场景large用于重要搜索或大屏幕显示2. 搜索按钮默认关闭showSearchButton: false适用于大多数场景明确操作需要明确搜索操作时开启搜索按钮回车搜索支持回车键触发搜索无需按钮3. 历史记录自动过滤根据输入内容自动过滤历史记录数量限制使用maxHistoryCount限制显示数量时间戳可选添加时间戳用于排序和显示4. 清除按钮默认开启showClearButton: true方便快速清除特殊场景可以关闭清除按钮强制用户手动删除5. 历史记录管理本地存储可以将历史记录保存到本地存储去重处理添加新历史记录时去重数量限制限制历史记录总数避免过多6. 搜索触发实时搜索可以在onChange中实现实时搜索按钮搜索点击搜索按钮或按回车键触发搜索防抖处理建议对实时搜索进行防抖处理常见问题Q1: 如何禁用清除按钮A: 设置showClearButton: falseSearchInput({value:$searchValue,showClearButton:false})Q2: 如何显示搜索按钮A: 设置showSearchButton: trueSearchInput({value:$searchValue,showSearchButton:true})Q3: 如何实现历史记录功能A: 使用showHistory和historyItems属性State historyItems:SearchHistoryItem[][{text:关键词1},{text:关键词2}]SearchInput({value:$searchValue,showHistory:true,historyItems:this.historyItems})Q4: 如何限制历史记录数量A: 使用maxHistoryCount属性SearchInput({value:$searchValue,showHistory:true,historyItems:this.historyItems,maxHistoryCount:5// 最多显示5条})Q5: 如何设置输入框宽度A: 使用inputWidth属性SearchInput({value:$searchValue,inputWidth:300// 固定宽度})SearchInput({value:$searchValue,inputWidth:100%// 百分比宽度})Q6: 如何实现搜索功能A: 可以在外部监听value变化或使用搜索按钮State searchValue:stringSearchInput({value:$searchValue,showSearchButton:true})// 在 aboutToUpdate 或其他地方处理搜索aboutToUpdate(){if(this.searchValue.trim().length0){// 执行搜索逻辑this.performSearch(this.searchValue)}}Q7: 历史记录如何持久化A: 可以使用本地存储// 保存历史记录localStorage.setItem(searchHistory,JSON.stringify(this.historyItems))// 读取历史记录constsavedlocalStorage.getItem(searchHistory)if(saved){this.historyItemsJSON.parse(saved)}总结SearchInput 是控件库中专用于搜索场景的输入框组件具有以下核心特性搜索图标左侧自动显示搜索图标直观明了清除按钮支持一键清除输入内容搜索按钮支持显示搜索按钮明确操作历史记录支持显示搜索历史快速复用易于使用简单的 API开箱即用主题配置所有样式都可通过代码配置品牌标识自动包含品牌标识保持视觉统一关键要点✅ 使用$variableName创建双向绑定✅ 使用showSearchButton显示搜索按钮✅ 使用showHistory和historyItems显示历史记录✅ 使用showClearButton控制清除按钮显示✅ 使用inputSize属性选择合适尺寸✅ 通过ComponentTheme自定义全局样式适用场景搜索功能筛选输入快速查找历史复用下一篇预告TextArea多行文本输入详解本文档属于《鸿蒙PC UI控件库开发系列文章》第10篇