/
DisplayValuePropertyDataFilterer.ts
66 lines (55 loc) · 2.07 KB
/
DisplayValuePropertyDataFilterer.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module PropertyGrid
*/
import type { PropertyRecord } from "@itwin/appui-abstract";
import { PropertyValueFormat } from "@itwin/appui-abstract";
import { countMatchesInString } from "../../../common/countMatchesInString";
import type { PropertyDataFilterResult } from "./PropertyDataFiltererBase";
import {
FilteredType,
PropertyRecordDataFiltererBase,
} from "./PropertyDataFiltererBase";
/**
* Property data filterer which matches on Primitive Property Record display value text.
* @public
*/
export class DisplayValuePropertyDataFilterer extends PropertyRecordDataFiltererBase {
private _filterText: string = "";
public constructor(filterText: string = "") {
super();
this._filterText = filterText.toLowerCase().trim();
}
public get filterText(): string {
return this._filterText;
}
public set filterText(value: string) {
const lowerValue = value.toLowerCase().trim();
if (lowerValue !== this.filterText) {
this._filterText = lowerValue;
this.onFilterChanged.raiseEvent();
}
}
public get isActive() {
return this.filterText !== "";
}
public async recordMatchesFilter(
node: PropertyRecord
): Promise<PropertyDataFilterResult> {
if (!this.isActive) return { matchesFilter: true };
if (node.value.valueFormat !== PropertyValueFormat.Primitive)
return { matchesFilter: false };
const displayValue = node.value.displayValue?.toLowerCase() ?? "";
const matchesCount = countMatchesInString(displayValue, this.filterText);
if (matchesCount === 0) return { matchesFilter: false };
return {
matchesFilter: true,
shouldExpandNodeParents: true,
matchesCount,
filteredTypes: [FilteredType.Value],
};
}
}