Skip to content

Commit 40517b9

Browse files
refactor(js): update types
1 parent f6ce779 commit 40517b9

File tree

12 files changed

+194
-173
lines changed

12 files changed

+194
-173
lines changed

packages/autocomplete-core/src/types/AutocompleteApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface AutocompleteScopeApi<TItem extends BaseItem>
88
/**
99
* Triggers a search to refresh the state.
1010
*/
11-
refresh: () => Promise<void>;
11+
refresh(): Promise<void>;
1212
}
1313

1414
export type AutocompleteApi<

packages/autocomplete-js/src/createAutocompleteDom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { AutocompleteClassNames, AutocompleteDom } from './types';
1818

1919
type CreateDomProps<TItem extends BaseItem> = AutocompleteCoreApi<TItem> & {
20-
classNames: AutocompleteClassNames;
20+
classNames: Partial<AutocompleteClassNames>;
2121
};
2222

2323
export function createAutocompleteDom<TItem extends BaseItem>({

packages/autocomplete-js/src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { setProperties, setPropertiesWithoutEvents } from './utils';
2020

2121
type RenderProps<TItem extends BaseItem> = {
2222
state: AutocompleteState<TItem>;
23-
classNames: AutocompleteClassNames;
23+
classNames: Partial<AutocompleteClassNames>;
2424
panelRoot: HTMLElement;
2525
} & AutocompleteCoreApi<TItem> &
2626
AutocompleteDom;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {
2+
AutocompleteScopeApi as AutocompleteCoreScopeApi,
3+
BaseItem,
4+
} from '@algolia/autocomplete-core';
5+
6+
export interface AutocompleteApi<TItem extends BaseItem>
7+
extends AutocompleteCoreScopeApi<TItem> {
8+
/**
9+
* Cleans up the DOM mutations and event listeners.
10+
*/
11+
destroy(): void;
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type AutocompleteClassNames = {
2+
root: string;
3+
form: string;
4+
label: string;
5+
inputWrapper: string;
6+
input: string;
7+
submitButton: string;
8+
resetButton: string;
9+
loadingIndicator: string;
10+
panel: string;
11+
panelLayout: string;
12+
source: string;
13+
sourceHeader: string;
14+
list: string;
15+
item: string;
16+
sourceFooter: string;
17+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BaseItem } from '@algolia/autocomplete-core';
2+
3+
import { InternalAutocompleteSource } from './AutocompleteSource';
4+
5+
export interface AutocompleteCollection<TItem extends BaseItem> {
6+
source: InternalAutocompleteSource<TItem>;
7+
items: TItem[];
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type AutocompleteDom = {
2+
inputWrapper: HTMLDivElement;
3+
input: HTMLInputElement;
4+
root: HTMLDivElement;
5+
form: HTMLFormElement;
6+
label: HTMLLabelElement;
7+
submitButton: HTMLButtonElement;
8+
resetButton: HTMLButtonElement;
9+
loadingIndicator: HTMLDivElement;
10+
panel: HTMLDivElement;
11+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
AutocompleteOptions as AutocompleteCoreOptions,
3+
BaseItem,
4+
GetSourcesParams,
5+
} from '@algolia/autocomplete-core';
6+
import { MaybePromise } from '@algolia/autocomplete-shared';
7+
8+
import { AutocompleteClassNames } from './AutocompleteClassNames';
9+
import { AutocompleteSource } from './AutocompleteSource';
10+
import { AutocompleteState } from './AutocompleteState';
11+
12+
export type AutocompleteRenderer<TItem extends BaseItem> = (params: {
13+
root: HTMLElement;
14+
sections: HTMLElement[];
15+
state: AutocompleteState<TItem>;
16+
}) => void;
17+
18+
export interface AutocompleteOptions<TItem extends BaseItem>
19+
extends AutocompleteCoreOptions<TItem> {
20+
/**
21+
* The container for the Autocomplete search box.
22+
*
23+
* You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). The first element matching the provided selector will be used as container.
24+
*/
25+
container: string | HTMLElement;
26+
/**
27+
* The container for the Autocomplete panel.
28+
*
29+
* You can either pass a [CSS selector](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) or an [Element](https://developer.mozilla.org/docs/Web/API/HTMLElement). The first element matching the provided selector will be used as container.
30+
*
31+
* @default document.body
32+
*/
33+
panelContainer?: string | HTMLElement;
34+
getSources?: (
35+
params: GetSourcesParams<TItem>
36+
) => MaybePromise<Array<AutocompleteSource<TItem>>>;
37+
/**
38+
* The panel horizontal position.
39+
*
40+
* @default "input-wrapper-width"
41+
*/
42+
panelPlacement?: 'start' | 'end' | 'full-width' | 'input-wrapper-width';
43+
/**
44+
* The class names to inject in each created DOM element.
45+
*
46+
* It it useful to design with external CSS frameworks.
47+
*/
48+
classNames?: Partial<AutocompleteClassNames>;
49+
/**
50+
* Function called to render the autocomplete results. It is useful for rendering sections in different row or column layouts.
51+
* The default implementation appends all the sections to the root:
52+
*
53+
* ```js
54+
* autocomplete({
55+
* // ...
56+
* render({ root, sections }) {
57+
* for (const section of sections) {
58+
* root.appendChild(section);
59+
* }
60+
* },
61+
* });
62+
* ```
63+
*/
64+
render?: AutocompleteRenderer<TItem>;
65+
initialState?: Partial<AutocompleteState<TItem>>;
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
AutocompleteSource as AutocompleteCoreSource,
3+
InternalAutocompleteSource as InternalAutocompleteCoreSource,
4+
BaseItem,
5+
} from '@algolia/autocomplete-core';
6+
7+
import { AutocompleteState } from './AutocompleteState';
8+
9+
type Template<TParams> = (params: TParams) => string | void;
10+
11+
/**
12+
* Templates to display in the autocomplete panel.
13+
*
14+
* A template can either return a string, or perform DOM mutations (manipulating DOM elements with JavaScript and attaching events) without returning a string.
15+
*/
16+
export type SourceTemplates<TItem extends BaseItem> = {
17+
/**
18+
* The template for the suggestion item.
19+
*/
20+
item: Template<{
21+
root: HTMLElement;
22+
item: TItem;
23+
state: AutocompleteState<TItem>;
24+
}>;
25+
/**
26+
* The template for the section header.
27+
*/
28+
header?: Template<{
29+
root: HTMLElement;
30+
state: AutocompleteState<TItem>;
31+
source: AutocompleteSource<TItem>;
32+
items: TItem[];
33+
}>;
34+
/**
35+
* The template for the section footer.
36+
*/
37+
footer?: Template<{
38+
root: HTMLElement;
39+
state: AutocompleteState<TItem>;
40+
source: AutocompleteSource<TItem>;
41+
items: TItem[];
42+
}>;
43+
};
44+
45+
type WithTemplates<TType, TItem extends BaseItem> = TType & {
46+
templates: SourceTemplates<TItem>;
47+
};
48+
49+
export type AutocompleteSource<TItem extends BaseItem> = WithTemplates<
50+
AutocompleteCoreSource<TItem>,
51+
TItem
52+
>;
53+
export type InternalAutocompleteSource<TItem extends BaseItem> = WithTemplates<
54+
InternalAutocompleteCoreSource<TItem>,
55+
TItem
56+
>;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
AutocompleteState as AutocompleteCoreState,
3+
BaseItem,
4+
} from '@algolia/autocomplete-core';
5+
6+
import { AutocompleteCollection } from './AutocompleteCollection';
7+
8+
export type AutocompleteState<TItem extends BaseItem> = Omit<
9+
AutocompleteCoreState<TItem>,
10+
'collections'
11+
> & {
12+
collections: Array<AutocompleteCollection<TItem>>;
13+
};

0 commit comments

Comments
 (0)