Skip to content

Commit 3468ac1

Browse files
🤖 Merge PR DefinitelyTyped#73377 jsoneditor: Enhance autocomplete object option support by @ahmed-saber
Co-authored-by: Ahmed Saber <ahmed.ali@semrush.com>
1 parent 6984573 commit 3468ac1

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

‎types/jsoneditor/index.d.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,28 @@ export interface Template {
7777
value: any;
7878
}
7979

80-
export type AutoCompleteCompletion = null | string[] | { startFrom: number; options: string[] };
80+
export interface AutoCompleteOption {
81+
/**
82+
* The display text for the autocomplete option
83+
*/
84+
text?: string;
85+
/**
86+
* The value to be used when the option is selected
87+
*/
88+
value: string;
89+
/**
90+
* Additional properties are allowed for custom metadata or future extensibility.
91+
* These properties will be preserved but ignored by the current JSONEditor implementation.
92+
*/
93+
[key: string]: any;
94+
}
95+
96+
export type AutoCompleteOptionItem = string | AutoCompleteOption;
97+
98+
export type AutoCompleteCompletion = null | AutoCompleteOptionItem[] | {
99+
startFrom: number;
100+
options: AutoCompleteOptionItem[];
101+
};
81102

82103
export type AutoCompleteMatchingStrategy = "start" | "contain";
83104

@@ -98,7 +119,10 @@ export interface AutoCompleteOptions {
98119
* - 'start': Match your input from the start, e.g. 'ap' matches 'apple' but 'pl' does not.
99120
* - 'contain': Contains the user's input or not, e.g. 'pl' matches 'apple' too.
100121
*/
101-
filter?: AutoCompleteMatchingStrategy | ((query: string) => boolean) | undefined;
122+
filter?:
123+
| AutoCompleteMatchingStrategy
124+
| ((query: string, match: AutoCompleteOptionItem, config: AutoCompleteOptions) => boolean)
125+
| undefined;
102126
/**
103127
* Indicate the way to trigger autocomplete menu.
104128
* - 'keydown': When you type something in the field or value, it will trigger autocomplete immediately.

‎types/jsoneditor/jsoneditor-tests.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,67 @@ autocomplete.filter = undefined;
1313
autocomplete.filter = "start";
1414
autocomplete.filter = "contain";
1515
autocomplete.filter = input => true;
16+
autocomplete.filter = (query, match, config) => true;
1617

1718
autocomplete.getOptions = undefined;
1819
autocomplete.getOptions = (text, path, type, editor) => [];
20+
autocomplete.getOptions = (text, path, type, editor) => ["string1", "string2"];
21+
autocomplete.getOptions = (text, path, type, editor) => [{ text: "Display Text", value: "actual_value" }];
22+
autocomplete.getOptions = (text, path, type, editor) => [
23+
"string_option",
24+
{ text: "Object Option", value: "object_value" },
25+
{ text: "Object with text only", value: "object_text_only" },
26+
];
27+
autocomplete.getOptions = (text, path, type, editor) => ({ startFrom: 0, options: ["option1", "option2"] });
28+
autocomplete.getOptions = (text, path, type, editor) => ({
29+
startFrom: 0,
30+
options: [{ text: "Display", value: "val" }],
31+
});
32+
autocomplete.getOptions = (text, path, type, editor) => new Promise(() => ["async1", "async2"]);
33+
autocomplete.getOptions = (text, path, type, editor) =>
34+
new Promise(() => [{ text: "Async Option", value: "async_val" }]);
35+
36+
// Test getOptions with extended AutoCompleteOption properties
37+
autocomplete.getOptions = (text, path, type, editor) => [
38+
"simple_string",
39+
{
40+
text: "Premium Feature",
41+
value: "premium_val",
42+
category: "premium",
43+
icon: "star",
44+
description: "This is a premium feature",
45+
},
46+
{
47+
text: "Advanced Option",
48+
value: "advanced_val",
49+
metadata: { id: 123, priority: "high" },
50+
customData: { anything: "custom" },
51+
},
52+
];
53+
54+
autocomplete.getOptions = (text, path, type, editor) => ({
55+
startFrom: 0,
56+
options: [
57+
"basic_option",
58+
{
59+
text: "Extended Option",
60+
value: "ext_val",
61+
tooltip: "Additional information",
62+
disabled: false,
63+
group: "category1",
64+
},
65+
],
66+
});
67+
68+
autocomplete.getOptions = (text, path, type, editor) =>
69+
Promise.resolve([
70+
{
71+
text: "Async Extended",
72+
value: "async_ext",
73+
category: "async",
74+
metadata: { loadTime: Date.now() },
75+
},
76+
]);
1977

2078
autocomplete.trigger = undefined;
2179
autocomplete.trigger = "keydown";

0 commit comments

Comments
 (0)