-
Notifications
You must be signed in to change notification settings - Fork 332
/
getDefaultOptions.ts
190 lines (177 loc) · 6.47 KB
/
getDefaultOptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { AutocompleteEnvironment, BaseItem } from '@algolia/autocomplete-core';
import {
generateAutocompleteId,
invariant,
warn,
} from '@algolia/autocomplete-shared';
import {
createElement as preactCreateElement,
Fragment as PreactFragment,
render,
} from 'preact';
import {
createHighlightComponent,
createReverseHighlightComponent,
createReverseSnippetComponent,
createSnippetComponent,
} from './components';
import {
AutocompleteClassNames,
AutocompleteComponents,
AutocompleteOptions,
AutocompleteRender,
AutocompleteRenderer,
AutocompleteTranslations,
} from './types';
import { getHTMLElement, mergeClassNames } from './utils';
const defaultClassNames: AutocompleteClassNames = {
clearButton: 'aa-ClearButton',
detachedCancelButton: 'aa-DetachedCancelButton',
detachedContainer: 'aa-DetachedContainer',
detachedFormContainer: 'aa-DetachedFormContainer',
detachedOverlay: 'aa-DetachedOverlay',
detachedSearchButton: 'aa-DetachedSearchButton',
detachedSearchButtonIcon: 'aa-DetachedSearchButtonIcon',
detachedSearchButtonPlaceholder: 'aa-DetachedSearchButtonPlaceholder',
detachedSearchButtonQuery: 'aa-DetachedSearchButtonQuery',
form: 'aa-Form',
input: 'aa-Input',
inputWrapper: 'aa-InputWrapper',
inputWrapperPrefix: 'aa-InputWrapperPrefix',
inputWrapperSuffix: 'aa-InputWrapperSuffix',
item: 'aa-Item',
label: 'aa-Label',
list: 'aa-List',
loadingIndicator: 'aa-LoadingIndicator',
panel: 'aa-Panel',
panelLayout: 'aa-PanelLayout aa-Panel--scrollable',
root: 'aa-Autocomplete',
source: 'aa-Source',
sourceFooter: 'aa-SourceFooter',
sourceHeader: 'aa-SourceHeader',
sourceNoResults: 'aa-SourceNoResults',
submitButton: 'aa-SubmitButton',
};
const defaultRender: AutocompleteRender<any> = ({ children, render }, root) => {
render(children, root);
};
const defaultRenderer: Required<AutocompleteRenderer> = {
createElement: preactCreateElement,
Fragment: PreactFragment,
render,
};
export function getDefaultOptions<TItem extends BaseItem>(
options: AutocompleteOptions<TItem>
) {
const {
classNames,
container,
getEnvironmentProps,
getFormProps,
getInputProps,
getItemProps,
getLabelProps,
getListProps,
getPanelProps,
getRootProps,
panelContainer,
panelPlacement,
render,
renderNoResults,
renderer,
detachedMediaQuery,
components,
translations,
...core
} = options;
/* eslint-disable no-restricted-globals */
const environment: AutocompleteEnvironment = (
typeof window !== 'undefined' ? window : {}
) as typeof window;
/* eslint-enable no-restricted-globals */
const containerElement = getHTMLElement(environment, container);
invariant(
containerElement.tagName !== 'INPUT',
'The `container` option does not support `input` elements. You need to change the container to a `div`.'
);
warn(
!(render && renderer && !renderer?.render),
`You provided the \`render\` option but did not provide a \`renderer.render\`. Since v1.6.0, you can provide a \`render\` function directly in \`renderer\`.` +
`\nTo get rid of this warning, do any of the following depending on your use case.` +
"\n- If you are using the `render` option only to override Autocomplete's default `render` function, pass the `render` function into `renderer` and remove the `render` option." +
'\n- If you are using the `render` option to customize the layout, pass your `render` function into `renderer` and use it from the provided parameters of the `render` option.' +
'\n- If you are using the `render` option to work with React 18, pass an empty `render` function into `renderer`.' +
'\nSee https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-render'
);
warn(
!renderer ||
render ||
(renderer.Fragment && renderer.createElement && renderer.render),
`You provided an incomplete \`renderer\` (missing: ${[
!renderer?.createElement && '`renderer.createElement`',
!renderer?.Fragment && '`renderer.Fragment`',
!renderer?.render && '`renderer.render`',
]
.filter(Boolean)
.join(', ')}). This can cause rendering issues.` +
'\nSee https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-renderer'
);
const defaultedRenderer = { ...defaultRenderer, ...renderer };
const defaultComponents: AutocompleteComponents = {
Highlight: createHighlightComponent(defaultedRenderer),
ReverseHighlight: createReverseHighlightComponent(defaultedRenderer),
ReverseSnippet: createReverseSnippetComponent(defaultedRenderer),
Snippet: createSnippetComponent(defaultedRenderer),
};
const defaultTranslations: AutocompleteTranslations = {
clearButtonTitle: 'Clear',
detachedCancelButtonText: 'Cancel',
detachedSearchButtonTitle: 'Search',
submitButtonTitle: 'Submit',
};
return {
renderer: {
classNames: mergeClassNames(
defaultClassNames,
classNames ?? {}
) as AutocompleteClassNames,
container: containerElement,
getEnvironmentProps: getEnvironmentProps ?? (({ props }) => props),
getFormProps: getFormProps ?? (({ props }) => props),
getInputProps: getInputProps ?? (({ props }) => props),
getItemProps: getItemProps ?? (({ props }) => props),
getLabelProps: getLabelProps ?? (({ props }) => props),
getListProps: getListProps ?? (({ props }) => props),
getPanelProps: getPanelProps ?? (({ props }) => props),
getRootProps: getRootProps ?? (({ props }) => props),
panelContainer: panelContainer
? getHTMLElement(environment, panelContainer)
: environment.document.body,
panelPlacement: panelPlacement ?? 'input-wrapper-width',
render: render ?? defaultRender,
renderNoResults,
renderer: defaultedRenderer,
detachedMediaQuery:
detachedMediaQuery ??
getComputedStyle(environment.document.documentElement).getPropertyValue(
'--aa-detached-media-query'
),
components: {
// @MAJOR Deal with registering components with the same name as the
// default ones. We could disallow registering these components by
// merging the default components second.
...defaultComponents,
...components,
},
translations: {
...defaultTranslations,
...translations,
},
},
core: {
...core,
id: core.id ?? generateAutocompleteId(),
environment,
},
};
}