-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathutils.ts
More file actions
159 lines (144 loc) · 5.1 KB
/
utils.ts
File metadata and controls
159 lines (144 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Big } from "big.js";
import { matchSorter, MatchSorterOptions } from "match-sorter";
import { createElement, PropsWithChildren, ReactElement } from "react";
import { ComboboxPreviewProps, FilterTypeEnum, SelectedItemsSortingEnum } from "typings/ComboboxProps";
import { MultiSelector, SortOrder } from "./types";
import { ObjectItem } from "mendix";
export const DEFAULT_LIMIT_SIZE = 100;
type ValueType = string | Big | boolean | Date | undefined;
export function getSelectedCaptionsPlaceholder(selector: MultiSelector, selectedItems: string[]): string {
if (selectedItems.length === 0) {
return selector.caption.emptyCaption;
}
if (
selector.selectedItemsStyle !== "text" ||
selector.customContentType === "yes" ||
selector.selectionMethod === "rowclick"
) {
return "";
}
const selected = selectedItems.map(v => selector.caption.get(v));
return selected.join(", ");
}
export interface CaptionContentProps extends PropsWithChildren {
htmlFor?: string;
onClick?: (e: MouseEvent) => void;
}
export function CaptionContent(props: CaptionContentProps): ReactElement {
const { htmlFor, children, onClick } = props;
return createElement(htmlFor == null ? "span" : "label", {
children,
className: "widget-combobox-caption-text",
htmlFor,
onClick: onClick
? onClick
: htmlFor
? (e: MouseEvent) => {
e.preventDefault();
}
: undefined
});
}
export function getDatasourcePlaceholderText(args: ComboboxPreviewProps): string {
const {
optionsSourceType,
optionsSourceAssociationDataSource,
attributeEnumeration,
attributeBoolean,
databaseAttributeString,
emptyOptionText,
source,
optionsSourceDatabaseDataSource,
staticAttribute,
optionsSourceStaticDataSource
} = args;
const emptyStringFormat = emptyOptionText ? `[${emptyOptionText}]` : "Combo box";
if (source === "context") {
switch (optionsSourceType) {
case "association":
return (optionsSourceAssociationDataSource as { caption?: string })?.caption || emptyStringFormat;
case "enumeration":
return `[${optionsSourceType}, ${attributeEnumeration}]`;
case "boolean":
return `[${optionsSourceType}, ${attributeBoolean}]`;
default:
return emptyStringFormat;
}
} else if (source === "database" && optionsSourceDatabaseDataSource) {
return (
(optionsSourceDatabaseDataSource as { caption?: string })?.caption ||
`${source}, ${databaseAttributeString}`
);
} else if (source === "static") {
return (optionsSourceStaticDataSource as { caption?: string })?.caption || `[${source}, ${staticAttribute}]`;
}
return emptyStringFormat;
}
export function getFilterTypeOptions(filter: FilterTypeEnum): MatchSorterOptions<string> {
switch (filter) {
case "contains":
return {};
case "containsExact":
return {
threshold: matchSorter.rankings.CONTAINS
};
case "startsWith":
return {
threshold: matchSorter.rankings.STARTS_WITH
};
case "none":
return {
threshold: matchSorter.rankings.NO_MATCH
};
}
}
export function _valuesIsEqual(valueA: ValueType, valueB: ValueType): boolean {
if (valueA === undefined || valueB === undefined) {
return valueA === valueB;
}
if (valueA instanceof Big && valueB instanceof Big) {
return valueA.eq(valueB);
}
if (valueA instanceof Date && valueB instanceof Date) {
return valueA.getTime() === valueB.getTime();
}
return valueA === valueB;
}
export function sortSelectedItems(
values: ObjectItem[] | null | undefined,
sortingType: SelectedItemsSortingEnum,
sortOrder: SortOrder,
captionGetter: (id: string) => string | undefined
): string[] | null {
if (values) {
return sortSelections(
values.map(v => (v?.id as string) ?? null),
sortingType,
sortOrder,
captionGetter
);
} else {
return null;
}
}
function sortSelections(
newValueIds: string[],
sortingType: SelectedItemsSortingEnum,
sortOrder: SortOrder,
captionGetter: (id: string) => string | undefined
): string[] {
if (sortingType === "caption") {
return newValueIds.sort((a, b) => {
const captionA = captionGetter(a)?.toString() ?? "";
const captionB = captionGetter(b)?.toString() ?? "";
return sortOrder === "asc" ? captionA.localeCompare(captionB) : captionB.localeCompare(captionA);
});
}
return newValueIds;
}
export function getInputLabel(inputId: string): Element | null {
return document.querySelector(`label[for="${inputId}"]`);
}
export function getValidationErrorId(inputId?: string): string | undefined {
return inputId ? inputId + "-validation-message" : undefined;
}