Skip to content

Commit 66bb654

Browse files
authored
refactor(*): replace mkenum with const assertion (#15882)
* refactor(*): replace mkenum with const assertion * fix(chip): correct broken variant type and restrict to union, reword
1 parent 9c31d23 commit 66bb654

File tree

25 files changed

+96
-111
lines changed

25 files changed

+96
-111
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes for each version of this project will be documented in this
88
- **Angular 20 Compatibility** - Ignite UI for Angular now plays nice with Angular 20! Upgrade your apps and enjoy the latest features.
99
- `IgxActionStrip`
1010
- **Behavioral Changes** - When using the Action Strip standalone, outside of Grid, scenarios the component is no longer initially visible and the `hidden` property now defaults to `true`.
11+
- `IgxChip`
12+
- **Behavioral Change** The `variant` is now strictly typed with the union of supported options and no longer accepts invalid values for the default state, provide no value (nullish) instead is needed.
1113

1214
## 19.2.0
1315

projects/igniteui-angular/src/lib/avatar/avatar.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import {
99
ViewChild
1010
} from '@angular/core';
1111

12-
import { mkenum, normalizeURI } from '../core/utils';
12+
import { normalizeURI } from '../core/utils';
1313
import { IgxIconComponent } from '../icon/icon.component';
1414

1515
let NEXT_ID = 0;
16-
export const IgxAvatarSize = /*@__PURE__*/mkenum({
16+
export const IgxAvatarSize = {
1717
SMALL: 'small',
1818
MEDIUM: 'medium',
1919
LARGE: 'large'
20-
});
20+
} as const;
2121
export type IgxAvatarSize = (typeof IgxAvatarSize)[keyof typeof IgxAvatarSize];
2222

23-
export const IgxAvatarType = /*@__PURE__*/mkenum({
23+
export const IgxAvatarType = {
2424
INITIALS: 'initials',
2525
IMAGE: 'image',
2626
ICON: 'icon',
2727
CUSTOM: 'custom'
28-
});
28+
} as const;
2929
export type IgxAvatarType = (typeof IgxAvatarType)[keyof typeof IgxAvatarType];
3030

3131
/**

projects/igniteui-angular/src/lib/badge/badge.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { booleanAttribute, Component, HostBinding, Input } from '@angular/core';
2-
import { mkenum } from '../core/utils';
32
import { IgxIconComponent } from '../icon/icon.component';
43

54
let NEXT_ID = 0;
65

76
/**
87
* Determines the igxBadge type
98
*/
10-
export const IgxBadgeType = /*@__PURE__*/mkenum({
9+
export const IgxBadgeType = {
1110
PRIMARY: 'primary',
1211
INFO: 'info',
1312
SUCCESS: 'success',
1413
WARNING: 'warning',
1514
ERROR: 'error'
16-
});
15+
} as const;
1716
export type IgxBadgeType = (typeof IgxBadgeType)[keyof typeof IgxBadgeType];
1817
/**
1918
* Badge provides visual notifications used to decorate avatars, menus, etc.

projects/igniteui-angular/src/lib/buttonGroup/buttonGroup.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ import { IgxRippleDirective } from '../directives/ripple/ripple.directive';
2020

2121
import { takeUntil } from 'rxjs/operators';
2222
import { IBaseEventArgs } from '../core/utils';
23-
import { mkenum } from '../core/utils';
2423
import { IgxIconComponent } from '../icon/icon.component';
2524

2625
/**
2726
* Determines the Button Group alignment
2827
*/
29-
export const ButtonGroupAlignment = mkenum({
28+
export const ButtonGroupAlignment = {
3029
horizontal: 'horizontal',
3130
vertical: 'vertical'
32-
});
31+
} as const;
3332
export type ButtonGroupAlignment = typeof ButtonGroupAlignment[keyof typeof ButtonGroupAlignment];
3433

3534
let NEXT_ID = 0;

projects/igniteui-angular/src/lib/calendar/calendar.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { mkenum } from '../core/utils';
21

32
/**
43
* Sets the selection type - single, multi or range.
54
*/
6-
export const CalendarSelection = /*@__PURE__*/mkenum({
5+
export const CalendarSelection = {
76
SINGLE: 'single',
87
MULTI: 'multi',
98
RANGE: 'range'
10-
});
9+
} as const;
1110
export type CalendarSelection = (typeof CalendarSelection)[keyof typeof CalendarSelection];
1211

1312
export const enum ScrollDirection {
@@ -21,11 +20,11 @@ export interface IViewDateChangeEventArgs {
2120
currentValue: Date;
2221
}
2322

24-
export const IgxCalendarView = /*@__PURE__*/mkenum({
23+
export const IgxCalendarView = {
2524
Month: 'month',
2625
Year: 'year',
2726
Decade: 'decade'
28-
});
27+
} as const;
2928

3029
/**
3130
* Determines the Calendar active view - days, months or years.

projects/igniteui-angular/src/lib/card/card.component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {
1111
booleanAttribute
1212
} from '@angular/core';
1313

14-
import { mkenum } from '../core/utils';
15-
1614
let NEXT_ID = 0;
1715

1816
/**
@@ -269,10 +267,10 @@ export class IgxCardComponent {
269267
public horizontal = false;
270268
}
271269

272-
export const IgxCardActionsLayout = /*@__PURE__*/mkenum({
270+
export const IgxCardActionsLayout = {
273271
START: 'start',
274272
JUSTIFY: 'justify'
275-
});
273+
} as const;
276274
export type IgxCardActionsLayout = (typeof IgxCardActionsLayout)[keyof typeof IgxCardActionsLayout];
277275

278276
/**
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { mkenum } from '../core/utils';
21

3-
export const CarouselAnimationType = /*@__PURE__*/mkenum({
2+
export const CarouselAnimationType = {
43
none: 'none',
54
slide: 'slide',
65
fade: 'fade'
7-
});
6+
} as const;
87
export type CarouselAnimationType = (typeof CarouselAnimationType)[keyof typeof CarouselAnimationType];
98

10-
export const CarouselIndicatorsOrientation = /*@__PURE__*/mkenum({
9+
export const CarouselIndicatorsOrientation = {
1110
/**
1211
* @deprecated in version 19.1.0. Use `end` instead.
1312
*/
@@ -18,5 +17,5 @@ export const CarouselIndicatorsOrientation = /*@__PURE__*/mkenum({
1817
top: 'top',
1918
start: 'start',
2019
end: 'end'
21-
});
20+
} as const;
2221
export type CarouselIndicatorsOrientation = (typeof CarouselIndicatorsOrientation)[keyof typeof CarouselIndicatorsOrientation];

projects/igniteui-angular/src/lib/checkbox/checkbox-base.directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
AfterViewInit,
1818
} from '@angular/core';
1919
import { NgControl, Validators } from '@angular/forms';
20-
import { IBaseEventArgs, getComponentTheme, mkenum } from '../core/utils';
20+
import { IBaseEventArgs, getComponentTheme } from '../core/utils';
2121
import { noop, Subject } from 'rxjs';
2222
import { takeUntil } from 'rxjs/operators';
2323
import {
@@ -26,10 +26,10 @@ import {
2626
ThemeToken,
2727
} from '../services/theme/theme.token';
2828

29-
export const LabelPosition = /*@__PURE__*/ mkenum({
29+
export const LabelPosition = {
3030
BEFORE: 'before',
3131
AFTER: 'after'
32-
});
32+
} as const;
3333
export type LabelPosition = typeof LabelPosition[keyof typeof LabelPosition];
3434

3535
export interface IChangeCheckboxEventArgs extends IBaseEventArgs {

projects/igniteui-angular/src/lib/chips/chip.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ import {
1717
DOCUMENT
1818
} from '@angular/core';
1919
import { IgxDragDirective, IDragBaseEventArgs, IDragStartEventArgs, IDropBaseEventArgs, IDropDroppedEventArgs, IgxDropDirective } from '../directives/drag-drop/drag-drop.directive';
20-
import { IBaseEventArgs, mkenum } from '../core/utils';
20+
import { IBaseEventArgs } from '../core/utils';
2121
import { ChipResourceStringsEN, IChipResourceStrings } from '../core/i18n/chip-resources';
2222
import { Subject } from 'rxjs';
2323
import { IgxIconComponent } from '../icon/icon.component';
2424
import { NgClass, NgTemplateOutlet } from '@angular/common';
2525
import { getCurrentResourceStrings } from '../core/i18n/resources';
2626
import { Size } from '../grids/common/enums';
2727

28-
export const IgxChipTypeVariant = /*@__PURE__*/mkenum({
28+
export const IgxChipTypeVariant = {
2929
PRIMARY: 'primary',
3030
INFO: 'info',
3131
SUCCESS: 'success',
3232
WARNING: 'warning',
3333
DANGER: 'danger'
34-
});
34+
} as const;
35+
export type IgxChipTypeVariant = (typeof IgxChipTypeVariant)[keyof typeof IgxChipTypeVariant];
3536

3637
export interface IBaseChipEventArgs extends IBaseEventArgs {
3738
originalEvent: IDragBaseEventArgs | IDropBaseEventArgs | KeyboardEvent | MouseEvent | TouchEvent;
@@ -93,15 +94,15 @@ export class IgxChipComponent implements OnInit, OnDestroy {
9394
*
9495
* @remarks
9596
* Allowed values are `primary`, `info`, `success`, `warning`, `danger`.
96-
* Providing an invalid value won't change the chip.
97+
* Providing no/nullish value leaves the chip in its default state.
9798
*
9899
* @example
99100
* ```html
100-
* <igx-chip [variant]="success"></igx-chip>
101+
* <igx-chip variant="success"></igx-chip>
101102
* ```
102103
*/
103104
@Input()
104-
public variant: string | typeof IgxChipTypeVariant;
105+
public variant?: IgxChipTypeVariant | null;
105106
/**
106107
* Sets the value of `id` attribute. If not provided it will be automatically generated.
107108
*

projects/igniteui-angular/src/lib/core/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export const showMessage = (message: string, isMessageShown: boolean): boolean =
2020
return true;
2121
};
2222

23-
export const mkenum = <T extends { [index: string]: U }, U extends string>(x: T) => x;
24-
2523
/**
2624
*
2725
* @hidden @internal
@@ -247,7 +245,7 @@ export class PlatformUtil {
247245
/** @hidden @internal */
248246
public isElements = inject(ELEMENTS_TOKEN, { optional: true });
249247

250-
public KEYMAP = mkenum({
248+
public KEYMAP = {
251249
ENTER: 'Enter',
252250
SPACE: ' ',
253251
ESCAPE: 'Escape',
@@ -269,7 +267,7 @@ export class PlatformUtil {
269267
X: 'x',
270268
Y: 'y',
271269
Z: 'z'
272-
});
270+
} as const;
273271

274272
constructor(@Inject(PLATFORM_ID) private platformId: any) { }
275273

0 commit comments

Comments
 (0)