'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _babelHelperVueJsxMergeProps = require('babel-helper-vue-jsx-merge-props');

var _babelHelperVueJsxMergeProps2 = _interopRequireDefault(_babelHelperVueJsxMergeProps);

var _lib = require('../../lib');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = {
  name: 'SuiDropdown',
  props: {
    button: Boolean,
    icon: String,
    floating: Boolean,
    search: Boolean,
    text: String
  },
  data: function data() {
    return {
      open: false,
      menu: null
    };
  },
  mounted: function mounted() {
    document.body.addEventListener('click', this.closeMenu);
  },

  methods: {
    register: function register(menu) {
      this.menu = menu;
    },
    openMenu: function openMenu(e) {
      if (e.target === this.$el || e.target === this.$refs.text || e.target === this.$refs.icon) {
        e.stopPropagation();
        this.menu.setOpen(true);
      }
    },
    closeMenu: function closeMenu() {
      this.menu.setOpen(false);
    }
  },
  render: function render() {
    var h = arguments[0];

    var ElementType = (0, _lib.getElementType)(this, this.button ? 'button' : 'div');
    return h(
      ElementType,
      (0, _babelHelperVueJsxMergeProps2.default)([{
        attrs: {
          role: 'listbox',
          'aria-expanded': this.open,
          tabindex: '0'
        }
      }, (0, _lib.getChildProps)(this), {
        'class': (0, _lib.classes)('ui', this.button && 'button', this.floating && 'floating', this.search && 'search', this.open && 'active visible', 'icon', 'dropdown'),
        on: {
          'click': this.openMenu
        },
        nativeOn: {
          'click': this.openMenu
        }
      }]),
      [this.text && h(
        'div',
        { ref: 'text', 'class': 'text', attrs: { role: 'alert', 'aria-live': 'polite' }
        },
        [this.text]
      ), h(
        'i',
        { ref: 'icon', attrs: { 'aria-hidden': 'true' },
          'class': (this.icon || 'dropdown') + ' icon' },
        []
      ), this.$slots.default]
    );
  }
};