Skip to content

Commit

Permalink
chorer(NcBreadcrumbs): make breadcrumbsRefs an array
Browse files Browse the repository at this point in the history
Signed-off-by: Raimund Schlüßler <[email protected]>
  • Loading branch information
raimund-schluessler committed Jan 14, 2024
1 parent 825607a commit 1ddda89
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/components/NcBreadcrumbs/NcBreadcrumbs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default {
// Is the menu open or not
open: false,
},
breadcrumbsRefs: {},
breadcrumbsRefs: [],
}
},
created() {
Expand Down Expand Up @@ -262,13 +262,11 @@ export default {
if (!this.$refs.container) {
return
}
// All breadcrumb components passed into the default slot
const breadcrumbs = Object.values(this.breadcrumbsRefs)
const nrCrumbs = breadcrumbs.length
const nrCrumbs = this.breadcrumbsRefs.length
const hiddenIndices = []
const availableWidth = this.$refs.container.offsetWidth
let totalWidth = this.getTotalWidth(breadcrumbs)
let totalWidth = this.getTotalWidth()
// If we have breadcrumbs actions, we have to take their width into account too.
if (this.$refs.breadcrumb__actions) {
totalWidth += this.$refs.breadcrumb__actions.offsetWidth
Expand All @@ -284,7 +282,7 @@ export default {
// We hide elements alternating to the left and right
const currentIndex = startIndex + ((i % 2) ? i + 1 : i) / 2 * Math.pow(-1, i + (nrCrumbs % 2))
// Calculate the remaining overflow width after hiding this breadcrumb
overflow -= this.getWidth(breadcrumbs[currentIndex]?.$el)
overflow -= this.getWidth(this.breadcrumbsRefs[currentIndex]?.$el)
hiddenIndices.push(currentIndex)
i++
}
Expand Down Expand Up @@ -317,11 +315,10 @@ export default {
/**
* Calculates the total width of all breadcrumbs
*
* @param {Array} breadcrumbs All breadcrumbs
* @return {number} The total width
*/
getTotalWidth(breadcrumbs) {
return breadcrumbs.reduce((width, crumb) => width + this.getWidth(crumb.$el), 0)
getTotalWidth() {
return this.breadcrumbsRefs.reduce((width, crumb) => width + this.getWidth(crumb.$el), 0)
},
/**
* Calculates the width of the provided element
Expand Down Expand Up @@ -457,8 +454,7 @@ export default {
* add it to the array of all crumbs.
*/
hideCrumbs() {
const crumbs = Object.values(this.breadcrumbsRefs)
crumbs.forEach((crumb, i) => {
this.breadcrumbsRefs.forEach((crumb, i) => {
if (crumb?.$el?.classList) {
if (this.hiddenIndices.includes(i)) {
crumb.$el.classList.add(`${crumbClass}--hidden`)
Expand Down Expand Up @@ -513,7 +509,7 @@ export default {
* and don't write to this.breadcrumbsRefs directly
* to not trigger a myriad of re-renders.
*/
const breadcrumbsRefs = {}
const breadcrumbsRefs = []
// Add the breadcrumbs to the array of the created VNodes, check if hiding them is necessary.
breadcrumbs = breadcrumbs.map((crumb, index) => cloneVNode(crumb, {
ref: (crumb) => {
Expand Down

0 comments on commit 1ddda89

Please sign in to comment.