Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

【建议】关于暴露表单回车事件的建议 #127

Closed
rovast opened this issue Nov 19, 2021 · 4 comments
Closed

【建议】关于暴露表单回车事件的建议 #127

rovast opened this issue Nov 19, 2021 · 4 comments

Comments

@rovast
Copy link
Contributor

rovast commented Nov 19, 2021

反馈问题请先查看文档和务必提供详细的复现代码,遵循如下格式,描述不清楚的问题将会直接关闭。

vue和ui框架

vue2 iview

问题描述

当前,当用户在输入时,点击回车会刷新页面。

如何复现

期望的结果

是否可以考虑在 上暴露出回车事件 @keyup.native.enter。这样监听用户回车后,可以做

  • 直接搜索(适用于把 vue-form 作为列表的搜索条件)
  • 防止误操作(适用于把 vue-form 作为添加表单)

类似 iview form 监听的回车处理

<Form
  @submit.native.prevent
  @keyup.native.enter="handleSearch"
>
@rovast
Copy link
Contributor Author

rovast commented Nov 26, 2021

通过拦截form 的 input 事件暂时先满足要求了。

<VueForm
  ref="vueFormRef"
  v-model="form"
  :schema="searchSchema"
  :form-props="searchFormProps"
  :form-footer="{show:false}"
>
</VueForm>
beforeDestroy() {
  this.removeFormInputEvents()
},

mounted() {
  this.hackFormInputEvents()
},

// methods
// FIXME 暂时 vueForm 没有公布 enter 事件,这里进行 hack 处理
hackFormInputEvents() {
  this.inputElements = this.$refs.vueFormRef.$el.getElementsByTagName('input')
  for (const el of this.inputElements) {
    el.addEventListener('keypress', this.keyPressHandler)
    el.addEventListener('keyup', this.keyUpHandler)
  }
},
removeFormInputEvents() {
  for (const el of this.inputElements) {
    console.log('unbind')
    el.removeEventListener('keypress', this.keyPressHandler)
    el.removeEventListener('keyup', this.keyUpHandler)
  }
},
keyPressHandler(event) {
  if (event.code === 'Enter') {
    event.preventDefault()
  }
},
keyUpHandler(event) {
  if (event.code === 'Enter') {
    this.$emit('on-enter')
  }
}

@lljj-x
Copy link
Owner

lljj-x commented Nov 27, 2021

可以支持,配置不会直接是透传 ui组件form 的emit,很容易和现有的事件冲突,比如 onSubmit

@lljj-x
Copy link
Owner

lljj-x commented Nov 27, 2021

另外你这个实现太复杂了,你只需要 @keyup.native.enter="this.$refs.form.$refs.genEditForm.validate()"

@lljj-x
Copy link
Owner

lljj-x commented Nov 28, 2021

升级到 1.10 版本,使用如下形式吧

<vueForm @keyup.native.enter="enterSubmit">
</vueForm>
methods: {
 async enterSubmit() {
   await this.$refs.schemaForm.$$uiFormRef.validate()
    this.doSomething()
  }
}

@rovast rovast closed this as completed Nov 29, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants