HarmonyOS textInput、textarea组件设置maxLength属性截断emoji表情,会导致输入框内容清空?

textInput、textarea组件设置maxLength属性截断emoji表情,会导致输入框内容清空

阅读 685
1 个回答

使用for of来判断数据:

@Component
export struct MyTextInput {
  @State private inputText: string = ""
  @Prop @Watch('onTextUpdated') text:string = ""
  public maxLength: number = 10

  onTextUpdated() {
    this.inputText = this.sliceTools(this.text, this.maxLength)
  }

  aboutToAppear(): void {
    this.onTextUpdated()
  }

  sliceTools(str: string, maxLength: number) {
    let res = "";
    for (const element of str) {
      if (res.length + element.length > maxLength) {
        return res
      } else {
        res = res + element
      }
    }
    return res;
  }

  build() {
    TextInput({ text: $$this.inputText })
      .maxLength(this.maxLength)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进