import { html, unsafeCSS } from "lit"; import { customElement, property } from "lit/decorators.js"; import { CheckboxOption, CheckboxOptions } from "../../types"; import eleStyle from "./f-checkbox-group.scss?inline"; import globalStyle from "./f-checkbox-group-global.scss?inline"; import { FRoot, FDiv, FText, FCheckbox } from "@nonfx/flow-core"; import { isEqual } from "lodash-es"; export type FCheckboxGroupValue = string[]; export const checkboxGroupStyles = eleStyle; import { injectCss } from "@nonfx/flow-core-config"; injectCss("f-checkbox-group", globalStyle); @customElement("f-checkbox-group") export class FCheckboxGroup extends FRoot { /** * css loaded from scss file */ static styles = [ ...FText.styles, unsafeCSS(globalStyle), unsafeCSS(eleStyle), ...FDiv.styles, ...FCheckbox.styles ]; /** * @attribute Controls size of all input elements within the form */ @property({ reflect: false, type: Array }) options: CheckboxOptions = []; /** * @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: Array, hasChanged(newVal: FCheckboxGroupValue, oldVal: FCheckboxGroupValue) { return JSON.stringify(newVal) !== JSON.stringify(oldVal); } }) value?: CheckboxOptions; /** * @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 checkbox group. */ @property({ reflect: true, type: Boolean }) disabled?: boolean = false; handleChange(e: CustomEvent, option: CheckboxOption) { e.stopPropagation(); let tempValues = this.value && this.value?.length > 0 ? [...this.value] : []; if (this.isChecked(option) === "unchecked") { tempValues.push(option); } else { tempValues = tempValues.filter(item => !isEqual(item, option)); } const event = new CustomEvent("input", { detail: { value: tempValues } }); this.value = tempValues; this.dispatchEvent(event); } isChecked(option: CheckboxOption) { return this.value?.find(item => isEqual(item, option)) ? "checked" : "unchecked"; } render() { /** * Final html to render */ return html`
${this.options?.map( item => html` this.handleChange(event, 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``}
`; } }