温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!
// 多选标签组实现
import { Tag } from "../components/AutoTags"
interface tagGroupClass {
groupId: string,
title: string,
tags: tagClass[]
}
interface tagClass {
id: string,
text: string,
type: string
groupId?: string
}
@Component
export struct FilterTags {
@State selectedTags: Set<string> = new Set()
@State tagGroups: tagGroupClass[] = [
{
groupId: 'g1',
title: '类型',
tags: [
{ id: '1', text: '重要', type: 'primary' },
{ id: '2', text: '普通', type: 'default' }
]
},
{
groupId: 'g2',
title: '状态',
tags: [
{ id: '3', text: '进行中', type: 'warning' },
{ id: '4', text: '已完成', type: 'success' }
]
}
]
build() {
Column({ space: 16 }) {
ForEach(this.tagGroups, (group) => {
Column({ space: 8 }) {
Text(group.title)
.fontSize(16)
.fontWeight(FontWeight.Medium)
Flex({ wrap: FlexWrap.Wrap }) {
ForEach(group.tags, (tag:tagClass) => {
Tag({
text: tag.text,
type: tag.type ?? 'default'
}).onClick(() => {
this.handleTagClick(tag.id)
})
})
}
}
})
}
}
private handleTagClick(tagId: string) {
if (this.selectedTags.has(tagId)) {
this.selectedTags.delete(tagId)
} else {
this.selectedTags.add(tagId)
}
this.notifyFilterChange()
}
private notifyFilterChange() {
// 处理筛选逻辑
console.log(`筛选条件:${Array.from(this.selectedTags).join(',')}`)
}
}
// 优化前
@State private tags: Array<string> = []
// 优化后:使用Set提高查找效率
@State private tagSet: Set<string> = new Set()
// 优化数据结构
interface TagItem {
id: string
text: string
type: string
selected?: boolean
}
// 使用Map优化查找
@State private tagMap: Map<string, TagItem> = new Map()
@Component
struct OptimizedTags {
// 使用@Builder抽取复用组件
@Builder
private TagItem(tag: TagItem) {
Tag({
text: tag.text,
type: tag.type,
closable: true
})
.margin(4)
}
// 使用懒加载优化大列表渲染
build() {
List({ space: 8 }) {
LazyForEach(this.dataSource, (tag: TagItem) => {
ListItem() {
this.TagItem(tag)
}
}, (tag: TagItem) => tag.id)
}
}
}
@Component
struct DraggableTags {
@State tags: TagClass[] = []
@State dragIndex: number = -1
build() {
Flex({ wrap: FlexWrap.Wrap }) {
ForEach(this.tags, (tag, index) => {
Tag({
text: tag.text,
type: tag.type
})
.gesture(
PanGesture()
.onActionStart(() => {
this.dragIndex = index
})
.onActionUpdate((event: GestureEvent) => {
// 处理拖拽逻辑
})
.onActionEnd(() => {
this.dragIndex = -1
})
)
})
}
}
}
@Component
struct AnimatedTag {
@State private isVisible: boolean = true
@State private scale: number = 1
build() {
Tag({
text: '动画标签',
closable: true,
onClose: () => {
animateTo({
duration: 300,
curve: Curve.EaseInOut,
onFinish: () => {
this.isVisible = false
}
}, () => {
this.scale = 0
})
}
})
.scale(this.scale)
.opacity(this.isVisible ? 1 : 0)
}
}
// 集中管理颜色配置
const TagColors = {
text: {
default: '#333333',
primary: '#2468f2',
// ...
},
background: {
default: '#ffffff',
primary: '#eef2ff',
// ...
},
// ...
} as const
// 抽取通用逻辑
class TagUtils {
static getColor(type: string, state: string): string {
return Reflect.get(TagColors[state], type) || TagColors[state].default
}
static validateType(type: string): boolean {
return ['default', 'primary', 'success', 'warning', 'danger'].includes(type)
}
}
在 HarmonyOS NEXT 仿uv-ui Tag组件开发教程系列中我们从零开始开发了Tag组件, 他的扩展性其实还是存在的, 当然在开发过程中需要注意的是,一定要注意性能优化的问题, 其次在案例源码中接口类型其实定义在当前的文件中 ,在正式开发的过程中建议创建一个 Types 文件夹 将定义的接口接口放在该文件夹下进行统一管理
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。