forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.js
51 lines (46 loc) · 1.38 KB
/
id.js
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
// SSR safe client-side ID attribute generation
// ID's can only be generated client-side, after mount
// `this._uid` is not synched between server and client
import { COMPONENT_UID_KEY, extend } from '../vue'
import { PROP_TYPE_STRING } from '../constants/props'
import { makeProp } from '../utils/props'
// --- Props ---
export const props = {
id: makeProp(PROP_TYPE_STRING)
}
// --- Mixin ---
// @vue/component
export const idMixin = extend({
props,
data() {
return {
localId_: null
}
},
computed: {
safeId() {
// Computed property that returns a dynamic function for creating the ID
// Reacts to changes in both `.id` and `.localId_` and regenerates a new function
const id = this.id || this.localId_
// We return a function that accepts an optional suffix string
// So this computed prop looks and works like a method
// but benefits from Vue's computed prop caching
const fn = suffix => {
if (!id) {
return null
}
suffix = String(suffix || '').replace(/\s+/g, '_')
return suffix ? id + '_' + suffix : id
}
return fn
}
},
mounted() {
// `mounted()` only occurs client-side
this.$nextTick(() => {
// Update DOM with auto-generated ID after mount
// to prevent SSR hydration errors
this.localId_ = `__BVID__${this[COMPONENT_UID_KEY]}`
})
}
})