-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbutton.tsx
More file actions
54 lines (47 loc) · 1.63 KB
/
button.tsx
File metadata and controls
54 lines (47 loc) · 1.63 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
import type { EventEmitter } from '@stencil/core';
import { Component, Element, Event, h, Prop } from '@stencil/core';
import { Button as ButtonComponent } from 'common/Button/Button';
import type { ButtonSizeEnum, ButtonVariantEnum } from '../../../common/Button/button.types';
@Component({
tag: 'mvx-button',
styleUrl: 'button.scss',
shadow: true,
})
export class Button {
@Element() hostElement!: HTMLElement;
@Event() buttonClick: EventEmitter<MouseEvent>;
@Prop() class?: string = '';
@Prop({ attribute: 'data-testid' }) dataTestId?: string = '';
@Prop() disabled?: boolean = false;
@Prop() size?: `${ButtonSizeEnum}` = 'large';
@Prop() variant?: `${ButtonVariantEnum}` = 'primary';
/**
* Stencil reflects `data-testid` to the host element based on the Prop decorator above.
* That causes two DOM nodes with the same `data-testid`: the `<mvx-button>` host and
* the inner `<button>`, which breaks Playwright strict mode. We keep using the
* attribute to populate `dataTestId`, but remove it from the host after render so that
* only the inner `<button>` keeps the `data-testid`.
*/
componentDidRender() {
if (this.hostElement.hasAttribute('data-testid')) {
this.hostElement.removeAttribute('data-testid');
}
}
private handleClick = (event: MouseEvent) => {
this.buttonClick.emit(event);
};
render() {
return (
<ButtonComponent
class={this.class}
data-testid={this.dataTestId}
disabled={this.disabled}
size={this.size}
variant={this.variant}
onClick={this.handleClick}
>
<slot />
</ButtonComponent>
);
}
}