Skip to content

Commit 87de334

Browse files
wKovacs64Martin Badin
authored andcommitted
Add types for react-dual-listbox (DefinitelyTyped#58390)
* Add types for react-dual-listbox * Remove examples and reformat comments for consistency
1 parent dab0aef commit 87de334

File tree

4 files changed

+476
-0
lines changed

4 files changed

+476
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
// Type definitions for react-dual-listbox 2.2
2+
// Project: https://github.com/jakezatecky/react-dual-listbox
3+
// Definitions by: Justin Hall <https://github.com/wKovacs64>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
// Minimum TypeScript Version: 3.9
6+
7+
import * as React from 'react';
8+
9+
/**
10+
* A value-based option.
11+
*/
12+
export interface ValueOption<T> {
13+
/**
14+
* Whether the option is disabled or not.
15+
*
16+
* @default false
17+
*/
18+
disabled?: boolean;
19+
/**
20+
* Adds the HTML `title` attribute to the option.
21+
*/
22+
title?: string;
23+
/**
24+
* The option label.
25+
*/
26+
label: string;
27+
/**
28+
* The option value.
29+
*/
30+
value: T;
31+
}
32+
33+
/**
34+
* A category with other categories and values.
35+
*/
36+
export interface CategoryOption<T> {
37+
/**
38+
* Whether the category is disabled or not.
39+
*
40+
* @default false
41+
*/
42+
disabled?: boolean;
43+
/**
44+
* Adds the HTML `title` attribute to the option.
45+
*/
46+
title?: string;
47+
/**
48+
* The category label.
49+
*/
50+
label: string;
51+
/**
52+
* The category child options.
53+
*/
54+
options: Array<Option<T>>;
55+
}
56+
57+
/**
58+
* Valid options include values and categories.
59+
*/
60+
export type Option<T> = ValueOption<T> | CategoryOption<T>;
61+
62+
/**
63+
* A filter.
64+
*/
65+
export interface Filter<T> {
66+
/**
67+
* Available options.
68+
*/
69+
available: T[];
70+
/**
71+
* Selected options.
72+
*/
73+
selected: T[];
74+
}
75+
76+
/**
77+
* Properties common to every `DualListBox`.
78+
*/
79+
export interface CommonProperties<T> {
80+
/**
81+
* Available options.
82+
*/
83+
options: Array<Option<T>>;
84+
/**
85+
* Selected options.
86+
*/
87+
selected?: T[];
88+
/**
89+
* A React function `ref` to the "selected" list box.
90+
*/
91+
selectedRef?: ((availableInput: HTMLSelectElement) => void) | null;
92+
/**
93+
* Override the default alignment of action buttons.
94+
*
95+
* @default "middle"
96+
*/
97+
alignActions?: 'top' | 'middle';
98+
/**
99+
* If true, duplicate options will be allowed in the selected list box.
100+
*
101+
* @default false
102+
*/
103+
allowDuplicates?: boolean;
104+
/**
105+
* A subset of the `options` array to optionally filter the available list
106+
* box.
107+
*/
108+
available?: T[];
109+
/**
110+
* A React function `ref` to the "available" list box.
111+
*
112+
* @default null
113+
*/
114+
availableRef?: ((availableInput: HTMLSelectElement) => void) | null;
115+
/**
116+
* An optional `className` to apply to the root node.
117+
*
118+
* @default null
119+
*/
120+
className?: string | null;
121+
/**
122+
* If true, both "available" and "selected" list boxes will be disabled.
123+
*
124+
* @default false
125+
*/
126+
disabled?: boolean;
127+
/**
128+
* A key-value pairing of action icons and their React nodes.
129+
*/
130+
icons?: {
131+
[k in
132+
| 'moveLeft'
133+
| 'moveAllLeft'
134+
| 'moveRight'
135+
| 'moveAllRight'
136+
| 'moveDown'
137+
| 'moveUp'
138+
| 'moveTop'
139+
| 'moveBottom']?: React.ReactNode;
140+
};
141+
/**
142+
* An HTML ID prefix for the various sub elements.
143+
*
144+
* @default null
145+
*/
146+
id?: string | null;
147+
/**
148+
* A key-value pairing of localized text.
149+
*/
150+
lang?: {
151+
[k in
152+
| 'availableFilterHeader'
153+
| 'availableHeader'
154+
| 'moveAllLeft'
155+
| 'moveAllRight'
156+
| 'moveLeft'
157+
| 'moveRight'
158+
| 'moveBottom'
159+
| 'moveDown'
160+
| 'moveUp'
161+
| 'moveTop'
162+
| 'noAvailableOptions'
163+
| 'noSelectedOptions'
164+
| 'selectedFilterHeader'
165+
| 'selectedHeader']?: string;
166+
};
167+
/**
168+
* A list of key codes that will trigger a toggle of the selected options.
169+
*
170+
* @default [13, 32]
171+
*/
172+
moveKeyCodes?: number[];
173+
/**
174+
* A value for the `name` attribute on the hidden `<input />` element. This is potentially
175+
* useful for form submissions.
176+
*
177+
* @default null
178+
*/
179+
name?: string | null;
180+
/**
181+
* This flag will preserve the selection order. By default, `react-dual-listbox` orders
182+
* selected items according to the order of the `options` property.
183+
*
184+
* @default false
185+
*/
186+
preserveSelectOrder?: boolean;
187+
/**
188+
* If true, labels above both the available and selected list boxes will appear. These labels
189+
* derive from `lang`.
190+
*
191+
* @default false
192+
*/
193+
showHeaderLabels?: boolean;
194+
/**
195+
* If true, text will appear in place of the available/selected list boxes when no options are
196+
* present.
197+
*
198+
* @default false;
199+
*/
200+
showNoOptionsText?: boolean;
201+
/**
202+
* If true, a set of up/down buttons will appear near the selected list box to allow the user
203+
* to re-arrange the items.
204+
*
205+
* @default false
206+
*/
207+
showOrderButtons?: boolean;
208+
}
209+
210+
/**
211+
* Additional `DualListBox` properties with filter.
212+
*/
213+
export interface FilterProperties<T, F extends boolean> {
214+
/**
215+
* Flag that determines whether filtering is enabled.
216+
*
217+
* @default false
218+
*/
219+
canFilter?: F;
220+
/**
221+
* Override the default filtering function.
222+
*/
223+
filterCallback?: F extends true ? (option: Option<T>, filterInput: string) => boolean : undefined;
224+
/**
225+
* Override the default filter placeholder.
226+
*/
227+
filterPlaceholder?: F extends true ? string : undefined;
228+
/**
229+
* Control the filter search text.
230+
*/
231+
filter?: Filter<T>;
232+
/**
233+
* Handle filter change.
234+
*/
235+
onFilterChange?: F extends true ? (filter: string) => void : undefined;
236+
}
237+
238+
/**
239+
* Additional `DualListBox` properties with complex selected values.
240+
*/
241+
export interface ValueProperties<T, V extends boolean> {
242+
/**
243+
* The handler called when options are moved to either side.
244+
*/
245+
// onChange?: (selected: (T | Option<T>)[]) => void;
246+
onChange?: (selected: V extends true ? T[] : Array<Option<T>>) => void;
247+
/**
248+
* If true, the selected value passed in onChange is an array of string values.
249+
* Otherwise, it is an array of options.
250+
*
251+
* @default true
252+
*/
253+
simpleValue?: V;
254+
}
255+
256+
/**
257+
* `DualListBox` component properties.
258+
*/
259+
// export type DualListBoxProperties<P> = CommonProperties<P> & FilterProperties<P> & ValueProperties<P>;
260+
export interface DualListBoxProperties<P, F extends boolean, V extends boolean>
261+
extends CommonProperties<P>,
262+
FilterProperties<P, F>,
263+
ValueProperties<P, V> {}
264+
265+
/**
266+
* A feature-rich dual list box for React.
267+
*
268+
* The `DualListBox` is a controlled component, so you have to update the `selected` property in
269+
* conjunction with the `onChange` handler if you want the selected values to change.
270+
*/
271+
export default class DualListBox<P, F extends boolean = false, V extends boolean = true> extends React.Component<
272+
DualListBoxProperties<P, F, V>
273+
> {}

0 commit comments

Comments
 (0)