Skip to content

Commit e06d5e2

Browse files
r0b1ngjulivan
authored andcommitted
fix: observe options as they might not be loaded on the first render
1 parent 5defa68 commit e06d5e2

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

packages/pluggableWidgets/dropdown-sort-web/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010

1111
- We fixed an issue with Gallery widget causing errors when Mendix app is being used in an iframe.
1212

13+
- We fixed an issue with Dropdown sort widget not showing attribute captions correctly.
14+
1315
## [1.2.2] - 2025-03-31
1416

1517
### Fixed

packages/shared/widget-plugin-sorting/src/helpers/SortStoreProvider.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { AttributeMetaData, DynamicValue } from "mendix";
21
import { SortOrderStore } from "../stores/SortOrderStore";
32
import { SortStoreHost } from "../stores/SortStoreHost";
43
import { SortInstruction } from "../types/store";
54

65
interface SortStoreProviderSpec {
76
host: SortStoreHost;
87
initSortOrder?: SortInstruction[];
9-
attributes: Array<{
10-
attribute: AttributeMetaData;
11-
caption?: DynamicValue<string>;
12-
}>;
138
}
149

1510
export class SortStoreProvider {
@@ -18,11 +13,7 @@ export class SortStoreProvider {
1813

1914
constructor(spec: SortStoreProviderSpec) {
2015
this._host = spec.host;
21-
const options = spec.attributes.map(item => ({
22-
value: item.attribute.id,
23-
caption: item.caption?.value ?? "empty"
24-
}));
25-
this.store = new SortOrderStore({ options, initSortOrder: spec.initSortOrder });
16+
this.store = new SortOrderStore({ initSortOrder: spec.initSortOrder });
2617
}
2718

2819
setup(): () => void {

packages/shared/widget-plugin-sorting/src/react/hocs/withLinkedSortStore.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ export function withLinkedSortStore<P extends RequiredProps>(
2020
() =>
2121
new SortStoreProvider({
2222
host: props.sortAPI.host,
23-
initSortOrder: props.sortAPI.host.sortOrder,
24-
attributes: props.attributes
23+
initSortOrder: props.sortAPI.host.sortOrder
2524
})
2625
);
2726

27+
store.setProps(props);
28+
2829
return <Component {...props} sortStore={store} />;
2930
};
3031
}

packages/shared/widget-plugin-sorting/src/stores/SortOrderStore.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,51 @@ import { PlainJs, Serializable } from "@mendix/filter-commons/typings/settings";
22
import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid";
33
import { action, computed, makeObservable, observable } from "mobx";
44
import { BasicSortStore, Option, SortInstruction } from "../types/store";
5+
import { AttributeMetaData, DynamicValue } from "mendix";
56

67
type StorableState = Array<[number, "asc" | "desc"]>;
78

9+
type Props = {
10+
attributes: Array<{
11+
attribute: AttributeMetaData;
12+
caption?: DynamicValue<string>;
13+
}>;
14+
};
15+
816
export class SortOrderStore implements BasicSortStore, Serializable {
917
private readonly _sortOrder: SortInstruction[] = [];
1018

1119
readonly id = `SortOrderStore@${generateUUID()}`;
12-
readonly options: Option[];
13-
readonly idToIndex: Map<string, number>;
14-
15-
constructor(spec: { options?: Option[]; initSortOrder?: SortInstruction[] } = {}) {
16-
const { options = [], initSortOrder = [] } = spec;
20+
options: Option[] = [];
21+
readonly idToIndex: Map<string, number> = new Map();
1722

18-
this.options = [...options];
19-
this.idToIndex = new Map(options.map((option, index) => [option.value, index]));
23+
constructor(spec: { initSortOrder?: SortInstruction[] }) {
24+
const { initSortOrder = [] } = spec;
2025
this._sortOrder = [...initSortOrder];
2126

2227
makeObservable<this, "_sortOrder">(this, {
2328
_sortOrder: observable,
29+
options: observable.struct,
2430
sortOrder: computed,
2531
setSortOrder: action,
32+
setProps: action,
2633
push: action,
2734
remove: action
2835
});
2936
}
3037

38+
setProps(props: Props): void {
39+
this.options = props.attributes.map(item => ({
40+
value: item.attribute.id,
41+
caption: item.caption?.value ?? "<empty>"
42+
}));
43+
44+
this.idToIndex.clear();
45+
this.options.forEach((option, index) => {
46+
this.idToIndex.set(option.value, index);
47+
});
48+
}
49+
3150
get sortOrder(): SortInstruction[] {
3251
return [...this._sortOrder];
3352
}

0 commit comments

Comments
 (0)