-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sd-radio-group and sd-option (#66)
* feat: add sd-radio * fix: equality checking * feat: add support for typed options * feat: add custom data for sd-option and sd-radio-list * refactor: rename radio list to radio group * docs: update JSDocs * refactor: update element name * refactor: update file name to reflect spacing --------- Co-authored-by: Richard Herman <[email protected]>
- Loading branch information
Showing
12 changed files
with
558 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import "./field"; | ||
import "./label"; | ||
import "./option"; | ||
import "./radio-group"; | ||
import "./switch"; | ||
import "./textfield"; | ||
import "./text-field"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { LitElement } from "lit"; | ||
import { customElement, property } from "lit/decorators.js"; | ||
|
||
import { parseBoolean, parseNumber } from "../../common/utils"; | ||
|
||
/** | ||
* Non-visual element that provides information for an option. | ||
*/ | ||
@customElement("sd-option") | ||
export class SDOptionElement extends LitElement { | ||
/** | ||
* Private backing field for {@link SDOptionElement.value}. | ||
*/ | ||
#value: boolean | number | string | undefined; | ||
|
||
/** | ||
* Determines whether the option is disabled; default `false`. | ||
*/ | ||
@property({ type: Boolean }) | ||
public accessor disabled: boolean = false; | ||
|
||
/** | ||
* Label that represents the option; read from the `innerText` of the element. | ||
* @returns The label. | ||
*/ | ||
public get label(): string { | ||
return this.innerText; | ||
} | ||
|
||
/** | ||
* Type of the value; allows for the value to be converted to a boolean or number. | ||
*/ | ||
@property() | ||
public accessor type: "boolean" | "number" | "string" = "string"; | ||
|
||
/** | ||
* Untyped value, as defined by the `value` attribute; use `value` property for the typed-value. | ||
*/ | ||
@property({ attribute: "value" }) | ||
public accessor htmlValue: string | undefined = undefined; | ||
|
||
/** | ||
* Value of the option. | ||
* @returns The value. | ||
*/ | ||
public get value(): boolean | number | string | undefined { | ||
return this.#value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected override willUpdate(_changedProperties: Map<PropertyKey, unknown>): void { | ||
super.willUpdate(_changedProperties); | ||
|
||
if (_changedProperties.has("type") || _changedProperties.has("value")) { | ||
if (this.type === "boolean") { | ||
this.#value = parseBoolean(this.htmlValue); | ||
} else if (this.type === "number") { | ||
this.#value = parseNumber(this.htmlValue); | ||
} else { | ||
this.#value = this.htmlValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
/** | ||
* Non-visual element that provides information for an option. | ||
*/ | ||
"sd-option": SDOptionElement; | ||
} | ||
} |
Oops, something went wrong.