@fluid-topics/ft-chip-choice 1.1.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ Chip-choice is a component used to make single-select or multi-select selections.
2
+ The appearance of chip-choice is based on the chip component, but the nature of chip-choice is different.
3
+ In multi-select mode, chip-choice behaves like a group of checkboxes.
4
+ In single-select mode, chip-choice behaves like a radiogroup.
5
+
6
+ ## Install
7
+
8
+ ```shell
9
+ npm install @fluid-topics/ft-chip-choice
10
+ yarn add @fluid-topics/ft-chip-choice
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { html } from "lit"
17
+ import "@fluid-topics/ft-chip-choice"
18
+
19
+ function render() {
20
+ return html`
21
+ <ft-chip-choice multiselect>
22
+ <ft-chip-choice-option value="1">Option 1</ft-chip-choice-option>
23
+ <ft-chip-choice-option value="2">Option 2</ft-chip-choice-option>
24
+ <ft-chip-choice-option value="3">Option 3</ft-chip-choice-option>
25
+ </ft-chip-choice>
26
+ `
27
+ }
28
+ ```
@@ -0,0 +1,26 @@
1
+ import { PropertyValues } from "lit";
2
+ import { ElementDefinitionsMap, FtLitElement } from "@fluid-topics/ft-wc-utils";
3
+ import { FtChipChoiceOptionProperties } from "./ft-chip-choice-option.properties";
4
+ import { FtChip } from "@fluid-topics/ft-chip";
5
+ export declare class FtChipChoiceOptionChangeEvent extends CustomEvent<{
6
+ value: string;
7
+ selected: boolean;
8
+ }> {
9
+ constructor(value: string, selected: boolean);
10
+ }
11
+ export declare class FtChipChoiceOption extends FtLitElement implements FtChipChoiceOptionProperties {
12
+ static elementDefinitions: ElementDefinitionsMap;
13
+ name: string;
14
+ value: string;
15
+ label: string;
16
+ selected: boolean;
17
+ dense: boolean;
18
+ multiLine: boolean;
19
+ role: string;
20
+ chip: FtChip;
21
+ static styles: import("lit").CSSResult;
22
+ protected render(): import("lit").TemplateResult<1>;
23
+ private onKeyDown;
24
+ protected update(changedProperties: PropertyValues): void;
25
+ private onChange;
26
+ }
@@ -0,0 +1,91 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { html } from "lit";
8
+ import { property, query } from "lit/decorators.js";
9
+ import { FtLitElement } from "@fluid-topics/ft-wc-utils";
10
+ import { optionStyles } from "./ft-chip-choice-option.styles";
11
+ import { FtChip } from "@fluid-topics/ft-chip";
12
+ export class FtChipChoiceOptionChangeEvent extends CustomEvent {
13
+ constructor(value, selected) {
14
+ super("change", { detail: { value: value, selected: selected }, bubbles: true, composed: true });
15
+ }
16
+ }
17
+ class FtChipChoiceOption extends FtLitElement {
18
+ constructor() {
19
+ super(...arguments);
20
+ this.name = "";
21
+ this.value = "";
22
+ this.label = "";
23
+ this.selected = false;
24
+ this.dense = false;
25
+ this.multiLine = false;
26
+ this.role = "radio";
27
+ }
28
+ render() {
29
+ return html `
30
+ <ft-chip clickable
31
+ role="radio"
32
+ part="chip"
33
+ icon=${this.selected ? "CHECK" : ""}
34
+ label=${this.label}
35
+ ?dense=${this.dense}
36
+ ?highlighted=${this.selected}
37
+ ?multiLine=${this.multiLine}
38
+ @click=${this.onChange}
39
+ @keydown=${this.onKeyDown}
40
+ >
41
+ <slot></slot>
42
+ </ft-chip>
43
+ `;
44
+ }
45
+ onKeyDown(e) {
46
+ // Prevent the page from scrolling on space by default
47
+ if (e.key == " ") {
48
+ e.preventDefault();
49
+ }
50
+ }
51
+ update(changedProperties) {
52
+ super.update(changedProperties);
53
+ if (changedProperties.has("selected")) {
54
+ this.ariaChecked = this.selected ? "true" : "false";
55
+ }
56
+ }
57
+ onChange(event) {
58
+ event.preventDefault();
59
+ this.selected = !this.selected;
60
+ this.dispatchEvent(new FtChipChoiceOptionChangeEvent(this.value, this.selected));
61
+ }
62
+ }
63
+ FtChipChoiceOption.elementDefinitions = {
64
+ "ft-chip": FtChip
65
+ };
66
+ FtChipChoiceOption.styles = optionStyles;
67
+ __decorate([
68
+ property()
69
+ ], FtChipChoiceOption.prototype, "name", void 0);
70
+ __decorate([
71
+ property()
72
+ ], FtChipChoiceOption.prototype, "value", void 0);
73
+ __decorate([
74
+ property()
75
+ ], FtChipChoiceOption.prototype, "label", void 0);
76
+ __decorate([
77
+ property({ type: Boolean })
78
+ ], FtChipChoiceOption.prototype, "selected", void 0);
79
+ __decorate([
80
+ property({ type: Boolean })
81
+ ], FtChipChoiceOption.prototype, "dense", void 0);
82
+ __decorate([
83
+ property({ type: Boolean })
84
+ ], FtChipChoiceOption.prototype, "multiLine", void 0);
85
+ __decorate([
86
+ property({ reflect: true, attribute: "role" })
87
+ ], FtChipChoiceOption.prototype, "role", void 0);
88
+ __decorate([
89
+ query("ft-chip")
90
+ ], FtChipChoiceOption.prototype, "chip", void 0);
91
+ export { FtChipChoiceOption };
@@ -0,0 +1,7 @@
1
+ export interface FtChipChoiceOptionProperties {
2
+ value: string;
3
+ label?: string;
4
+ dense?: boolean;
5
+ selected?: boolean;
6
+ multiline?: boolean;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export declare const FtChipChoiceOptionCssVariables: {
2
+ color: import("@fluid-topics/ft-wc-utils").FtCssVariable;
3
+ borderColor: import("@fluid-topics/ft-wc-utils").FtCssVariable;
4
+ backgroundColor: import("@fluid-topics/ft-wc-utils").FtCssVariable;
5
+ selectedColor: import("@fluid-topics/ft-wc-utils").FtCssVariable;
6
+ selectedBackgroundColor: import("@fluid-topics/ft-wc-utils").FtCssVariable;
7
+ };
8
+ export declare const optionStyles: import("lit").CSSResult;
@@ -0,0 +1,20 @@
1
+ import { css } from "lit";
2
+ import { designSystemVariables, FtCssVariableFactory, setVariable } from "@fluid-topics/ft-wc-utils";
3
+ import { FtChipCssVariables, FtChipHighlightedCssVariables } from "@fluid-topics/ft-chip/build/ft-chip.styles";
4
+ export const FtChipChoiceOptionCssVariables = {
5
+ color: FtCssVariableFactory.create("--ft-chip-choice-option--color", "", "COLOR", designSystemVariables.colorOnSurface),
6
+ borderColor: FtCssVariableFactory.create("--ft-chip-choice-option--border-color", "", "COLOR", designSystemVariables.colorOutline),
7
+ backgroundColor: FtCssVariableFactory.create("--ft-chip-choice-option--background-color", "", "COLOR", designSystemVariables.colorSurface),
8
+ selectedColor: FtCssVariableFactory.create("--ft-chip-choice-option--selected-color", "", "COLOR", designSystemVariables.colorOnPrimary),
9
+ selectedBackgroundColor: FtCssVariableFactory.create("--ft-chip-choice-option--selected-background-color", "", "COLOR", designSystemVariables.colorPrimary),
10
+ };
11
+ // language=CSS
12
+ export const optionStyles = css `
13
+ [part="chip"] {
14
+ ${setVariable(FtChipCssVariables.color, FtChipChoiceOptionCssVariables.color)};
15
+ ${setVariable(FtChipCssVariables.borderColor, FtChipChoiceOptionCssVariables.borderColor)};
16
+ ${setVariable(FtChipCssVariables.backgroundColor, FtChipChoiceOptionCssVariables.backgroundColor)};
17
+ ${setVariable(FtChipHighlightedCssVariables.color, FtChipChoiceOptionCssVariables.selectedColor)};
18
+ ${setVariable(FtChipHighlightedCssVariables.backgroundColor, FtChipChoiceOptionCssVariables.selectedBackgroundColor)};
19
+ }
20
+ `;
@@ -0,0 +1,22 @@
1
+ import { PropertyValues } from "lit";
2
+ import { FtLitElement } from "@fluid-topics/ft-wc-utils";
3
+ import { FtChipChoiceProperties } from "./ft-chip-choice.properties";
4
+ import { FtChipChoiceOption } from "./ft-chip-choice-option";
5
+ export declare class FtChipChoiceChangeEvent extends CustomEvent<{
6
+ value: Set<string>;
7
+ }> {
8
+ constructor(value: Set<string>);
9
+ }
10
+ export declare class FtChipChoice extends FtLitElement implements FtChipChoiceProperties {
11
+ static styles: import("lit").CSSResult;
12
+ name: string;
13
+ multiselect: boolean;
14
+ role: string;
15
+ ariaLabelledBy: string;
16
+ chipChoiceOptions: Array<FtChipChoiceOption>;
17
+ selectedOptions: Set<string>;
18
+ protected render(): import("lit").TemplateResult<1>;
19
+ private onSlotChange;
20
+ protected update(changedProperties: PropertyValues): void;
21
+ private onChange;
22
+ }
@@ -0,0 +1,89 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { html } from "lit";
8
+ import { property, queryAssignedElements, state } from "lit/decorators.js";
9
+ import { FtLitElement } from "@fluid-topics/ft-wc-utils";
10
+ import { styles } from "./ft-chip-choice.styles";
11
+ export class FtChipChoiceChangeEvent extends CustomEvent {
12
+ constructor(value) {
13
+ super("change", { detail: { value }, bubbles: true, composed: true });
14
+ }
15
+ }
16
+ class FtChipChoice extends FtLitElement {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.name = "";
20
+ this.multiselect = false;
21
+ this.role = "radiogroup";
22
+ this.ariaLabelledBy = "";
23
+ this.selectedOptions = new Set();
24
+ }
25
+ render() {
26
+ return html `
27
+ <slot @slotchange=${this.onSlotChange}
28
+ @change=${this.onChange}>
29
+ </slot>
30
+ `;
31
+ }
32
+ onSlotChange() {
33
+ this.chipChoiceOptions.forEach(option => {
34
+ option.name = this.name;
35
+ if (option.selected) {
36
+ this.selectedOptions.add(option.value);
37
+ this.requestUpdate();
38
+ }
39
+ });
40
+ }
41
+ update(changedProperties) {
42
+ super.update(changedProperties);
43
+ if (changedProperties.has("multiselect")) {
44
+ this.role = this.multiselect ? "group" : "radiogroup";
45
+ this.chipChoiceOptions.forEach(option => {
46
+ option.role = this.multiselect ? "checkbox" : "radio";
47
+ });
48
+ }
49
+ }
50
+ onChange(event) {
51
+ event.stopPropagation();
52
+ if (!event.detail.selected) {
53
+ this.selectedOptions.delete(event.detail.value);
54
+ }
55
+ else {
56
+ if (this.multiselect) {
57
+ this.selectedOptions.add(event.detail.value);
58
+ }
59
+ else {
60
+ this.selectedOptions = new Set([event.detail.value]);
61
+ this.chipChoiceOptions.map((chip) => {
62
+ chip.selected = chip.value === event.detail.value;
63
+ });
64
+ }
65
+ }
66
+ this.requestUpdate();
67
+ this.dispatchEvent(new FtChipChoiceChangeEvent(this.selectedOptions));
68
+ }
69
+ }
70
+ FtChipChoice.styles = styles;
71
+ __decorate([
72
+ property()
73
+ ], FtChipChoice.prototype, "name", void 0);
74
+ __decorate([
75
+ property({ type: Boolean })
76
+ ], FtChipChoice.prototype, "multiselect", void 0);
77
+ __decorate([
78
+ property({ reflect: true, attribute: "role" })
79
+ ], FtChipChoice.prototype, "role", void 0);
80
+ __decorate([
81
+ property({ reflect: true, attribute: "aria-labelledby" })
82
+ ], FtChipChoice.prototype, "ariaLabelledBy", void 0);
83
+ __decorate([
84
+ queryAssignedElements()
85
+ ], FtChipChoice.prototype, "chipChoiceOptions", void 0);
86
+ __decorate([
87
+ state()
88
+ ], FtChipChoice.prototype, "selectedOptions", void 0);
89
+ export { FtChipChoice };