-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
td.js
202 lines (194 loc) · 5.91 KB
/
td.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Vue from '../../utils/vue'
import { isTag } from '../../utils/dom'
import { isUndefinedOrNull } from '../../utils/inspect'
import { toInteger } from '../../utils/number'
import { toString } from '../../utils/string'
import attrsMixin from '../../mixins/attrs'
import listenersMixin from '../../mixins/listeners'
import normalizeSlotMixin from '../../mixins/normalize-slot'
// --- Utility methods ---
// Parse a rowspan or colspan into a digit (or `null` if < `1` )
const parseSpan = value => {
value = toInteger(value, 0)
return value > 0 ? value : null
}
/* istanbul ignore next */
const spanValidator = val => isUndefinedOrNull(val) || parseSpan(val) > 0
// --- Props ---
export const props = {
variant: {
type: String,
default: null
},
colspan: {
type: [Number, String],
default: null,
validator: spanValidator
},
rowspan: {
type: [Number, String],
default: null,
validator: spanValidator
},
stackedHeading: {
type: String,
default: null
},
stickyColumn: {
type: Boolean,
default: false
}
}
// --- Main component ---
// TODO:
// In Bootstrap v5, we won't need "sniffing" as table element variants properly inherit
// to the child elements, so this can be converted to a functional component
// @vue/component
export const BTd = /*#__PURE__*/ Vue.extend({
name: 'BTableCell',
// Mixin order is important!
mixins: [attrsMixin, listenersMixin, normalizeSlotMixin],
inheritAttrs: false,
inject: {
bvTableTr: {
/* istanbul ignore next */
default() /* istanbul ignore next */ {
return {}
}
}
},
props,
computed: {
tag() {
// Overridden by <b-th>
return 'td'
},
inTbody() {
return this.bvTableTr.inTbody
},
inThead() {
return this.bvTableTr.inThead
},
inTfoot() {
return this.bvTableTr.inTfoot
},
isDark() {
return this.bvTableTr.isDark
},
isStacked() {
return this.bvTableTr.isStacked
},
isStackedCell() {
// We only support stacked-heading in tbody in stacked mode
return this.inTbody && this.isStacked
},
isResponsive() {
return this.bvTableTr.isResponsive
},
isStickyHeader() {
// Needed to handle header background classes, due to lack of
// background color inheritance with Bootstrap v4 table CSS
// Sticky headers only apply to cells in table `thead`
return this.bvTableTr.isStickyHeader
},
hasStickyHeader() {
// Needed to handle header background classes, due to lack of
// background color inheritance with Bootstrap v4 table CSS
return this.bvTableTr.hasStickyHeader
},
isStickyColumn() {
// Needed to handle background classes, due to lack of
// background color inheritance with Bootstrap v4 table CSS
// Sticky column cells are only available in responsive
// mode (horizontal scrolling) or when sticky header mode
// Applies to cells in `thead`, `tbody` and `tfoot`
return !this.isStacked && (this.isResponsive || this.hasStickyHeader) && this.stickyColumn
},
rowVariant() {
return this.bvTableTr.variant
},
headVariant() {
return this.bvTableTr.headVariant
},
footVariant() {
return this.bvTableTr.footVariant
},
tableVariant() {
return this.bvTableTr.tableVariant
},
computedColspan() {
return parseSpan(this.colspan)
},
computedRowspan() {
return parseSpan(this.rowspan)
},
cellClasses() {
// We use computed props here for improved performance by caching
// the results of the string interpolation
let variant = this.variant
if (
(!variant && this.isStickyHeader && !this.headVariant) ||
(!variant && this.isStickyColumn && this.inTfoot && !this.footVariant) ||
(!variant && this.isStickyColumn && this.inThead && !this.headVariant) ||
(!variant && this.isStickyColumn && this.inTbody)
) {
// Needed for sticky-header mode as Bootstrap v4 table cells do
// not inherit parent's background-color. Boo!
variant = this.rowVariant || this.tableVariant || 'b-table-default'
}
return [
variant ? `${this.isDark ? 'bg' : 'table'}-${variant}` : null,
this.isStickyColumn ? 'b-table-sticky-column' : null
]
},
cellAttrs() {
// We use computed props here for improved performance by caching
// the results of the object spread (Object.assign)
const headOrFoot = this.inThead || this.inTfoot
// Make sure col/rowspan's are > 0 or null
const colspan = this.computedColspan
const rowspan = this.computedRowspan
// Default role and scope
let role = 'cell'
let scope = null
// Compute role and scope
// We only add scopes with an explicit span of 1 or greater
if (headOrFoot) {
// Header or footer cells
role = 'columnheader'
scope = colspan > 0 ? 'colspan' : 'col'
} else if (isTag(this.tag, 'th')) {
// th's in tbody
role = 'rowheader'
scope = rowspan > 0 ? 'rowgroup' : 'row'
}
return {
colspan: colspan,
rowspan: rowspan,
role: role,
scope: scope,
// Allow users to override role/scope plus add other attributes
...this.bvAttrs,
// Add in the stacked cell label data-attribute if in
// stacked mode (if a stacked heading label is provided)
'data-label':
this.isStackedCell && !isUndefinedOrNull(this.stackedHeading)
? /* istanbul ignore next */ toString(this.stackedHeading)
: null
}
}
},
render(h) {
const content = [this.normalizeSlot('default')]
return h(
this.tag,
{
class: this.cellClasses,
attrs: this.cellAttrs,
// Transfer any native listeners
on: this.bvListeners
},
[this.isStackedCell ? h('div', [content]) : content]
)
}
})