Skip to content

Commit 7895fb3

Browse files
authored
🤖 Merge PR DefinitelyTyped#45223 Add Types for quicksettings by @janizde
* copy files with types and tests * run prettier over definitions and tests * add meta data * remove path from typings meta data
1 parent 6e29c70 commit 7895fb3

File tree

4 files changed

+983
-0
lines changed

4 files changed

+983
-0
lines changed

types/quicksettings/index.d.ts

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Type definitions for quicksettings 3.0
2+
// Project: https://github.com/bit101/quicksettings
3+
// Definitions by: janizde <https://github.com/janizde>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
// TypeScript Version: 3.7
6+
7+
export type ChangeHandler<T> = (value: T) => void;
8+
export type AnyModel = Record<string, any>;
9+
10+
/**
11+
* Mapped type picking only the properties of the type `M` whose value declaration extends `V`
12+
*/
13+
type KeyWhereType<M, V> = {
14+
[K in keyof M]: M[K] extends V ? K : never;
15+
}[keyof M];
16+
17+
/**
18+
* Represents the type of object passed to a change callback of a DropDown
19+
*/
20+
export interface DropDownSelection<T> {
21+
index: number;
22+
label: string;
23+
value: T;
24+
}
25+
26+
/**
27+
* Represents an option passed into `addDropDown` or `bindDropDown`
28+
*/
29+
export interface DropDownOption<T> {
30+
label: string | number;
31+
value: T;
32+
}
33+
34+
/**
35+
* Represents the type of options that can be passed into an `addDropDown` or `bindDropDown` call.
36+
* string and number can be directly passed as values whereas other types have to be wrapped in
37+
* a `DropDownOption`
38+
*
39+
* @template T The type of the dropdown options' values
40+
*/
41+
export type DropDownItems<T> = Array<(T & (string | number)) | DropDownOption<T>>;
42+
43+
/**
44+
* Represents a Panel of quick settings. The interface is parametrized with a model type
45+
* and a literal type describing the controls that do not attach a value to the model (e.g., buttons).
46+
* Both of the types are optional and default to a permissive counterpart.
47+
*
48+
* @example
49+
* ```ts
50+
* const permissive = new QuickSettings();
51+
* permissive.addText('foo', 'value', (nextValue: string) => console.log(nextValue)); // Ok
52+
* permissive.addButton('anybutton', () => console.log('any button clicked')); // Ok
53+
*
54+
* interface Model {
55+
* bar: string;
56+
* }
57+
*
58+
* const restrictive = new QuickSettings<Model, 'mybutton'>();
59+
* permissive.addText('foo', 'value', (nextValue: string) => console.log(nextValue)); // Error, must be 'bar'
60+
* permissive.addButton('anybutton', () => console.log('any button clicked')); // Error, must be 'mybutton'
61+
* ```
62+
*
63+
* @template M The type of the model that is being managed by the quick settings.
64+
* @template S Type of names of controls that do not attach to the model (e.g., button or element)
65+
*/
66+
export interface QuickSettingsPanel<M = AnyModel, S = string> {
67+
destroy(): void;
68+
getValuesAsJSON(asString: true): string;
69+
getValuesAsJSON(asString?: false): M;
70+
setValuesFromJSON(json: M | string): this;
71+
saveInLocalStorage(name: string): this;
72+
clearLocalStorage(name: string): this;
73+
setPosition(x: number, y: number): this;
74+
setSize(w: number, h: number): this;
75+
setWidth(w: number): this;
76+
setHeight(h: number): this;
77+
setDraggable(draggable: boolean): this;
78+
setGlobalChangeHandler(handler: ChangeHandler<M>): this;
79+
hide(): this;
80+
show(): this;
81+
toggleVisibility(): this;
82+
setCollapsible(collapsible: boolean): this;
83+
collapse(): this;
84+
expand(): this;
85+
toggleCollapsed(): this;
86+
setKey(char: string): this;
87+
removeControl(title: keyof M | S): this;
88+
enableControl(title: keyof M | S): this;
89+
disableControl(title: keyof M | S): this;
90+
hideControl(title: keyof M | S): this;
91+
showControl(title: keyof M | S): this;
92+
overrideStyle(title: keyof M | S, style: string, value: string): this;
93+
hideTitle(title: keyof M | S): this;
94+
showTitle(title: keyof M | S): this;
95+
hideAllTitles(): this;
96+
showAllTitles(): this;
97+
98+
getValue<K extends keyof M>(title: K): M[K];
99+
setValue<K extends keyof M>(title: K, value: M[K]): this;
100+
101+
addBoolean(title: KeyWhereType<M, boolean>, value: boolean, callback?: ChangeHandler<boolean>): this;
102+
bindBoolean<K extends KeyWhereType<M, boolean>>(title: K, value: boolean, object: Record<K, boolean>): this;
103+
104+
addColor(title: KeyWhereType<M, string>, color: string, callback?: ChangeHandler<string>): this;
105+
bindColor<K extends KeyWhereType<M, string>>(title: K, color: string, object: Record<K, string>): this;
106+
107+
addDate<K extends KeyWhereType<M, string | Date>, V extends M[K]>(
108+
title: K,
109+
date: V,
110+
callback?: ChangeHandler<V>,
111+
): this;
112+
bindDate<K extends KeyWhereType<M, string | Date>, V extends M[K]>(title: K, date: V, object: Record<K, V>): this;
113+
114+
addTime<K extends KeyWhereType<M, string | Date>, V extends M[K]>(
115+
title: K,
116+
date: V,
117+
callback?: ChangeHandler<V>,
118+
): this;
119+
bindTime<K extends KeyWhereType<M, string | Date>, V extends M[K]>(title: K, date: V, object: Record<K, V>): this;
120+
121+
addDropDown<K extends keyof M>(
122+
title: K,
123+
items: DropDownItems<M[K]>,
124+
callback?: ChangeHandler<DropDownSelection<M[K]>>,
125+
): this;
126+
bindDropDown<K extends keyof M>(title: K, items: DropDownItems<M[K]>, object: Pick<M, K>): this;
127+
128+
addButton(title: S, callback?: () => void): this;
129+
addElement(title: S, element: HTMLElement): this;
130+
131+
addFileChooser(
132+
title: KeyWhereType<M, File>,
133+
labelStr: string,
134+
filter: string,
135+
callback?: ChangeHandler<File>,
136+
): this;
137+
addHTML(title: KeyWhereType<M, string>, html: string): this;
138+
addImage(title: KeyWhereType<M, string>, imageUrl: string, callback?: ChangeHandler<string>): this;
139+
140+
addRange(
141+
title: KeyWhereType<M, number>,
142+
min: number,
143+
max: number,
144+
value: number,
145+
step: number,
146+
callback?: ChangeHandler<number>,
147+
): this;
148+
bindRange<K extends KeyWhereType<M, number>>(
149+
title: K,
150+
min: number,
151+
max: number,
152+
value: number,
153+
step: number,
154+
object: Record<K, number>,
155+
): this;
156+
setRangeParameters(title: KeyWhereType<M, number>, min: number, max: number, step: number): this;
157+
158+
addNumber(
159+
title: KeyWhereType<M, number>,
160+
min: number,
161+
max: number,
162+
value: number,
163+
step: number,
164+
callback?: ChangeHandler<number>,
165+
): this;
166+
bindNumber<K extends KeyWhereType<M, number>>(
167+
title: K,
168+
min: number,
169+
max: number,
170+
value: number,
171+
step: number,
172+
object: Record<K, number>,
173+
): this;
174+
setNumberParameters(title: KeyWhereType<M, number>, min: number, max: number, step: number): this;
175+
176+
addPassword(title: KeyWhereType<M, string>, text: string, callback?: ChangeHandler<string>): this;
177+
bindPassword<K extends KeyWhereType<M, string>>(title: K, text: string, object: Record<K, string>): this;
178+
179+
addProgressBar(title: string, max: number, value: number, valueDisplay?: 'numbers' | 'percent'): this;
180+
setProgressMax(title: string, max: number): this;
181+
182+
addText(title: KeyWhereType<M, string>, text: string, callback?: ChangeHandler<string>): this;
183+
bindText<K extends KeyWhereType<M, string>>(title: K, text: string, object: Record<K, string>): this;
184+
185+
addTextArea(title: KeyWhereType<M, string>, text: string, callback?: ChangeHandler<string>): this;
186+
bindTextArea<K extends KeyWhereType<M, string>>(title: K, text: string, object: Record<K, string>): this;
187+
setTextAreaRows(title: KeyWhereType<M, string>, rows: number): this;
188+
}
189+
190+
interface QuickSettings {
191+
/**
192+
* Creates a QuickSettingsPanel with the provided parameters.
193+
*
194+
* @template M The type of the model that is being managed by the quick settings.
195+
* @template S Type of names of controls that do not attach to the model (e.g., button or element)
196+
* @param x x position of panel (default 0)
197+
* @param y y position of panel (default 0)
198+
* @param panelTitle title of panel (default "QuickSettings")
199+
* @param parent title of panel (default "QuickSettings")
200+
* @returns New QuickSettings Panel
201+
*/
202+
create<M = AnyModel, S = string>(
203+
x?: number,
204+
y?: number,
205+
panelTitle?: string,
206+
parent?: HTMLElement,
207+
): QuickSettingsPanel<M, S>; // tslint:disable-line no-unnecessary-generics
208+
useExtStyleSheet(): void;
209+
}
210+
211+
declare const QuickSettings: QuickSettings;
212+
export default QuickSettings;

0 commit comments

Comments
 (0)