Skip to content

Commit

Permalink
refactor: extract attributes in badge vue to avoid repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsos committed Oct 28, 2024
1 parent 9a82d67 commit 9e5ebf2
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions example/src/views/BadgeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ const icon = 'star-fill';
const placement = 'bottom';
const colors = ['default', 'primary', 'secondary', 'error', 'warning', 'info', 'success'] as const;
const attributes: Partial<BadgeProps>[] = [
{},
{ placement },
{ left: true },
{ left: true, placement },
{ icon },
{ placement, icon },
{ left: true, icon, offsetX: -20 },
{ left: true, placement, icon, offsetX: -20 },
{ icon, text: undefined },
{ icon, text: undefined, placement },
{ icon, text: undefined, left: true },
{ icon, text: undefined, left: true, placement },
{ dot: true },
{ dot: true, placement },
{ dot: true, left: true },
{ dot: true, left: true, placement },
];
const badges = ref<BadgeProps[]>();
function createBadge(color: typeof colors[number], options: Partial<BadgeProps> = {}): BadgeProps {
Expand All @@ -24,24 +43,13 @@ function createBadge(color: typeof colors[number], options: Partial<BadgeProps>
}
function generateBadges(): BadgeProps[] {
return [
...colors.map(color => createBadge(color)),
...colors.map(color => createBadge(color, { placement })),
...colors.map(color => createBadge(color, { left: true })),
...colors.map(color => createBadge(color, { left: true, placement })),
...colors.map(color => createBadge(color, { icon })),
...colors.map(color => createBadge(color, { placement, icon })),
...colors.map(color => createBadge(color, { left: true, icon, offsetX: -20 })),
...colors.map(color => createBadge(color, { left: true, placement, icon, offsetX: -20 })),
...colors.map(color => createBadge(color, { icon, text: undefined })),
...colors.map(color => createBadge(color, { icon, text: undefined, placement })),
...colors.map(color => createBadge(color, { icon, text: undefined, left: true })),
...colors.map(color => createBadge(color, { icon, text: undefined, left: true, placement })),
...colors.map(color => createBadge(color, { dot: true })),
...colors.map(color => createBadge(color, { dot: true, placement })),
...colors.map(color => createBadge(color, { dot: true, left: true })),
...colors.map(color => createBadge(color, { dot: true, left: true, placement })),
];
const badges: BadgeProps[] = [];
for (const attribute of attributes) {
for (const color of colors) {
badges.push(createBadge(color, attribute));
}
}
return badges;
}
onBeforeMount(() => {
Expand Down

0 comments on commit 9e5ebf2

Please sign in to comment.