Skip to content

Commit c9ffb55

Browse files
Fix: #7817 (Autocomplete type issue) (#7823)
* Fix:#7817(Autocomplete type issue): Fixed AutoCompleteSelectEvent (and AutoCompleteUnselectEvent in extension) has value attribute typed as explicit any instead of being inferred from AutoComplete's value * Fix: #7817: Fixed the issues in taking generic type from autocomplete props * Fix: #7817: added types for the autocomplete suggessions * Fix:#7817(Autocomplete type issue): Fixed multi-select and value prop issues --------- Co-authored-by: sarathkumarsasi <sarathkumarps@outlook.com>
1 parent ef595a3 commit c9ffb55

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

components/lib/autocomplete/autocomplete.d.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,33 @@ export interface AutoCompletePassThroughMethodOptions {
3838
* @extends {FormEvent}
3939
* @event
4040
*/
41-
interface AutoCompleteChangeEvent extends FormEvent {}
41+
interface AutoCompleteChangeEvent<T = any> extends FormEvent<T> {
42+
value: T;
43+
}
4244

4345
/**
44-
* Custom select event.
46+
* Custom select event with generic type support
4547
* @see {@link AutoCompleteProps.onSelect}
4648
* @event
4749
*/
48-
interface AutoCompleteSelectEvent {
50+
interface AutoCompleteSelectEvent<T = any> {
4951
/**
5052
* Browser event
5153
*/
5254
originalEvent: React.SyntheticEvent;
5355
/**
5456
* Selected option value
5557
*/
56-
value: any;
58+
value: T extends any[] ? T[number] : T;
5759
}
5860

5961
/**
60-
* Custom unselect event.
62+
* Custom unselect event extending the select event
6163
* @see {@link AutoCompleteProps.onUnselect}
6264
* @extends {AutoCompleteSelectEvent}
6365
* @event
6466
*/
65-
interface AutoCompleteUnselectEvent extends AutoCompleteSelectEvent {}
67+
interface AutoCompleteUnselectEvent<T = any> extends AutoCompleteSelectEvent<T> {}
6668

6769
/**
6870
* Custom click event.
@@ -228,7 +230,7 @@ export interface AutoCompleteContext {
228230
* Defines valid properties in AutoComplete component. In addition to these, all properties of HTMLSpanElement can be used in this component.
229231
* @group Properties
230232
*/
231-
export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, 'onChange' | 'onSelect' | 'ref'> {
233+
export interface AutoCompleteProps<T = any> extends Omit<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, 'onChange' | 'onSelect' | 'ref' | 'value'> {
232234
/**
233235
* Unique identifier of the element.
234236
*/
@@ -290,7 +292,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
290292
/**
291293
* Icon of the dropdown.
292294
*/
293-
dropdownIcon?: IconType<AutoCompleteProps> | undefined;
295+
dropdownIcon?: IconType<AutoCompleteProps<T>> | undefined;
294296
/**
295297
* Specifies the behavior dropdown button. Default "blank" mode sends an empty string and "current" mode sends the input value.
296298
* @defaultValue blank
@@ -333,7 +335,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
333335
/**
334336
* Template of a list item.
335337
*/
336-
itemTemplate?: React.ReactNode | ((suggestion: any, index: number) => React.ReactNode);
338+
itemTemplate?: React.ReactNode | ((suggestion: T, index: number) => React.ReactNode);
337339
/**
338340
* Maximum number of characters to initiate a search.
339341
*/
@@ -367,7 +369,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
367369
/**
368370
* Template of an option group item.
369371
*/
370-
optionGroupTemplate?: React.ReactNode | ((suggestion: any, index: number) => React.ReactNode);
372+
optionGroupTemplate?: React.ReactNode | ((suggestion: T, index: number) => React.ReactNode);
371373
/**
372374
* Style class of the overlay panel element.
373375
*/
@@ -406,7 +408,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
406408
/**
407409
* Template of a selected item. In multiple mode, it is used to customize the chips using a ReactNode. In single mode, it is used to customize the text using a string.
408410
*/
409-
selectedItemTemplate?: React.ReactNode | string | undefined | null | ((value: any) => React.ReactNode | string | undefined | null);
411+
selectedItemTemplate?: React.ReactNode | string | undefined | null | ((value: T) => React.ReactNode | string | undefined | null);
410412
/**
411413
* Whether to show the empty message or not.
412414
* @defaultValue false
@@ -423,7 +425,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
423425
/**
424426
* An array of suggestions to display.
425427
*/
426-
suggestions?: any[];
428+
suggestions?: (T extends any[] ? T[number] : T)[];
427429
/**
428430
* Index of the element in tabbing order.
429431
*/
@@ -449,7 +451,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
449451
/**
450452
* Value of the component.
451453
*/
452-
value?: any;
454+
value?: this['multiple'] extends true ? T[] : T | string | number | readonly string[] | undefined;
453455
/**
454456
* Whether to use the virtualScroller feature. The properties of VirtualScroller component can be used like an object in it.
455457
* @type {VirtualScrollerProps}
@@ -469,7 +471,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
469471
* Callback to invoke when autocomplete value changes.
470472
* @param {AutoCompleteChangeEvent} event - Custom change event.
471473
*/
472-
onChange?(event: AutoCompleteChangeEvent): void;
474+
onChange?(event: AutoCompleteChangeEvent<T>): void;
473475
/**
474476
* Callback to invoke when input is cleared by the user.
475477
* @param {React.SyntheticEvent} event - Browser event.
@@ -523,7 +525,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
523525
* Callback to invoke when a suggestion is selected.
524526
* @param {AutoCompleteSelectEvent} event - Custom select event.
525527
*/
526-
onSelect?(event: AutoCompleteSelectEvent): void;
528+
onSelect?(event: AutoCompleteSelectEvent<T>): void;
527529
/**
528530
* Callback to invoke when overlay panel becomes visible.
529531
*/
@@ -532,7 +534,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
532534
* Callback to invoke when a selected value is removed.
533535
* @param {AutoCompleteUnselectEvent} event - Custom unselect event.
534536
*/
535-
onUnselect?(event: AutoCompleteUnselectEvent): void;
537+
onUnselect?(event: AutoCompleteUnselectEvent<T>): void;
536538
/**
537539
* Used to get the child elements of the component.
538540
* @readonly
@@ -566,7 +568,7 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In
566568
*
567569
* @group Component
568570
*/
569-
export declare class AutoComplete extends React.Component<AutoCompleteProps, any> {
571+
export declare class AutoComplete<T = any> extends React.Component<AutoCompleteProps<T>, any> {
570572
/**
571573
* Used to show the overlay.
572574
*/

0 commit comments

Comments
 (0)