🎯 目标
封装一个统一风格的选择器组件 CommonSelector
,支持以下功能:
-
单选 / 多选模式切换
-
支持下拉展开菜单(模拟 Picker 效果)
-
可自定义选项内容样式(文字 / 图标 / 颜色)
-
支持禁用、placeholder、最大选择数量等
-
搭配
FormItem
适用于表单选择项
🧱 使用形式示意
[ 年级 ] 请选择年级 ▾
点击后弹出:
✓ 七年级
八年级
九年级
🧰 组件实现:CommonSelector.ets
@Component
export struct CommonSelector {
@Prop options: Array<{ label: string, value: any, disabled?: boolean }> = []
@Prop multiple: boolean = false
@Prop placeholder: string = '请选择'
@Prop disabled: boolean = false
@Prop max: number = 999
@Prop selected: any | any[] = this.multiple ? [] : null
@Prop onChange: (val: any | any[]) => void = () => {}
@State showPopup: boolean = false
build() {
Column() {
// 选择器主显示区
Row()
.height(44)
.border({ width: 1, color: '#ddd