@fluid-topics/ft-search-sort-selector 1.3.17

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,19 @@
1
+ Sort selector component for fluid-topics search pages
2
+
3
+ ## Install
4
+
5
+ ```shell
6
+ npm install @fluid-topics/ft-search-sort-selector
7
+ yarn add @fluid-topics/ft-search-sort-selector
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```typescript
13
+ import { html } from "lit"
14
+ import "@fluid-topics/ft-search-sort-selector"
15
+
16
+ function render() {
17
+ return html` <ft-search-sort-selector foo="bar"></ft-search-sort-selector> `
18
+ }
19
+ ```
@@ -0,0 +1,6 @@
1
+ import { DefaultI18nMessages, I18nMessageContext, I18nMessages } from "@fluid-topics/ft-i18n/build/generator/I18nMessageContext";
2
+ export interface SearchSortSelectorMessages extends I18nMessages {
3
+ label(): string;
4
+ }
5
+ export declare const context: I18nMessageContext<SearchSortSelectorMessages>;
6
+ export declare const defaultMessages: DefaultI18nMessages<SearchSortSelectorMessages>;
@@ -0,0 +1,5 @@
1
+ import { I18nMessageContext } from "@fluid-topics/ft-i18n/build/generator/I18nMessageContext";
2
+ export const context = I18nMessageContext.build("designedSearchSortSelector");
3
+ export const defaultMessages = {
4
+ label: "Sort by",
5
+ };
@@ -0,0 +1,23 @@
1
+ import { nothing } from "lit";
2
+ import { ElementDefinitionsMap } from "@fluid-topics/ft-wc-utils";
3
+ import { FtSearchSortSelectorProperties } from "./ft-search-sort-selector.properties";
4
+ import { FtSearchSortOption } from "@fluid-topics/public-api";
5
+ import { FtSearchComponent } from "@fluid-topics/ft-search-context/build/registration";
6
+ import { I18nAttributeValue } from "@fluid-topics/ft-i18n/build/decorators/i18nAttribute";
7
+ import { PropertyValues } from "lit/development";
8
+ declare const FtSearchSortSelector_base: typeof FtSearchComponent & import("@fluid-topics/ft-wc-utils").Constructor<import("@fluid-topics/ft-i18n").FtLitElementWithI18nInterface>;
9
+ export declare class FtSearchSortSelector extends FtSearchSortSelector_base implements FtSearchSortSelectorProperties {
10
+ static elementDefinitions: ElementDefinitionsMap;
11
+ static styles: import("lit").CSSResult;
12
+ constructor();
13
+ labels: Array<I18nAttributeValue>;
14
+ configuredSortOptions: string[];
15
+ currentSortId?: string;
16
+ sortOptions?: FtSearchSortOption[];
17
+ selectedSort?: string;
18
+ getDisplayedSortOptions(): FtSearchSortOption[];
19
+ protected render(): typeof nothing | import("lit-html").TemplateResult<1>;
20
+ private setSort;
21
+ protected willUpdate(props: PropertyValues<FtSearchSortSelector>): void;
22
+ }
23
+ export {};
@@ -0,0 +1,109 @@
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, nothing } from "lit";
8
+ import { state } from "lit/decorators.js";
9
+ import { jsonProperty, redux } from "@fluid-topics/ft-wc-utils";
10
+ import { styles } from "./ft-search-sort-selector.styles";
11
+ import { FtSelect, FtSelectOption } from "@fluid-topics/ft-select";
12
+ import { repeat } from "lit/directives/repeat.js";
13
+ import { withI18n } from "@fluid-topics/ft-i18n";
14
+ import { FtSearchComponent } from "@fluid-topics/ft-search-context/build/registration";
15
+ import { i18nListAttribute } from "@fluid-topics/ft-i18n/build/decorators/i18nAttribute";
16
+ import { i18nAttributeConverter } from "@fluid-topics/ft-i18n/build/decorators/I18nAttributeConverter";
17
+ import { context, defaultMessages } from "./SearchSortSelectorMessages";
18
+ class FtSearchSortSelector extends withI18n(FtSearchComponent) {
19
+ constructor() {
20
+ super();
21
+ this.labels = [];
22
+ this.configuredSortOptions = [];
23
+ this.addI18nContext(context, defaultMessages);
24
+ }
25
+ getDisplayedSortOptions() {
26
+ var _a;
27
+ if (this.configuredSortOptions.length == 0) {
28
+ return (_a = this.sortOptions) !== null && _a !== void 0 ? _a : [];
29
+ }
30
+ return this.configuredSortOptions
31
+ .map((sortId) => { var _a; return (_a = this.sortOptions) === null || _a === void 0 ? void 0 : _a.find((option) => option.id == sortId); })
32
+ .filter((option) => option != null)
33
+ .map((option) => option);
34
+ }
35
+ render() {
36
+ const displayedSortOptions = this.getDisplayedSortOptions();
37
+ if (displayedSortOptions.length <= 1) {
38
+ return nothing;
39
+ }
40
+ return html `
41
+ <div part="container">
42
+ <ft-select
43
+ outlined
44
+ label="${context.messages.label()}"
45
+ aria-label="${context.messages.label()}"
46
+ @change=${(e) => this.setSort(e)}>
47
+ ${repeat(displayedSortOptions, (sort) => sort.id, (sort, index) => {
48
+ var _a, _b;
49
+ return html `
50
+ <ft-select-option value="${sort.id}"
51
+ label="${((_b = (_a = this.labels) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b.message) || sort.label.key}"
52
+ ?selected=${sort.id == this.selectedSort}></ft-select-option>
53
+ `;
54
+ })}
55
+ </ft-select>
56
+ </div>
57
+ `;
58
+ }
59
+ setSort(e) {
60
+ var _a;
61
+ this.selectedSort = (_a = this.stateManager) === null || _a === void 0 ? void 0 : _a.setSortId(e.detail);
62
+ }
63
+ willUpdate(props) {
64
+ var _a, _b, _c, _d, _e, _f;
65
+ super.willUpdate(props);
66
+ const displayedSortOptions = this.getDisplayedSortOptions();
67
+ const watchedKeys = [
68
+ "currentSortId",
69
+ "sortOptions",
70
+ ];
71
+ if (watchedKeys.some((p) => props.has(p)) && displayedSortOptions.length > 0) {
72
+ this.selectedSort = (_b = (_a = displayedSortOptions.find(({ id }) => id === this.currentSortId)) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : (_c = displayedSortOptions[0]) === null || _c === void 0 ? void 0 : _c.id;
73
+ if (this.selectedSort && this.selectedSort != this.currentSortId) {
74
+ (_d = this.stateManager) === null || _d === void 0 ? void 0 : _d.setSortId(this.selectedSort);
75
+ }
76
+ }
77
+ // Focus on first option in designer when the list of configured sorts changes
78
+ if (props.has("configuredSortOptions")) {
79
+ this.selectedSort = (_e = displayedSortOptions[0]) === null || _e === void 0 ? void 0 : _e.id;
80
+ if (this.selectedSort && this.selectedSort != this.currentSortId) {
81
+ (_f = this.stateManager) === null || _f === void 0 ? void 0 : _f.setSortId(this.selectedSort);
82
+ }
83
+ }
84
+ if (props.has("sortOptions") || props.has("configuredSortOptions")) {
85
+ this.labels = displayedSortOptions.map((option) => i18nAttributeConverter.fromLocalizableLabel(option.label));
86
+ }
87
+ }
88
+ }
89
+ FtSearchSortSelector.elementDefinitions = {
90
+ "ft-select": FtSelect,
91
+ "ft-select-option": FtSelectOption,
92
+ };
93
+ FtSearchSortSelector.styles = styles;
94
+ __decorate([
95
+ i18nListAttribute()
96
+ ], FtSearchSortSelector.prototype, "labels", void 0);
97
+ __decorate([
98
+ jsonProperty([])
99
+ ], FtSearchSortSelector.prototype, "configuredSortOptions", void 0);
100
+ __decorate([
101
+ redux({ store: "search", selector: (state) => state.request.sortId })
102
+ ], FtSearchSortSelector.prototype, "currentSortId", void 0);
103
+ __decorate([
104
+ redux({ store: "search", selector: (state) => { var _a; return (_a = state.configuration) === null || _a === void 0 ? void 0 : _a.sortOptions; } })
105
+ ], FtSearchSortSelector.prototype, "sortOptions", void 0);
106
+ __decorate([
107
+ state()
108
+ ], FtSearchSortSelector.prototype, "selectedSort", void 0);
109
+ export { FtSearchSortSelector };