Skip to content

Commit

Permalink
Update migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
alizedebray committed Apr 5, 2024
1 parent d7134db commit 0a5f455
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/migrations/src/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
"description": "Improves the post stepper accessibility.",
"factory": "./migrations/post/stepper"
},
"migration-bootstrap-chip": {
"migration-badge-to-chip-or-tag": {
"version": "7.0.0",
"description": "Renames badges to chips.",
"factory": "./migrations/bootstrap/chip"
"description": "Migrates badges to chip or tag depending on the content.",
"factory": "./migrations/v7/badge"
}
}
}
106 changes: 106 additions & 0 deletions packages/migrations/src/migrations/v7/badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Rule } from '@angular-devkit/schematics';
import type { AnyNode, Cheerio, CheerioAPI } from 'cheerio';
import { DomUpdate, getDomMigrationRule } from '../../../utils/dom-migration';
import { themeColors } from '../../../utils/constants';

export default function (): Rule {
return getDomMigrationRule(
new BadgeCheckToChipFilterUpdate(),
new BadgeToChipDismissibleUpdate(),
new BadgeToTagUpdate(),
);
}

class BadgeCheckToChipFilterUpdate implements DomUpdate {
selector = '.badge-check';

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

$element.removeClass('badge-check').addClass('chip-filter');

const $input = $element.children('.badge-check-input');
if ($input) $input.removeClass('badge-check-input').addClass('chip-filter-input');

const $label = $element.children('.badge-check-label');
if ($label) $label.removeClass('badge-check-label').addClass('chip-filter-label');

addChipTextClass($label);
});
}
}

class BadgeToChipDismissibleUpdate implements DomUpdate {
selector = '.badge';

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

// only update badge with close button
const $closeBtn = $element.children('.btn-close');
if (!$closeBtn.length) {
return;
}

addChipTextClass($element);

$closeBtn.removeAttr('class').addClass('chip chip-dismissible');

const ariaLabel = $closeBtn.attr('aria-label');
if (ariaLabel) {
$closeBtn.removeAttr('aria-label');
$closeBtn.append(`<span class="visually-hidden">${ariaLabel}</span>`);
}

$closeBtn.append($element.children()).data($element.data());

$element.replaceWith($closeBtn);
});
}
}

class BadgeToTagUpdate implements DomUpdate {
selector = '.badge';

bgClassRegex: RegExp = new RegExp(`^bg-(${themeColors.join('|')})$`);

update($elements: Cheerio<AnyNode>, $: CheerioAPI) {
$elements.each((_i, element) => {
const $element = $(element);

// do not update nested badges
const $parent = $element.parent();
if ($parent.hasClass('tag')) {
return;
}

$element.removeClass('badge').addClass('tag');

if ($element.hasClass('badge-sm')) $element.removeClass('badge-sm').addClass('tag-sm');

$element
.attr('class')
?.split(' ')
.forEach(cssClass => {
const [_, bgColor] = cssClass.match(this.bgClassRegex) ?? [];

if (!bgColor) return;

if (bgColor === 'active' || bgColor === 'yellow') $element.addClass('tag-yellow');
if (bgColor === 'white') $element.addClass('tag-white');
if (bgColor === 'info') $element.addClass('tag-info');
if (bgColor === 'success') $element.addClass('tag-success');
if (bgColor === 'danger') $element.addClass('tag-danger');
if (bgColor === 'warning') $element.addClass('tag-warning');

$element.removeClass(cssClass);
});
});
}
}

function addChipTextClass($element: Cheerio<AnyNode>) {
$element.children('span:not(.badge)').addClass('chip-text');
}

0 comments on commit 0a5f455

Please sign in to comment.