forked from DevCloudFE/ng-devui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.component.ts
More file actions
executable file
·122 lines (112 loc) · 3.11 KB
/
button.component.ts
File metadata and controls
executable file
·122 lines (112 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
AfterContentChecked,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
Output, Renderer2, SimpleChanges,
TemplateRef,
ViewChild, OnChanges
} from '@angular/core';
import { AnimationNumberDuration } from 'ng-devui/utils';
export type IButtonType = 'button' | 'submit' | 'reset';
/**
* 类型中text-dark参数废弃
*/
export type IButtonStyle = 'common' | 'primary' | 'text' | 'text-dark' | 'danger' | 'success' | 'warning';
export type IButtonPosition = 'left' | 'right' | 'default';
export type IButtonSize = 'lg' | 'md' | 'sm' | 'xs';
@Component({
selector: 'd-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
preserveWhitespaces: false,
standalone: false
})
export class ButtonComponent implements AfterContentChecked, OnChanges {
@Input() id: string;
@Input() type: IButtonType = 'button';
@Input() bsStyle: IButtonStyle = 'primary';
@Input() shape: 'circle';
@Input() bsSize: IButtonSize = 'md';
/**
* @deprecated
* 原左右按钮用按钮组实现
*/
@Input() bsPosition: IButtonPosition = 'default';
@Input() bordered: boolean;
@Input() icon: string;
@Input() disabled = false;
@Input() showLoading = false;
@Input() width?: string;
@Input() autofocus = false;
@Input() loadingTemplateRef: TemplateRef<any>;
@Output() btnClick = new EventEmitter<MouseEvent>();
@ViewChild('buttonContent', { static: true }) buttonContent: ElementRef;
@HostListener('click', ['$event'])
handleDisabled($event: Event) {
if (this.disabled) {
$event.preventDefault();
$event.stopImmediatePropagation();
}
}
waveLeft = 0;
waveTop = 0;
showWave = false;
isMouseDown = false;
constructor(
private cd: ChangeDetectorRef,
private renderer: Renderer2,
private el: ElementRef
) {
}
ngOnChanges(changes: SimpleChanges) {
if (changes?.disabled?.currentValue) {
this.renderer.setStyle(
this.el.nativeElement,
'cursor',
'not-allowed'
);
this.renderer.setStyle(
this.el.nativeElement,
'pointer-events',
'none'
);
} else if (!changes?.disabled?.currentValue) {
this.renderer.removeStyle(
this.el.nativeElement,
'cursor'
);
this.renderer.removeStyle(
this.el.nativeElement,
'pointer-events'
);
}
}
// 新增click事件,解决直接在host上使用click,在disabled状态下还能触发事件
onClick(event) {
if (!this.showLoading) {
this.btnClick.emit(event);
}
this.showClickWave(event);
}
showClickWave(event) {
this.waveLeft = event.offsetX;
this.waveTop = event.offsetY;
this.showWave = true;
setTimeout(() => {
this.showWave = false;
this.cd.detectChanges();
}, AnimationNumberDuration.SLOW);
}
ngAfterContentChecked(): void {
this.cd.detectChanges();
}
hasContent() {
return !!this.buttonContent && this.buttonContent.nativeElement && this.buttonContent.nativeElement.innerHTML.trim();
}
}