Blame view

node_modules/bootstrap-vue/src/components/navbar/navbar-toggle.js 2.43 KB
4cd4fd28   郭伟龙   feat: 初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { extend } from '../../vue'
import { NAME_COLLAPSE, NAME_NAVBAR_TOGGLE } from '../../constants/components'
import { EVENT_NAME_CLICK } from '../../constants/events'
import { PROP_TYPE_ARRAY_STRING, PROP_TYPE_BOOLEAN, PROP_TYPE_STRING } from '../../constants/props'
import { SLOT_NAME_DEFAULT } from '../../constants/slots'
import { getRootEventName } from '../../utils/events'
import { makeProp, makePropsConfigurable } from '../../utils/props'
import { listenOnRootMixin } from '../../mixins/listen-on-root'
import { normalizeSlotMixin } from '../../mixins/normalize-slot'
import { VBToggle } from '../../directives/toggle/toggle'

// --- Constants ---

const CLASS_NAME = 'navbar-toggler'

const ROOT_EVENT_NAME_STATE = getRootEventName(NAME_COLLAPSE, 'state')
const ROOT_EVENT_NAME_SYNC_STATE = getRootEventName(NAME_COLLAPSE, 'sync-state')

// --- Props ---

export const props = makePropsConfigurable(
  {
    disabled: makeProp(PROP_TYPE_BOOLEAN, false),
    label: makeProp(PROP_TYPE_STRING, 'Toggle navigation'),
    target: makeProp(PROP_TYPE_ARRAY_STRING, undefined, true) // Required
  },
  NAME_NAVBAR_TOGGLE
)

// --- Main component ---

// @vue/component
export const BNavbarToggle = /*#__PURE__*/ extend({
  name: NAME_NAVBAR_TOGGLE,
  directives: { VBToggle },
  mixins: [listenOnRootMixin, normalizeSlotMixin],
  props,
  data() {
    return {
      toggleState: false
    }
  },
  created() {
    this.listenOnRoot(ROOT_EVENT_NAME_STATE, this.handleStateEvent)
    this.listenOnRoot(ROOT_EVENT_NAME_SYNC_STATE, this.handleStateEvent)
  },
  methods: {
    onClick(event) {
      if (!this.disabled) {
        // Emit courtesy `click` event
        this.$emit(EVENT_NAME_CLICK, event)
      }
    },
    handleStateEvent(id, state) {
      // We listen for state events so that we can pass the
      // boolean expanded state to the default scoped slot
      if (id === this.target) {
        this.toggleState = state
      }
    }
  },
  render(h) {
    const { disabled } = this

    return h(
      'button',
      {
        staticClass: CLASS_NAME,
        class: { disabled },
        directives: [{ name: 'VBToggle', value: this.target }],
        attrs: {
          type: 'button',
          disabled,
          'aria-label': this.label
        },
        on: { click: this.onClick }
      },
      [
        this.normalizeSlot(SLOT_NAME_DEFAULT, { expanded: this.toggleState }) ||
          h('span', { staticClass: `${CLASS_NAME}-icon` })
      ]
    )
  }
})