🎯 目标
封装一个轻量级分段切换组件 CommonSegmented
,适用于:
-
分类切换(如:全部 / 进行中 / 已完成)
-
表单模式选择(如:日 / 周 / 月)
-
内容联动(与
Swiper
/ 内容列表联动) -
样式简洁清晰,选中高亮、底部滑块指示
-
支持禁用项、图标与文字组合等扩展
🧱 交互样式示意
[ 全部 ] [ 进行中 ] [ 已完成 ]
↑ 当前高亮
🧰 组件实现:CommonSegmented.ets
@Component
export struct CommonSegmented {
@Prop options: Array<{ label: string, value: any, disabled?: boolean }> = []
@Prop value: any = null
@Prop onChange: (v: any) => void = () => {}
build() {
Row({ space: 12 }).justifyContent(FlexAlign.SpaceAround).padding(8) {
this.options.forEach(opt => {
const isActive = this.value === opt.value
Column()
.alignItems(HorizontalAlign.Center)
.padding({ left: 12, right: 12, top: 6, bottom: 6 })
.backgroundColor(isActive ? '#00