Skip to content

Commit 4421826

Browse files
saraparsa13Sara.Parsaee
and
Sara.Parsaee
authored
feat: add redispatch to the input based elements (#187)
* feat: add redispatch to the input based elements * fix: remove select for checkbox and radio --------- Co-authored-by: Sara.Parsaee <Sara.Parsaee@Tapsi.cab>
1 parent acdd7e2 commit 4421826

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/checkbox/checkbox.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LitElement, PropertyValues, html, nothing } from 'lit';
22
import { property } from 'lit/decorators.js';
3+
import { redispatchEvent } from '../utils/utils';
34

45
export class Checkbox extends LitElement {
56
static override shadowRootOptions: ShadowRootInit = {
@@ -52,6 +53,10 @@ export class Checkbox extends LitElement {
5253
this.checked = state === 'true';
5354
}
5455

56+
private redispatchEvent(event: Event) {
57+
redispatchEvent(this, event);
58+
}
59+
5560
render() {
5661
return html`
5762
<input
@@ -69,6 +74,7 @@ export class Checkbox extends LitElement {
6974
.indeterminate=${this.indeterminate}
7075
.checked=${this.checked}
7176
@input=${this.handleInput}
77+
@change=${this.redispatchEvent}
7278
/>
7379
${this.renderCheckIcon()} ${this.renderIndeterminateIcon()}
7480
`;

src/radio/radio.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LitElement, PropertyValues, html, nothing } from 'lit';
22
import { property } from 'lit/decorators.js';
3+
import { redispatchEvent } from '../utils/utils';
34

45
export class Radio extends LitElement {
56
static override shadowRootOptions: ShadowRootInit = {
@@ -57,6 +58,10 @@ export class Radio extends LitElement {
5758
this.checked = state === 'true';
5859
}
5960

61+
private redispatchEvent(event: Event) {
62+
redispatchEvent(this, event);
63+
}
64+
6065
render() {
6166
return html`
6267
<input
@@ -72,6 +77,7 @@ export class Radio extends LitElement {
7277
?disabled=${this.disabled}
7378
.checked=${this.checked}
7479
@input=${this.handleInput}
80+
@change=${this.redispatchEvent}
7581
/>
7682
${this.renderCheckIcon()}
7783
`;

src/text-field/text-field.ts

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { property } from 'lit/decorators.js';
33
import { ifDefined } from 'lit/directives/if-defined.js';
44
import { live } from 'lit/directives/live.js';
55
import { Input } from '../input';
6+
import { redispatchEvent } from '../utils/utils';
67

78
export class TextField extends Input {
89
@property({ type: String }) inputmode?:
@@ -41,6 +42,10 @@ export class TextField extends Input {
4142

4243
@property({ type: String }) autocomplete?: string; // TODO: add type
4344

45+
private redispatchEvent(event: Event) {
46+
redispatchEvent(this, event);
47+
}
48+
4449
// TODO: check if using generic ids for caption and input is ok
4550
protected renderInput(): TemplateResult {
4651
return html`
@@ -66,6 +71,8 @@ export class TextField extends Input {
6671
type=${this.type}
6772
.value=${live(this.value)}
6873
@input=${this.handleInput}
74+
@change=${this.redispatchEvent}
75+
@select=${this.redispatchEvent}
6976
/>
7077
<slot part="trailing" name="trailing"></slot>
7178
`;

src/utils/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,23 @@ export function debounce<T>(func: (...args: T[]) => void, delay: number) {
99
}, delay);
1010
};
1111
}
12+
13+
/**
14+
* Re-dispatches an event from the provided element.
15+
* source code: https://github.com/material-components/material-web/blob/a9ee4f5bc1d6702e5dc352eefed13a1d849577e3/internal/events/redispatch-event.ts#L28
16+
*/
17+
export function redispatchEvent(element: Element, event: Event) {
18+
// For bubbling events in SSR light DOM (or composed), stop their propagation
19+
// and dispatch the copy.
20+
if (event.bubbles && (!element.shadowRoot || event.composed)) {
21+
event.stopPropagation();
22+
}
23+
24+
const copy = Reflect.construct(event.constructor, [event.type, event]);
25+
const dispatched = element.dispatchEvent(copy);
26+
if (!dispatched) {
27+
event.preventDefault();
28+
}
29+
30+
return dispatched;
31+
}

0 commit comments

Comments
 (0)