Skip to content

Commit a710db7

Browse files
authored
Update form-group.js
1 parent cc1a6a5 commit a710db7

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/components/form-group/form-group.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@ import BFormValidFeedback from '../form/form-valid-feedback'
1919
// Selector for finding first input in the form-group
2020
const SELECTOR = 'input:not(:disabled),textarea:not(:disabled),select:not(:disabled)'
2121

22-
// Breakpoint names for label-cols and label-align props
23-
const BREAKPOINTS = getBreakpointsUp()
24-
2522
// Memoize this function to return cached values to save time in computed functions
2623
const makePropName = memoize((breakpoint = '', prefix) => {
2724
return `${prefix}${upperFirst(breakpoint)}`
2825
})
2926

3027
// Generate the labelCol breakpoint props
31-
const bpLabelColProps = BREAKPOINTS.reduce((props, breakpoint) => {
32-
// i.e. label-cols, label-cols-sm, label-cols-md, ...
33-
props[makePropName(breakpoint, 'labelCols')] = {
34-
type: [Number, String, Boolean],
35-
default: breakpoint ? false : null
36-
}
37-
return props
38-
}, create(null))
39-
28+
const bpLabelColProps = () => {
29+
return getBreakpointsUp().reduce((props, breakpoint) => {
30+
// i.e. label-cols, label-cols-sm, label-cols-md, ...
31+
props[makePropName(breakpoint, 'labelCols')] = {
32+
type: [Number, String, Boolean],
33+
default: breakpoint ? false : null
34+
}
35+
return props
36+
}, create(null))
37+
}
4038
// Generate the labelAlign breakpoint props
41-
const bpLabelAlignProps = BREAKPOINTS.reduce((props, breakpoint) => {
42-
// label-align, label-align-sm, label-align-md, ...
43-
props[makePropName(breakpoint, 'labelAlign')] = {
44-
type: String, // left, right, center
45-
default: null
46-
}
47-
return props
48-
}, create(null))
39+
const bpLabelAlignProps = () => {
40+
return getBreakpointsUp().reduce((props, breakpoint) => {
41+
// label-align, label-align-sm, label-align-md, ...
42+
props[makePropName(breakpoint, 'labelAlign')] = {
43+
type: String, // left, right, center
44+
default: null
45+
}
46+
return props
47+
}, create(null))
48+
}
4949

5050
// render helper functions (here rather than polluting the instance with more methods)
5151
function renderInvalidFeedback(h, ctx) {
@@ -233,9 +233,9 @@ export default {
233233
default: false
234234
},
235235
// label-cols prop and all label-cols-{bp} props
236-
...bpLabelColProps,
236+
...bpLabelColProps(),
237237
// label-align prop and all label-align-{bp} props
238-
...bpLabelAlignProps,
238+
...bpLabelAlignProps(),
239239
horizontal: {
240240
// Deprecated
241241
type: Boolean,
@@ -250,8 +250,13 @@ export default {
250250
}
251251
},
252252
computed: {
253+
breakpoints() {
254+
// This will be cached the first time it is called
255+
return getBreakpointsUp()
256+
},
253257
labelColProps() {
254258
const props = {}
259+
const breakpoints = this.breakpoints
255260
/* istanbul ignore next: deprecated */
256261
if (this.horizontal) {
257262
// Deprecated setting of horizontal/breakpoint props
@@ -260,13 +265,13 @@ export default {
260265
"b-form-group: Props 'horizontal' and 'breakpoint' are deprecated. Use 'label-cols(-{breakpoint})' props instead."
261266
)
262267
// Legacy default is breakpoint sm and cols 3
263-
const bp = this.breakpoint || BREAKPOINTS[1] // 'sm'
268+
const bp = this.breakpoint || breakpoints[1] // 'sm'
264269
const cols = parseInt(this.labelCols, 10) || 3
265270
props[bp] = cols > 0 ? cols : 3
266271
// We then return the single breakpoint prop for legacy compatability
267272
return props
268273
}
269-
BREAKPOINTS.forEach(breakpoint => {
274+
breakpoints.forEach(breakpoint => {
270275
// Grab the value if the label column breakpoint prop
271276
let propVal = this[makePropName(breakpoint, 'labelCols')]
272277
// Handle case where the prop's value is an empty string, which represents true
@@ -289,7 +294,8 @@ export default {
289294
},
290295
labelAlignClasses() {
291296
const classes = []
292-
BREAKPOINTS.forEach(breakpoint => {
297+
const breakpoints = this.breakpoints
298+
breakpoints.forEach(breakpoint => {
293299
// assemble the label column breakpoint align classes
294300
const propVal = this[makePropName(breakpoint, 'labelAlign')] || null
295301
if (propVal) {

0 commit comments

Comments
 (0)