-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add checkbox, radio, toggle components (#175)
* feat: add checkbox * refacto * feat: add radio * Update README.md * feat: add toggle * fix: merge * docs * feat: avoid input to be shrink * feat: add label property, add inputId for radio
- Loading branch information
Showing
9 changed files
with
396 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {Constructor} from './utils'; | ||
import {LitElement, PropertyValues} from 'lit'; | ||
import {property} from 'lit/decorators.js'; | ||
import {BasicEvents} from '../utils/enums'; | ||
|
||
export interface CheckedInputMixinInterface { | ||
checked: boolean; | ||
name: string; | ||
label: string; | ||
getInputElement(): HTMLInputElement | null; | ||
_dispatchChangeEvent(checked: boolean, name: string): void; | ||
refreshInput(): void; | ||
_handleChange(e: {target: HTMLInputElement}): void; | ||
} | ||
|
||
export const CheckedInputMixin = <T extends Constructor<LitElement>>( | ||
superClass: T | ||
) => { | ||
class CheckedInputMixinClass extends superClass { | ||
/** | ||
* Represents the checked state of the input. | ||
* @type {boolean} | ||
*/ | ||
@property({type: Boolean}) | ||
checked = false; | ||
|
||
/** | ||
* Represents the name of the input. | ||
* @type {string} | ||
*/ | ||
@property({type: String}) | ||
name = ''; | ||
|
||
/** | ||
* Represents the label of the input. | ||
* @type {string} | ||
*/ | ||
@property({type: String}) | ||
label = ''; | ||
|
||
getInputElement() { | ||
return this.shadowRoot?.querySelector('input'); | ||
} | ||
|
||
_dispatchChangeEvent(checked: boolean, name: string) { | ||
const inputEvent = new CustomEvent(BasicEvents.CHANGE, { | ||
detail: {checked, name}, | ||
bubbles: true, | ||
composed: true, | ||
}); | ||
this.dispatchEvent(inputEvent); | ||
} | ||
refreshInput() { | ||
const inputElement = this.getInputElement(); | ||
if (inputElement) { | ||
inputElement.checked = this.checked; | ||
} | ||
|
||
/** | ||
* Called when the element’s DOM has been updated and rendered. | ||
* @param {PropertyValues} _changedProperties - The changed properties. | ||
*/ | ||
} | ||
protected override updated(_changedProperties: PropertyValues) { | ||
this.refreshInput(); | ||
super.updated(_changedProperties); | ||
} | ||
|
||
/** | ||
* Handles the change event on the radio. | ||
* @param {Event} e - The change event. | ||
*/ | ||
_handleChange(e: {target: HTMLInputElement}) { | ||
this.checked = e.target.checked; | ||
this._dispatchChangeEvent(this.checked, this.name); | ||
} | ||
} | ||
return CheckedInputMixinClass as Constructor<CheckedInputMixinInterface> & T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.