-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStaticTypeaheadInput.tsx
More file actions
206 lines (195 loc) · 7.02 KB
/
StaticTypeaheadInput.tsx
File metadata and controls
206 lines (195 loc) · 7.02 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* eslint-disable max-lines */
import { useEffect, useMemo, useState } from "react";
import { FieldValues, useController } from "react-hook-form";
import { useSafeNameId } from "src/lib/hooks/useSafeNameId";
import { CommonTypeaheadProps, StaticTypeaheadAutocompleteProps, TypeaheadOption, TypeaheadOptions } from "./types/Typeahead";
import { useFormContext } from "./context/FormContext";
import Autocomplete from "@mui/material/Autocomplete";
import {
convertAutoCompleteOptionsToStringArray,
getMultipleAutoCompleteValue,
getSingleAutoCompleteValue,
isDisabledGroup,
sortOptionsByGroup,
groupOptions,
renderHighlightedOptionFunction,
combineOptions,
validateFixedOptions,
createTagRenderer,
resolveInputValue,
getOptionsFromValue,
} from "./helpers/typeahead";
import { TypeaheadTextField } from "./components/Typeahead/TypeaheadTextField";
import { FormGroupLayout } from "./FormGroupLayout";
import { LabelValueOption } from "./types/LabelValueOption";
interface StaticTypeaheadInputProps<T extends FieldValues> extends CommonTypeaheadProps<T> {
options: TypeaheadOptions;
isLoading?: boolean;
autocompleteProps?: StaticTypeaheadAutocompleteProps;
}
// eslint-disable-next-line complexity
const StaticTypeaheadInput = <T extends FieldValues>(props: StaticTypeaheadInputProps<T>) => {
const {
options,
multiple,
disabled,
variant,
label,
helpText,
hideValidationMessage,
onChange,
onInputChange,
onClose,
onOpen,
getOptionDisabled,
onBlur,
paginationText,
paginationIcon,
limitResults,
markAllOnFocus,
addonLeft,
addonRight,
style,
inputGroupStyle,
className,
placeholder,
useGroupBy = false,
readOnly,
highlightOptions = true,
autoHighlight = true,
autoSelect,
useBootstrapStyle = false,
isLoading = false,
autocompleteProps,
fixedOptions,
withFixedOptionsInValue = true,
} = props;
const [page, setPage] = useState(1);
const [loadMoreOptions, setLoadMoreOptions] = useState(limitResults !== undefined && limitResults < options.length);
const { name, id } = useSafeNameId(props.name ?? "", props.id);
const { control, disabled: formDisabled, getFieldState, clearErrors, watch } = useFormContext();
const isDisabled = useMemo(() => formDisabled || disabled, [formDisabled, disabled]);
const {
field: { ref, ...field },
} = useController({
name,
control,
shouldUnregister: isDisabled,
rules: {
validate: {
required: () => getFieldState(name)?.error?.message,
},
},
});
const paginatedOptions = useMemo(
() => (limitResults === undefined ? options : options.slice(0, page * limitResults)),
[limitResults, page, options],
);
const fieldValue = watch(name) as string | number | string[] | number[] | undefined;
const value = useMemo(
() =>
multiple
? getMultipleAutoCompleteValue(combineOptions(options, fixedOptions), fieldValue as string[] | number[] | undefined)
: getSingleAutoCompleteValue(options, fieldValue as string | number | undefined),
[fieldValue, multiple, options, fixedOptions],
);
validateFixedOptions(fixedOptions, multiple, autocompleteProps, withFixedOptionsInValue, value);
useEffect(() => {
if (limitResults !== undefined) {
setLoadMoreOptions(page * limitResults < options.length);
}
}, [options, page, limitResults]);
return (
<FormGroupLayout
name={name}
label={useBootstrapStyle ? label : undefined}
labelStyle={useBootstrapStyle ? { color: "#8493A5", fontSize: 14 } : undefined}
layout="muiInput"
>
<Autocomplete<TypeaheadOption, boolean, boolean, boolean>
{...autocompleteProps}
{...field}
id={id}
multiple={multiple}
groupBy={useGroupBy ? groupOptions : undefined}
options={useGroupBy ? sortOptionsByGroup(paginatedOptions) : paginatedOptions}
isOptionEqualToValue={
autocompleteProps?.isOptionEqualToValue ??
((option, value) => (typeof option === "string" ? option === value : option.value === (value as LabelValueOption).value))
}
getOptionKey={
autocompleteProps?.getOptionKey ??
((option: TypeaheadOption) => (typeof option === "string" ? option : `${option.label}-${option.value ?? ""}`))
}
disableCloseOnSelect={multiple}
value={resolveInputValue(multiple, fixedOptions, withFixedOptionsInValue, value)}
getOptionLabel={(option: TypeaheadOption) => (typeof option === "string" ? option : option.label)}
getOptionDisabled={(option) =>
getOptionDisabled?.(option) ||
(useGroupBy && isDisabledGroup(option)) ||
(typeof option === "string" ? false : (option.disabled ?? false))
}
disabled={isDisabled}
readOnly={readOnly}
selectOnFocus={markAllOnFocus}
style={inputGroupStyle}
className={className}
autoSelect={autoSelect}
autoHighlight={autoHighlight}
onClose={readOnly ? undefined : onClose}
onOpen={readOnly ? undefined : onOpen}
onBlur={() => {
if (onBlur) {
onBlur();
}
field.onBlur();
}}
onChange={(_, value) => {
// value is typed as Autocomplete<Value> (aka TypeaheadOption) or an array of Autocomplete<Value> (aka TypeaheadOption[])
// however, the component is not intended to be used with mixed types
const optionsArray = getOptionsFromValue(value, fixedOptions, withFixedOptionsInValue);
const values = convertAutoCompleteOptionsToStringArray(optionsArray);
const finalValue = multiple ? values : values[0];
clearErrors(field.name);
if (onChange) {
onChange(finalValue);
}
field.onChange(finalValue);
}}
onInputChange={(_e, value, reason) => {
if (onInputChange) {
onInputChange(value, reason);
}
}}
renderOption={highlightOptions ? renderHighlightedOptionFunction : undefined}
renderInput={(params) => (
<TypeaheadTextField
isLoading={isLoading}
name={name}
label={label}
addonLeft={addonLeft}
addonRight={addonRight}
addonProps={{
isDisabled,
}}
style={style}
hideValidationMessage={hideValidationMessage}
useBootstrapStyle={useBootstrapStyle}
helpText={helpText}
placeholder={multiple && value.length > 0 ? undefined : placeholder}
paginationIcon={paginationIcon}
paginationText={paginationText}
variant={variant}
limitResults={limitResults}
loadMoreOptions={loadMoreOptions}
setPage={setPage}
{...params}
inputRef={(elem) => ref(elem)}
/>
)}
renderTags={createTagRenderer(fixedOptions, autocompleteProps)}
/>
</FormGroupLayout>
);
};
export { StaticTypeaheadInput, StaticTypeaheadInputProps };