ngx-opalbytes-components 1.25.0 → 1.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +329 -0
- package/fesm2022/ngx-opalbytes-components.mjs +626 -5
- package/fesm2022/ngx-opalbytes-components.mjs.map +1 -1
- package/package.json +1 -1
- package/src/lib/shared/components/index.ts +3 -0
- package/src/lib/shared/components/radio/radio-button.component.css +95 -0
- package/src/lib/shared/components/radio/radio-button.component.html +32 -0
- package/src/lib/shared/components/radio/radio-button.component.ts +133 -0
- package/src/lib/shared/components/radio/radio-button.stories.ts +94 -0
- package/src/lib/shared/components/radio/radio-group.component.css +21 -0
- package/src/lib/shared/components/radio/radio-group.component.ts +216 -0
- package/src/lib/shared/components/radio/radio.component.spec.ts +128 -0
- package/src/lib/shared/components/radio/radio.stories.ts +128 -0
- package/src/lib/shared/components/slide-toggle/slide-toggle.component.css +135 -0
- package/src/lib/shared/components/slide-toggle/slide-toggle.component.html +48 -0
- package/src/lib/shared/components/slide-toggle/slide-toggle.component.spec.ts +155 -0
- package/src/lib/shared/components/slide-toggle/slide-toggle.component.ts +233 -0
- package/src/lib/shared/components/slide-toggle/slide-toggle.stories.ts +165 -0
- package/types/ngx-opalbytes-components.d.ts +161 -3
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Component, Input } from "@angular/core";
|
|
2
|
+
import { ComponentFixture, TestBed } from "@angular/core/testing";
|
|
3
|
+
import { FormsModule } from "@angular/forms";
|
|
4
|
+
|
|
5
|
+
import { CaoRadioButtonComponent } from "./radio-button.component";
|
|
6
|
+
import { CaoRadioGroupComponent } from "./radio-group.component";
|
|
7
|
+
|
|
8
|
+
@Component({
|
|
9
|
+
standalone: true,
|
|
10
|
+
imports: [FormsModule, CaoRadioGroupComponent, CaoRadioButtonComponent],
|
|
11
|
+
template: `
|
|
12
|
+
<cao-radio-group
|
|
13
|
+
[(ngModel)]="value"
|
|
14
|
+
[isDisabled]="isDisabled"
|
|
15
|
+
ariaLabel="Opções">
|
|
16
|
+
<cao-radio-button value="option-1" label="Opção 1" />
|
|
17
|
+
<cao-radio-button value="option-2" label="Opção 2" />
|
|
18
|
+
<cao-radio-button
|
|
19
|
+
value="option-3"
|
|
20
|
+
label="Opção 3"
|
|
21
|
+
[isDisabled]="isOptionDisabled" />
|
|
22
|
+
</cao-radio-group>
|
|
23
|
+
`,
|
|
24
|
+
})
|
|
25
|
+
class RadioHostComponent {
|
|
26
|
+
@Input() value = "option-1";
|
|
27
|
+
@Input() isDisabled = false;
|
|
28
|
+
@Input() isOptionDisabled = false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("CaoRadioGroupComponent", () => {
|
|
32
|
+
let fixture: ComponentFixture<RadioHostComponent>;
|
|
33
|
+
let host: RadioHostComponent;
|
|
34
|
+
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
await TestBed.configureTestingModule({
|
|
37
|
+
imports: [RadioHostComponent],
|
|
38
|
+
}).compileComponents();
|
|
39
|
+
|
|
40
|
+
fixture = TestBed.createComponent(RadioHostComponent);
|
|
41
|
+
host = fixture.componentInstance;
|
|
42
|
+
fixture.detectChanges();
|
|
43
|
+
await fixture.whenStable();
|
|
44
|
+
fixture.changeDetectorRef.detectChanges();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function getInputs(): HTMLInputElement[] {
|
|
48
|
+
return Array.from(fixture.nativeElement.querySelectorAll("input"));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function select(input: HTMLInputElement): void {
|
|
52
|
+
input.checked = true;
|
|
53
|
+
input.dispatchEvent(new Event("change"));
|
|
54
|
+
fixture.detectChanges();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
it("should create the group and radio buttons", () => {
|
|
58
|
+
expect(fixture.nativeElement.querySelector("cao-radio-group")).toBeTruthy();
|
|
59
|
+
expect(fixture.nativeElement.querySelectorAll("cao-radio-button").length).toBe(3);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should reflect the initial value", () => {
|
|
63
|
+
const inputs = getInputs();
|
|
64
|
+
|
|
65
|
+
expect(inputs[0].checked).toBe(true);
|
|
66
|
+
expect(inputs[1].checked).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should select an option and emit the value", () => {
|
|
70
|
+
const group = fixture.debugElement.query(
|
|
71
|
+
(element) => element.componentInstance instanceof CaoRadioGroupComponent,
|
|
72
|
+
).componentInstance as CaoRadioGroupComponent;
|
|
73
|
+
const valueChange = vi.fn();
|
|
74
|
+
const selectionChange = vi.fn();
|
|
75
|
+
|
|
76
|
+
group.valueChange.subscribe(valueChange);
|
|
77
|
+
group.selectionChange.subscribe(selectionChange);
|
|
78
|
+
|
|
79
|
+
select(getInputs()[1]);
|
|
80
|
+
|
|
81
|
+
expect(host.value).toBe("option-2");
|
|
82
|
+
expect(valueChange).toHaveBeenCalledWith("option-2");
|
|
83
|
+
expect(selectionChange).toHaveBeenCalledWith(
|
|
84
|
+
expect.objectContaining({ value: "option-2" }),
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should disable every option when the group is disabled", () => {
|
|
89
|
+
fixture.componentRef.setInput("isDisabled", true);
|
|
90
|
+
fixture.detectChanges();
|
|
91
|
+
|
|
92
|
+
const inputs = getInputs();
|
|
93
|
+
expect(inputs.every((input) => input.disabled)).toBe(true);
|
|
94
|
+
|
|
95
|
+
select(inputs[1]);
|
|
96
|
+
expect(host.value).toBe("option-1");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should not select an individually disabled option", () => {
|
|
100
|
+
fixture.componentRef.setInput("isOptionDisabled", true);
|
|
101
|
+
fixture.detectChanges();
|
|
102
|
+
|
|
103
|
+
const inputs = getInputs();
|
|
104
|
+
expect(inputs[2].disabled).toBe(true);
|
|
105
|
+
|
|
106
|
+
select(inputs[2]);
|
|
107
|
+
expect(host.value).toBe("option-1");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should expose radiogroup accessibility attributes", () => {
|
|
111
|
+
const group: HTMLElement =
|
|
112
|
+
fixture.nativeElement.querySelector("cao-radio-group");
|
|
113
|
+
|
|
114
|
+
expect(group.getAttribute("role")).toBe("radiogroup");
|
|
115
|
+
expect(group.getAttribute("aria-label")).toBe("Opções");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should support ControlValueAccessor on the group", () => {
|
|
119
|
+
const group = fixture.debugElement.query(
|
|
120
|
+
(element) => element.componentInstance instanceof CaoRadioGroupComponent,
|
|
121
|
+
).componentInstance as CaoRadioGroupComponent;
|
|
122
|
+
|
|
123
|
+
group.writeValue("option-2");
|
|
124
|
+
fixture.detectChanges();
|
|
125
|
+
|
|
126
|
+
expect(getInputs()[1].checked).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { FormsModule } from '@angular/forms';
|
|
2
|
+
|
|
3
|
+
import type { Meta, StoryObj } from '@storybook/angular-vite';
|
|
4
|
+
import { fn } from 'storybook/test';
|
|
5
|
+
|
|
6
|
+
import { CaoRadioButtonComponent } from './radio-button.component';
|
|
7
|
+
import { CaoRadioGroupComponent } from './radio-group.component';
|
|
8
|
+
|
|
9
|
+
const meta: Meta<CaoRadioGroupComponent> = {
|
|
10
|
+
title: 'Componentes/Radio',
|
|
11
|
+
component: CaoRadioGroupComponent,
|
|
12
|
+
tags: ['autodocs'],
|
|
13
|
+
decorators: [
|
|
14
|
+
(story) => ({
|
|
15
|
+
...story,
|
|
16
|
+
moduleMetadata: {
|
|
17
|
+
imports: [FormsModule, CaoRadioGroupComponent, CaoRadioButtonComponent],
|
|
18
|
+
},
|
|
19
|
+
}),
|
|
20
|
+
],
|
|
21
|
+
argTypes: {
|
|
22
|
+
orientation: { control: 'select', options: ['vertical', 'horizontal'] },
|
|
23
|
+
labelPosition: { control: 'select', options: ['before', 'after'] },
|
|
24
|
+
isDisabled: { control: 'boolean' },
|
|
25
|
+
disabled: { control: 'boolean' },
|
|
26
|
+
required: { control: 'boolean' },
|
|
27
|
+
color: { control: 'color' },
|
|
28
|
+
unselectedColor: { control: 'color' },
|
|
29
|
+
disabledColor: { control: 'color' },
|
|
30
|
+
labelColor: { control: 'color' },
|
|
31
|
+
disabledLabelColor: { control: 'color' },
|
|
32
|
+
focusRingColor: { control: 'color' },
|
|
33
|
+
size: { control: 'text' },
|
|
34
|
+
animationDuration: { control: 'text' },
|
|
35
|
+
gap: { control: 'text' },
|
|
36
|
+
},
|
|
37
|
+
args: {
|
|
38
|
+
value: 'option-1',
|
|
39
|
+
orientation: 'vertical',
|
|
40
|
+
labelPosition: 'after',
|
|
41
|
+
isDisabled: false,
|
|
42
|
+
required: false,
|
|
43
|
+
valueChange: fn(),
|
|
44
|
+
selectionChange: fn(),
|
|
45
|
+
},
|
|
46
|
+
render: (args) => ({
|
|
47
|
+
props: args,
|
|
48
|
+
template: `
|
|
49
|
+
<cao-radio-group
|
|
50
|
+
[(ngModel)]="value"
|
|
51
|
+
[orientation]="orientation"
|
|
52
|
+
[labelPosition]="labelPosition"
|
|
53
|
+
[isDisabled]="isDisabled"
|
|
54
|
+
[required]="required"
|
|
55
|
+
[color]="color"
|
|
56
|
+
[unselectedColor]="unselectedColor"
|
|
57
|
+
[disabledColor]="disabledColor"
|
|
58
|
+
[labelColor]="labelColor"
|
|
59
|
+
[disabledLabelColor]="disabledLabelColor"
|
|
60
|
+
[focusRingColor]="focusRingColor"
|
|
61
|
+
[size]="size"
|
|
62
|
+
[animationDuration]="animationDuration"
|
|
63
|
+
[gap]="gap"
|
|
64
|
+
(valueChange)="valueChange($event)"
|
|
65
|
+
(selectionChange)="selectionChange($event)">
|
|
66
|
+
<cao-radio-button value="option-1" label="Opção 1"></cao-radio-button>
|
|
67
|
+
<cao-radio-button value="option-2" label="Opção 2"></cao-radio-button>
|
|
68
|
+
<cao-radio-button value="option-3" label="Opção 3"></cao-radio-button>
|
|
69
|
+
</cao-radio-group>
|
|
70
|
+
`,
|
|
71
|
+
}),
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default meta;
|
|
75
|
+
type Story = StoryObj<CaoRadioGroupComponent>;
|
|
76
|
+
|
|
77
|
+
export const Padrao: Story = {};
|
|
78
|
+
|
|
79
|
+
export const Horizontal: Story = {
|
|
80
|
+
args: {
|
|
81
|
+
orientation: 'horizontal',
|
|
82
|
+
gap: '16px',
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const Desabilitado: Story = {
|
|
87
|
+
args: {
|
|
88
|
+
isDisabled: true,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const OpcaoDesabilitada: Story = {
|
|
93
|
+
render: (args) => ({
|
|
94
|
+
props: args,
|
|
95
|
+
template: `
|
|
96
|
+
<cao-radio-group
|
|
97
|
+
[(ngModel)]="value"
|
|
98
|
+
[orientation]="orientation"
|
|
99
|
+
[labelPosition]="labelPosition"
|
|
100
|
+
[isDisabled]="isDisabled"
|
|
101
|
+
[color]="color"
|
|
102
|
+
[unselectedColor]="unselectedColor"
|
|
103
|
+
[disabledColor]="disabledColor"
|
|
104
|
+
(valueChange)="valueChange($event)"
|
|
105
|
+
(selectionChange)="selectionChange($event)">
|
|
106
|
+
<cao-radio-button value="option-1" label="Disponível"></cao-radio-button>
|
|
107
|
+
<cao-radio-button value="option-2" label="Indisponível" [isDisabled]="true"></cao-radio-button>
|
|
108
|
+
<cao-radio-button value="option-3" label="Disponível"></cao-radio-button>
|
|
109
|
+
</cao-radio-group>
|
|
110
|
+
`,
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const CoresCustomizadas: Story = {
|
|
115
|
+
args: {
|
|
116
|
+
color: '#16a34a',
|
|
117
|
+
unselectedColor: '#94a3b8',
|
|
118
|
+
labelColor: '#166534',
|
|
119
|
+
size: '24px',
|
|
120
|
+
gap: '12px',
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const LabelAntes: Story = {
|
|
125
|
+
args: {
|
|
126
|
+
labelPosition: 'before',
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
:host {
|
|
2
|
+
--cao-slide-toggle-track: #d1d5db;
|
|
3
|
+
--cao-slide-toggle-track-checked: #4f46e5;
|
|
4
|
+
--cao-slide-toggle-thumb: #ffffff;
|
|
5
|
+
--cao-slide-toggle-thumb-checked: #ffffff;
|
|
6
|
+
--cao-slide-toggle-track-disabled: #e5e7eb;
|
|
7
|
+
--cao-slide-toggle-thumb-disabled: #f3f4f6;
|
|
8
|
+
--cao-slide-toggle-label: #111827;
|
|
9
|
+
--cao-slide-toggle-label-disabled: #9ca3af;
|
|
10
|
+
--cao-slide-toggle-focus-ring: rgba(79, 70, 229, 0.35);
|
|
11
|
+
--cao-slide-toggle-track-width: 36px;
|
|
12
|
+
--cao-slide-toggle-track-height: 20px;
|
|
13
|
+
--cao-slide-toggle-thumb-size: 16px;
|
|
14
|
+
--cao-slide-toggle-thumb-icon-size: 12px;
|
|
15
|
+
--cao-slide-toggle-thumb-icon-color: currentColor;
|
|
16
|
+
--cao-slide-toggle-duration: 200ms;
|
|
17
|
+
|
|
18
|
+
display: inline-flex;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.cao-slide-toggle {
|
|
22
|
+
display: inline-flex;
|
|
23
|
+
align-items: center;
|
|
24
|
+
gap: 8px;
|
|
25
|
+
cursor: pointer;
|
|
26
|
+
user-select: none;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.cao-slide-toggle--disabled {
|
|
30
|
+
cursor: not-allowed;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.cao-slide-toggle__input {
|
|
34
|
+
position: absolute;
|
|
35
|
+
width: 1px;
|
|
36
|
+
height: 1px;
|
|
37
|
+
margin: -1px;
|
|
38
|
+
padding: 0;
|
|
39
|
+
border: 0;
|
|
40
|
+
overflow: hidden;
|
|
41
|
+
clip: rect(0 0 0 0);
|
|
42
|
+
clip-path: inset(50%);
|
|
43
|
+
white-space: nowrap;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.cao-slide-toggle__track {
|
|
47
|
+
position: relative;
|
|
48
|
+
flex: none;
|
|
49
|
+
width: var(--cao-slide-toggle-track-width);
|
|
50
|
+
height: var(--cao-slide-toggle-track-height);
|
|
51
|
+
border-radius: 999px;
|
|
52
|
+
background-color: var(--cao-slide-toggle-track);
|
|
53
|
+
transition: background-color var(--cao-slide-toggle-duration) ease;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.cao-slide-toggle__thumb {
|
|
57
|
+
position: absolute;
|
|
58
|
+
top: 50%;
|
|
59
|
+
left: 2px;
|
|
60
|
+
display: flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
width: var(--cao-slide-toggle-thumb-size);
|
|
64
|
+
height: var(--cao-slide-toggle-thumb-size);
|
|
65
|
+
overflow: hidden;
|
|
66
|
+
border-radius: 50%;
|
|
67
|
+
background-color: var(--cao-slide-toggle-thumb);
|
|
68
|
+
transform: translateY(-50%);
|
|
69
|
+
transition:
|
|
70
|
+
transform var(--cao-slide-toggle-duration) ease,
|
|
71
|
+
background-color var(--cao-slide-toggle-duration) ease;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.cao-slide-toggle__thumb-icon {
|
|
75
|
+
flex: none;
|
|
76
|
+
width: var(--cao-slide-toggle-thumb-icon-size);
|
|
77
|
+
height: var(--cao-slide-toggle-thumb-icon-size);
|
|
78
|
+
color: var(--cao-slide-toggle-thumb-icon-color);
|
|
79
|
+
pointer-events: none;
|
|
80
|
+
object-fit: contain;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.cao-slide-toggle__label {
|
|
84
|
+
color: var(--cao-slide-toggle-label);
|
|
85
|
+
font-size: 0.875rem;
|
|
86
|
+
line-height: 1.25rem;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.cao-slide-toggle__label:empty {
|
|
90
|
+
display: none;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.cao-slide-toggle--checked .cao-slide-toggle__track {
|
|
94
|
+
background-color: var(--cao-slide-toggle-track-checked);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.cao-slide-toggle--checked .cao-slide-toggle__thumb {
|
|
98
|
+
background-color: var(--cao-slide-toggle-thumb-checked);
|
|
99
|
+
transform: translate(
|
|
100
|
+
calc(
|
|
101
|
+
var(--cao-slide-toggle-track-width) -
|
|
102
|
+
var(--cao-slide-toggle-thumb-size) -
|
|
103
|
+
4px
|
|
104
|
+
),
|
|
105
|
+
-50%
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.cao-slide-toggle--disabled .cao-slide-toggle__track {
|
|
110
|
+
background-color: var(--cao-slide-toggle-track-disabled);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.cao-slide-toggle--disabled .cao-slide-toggle__thumb {
|
|
114
|
+
background-color: var(--cao-slide-toggle-thumb-disabled);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.cao-slide-toggle--disabled .cao-slide-toggle__thumb-icon {
|
|
118
|
+
opacity: 0.65;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.cao-slide-toggle--disabled .cao-slide-toggle__label {
|
|
122
|
+
color: var(--cao-slide-toggle-label-disabled);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.cao-slide-toggle__input:focus-visible + .cao-slide-toggle__track {
|
|
126
|
+
box-shadow: 0 0 0 3px var(--cao-slide-toggle-focus-ring);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.cao-slide-toggle--label-before .cao-slide-toggle__track {
|
|
130
|
+
order: 2;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.cao-slide-toggle--label-before .cao-slide-toggle__label {
|
|
134
|
+
order: 1;
|
|
135
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<label
|
|
2
|
+
class="cao-slide-toggle"
|
|
3
|
+
[class.cao-slide-toggle--checked]="checked"
|
|
4
|
+
[class.cao-slide-toggle--disabled]="isDisabled"
|
|
5
|
+
[class.cao-slide-toggle--label-before]="labelPosition === 'before'">
|
|
6
|
+
<input
|
|
7
|
+
#input
|
|
8
|
+
class="cao-slide-toggle__input"
|
|
9
|
+
type="checkbox"
|
|
10
|
+
role="switch"
|
|
11
|
+
[checked]="checked"
|
|
12
|
+
[disabled]="isDisabled"
|
|
13
|
+
[required]="required"
|
|
14
|
+
[attr.name]="name || null"
|
|
15
|
+
[attr.id]="id || null"
|
|
16
|
+
[attr.aria-label]="ariaLabel || null"
|
|
17
|
+
[attr.data-cy]="dataCy || null"
|
|
18
|
+
(change)="onInputChange($event)"
|
|
19
|
+
(blur)="onBlur()"
|
|
20
|
+
(keydown.enter)="onEnter($event)" />
|
|
21
|
+
|
|
22
|
+
<span class="cao-slide-toggle__track" aria-hidden="true">
|
|
23
|
+
<span class="cao-slide-toggle__thumb">
|
|
24
|
+
@if (resolvedThumbIcon) {
|
|
25
|
+
@if (isLucideThumbIcon) {
|
|
26
|
+
<svg
|
|
27
|
+
class="cao-slide-toggle__thumb-icon"
|
|
28
|
+
[lucideIcon]="resolvedThumbIcon"
|
|
29
|
+
[size]="thumbIconSize"
|
|
30
|
+
[strokeWidth]="thumbIconStrokeWidth"></svg>
|
|
31
|
+
} @else {
|
|
32
|
+
<img
|
|
33
|
+
class="cao-slide-toggle__thumb-icon"
|
|
34
|
+
alt=""
|
|
35
|
+
[src]="resolvedThumbIcon" />
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
</span>
|
|
39
|
+
</span>
|
|
40
|
+
|
|
41
|
+
<span class="cao-slide-toggle__label">
|
|
42
|
+
@if (label) {
|
|
43
|
+
{{ label }}
|
|
44
|
+
} @else {
|
|
45
|
+
<ng-content />
|
|
46
|
+
}
|
|
47
|
+
</span>
|
|
48
|
+
</label>
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from "@angular/core/testing";
|
|
2
|
+
import { LucideCheck, LucideX, provideLucideIcons } from "@lucide/angular";
|
|
3
|
+
|
|
4
|
+
import { CaoSlideToggleComponent } from "./slide-toggle.component";
|
|
5
|
+
|
|
6
|
+
describe("CaoSlideToggleComponent", () => {
|
|
7
|
+
let component: CaoSlideToggleComponent;
|
|
8
|
+
let fixture: ComponentFixture<CaoSlideToggleComponent>;
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
await TestBed.configureTestingModule({
|
|
12
|
+
imports: [CaoSlideToggleComponent],
|
|
13
|
+
providers: [provideLucideIcons(LucideCheck, LucideX)],
|
|
14
|
+
}).compileComponents();
|
|
15
|
+
|
|
16
|
+
fixture = TestBed.createComponent(CaoSlideToggleComponent);
|
|
17
|
+
component = fixture.componentInstance;
|
|
18
|
+
fixture.detectChanges();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should create", () => {
|
|
22
|
+
expect(component).toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should toggle and emit changes", () => {
|
|
26
|
+
const checkedChange = vi.fn();
|
|
27
|
+
const toggleChange = vi.fn();
|
|
28
|
+
const input: HTMLInputElement =
|
|
29
|
+
fixture.nativeElement.querySelector("input");
|
|
30
|
+
|
|
31
|
+
component.checkedChange.subscribe(checkedChange);
|
|
32
|
+
component.toggleChange.subscribe(toggleChange);
|
|
33
|
+
|
|
34
|
+
input.checked = true;
|
|
35
|
+
input.dispatchEvent(new Event("change"));
|
|
36
|
+
fixture.detectChanges();
|
|
37
|
+
|
|
38
|
+
expect(component.checked).toBe(true);
|
|
39
|
+
expect(checkedChange).toHaveBeenCalledWith(true);
|
|
40
|
+
expect(toggleChange).toHaveBeenCalledWith({
|
|
41
|
+
checked: true,
|
|
42
|
+
source: component,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should not toggle when disabled", () => {
|
|
47
|
+
const input: HTMLInputElement =
|
|
48
|
+
fixture.nativeElement.querySelector("input");
|
|
49
|
+
component.isDisabled = true;
|
|
50
|
+
fixture.detectChanges();
|
|
51
|
+
|
|
52
|
+
input.checked = true;
|
|
53
|
+
input.dispatchEvent(new Event("change"));
|
|
54
|
+
fixture.detectChanges();
|
|
55
|
+
|
|
56
|
+
expect(component.checked).toBe(false);
|
|
57
|
+
expect(input.disabled).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should support ControlValueAccessor", () => {
|
|
61
|
+
const onChange = vi.fn();
|
|
62
|
+
const onTouched = vi.fn();
|
|
63
|
+
|
|
64
|
+
component.registerOnChange(onChange);
|
|
65
|
+
component.registerOnTouched(onTouched);
|
|
66
|
+
component.writeValue(true);
|
|
67
|
+
component.onBlur();
|
|
68
|
+
|
|
69
|
+
expect(component.checked).toBe(true);
|
|
70
|
+
expect(onTouched).toHaveBeenCalled();
|
|
71
|
+
|
|
72
|
+
component.toggle();
|
|
73
|
+
expect(onChange).toHaveBeenCalledWith(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should enable and disable programmatically", () => {
|
|
77
|
+
component.disable();
|
|
78
|
+
expect(component.isDisabled).toBe(true);
|
|
79
|
+
|
|
80
|
+
component.enable();
|
|
81
|
+
expect(component.isDisabled).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should disable through setDisabledState", () => {
|
|
85
|
+
component.setDisabledState(true);
|
|
86
|
+
expect(component.isDisabled).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should render Lucide icons for both states", () => {
|
|
90
|
+
fixture.componentRef.setInput("thumbIcon", "x");
|
|
91
|
+
fixture.componentRef.setInput("thumbCheckedIcon", "check");
|
|
92
|
+
fixture.componentRef.setInput("isLucideThumbIcon", true);
|
|
93
|
+
fixture.componentRef.setInput("thumbIconColor", "#6b7280");
|
|
94
|
+
fixture.componentRef.setInput("thumbCheckedIconColor", "#16a34a");
|
|
95
|
+
fixture.componentRef.setInput("checked", true);
|
|
96
|
+
fixture.detectChanges();
|
|
97
|
+
|
|
98
|
+
const icon = fixture.nativeElement.querySelector(
|
|
99
|
+
".cao-slide-toggle__thumb svg",
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
expect(
|
|
103
|
+
fixture.nativeElement.querySelector(".cao-slide-toggle__thumb img"),
|
|
104
|
+
).toBeNull();
|
|
105
|
+
expect(icon).toBeTruthy();
|
|
106
|
+
expect(component.resolvedThumbIcon).toBe("check");
|
|
107
|
+
expect(component.resolvedThumbIconColor).toBe("#16a34a");
|
|
108
|
+
|
|
109
|
+
fixture.componentRef.setInput("checked", false);
|
|
110
|
+
fixture.detectChanges();
|
|
111
|
+
|
|
112
|
+
expect(component.resolvedThumbIcon).toBe("x");
|
|
113
|
+
expect(component.resolvedThumbIconColor).toBe("#6b7280");
|
|
114
|
+
expect(
|
|
115
|
+
fixture.nativeElement.querySelector(".cao-slide-toggle__thumb svg"),
|
|
116
|
+
).toBeTruthy();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should fall back to thumbIcon when thumbCheckedIcon is empty", () => {
|
|
120
|
+
fixture.componentRef.setInput("thumbIcon", "check");
|
|
121
|
+
fixture.componentRef.setInput("isLucideThumbIcon", true);
|
|
122
|
+
fixture.componentRef.setInput("checked", true);
|
|
123
|
+
fixture.detectChanges();
|
|
124
|
+
|
|
125
|
+
expect(component.resolvedThumbIcon).toBe("check");
|
|
126
|
+
expect(
|
|
127
|
+
fixture.nativeElement.querySelector(".cao-slide-toggle__thumb svg"),
|
|
128
|
+
).toBeTruthy();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should render an image when isLucideThumbIcon is false", () => {
|
|
132
|
+
fixture.componentRef.setInput("thumbIcon", "icon.svg");
|
|
133
|
+
fixture.detectChanges();
|
|
134
|
+
|
|
135
|
+
const image: HTMLImageElement = fixture.nativeElement.querySelector(
|
|
136
|
+
".cao-slide-toggle__thumb img",
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
expect(image).toBeTruthy();
|
|
140
|
+
expect(image.getAttribute("src")).toBe("icon.svg");
|
|
141
|
+
expect(
|
|
142
|
+
fixture.nativeElement.querySelector(".cao-slide-toggle__thumb svg"),
|
|
143
|
+
).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("should not render an icon when no thumb icon is provided", () => {
|
|
147
|
+
fixture.detectChanges();
|
|
148
|
+
|
|
149
|
+
expect(
|
|
150
|
+
fixture.nativeElement.querySelector(
|
|
151
|
+
".cao-slide-toggle__thumb svg, .cao-slide-toggle__thumb img",
|
|
152
|
+
),
|
|
153
|
+
).toBeNull();
|
|
154
|
+
});
|
|
155
|
+
});
|