// import { FRoot } from "@nonfx/flow-core"; import { html, PropertyValueMap, unsafeCSS } from "lit"; import { customElement, property } from "lit/decorators.js"; import { RadioOption, RadioOptions } from "../../types"; import eleStyle from "./f-radio-group.scss?inline"; import globalStyle from "./f-radio-group-global.scss?inline"; import { FDiv, FRadio, FRoot, FText } from "@nonfx/flow-core"; import { isEqual } from "lodash-es"; import { injectCss } from "@nonfx/flow-core-config"; injectCss("f-checkbox-group", globalStyle); export const radioGroupStyles = eleStyle; /** * @summary Text component includes Headings, titles, body texts and links. */ @customElement("f-radio-group") export class FRadioGroup extends FRoot { /** * css loaded from scss file */ static styles = [ unsafeCSS(eleStyle), unsafeCSS(globalStyle), ...FDiv.styles, ...FText.styles, ...FRadio.styles ]; /** * @attribute Controls size of all input elements within the form */ @property({ reflect: false, type: Array }) options: RadioOptions = []; /** * @attribute Controls size of all input elements within the form */ @property({ reflect: true, type: String }) state?: "primary" | "default" | "success" | "warning" | "danger" = "default"; /** * @attribute Controls size of all input elements within the form */ @property({ reflect: true, type: String }) value?: RadioOption; /** * @attribute Decides the direction of the input elements within the group. */ @property({ type: String, reflect: true }) direction?: "vertical" | "horizontal" = "vertical"; /** * @attribute decides the gap between elements of a group */ @property({ type: String, reflect: true }) gap?: "large" | "medium" | "small" | "x-small" = "small"; @property({ type: String, reflect: true }) helperText?: string; /** * @attribute The disabled attribute can be set to keep a user from clicking on the radio group. */ @property({ reflect: true, type: Boolean }) disabled?: boolean = false; handleChange(option: RadioOption) { const event = new CustomEvent("input", { detail: { value: option } }); this.value = option; this.dispatchEvent(event); } isChecked(option: RadioOption) { return isEqual(option, this.value) ? "selected" : "unselected"; } protected willUpdate(changedProperties: PropertyValueMap | Map): void { super.willUpdate(changedProperties); this.role = "radiogroup"; } render() { /** * Final html to render */ return html`
${this.options?.map( item => html` this.handleChange(item)} .state=${this.state} ?disabled=${item.disabled} > ${typeof item.title === "object" ? item.title : html`${item.title ?? item.id}`} ${item.description ? html` ${item.description}` : ""} ${item.iconTooltip ? html` ` : ""} ${item?.subTitle ? html` ${item?.subTitle} ` : ""} ` )}
${this?.helperText ? html`${this?.helperText}` : html``}
`; } }