Skip to content

Commit b848aca

Browse files
committed
feat: code refactoring to reparte constants from utils and better code sharing
1 parent 4528c28 commit b848aca

File tree

169 files changed

+1238
-970
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+1238
-970
lines changed

src/components/alert/alert.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
import { NAME_ALERT } from '../../constants/components'
12
import Vue from '../../utils/vue'
23
import { getComponentConfig } from '../../utils/config'
34
import { requestAF } from '../../utils/dom'
4-
import { isBoolean } from '../../utils/inspect'
5+
import { isBoolean, isNumeric } from '../../utils/inspect'
56
import { toInteger } from '../../utils/number'
67
import BVTransition from '../../utils/bv-transition'
78
import normalizeSlotMixin from '../../mixins/normalize-slot'
89
import { BButtonClose } from '../button/button-close'
910

10-
const NAME = 'BAlert'
11-
1211
// Convert `show` value to a number
1312
const parseCountDown = show => {
1413
if (show === '' || isBoolean(show)) {
@@ -30,12 +29,9 @@ const parseShow = show => {
3029
return !!show
3130
}
3231

33-
// Is a value number like (i.e. a number or a number as string)
34-
const isNumericLike = value => !isNaN(toInteger(value))
35-
3632
// @vue/component
3733
export const BAlert = /*#__PURE__*/ Vue.extend({
38-
name: NAME,
34+
name: NAME_ALERT,
3935
mixins: [normalizeSlotMixin],
4036
model: {
4137
prop: 'show',
@@ -44,15 +40,15 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
4440
props: {
4541
variant: {
4642
type: String,
47-
default: () => getComponentConfig(NAME, 'variant')
43+
default: () => getComponentConfig(NAME_ALERT, 'variant')
4844
},
4945
dismissible: {
5046
type: Boolean,
5147
default: false
5248
},
5349
dismissLabel: {
5450
type: String,
55-
default: () => getComponentConfig(NAME, 'dismissLabel')
51+
default: () => getComponentConfig(NAME_ALERT, 'dismissLabel')
5652
},
5753
show: {
5854
type: [Boolean, Number, String],
@@ -78,7 +74,7 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
7874
},
7975
countDown(newVal) {
8076
this.clearCountDownInterval()
81-
if (isNumericLike(this.show)) {
77+
if (isNumeric(this.show)) {
8278
// Ignore if this.show transitions to a boolean value.
8379
this.$emit('dismiss-count-down', newVal)
8480
if (this.show !== newVal) {
@@ -101,11 +97,11 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
10197
}
10298
},
10399
localShow(newVal) {
104-
if (!newVal && (this.dismissible || isNumericLike(this.show))) {
100+
if (!newVal && (this.dismissible || isNumeric(this.show))) {
105101
// Only emit dismissed events for dismissible or auto dismissing alerts
106102
this.$emit('dismissed')
107103
}
108-
if (!isNumericLike(this.show) && this.show !== newVal) {
104+
if (!isNumeric(this.show) && this.show !== newVal) {
109105
// Only emit booleans if we weren't passed a number via `this.show`
110106
this.$emit('input', newVal)
111107
}
@@ -158,7 +154,7 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
158154
},
159155
attrs: { role: 'alert', 'aria-live': 'polite', 'aria-atomic': true }
160156
},
161-
[$dismissBtn, this.normalizeSlot('default')]
157+
[$dismissBtn, this.normalizeSlot()]
162158
)
163159
$alert = [$alert]
164160
}

src/components/aspect/aspect.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
import { NAME_ASPECT } from '../../constants/components'
2+
import { RX_ASPECT, RX_ASPECT_SEPARATOR } from '../../constants/regex'
13
import Vue from '../../utils/vue'
24
import { mathAbs } from '../../utils/math'
35
import { toFloat } from '../../utils/number'
46
import normalizeSlotMixin from '../../mixins/normalize-slot'
57

68
// --- Constants ---
7-
const NAME = 'BAspect'
89
const CLASS_NAME = 'b-aspect'
910

10-
const RX_ASPECT = /^\d+(\.\d*)?[/:]\d+(\.\d*)?$/
11-
const RX_SEPARATOR = /[/:]/
12-
1311
// --- Main Component ---
1412
export const BAspect = /*#__PURE__*/ Vue.extend({
15-
name: NAME,
13+
name: NAME_ASPECT,
1614
mixins: [normalizeSlotMixin],
1715
props: {
1816
aspect: {
@@ -33,7 +31,7 @@ export const BAspect = /*#__PURE__*/ Vue.extend({
3331
if (RX_ASPECT.test(aspect)) {
3432
// Width and/or Height can be a decimal value below `1`, so
3533
// we only fallback to `1` if the value is `0` or `NaN`
36-
const [width, height] = aspect.split(RX_SEPARATOR).map(v => toFloat(v) || 1)
34+
const [width, height] = aspect.split(RX_ASPECT_SEPARATOR).map(v => toFloat(v) || 1)
3735
ratio = width / height
3836
} else {
3937
ratio = toFloat(aspect) || 1
@@ -52,7 +50,7 @@ export const BAspect = /*#__PURE__*/ Vue.extend({
5250
staticClass: `${CLASS_NAME}-content flex-grow-1 w-100 mw-100`,
5351
style: { marginLeft: '-100%' }
5452
},
55-
[this.normalizeSlot('default')]
53+
[this.normalizeSlot()]
5654
)
5755
return h(this.tag, { staticClass: `${CLASS_NAME} d-flex` }, [$sizer, $content])
5856
}

src/components/avatar/avatar-group.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
import { NAME_AVATAR_GROUP } from '../../constants/components'
12
import Vue from '../../utils/vue'
23
import normalizeSlotMixin from '../../mixins/normalize-slot'
34
import { mathMax, mathMin } from '../../utils/math'
45
import { toFloat } from '../../utils/number'
56
import { computeSize } from './avatar'
67

7-
// --- Constants ---
8-
const NAME = 'BAvatarGroup'
9-
108
// --- Main component ---
119
// @vue/component
1210
export const BAvatarGroup = /*#__PURE__*/ Vue.extend({
13-
name: NAME,
11+
name: NAME_AVATAR_GROUP,
1412
mixins: [normalizeSlotMixin],
1513
provide() {
1614
return { bvAvatarGroup: this }
@@ -60,7 +58,7 @@ export const BAvatarGroup = /*#__PURE__*/ Vue.extend({
6058
},
6159
render(h) {
6260
const $inner = h('div', { staticClass: 'b-avatar-group-inner', style: this.paddingStyle }, [
63-
this.normalizeSlot('default')
61+
this.normalizeSlot()
6462
])
6563

6664
return h(this.tag, { staticClass: 'b-avatar-group', attrs: { role: 'group' } }, [$inner])

src/components/avatar/avatar.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NAME_AVATAR } from '../../constants/components'
2+
import { RX_NUMBER } from '../../constants/regex'
13
import Vue from '../../utils/vue'
24
import { getComponentConfig } from '../../utils/config'
35
import { isNumber, isString } from '../../utils/inspect'
@@ -12,13 +14,10 @@ import { BIconPersonFill } from '../../icons/icons'
1214
import normalizeSlotMixin from '../../mixins/normalize-slot'
1315

1416
// --- Constants ---
15-
const NAME = 'BAvatar'
1617
const CLASS_NAME = 'b-avatar'
1718

1819
const SIZES = ['sm', null, 'lg']
1920

20-
const RX_NUMBER = /^[0-9]*\.?[0-9]+$/
21-
2221
const FONT_SIZE_SCALE = 0.4
2322
const BADGE_FONT_SIZE_SCALE = FONT_SIZE_SCALE * 0.7
2423

@@ -44,7 +43,7 @@ const props = {
4443
},
4544
variant: {
4645
type: String,
47-
default: () => getComponentConfig(NAME, 'variant')
46+
default: () => getComponentConfig(NAME_AVATAR, 'variant')
4847
},
4948
size: {
5049
type: [Number, String],
@@ -72,7 +71,7 @@ const props = {
7271
},
7372
badgeVariant: {
7473
type: String,
75-
default: () => getComponentConfig(NAME, 'badgeVariant')
74+
default: () => getComponentConfig(NAME_AVATAR, 'badgeVariant')
7675
},
7776
badgeTop: {
7877
type: Boolean,
@@ -104,7 +103,7 @@ export const computeSize = value => {
104103
// --- Main component ---
105104
// @vue/component
106105
export const BAvatar = /*#__PURE__*/ Vue.extend({
107-
name: NAME,
106+
name: NAME_AVATAR,
108107
mixins: [normalizeSlotMixin],
109108
inject: {
110109
bvAvatarGroup: { default: null }
@@ -193,9 +192,9 @@ export const BAvatar = /*#__PURE__*/ Vue.extend({
193192
const ariaLabel = this.ariaLabel || null
194193

195194
let $content = null
196-
if (this.hasNormalizedSlot('default')) {
195+
if (this.hasNormalizedSlot()) {
197196
// Default slot overrides props
198-
$content = h('span', { staticClass: 'b-avatar-custom' }, [this.normalizeSlot('default')])
197+
$content = h('span', { staticClass: 'b-avatar-custom' }, [this.normalizeSlot()])
199198
} else if (src) {
200199
$content = h('img', {
201200
style: variant ? {} : { width: '100%', height: '100%' },

src/components/badge/badge.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
import { NAME_BADGE } from '../../constants/components'
12
import Vue, { mergeData } from '../../utils/vue'
23
import { getComponentConfig } from '../../utils/config'
34
import { omit } from '../../utils/object'
45
import { pluckProps } from '../../utils/props'
56
import { isLink } from '../../utils/router'
67
import { BLink, props as BLinkProps } from '../link/link'
78

8-
// --- Constants ---
9-
10-
const NAME = 'BBadge'
11-
129
// --- Props ---
1310

1411
const linkProps = omit(BLinkProps, ['event', 'routerTag'])
@@ -22,7 +19,7 @@ export const props = {
2219
},
2320
variant: {
2421
type: String,
25-
default: () => getComponentConfig(NAME, 'variant')
22+
default: () => getComponentConfig(NAME_BADGE, 'variant')
2623
},
2724
pill: {
2825
type: Boolean,
@@ -34,7 +31,7 @@ export const props = {
3431
// --- Main component ---
3532
// @vue/component
3633
export const BBadge = /*#__PURE__*/ Vue.extend({
37-
name: NAME,
34+
name: NAME_BADGE,
3835
functional: true,
3936
props,
4037
render(h, { props, data, children }) {

src/components/breadcrumb/breadcrumb-item.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { NAME_BREADCRUMB_ITEM } from '../../constants/components'
12
import Vue, { mergeData } from '../../utils/vue'
23
import { BBreadcrumbLink, props } from './breadcrumb-link'
34

45
// @vue/component
56
export const BBreadcrumbItem = /*#__PURE__*/ Vue.extend({
6-
name: 'BBreadcrumbItem',
7+
name: NAME_BREADCRUMB_ITEM,
78
functional: true,
89
props,
910
render(h, { props, data, children }) {

src/components/breadcrumb/breadcrumb-link.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NAME_BREADCRUMB_LINK } from '../../constants/components'
12
import Vue, { mergeData } from '../../utils/vue'
23
import { htmlOrText } from '../../utils/html'
34
import { omit } from '../../utils/object'
@@ -25,7 +26,7 @@ export const props = {
2526
// --- Main component ---
2627
// @vue/component
2728
export const BBreadcrumbLink = /*#__PURE__*/ Vue.extend({
28-
name: 'BBreadcrumbLink',
29+
name: NAME_BREADCRUMB_LINK,
2930
functional: true,
3031
props,
3132
render(h, { props: suppliedProps, data, children }) {

src/components/breadcrumb/breadcrumb.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NAME_BREADCRUMB } from '../../constants/components'
12
import Vue, { mergeData } from '../../utils/vue'
23
import { isArray, isObject } from '../../utils/inspect'
34
import { toString } from '../../utils/string'
@@ -12,7 +13,7 @@ export const props = {
1213

1314
// @vue/component
1415
export const BBreadcrumb = /*#__PURE__*/ Vue.extend({
15-
name: 'BBreadcrumb',
16+
name: NAME_BREADCRUMB,
1617
functional: true,
1718
props,
1819
render(h, { props, data, children }) {

src/components/button-group/button-group.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
import { NAME_BUTTON, NAME_BUTTON_GROUP } from '../../constants/components'
12
import Vue, { mergeData } from '../../utils/vue'
23
import { getComponentConfig } from '../../utils/config'
34

4-
const NAME = 'BButtonGroup'
5-
65
export const props = {
76
vertical: {
87
type: Boolean,
98
default: false
109
},
1110
size: {
1211
type: String,
13-
default: () => getComponentConfig('BButton', 'size')
12+
default: () => getComponentConfig(NAME_BUTTON, 'size')
1413
},
1514
tag: {
1615
type: String,
@@ -24,7 +23,7 @@ export const props = {
2423

2524
// @vue/component
2625
export const BButtonGroup = /*#__PURE__*/ Vue.extend({
27-
name: NAME,
26+
name: NAME_BUTTON_GROUP,
2827
functional: true,
2928
props,
3029
render(h, { props, data, children }) {

src/components/button-toolbar/button-toolbar.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { DOWN, LEFT, RIGHT, UP } from '../../constants/key-codes'
1+
import { NAME_BUTTON_TOOLBAR } from '../../constants/components'
2+
import { CODE_DOWN, CODE_LEFT, CODE_RIGHT, CODE_UP } from '../../constants/key-codes'
23
import Vue from '../../utils/vue'
34
import { attemptFocus, contains, isVisible, selectAll } from '../../utils/dom'
45
import { stopEvent } from '../../utils/events'
@@ -18,7 +19,7 @@ const ITEM_SELECTOR = [
1819

1920
// @vue/component
2021
export const BButtonToolbar = /*#__PURE__*/ Vue.extend({
21-
name: 'BButtonToolbar',
22+
name: NAME_BUTTON_TOOLBAR,
2223
mixins: [normalizeSlotMixin],
2324
props: {
2425
justify: {
@@ -79,10 +80,10 @@ export const BButtonToolbar = /*#__PURE__*/ Vue.extend({
7980
},
8081
onKeydown(evt) {
8182
const { keyCode, shiftKey } = evt
82-
if (keyCode === UP || keyCode === LEFT) {
83+
if (keyCode === CODE_UP || keyCode === CODE_LEFT) {
8384
stopEvent(evt)
8485
shiftKey ? this.focusFirst(evt) : this.focusPrev(evt)
85-
} else if (keyCode === DOWN || keyCode === RIGHT) {
86+
} else if (keyCode === CODE_DOWN || keyCode === CODE_RIGHT) {
8687
stopEvent(evt)
8788
shiftKey ? this.focusLast(evt) : this.focusNext(evt)
8889
}
@@ -105,7 +106,7 @@ export const BButtonToolbar = /*#__PURE__*/ Vue.extend({
105106
}
106107
: {}
107108
},
108-
[this.normalizeSlot('default')]
109+
[this.normalizeSlot()]
109110
)
110111
}
111112
})

0 commit comments

Comments
 (0)