简体   繁体   English

在内部点击时隐藏弹出框

[英]Hiding Popover on internal click

I'm currently using Vue-PopperJS , and have pretty much set it up as they have on the attached link with some slight changes:我目前正在使用Vue-PopperJS ,并且几乎按照附加链接上的方式进行了设置,并进行了一些细微的更改:

<template>
  <Popper
    ref="popover"
    trigger="clickToToggle"
    :options="{
      placement: position,
    }"
  >
    <slot
      slot="reference"
      name="activator"
    />
    <div class="base-dropdown--dropdown popper">
      <slot name="popover" />
    </div>
  </Popper>
</template>

<script>
import Popper from 'vue-popperjs'
import 'vue-popperjs/dist/vue-popper.css'

export default {
  name: 'BaseDropdown',
  components: {
    Popper
  },
  props: {
    position: {
      type: String,
      default: 'bottom'
    }
  }
}
</script>

<style scoped lang="sass">
.base-dropdown
  &--dropdown
    min-width: 180px
    @apply rounded-large shadow-sm
</style>

And for the items inside the popover we created another component like so:对于弹出框内的项目,我们创建了另一个组件,如下所示:

<template>
  <div
    class="base-dropdown-item"
    :class="getDropdownItemClass"
    @click="$emit('click')"
  >
    <slot />
  </div>
</template>

<script>
export default {
  name: 'BaseDropdownItem',
  props: {
    hoverable: {
      type: Boolean,
      required: false
    }
  },
  computed: {
    getDropdownItemClass () {
      if (this.hoverable) return ['hover:bg-blue']
      return []
    }
  }
}
</script>

<style scoped lang="sass">
.base-dropdown-item
  text-overflow: ellipsis
  padding: 12px 0 12px 12px
  @apply cursor-pointer body-3 flex items-center overflow-hidden text-grey-800 transition-all whitespace-nowrap

  &:hover
    @apply text-black
</style>

My question is, say I click on the popover content (ie, a button that does something), how can I trigger it to close the popover too?我的问题是,假设我点击了弹出框内容(即,一个做某事的按钮),我怎样才能触发它来关闭弹出框呢?

I know there is an event to hide it but I'm not sure how to use it.我知道有一个事件可以隐藏它,但我不知道如何使用它。

Should you need more information please don't hesitate to ask!如果您需要更多信息,请随时询问!

Not sure how 'efficient' this solution is.不确定这个解决方案有多“有效”。 However, Chase DeAnda's comment gave me an idea for an elegant solution.然而,Chase DeAnda 的评论让我想到了一个优雅的解决方案。

I ended up using the EventBus that would $emit('close-popver') .我最终使用了$emit('close-popver') EventBus This emit would be from whatever (in this case a <BaseDropDownItem /> component) that calls this method on a @click .该发射将来自在@click上调用此方法的任何内容(在本例中为<BaseDropDownItem />组件)。

methods: {
  handleClick() {
    this.$emit('click')
    EventBus.$emit('close-popover')
  }
}

And to close the popover:并关闭弹出窗口:

closePopover(){
  const { popover } = this.$refs
  popover.doClose()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM