目录

TextInput绑定的Popup气泡无法弹出

问题现象

当TextInput组件提示文本为'请输入服务器标识'时,点击输入框组件,Popup气泡可以正常弹出。当提示文本为'请输入服务器标识/IP/域名'时,气泡无法弹出。

问题效果预览:

效果预览

背景知识

问题定位

输入组件(即TextInput、TextArea等输入组件)会解析placeholder得到相关字段触发智能填充,当前TextInput的placeholder属性存在关键字'名'会产生联想填充,导致气泡无法弹出。

分析结论

配置了TextInput组件的placeholder属性会触发智能填充,与自定义的bindPopup冲突了,导致bindPopup被关闭,无法显示。

修改建议

TextInput组件设置enableAutoFill属性值为false,禁用自动填充。

@Entry
@Component
struct CustomPopup {
  @State message: string = '';
  @State customPopup: boolean = false;

  @Builder
  popupBuilder() {
    Row({ space: 2 }) {
      Image($r('app.media.startIcon')).width(24).height(24)
      Text('Custom Popup').fontSize(10);
    }.width(110).height(50).padding(10).backgroundColor('#ffffff')
  }

  build() {
    Column() {
      TextInput({ text: this.message, placeholder: '请输入服务器标识/IP/域名' })
        .margin({
          top: 50
        })
        .enableAutoFill(false) // 属性值为false,禁用自动填充。
        .bindPopup(this.customPopup, {
          builder: this.popupBuilder,
          placement: Placement.Top,
          mask: false,
          popupColor: Color.Yellow,
          enableArrow: true,
          showInSubWindow: false,
          onStateChange: (e) => {
            if (!e.isVisible) {
              this.customPopup = false;
            }
          }
        })
        .onChange((value: string) => {
          if (value === '') {
            this.customPopup = true;
          } else {
            this.customPopup = false;
          }
        })
        .onEditChange((isEditing: boolean) => {
          if (isEditing) {
            this.customPopup = true;
          }
        })
    }
    .padding({ left: 16, right: 16 })
    .width('100%')
    .height('100%')
  }
}