@fluid-topics/ft-search-sort-selector 2.0.15 → 2.0.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.
@@ -14,10 +14,9 @@ export declare class FtSearchSortSelector extends FtSearchSortSelector_base impl
14
14
  configuredSortOptions: string[];
15
15
  currentSortId?: string;
16
16
  sortOptions?: FtSearchSortOption[];
17
- selectedSort?: string;
18
17
  getDisplayedSortOptions(): FtSearchSortOption[];
19
18
  protected render(): typeof nothing | import("lit-html").TemplateResult<1>;
20
- private setSort;
21
19
  protected willUpdate(props: PropertyValues<FtSearchSortSelector>): void;
20
+ private currentSortIdIsEmptyOrNotInAvailableOptions;
22
21
  }
23
22
  export {};
@@ -4,18 +4,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
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
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { html, nothing } from "lit";
8
- import { state } from "lit/decorators.js";
9
- import { jsonProperty, redux } from "@fluid-topics/ft-wc-utils";
7
+ import { html, nothing, } from "lit";
8
+ import { jsonProperty, redux, } from "@fluid-topics/ft-wc-utils";
10
9
  import { styles } from "./ft-search-sort-selector.styles";
11
- import { FtSelect, FtSelectOption } from "@fluid-topics/ft-select";
10
+ import { FtSelect, FtSelectOption, } from "@fluid-topics/ft-select";
12
11
  import { repeat } from "lit/directives/repeat.js";
13
12
  import { withI18n } from "@fluid-topics/ft-i18n";
14
13
  import { FtSearchComponent } from "@fluid-topics/ft-search-context/build/registration";
15
- import { i18nListAttribute } from "@fluid-topics/ft-i18n/build/decorators/i18nAttribute";
14
+ import { i18nListAttribute, } from "@fluid-topics/ft-i18n/build/decorators/i18nAttribute";
16
15
  import { i18nAttributeConverter } from "@fluid-topics/ft-i18n/build/decorators/I18nAttributeConverter";
17
- import { context, defaultMessages } from "./SearchSortSelectorMessages";
18
- class FtSearchSortSelector extends withI18n(FtSearchComponent) {
16
+ import { context, defaultMessages, } from "./SearchSortSelectorMessages";
17
+ export class FtSearchSortSelector extends withI18n(FtSearchComponent) {
19
18
  constructor() {
20
19
  super();
21
20
  this.labels = [];
@@ -43,48 +42,41 @@ class FtSearchSortSelector extends withI18n(FtSearchComponent) {
43
42
  outlined
44
43
  label="${context.messages.label()}"
45
44
  aria-label="${context.messages.label()}"
46
- @change=${(e) => this.setSort(e)}>
45
+ @change=${(e) => { var _a; return (_a = this.stateManager) === null || _a === void 0 ? void 0 : _a.setSortId(e.detail); }}>
47
46
  ${repeat(displayedSortOptions, (sort) => sort.id, (sort, index) => {
48
47
  var _a, _b;
49
48
  return html `
50
49
  <ft-select-option value="${sort.id}"
51
50
  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>
51
+ ?selected=${sort.id == this.currentSortId}></ft-select-option>
53
52
  `;
54
53
  })}
55
54
  </ft-select>
56
55
  </div>
57
56
  `;
58
57
  }
59
- setSort(e) {
60
- var _a;
61
- this.selectedSort = (_a = this.stateManager) === null || _a === void 0 ? void 0 : _a.setSortId(e.detail);
62
- }
63
58
  willUpdate(props) {
64
- var _a, _b, _c, _d, _e, _f;
59
+ var _a, _b;
65
60
  super.willUpdate(props);
66
- const displayedSortOptions = this.getDisplayedSortOptions();
67
61
  const watchedKeys = [
68
62
  "currentSortId",
69
63
  "sortOptions",
70
64
  ];
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
65
  // Focus on first option in designer when the list of configured sorts changes
78
- if (props.has("configuredSortOptions") && props.get("configuredSortOptions") !== undefined) {
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);
66
+ if (watchedKeys.some((p) => props.has(p))) {
67
+ const displayedSortOptions = this.getDisplayedSortOptions();
68
+ if (displayedSortOptions.length > 0 && this.currentSortIdIsEmptyOrNotInAvailableOptions(displayedSortOptions)) {
69
+ (_a = this.stateManager) === null || _a === void 0 ? void 0 : _a.setSortId((_b = displayedSortOptions[0]) === null || _b === void 0 ? void 0 : _b.id, true);
82
70
  }
83
71
  }
84
72
  if (props.has("sortOptions") || props.has("configuredSortOptions")) {
73
+ const displayedSortOptions = this.getDisplayedSortOptions();
85
74
  this.labels = displayedSortOptions.map((option) => i18nAttributeConverter.fromLocalizableLabel(option.label));
86
75
  }
87
76
  }
77
+ currentSortIdIsEmptyOrNotInAvailableOptions(availableOptions) {
78
+ return !this.currentSortId || !availableOptions.some((sortOption) => this.currentSortId == sortOption.id);
79
+ }
88
80
  }
89
81
  FtSearchSortSelector.elementDefinitions = {
90
82
  "ft-select": FtSelect,
@@ -103,7 +95,3 @@ __decorate([
103
95
  __decorate([
104
96
  redux({ store: "search", selector: (state) => { var _a; return (_a = state.configuration) === null || _a === void 0 ? void 0 : _a.sortOptions; } })
105
97
  ], FtSearchSortSelector.prototype, "sortOptions", void 0);
106
- __decorate([
107
- state()
108
- ], FtSearchSortSelector.prototype, "selectedSort", void 0);
109
- export { FtSearchSortSelector };