|
1 |
| -import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core'; |
| 1 | +import { |
| 2 | + booleanAttribute, |
| 3 | + computed, |
| 4 | + Directive, |
| 5 | + HostBinding, |
| 6 | + input, |
| 7 | + InputSignal, |
| 8 | + InputSignalWithTransform |
| 9 | +} from '@angular/core'; |
2 | 10 |
|
3 | 11 | import { ButtonType, Colors, Shapes } from '../coreui.types';
|
4 | 12 |
|
5 | 13 | @Directive({
|
6 | 14 | selector: '[cButton]',
|
7 | 15 | exportAs: 'cButton',
|
8 | 16 | standalone: true,
|
9 |
| - host: { class: 'btn' } |
| 17 | + host: { class: 'btn', '[class]': 'hostClasses()', '[attr.type]': 'type()' } |
10 | 18 | })
|
11 | 19 | export class ButtonDirective {
|
12 | 20 | /**
|
13 | 21 | * Toggle the active state for the component. [docs]
|
14 |
| - * @type boolean |
| 22 | + * @type InputSignalWithTransform<boolean, unknown> |
15 | 23 | */
|
16 |
| - @Input({ transform: booleanAttribute }) active: string | boolean = false; |
| 24 | + active: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
17 | 25 |
|
18 | 26 | /**
|
19 | 27 | * Sets the color context of the component to one of CoreUI’s themed colors. [docs]
|
20 |
| - * @type Colors |
| 28 | + * @type InputSignal<Colors> |
21 | 29 | */
|
22 |
| - @Input() color?: Colors = 'primary'; |
| 30 | + color: InputSignal<Colors> = input<Colors>('primary'); |
| 31 | + |
23 | 32 | /**
|
24 | 33 | * Toggle the disabled state for the component.
|
25 |
| - * @type boolean |
| 34 | + * @type InputSignalWithTransform<boolean, unknown> |
26 | 35 | */
|
27 |
| - @Input({ transform: booleanAttribute }) disabled: string | boolean = false; |
| 36 | + disabled: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
28 | 37 |
|
29 | 38 | /**
|
30 | 39 | * Select the shape of the component.
|
31 |
| - * @type { 'rounded' | 'rounded-top' | 'rounded-end' | 'rounded-bottom' | 'rounded-start' | 'rounded-circle' | 'rounded-pill' | 'rounded-0' | 'rounded-1' | 'rounded-2' | 'rounded-3' | string } |
| 40 | + * @type InputSignal<Shapes> |
32 | 41 | */
|
33 |
| - @Input() shape?: Shapes; |
| 42 | + shape: InputSignal<Shapes | undefined> = input<Shapes>(); |
34 | 43 |
|
35 | 44 | /**
|
36 | 45 | * Size the component small or large.
|
37 |
| - * @type {'sm' | 'lg'} |
| 46 | + * @type InputSignal<'sm' | 'lg' | ''> |
38 | 47 | */
|
39 |
| - @Input() size?: 'sm' | 'lg' | '' = ''; |
| 48 | + size: InputSignal<'' | 'sm' | 'lg'> = input<'' | 'sm' | 'lg'>(''); |
40 | 49 |
|
41 | 50 | /**
|
42 | 51 | * Specifies the type of button. Always specify the type attribute for the `<button>` element.
|
43 | 52 | * Different browsers may use different default types for the `<button>` element.
|
| 53 | + * @type InputSignal<ButtonType> |
| 54 | + * @default 'button' |
44 | 55 | */
|
45 |
| - @HostBinding('attr.type') |
46 |
| - @Input() |
47 |
| - type: ButtonType = 'button'; |
| 56 | + type: InputSignal<ButtonType> = input<ButtonType>('button'); |
48 | 57 |
|
49 | 58 | /**
|
50 | 59 | * Set the button variant to an outlined button or a ghost button.
|
51 |
| - * @type {'ghost' | 'outline'} |
| 60 | + * @type InputSignal<'ghost' | 'outline' | undefined> |
52 | 61 | */
|
53 |
| - @Input() variant?: 'ghost' | 'outline'; |
| 62 | + variant: InputSignal<'ghost' | 'outline' | undefined> = input<'ghost' | 'outline'>(); |
54 | 63 |
|
55 |
| - @HostBinding('class') |
56 |
| - get hostClasses(): any { |
| 64 | + hostClasses = computed(() => { |
57 | 65 | return {
|
58 | 66 | btn: true,
|
59 |
| - [`btn-${this.color}`]: !!this.color && !this.variant, |
60 |
| - [`btn-${this.variant}`]: !!this.variant && !this.color, |
61 |
| - [`btn-${this.variant}-${this.color}`]: !!this.variant && !!this.color, |
62 |
| - [`btn-${this.size}`]: !!this.size, |
63 |
| - [`${this.shape}`]: !!this.shape, |
64 |
| - disabled: this.disabled, |
65 |
| - active: this.active |
66 |
| - }; |
67 |
| - } |
| 67 | + [`btn-${this.color()}`]: !!this.color() && !this.variant(), |
| 68 | + [`btn-${this.variant()}`]: !!this.variant() && !this.color(), |
| 69 | + [`btn-${this.variant()}-${this.color()}`]: !!this.variant() && !!this.color(), |
| 70 | + [`btn-${this.size()}`]: !!this.size(), |
| 71 | + [`${this.shape()}`]: !!this.shape(), |
| 72 | + disabled: this.disabled(), |
| 73 | + active: this.active() |
| 74 | + } as Record<string, boolean>; |
| 75 | + }); |
68 | 76 |
|
69 | 77 | @HostBinding('attr.aria-disabled')
|
70 | 78 | get ariaDisabled() {
|
71 |
| - return this.disabled || null; |
| 79 | + return this.disabled() || null; |
72 | 80 | }
|
73 | 81 |
|
74 | 82 | @HostBinding('attr.aria-pressed')
|
75 | 83 | get isActive(): boolean | null {
|
76 |
| - return <boolean>this.active || null; |
| 84 | + return <boolean>this.active() || null; |
77 | 85 | }
|
78 | 86 |
|
79 | 87 | @HostBinding('attr.disabled')
|
80 | 88 | get attrDisabled() {
|
81 |
| - return this.disabled ? '' : null; |
| 89 | + return this.disabled() ? '' : null; |
82 | 90 | }
|
83 | 91 |
|
84 | 92 | @HostBinding('attr.tabindex')
|
85 | 93 | get tabIndex(): string | null {
|
86 |
| - return this.disabled ? '-1' : null; |
| 94 | + return this.disabled() ? '-1' : null; |
87 | 95 | }
|
88 | 96 | }
|
0 commit comments