@okta/odyssey-react-mui 1.6.18 → 1.6.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okta/odyssey-react-mui",
3
- "version": "1.6.18",
3
+ "version": "1.6.19",
4
4
  "description": "React MUI components for Odyssey, Okta's design system",
5
5
  "author": "Okta, Inc.",
6
6
  "license": "Apache-2.0",
@@ -51,7 +51,7 @@
51
51
  "@mui/system": "^5.14.9",
52
52
  "@mui/utils": "^5.11.2",
53
53
  "@mui/x-date-pickers": "^5.0.15",
54
- "@okta/odyssey-design-tokens": "1.6.18",
54
+ "@okta/odyssey-design-tokens": "1.6.19",
55
55
  "date-fns": "^2.30.0",
56
56
  "i18next": "^23.5.1",
57
57
  "material-react-table": "^1.14.0",
@@ -63,5 +63,5 @@
63
63
  "react": ">=17 <19",
64
64
  "react-dom": ">=17 <19"
65
65
  },
66
- "gitHead": "cfe632641bc22d95e55d7043c5d134347117eb74"
66
+ "gitHead": "8a5e1cf86a04424f2cba1d27ae94e41ed95e7968"
67
67
  }
@@ -14,18 +14,31 @@ import {
14
14
  Autocomplete as MuiAutocomplete,
15
15
  AutocompleteProps as MuiAutocompleteProps,
16
16
  InputBase,
17
+ UseAutocompleteProps,
18
+ AutocompleteValue,
17
19
  } from "@mui/material";
18
- import { memo, useCallback } from "react";
20
+ import { memo, useCallback, useMemo } from "react";
19
21
 
20
22
  import { Field } from "./Field";
21
23
  import { FieldComponentProps } from "./FieldComponentProps";
22
24
  import type { SeleniumProps } from "./SeleniumProps";
25
+ import { useControlledState } from "./useControlledState";
23
26
 
24
27
  export type AutocompleteProps<
25
28
  OptionType,
26
29
  HasMultipleChoices extends boolean | undefined,
27
30
  IsCustomValueAllowed extends boolean | undefined
28
31
  > = {
32
+ /**
33
+ * The default value. Use when the component is not controlled.
34
+ * @default props.multiple ? [] : null
35
+ */
36
+ defaultValue?: UseAutocompleteProps<
37
+ OptionType,
38
+ HasMultipleChoices,
39
+ undefined,
40
+ IsCustomValueAllowed
41
+ >["defaultValue"];
29
42
  /**
30
43
  * Enables multiple choice selection
31
44
  */
@@ -35,6 +48,15 @@ export type AutocompleteProps<
35
48
  undefined,
36
49
  IsCustomValueAllowed
37
50
  >["multiple"];
51
+ /**
52
+ * The value for the input
53
+ */
54
+ inputValue?: UseAutocompleteProps<
55
+ OptionType,
56
+ HasMultipleChoices,
57
+ undefined,
58
+ IsCustomValueAllowed
59
+ >["inputValue"];
38
60
  /**
39
61
  * Allows the input of custom values
40
62
  */
@@ -87,7 +109,7 @@ export type AutocompleteProps<
87
109
  /**
88
110
  * Callback fired when a selection is made.
89
111
  */
90
- onChange?: MuiAutocompleteProps<
112
+ onChange?: UseAutocompleteProps<
91
113
  OptionType,
92
114
  HasMultipleChoices,
93
115
  undefined,
@@ -114,21 +136,26 @@ export type AutocompleteProps<
114
136
  /**
115
137
  * The options for the Autocomplete input
116
138
  */
117
- options: MuiAutocompleteProps<
118
- OptionType,
119
- HasMultipleChoices,
120
- undefined,
121
- IsCustomValueAllowed
122
- >["options"];
139
+ options: ReadonlyArray<OptionType>;
123
140
  /**
124
141
  * The value of the Autocomplete input
125
142
  */
126
- value?: MuiAutocompleteProps<
143
+ value?: UseAutocompleteProps<
127
144
  OptionType,
128
145
  HasMultipleChoices,
129
146
  undefined,
130
147
  IsCustomValueAllowed
131
148
  >["value"];
149
+
150
+ /**
151
+ * Used to determine if the option represents the given value. Uses strict equality by default if none provided.
152
+ * Both arguments need to be handled, an option can only match with one value.
153
+ * option: the option to test
154
+ * value: the value to test against
155
+ *
156
+ * You will need to implement this function if your `option` items are objects.
157
+ */
158
+ getIsOptionEqualToValue?: (option: OptionType, value: OptionType) => boolean;
132
159
  } & Pick<
133
160
  FieldComponentProps,
134
161
  "errorMessage" | "hint" | "id" | "isOptional" | "name"
@@ -140,9 +167,11 @@ const Autocomplete = <
140
167
  HasMultipleChoices extends boolean | undefined,
141
168
  IsCustomValueAllowed extends boolean | undefined
142
169
  >({
170
+ defaultValue,
143
171
  errorMessage,
144
172
  hasMultipleChoices,
145
173
  id: idOverride,
174
+ inputValue,
146
175
  isCustomValueAllowed,
147
176
  isDisabled,
148
177
  isLoading,
@@ -152,11 +181,12 @@ const Autocomplete = <
152
181
  label,
153
182
  name: nameOverride,
154
183
  onBlur,
155
- onChange,
156
- onInputChange,
184
+ onChange: onChangeProp,
185
+ onInputChange: onInputChangeProp,
157
186
  onFocus,
158
187
  options,
159
188
  value,
189
+ getIsOptionEqualToValue,
160
190
  testId,
161
191
  }: AutocompleteProps<OptionType, HasMultipleChoices, IsCustomValueAllowed>) => {
162
192
  const renderInput = useCallback(
@@ -194,11 +224,78 @@ const Autocomplete = <
194
224
  [errorMessage, hint, isOptional, label, nameOverride]
195
225
  );
196
226
 
227
+ const defaultValuesProp = useMemo<
228
+ | AutocompleteValue<
229
+ OptionType,
230
+ HasMultipleChoices,
231
+ undefined,
232
+ IsCustomValueAllowed
233
+ >
234
+ | undefined
235
+ >(() => {
236
+ if (hasMultipleChoices) {
237
+ return defaultValue === undefined
238
+ ? ([] as AutocompleteValue<
239
+ OptionType,
240
+ HasMultipleChoices,
241
+ undefined,
242
+ IsCustomValueAllowed
243
+ >)
244
+ : defaultValue;
245
+ }
246
+ return defaultValue ?? undefined;
247
+ }, [defaultValue, hasMultipleChoices]);
248
+
249
+ const [localValue, setLocalValue] = useControlledState({
250
+ controlledValue: value,
251
+ uncontrolledValue: defaultValuesProp,
252
+ });
253
+
254
+ const [localInputValue, setLocalInputValue] = useControlledState({
255
+ controlledValue: inputValue,
256
+ uncontrolledValue: undefined,
257
+ });
258
+
259
+ const onChange = useCallback<
260
+ NonNullable<
261
+ UseAutocompleteProps<
262
+ OptionType,
263
+ HasMultipleChoices,
264
+ undefined,
265
+ IsCustomValueAllowed
266
+ >["onChange"]
267
+ >
268
+ >(
269
+ (event, value, reason, details) => {
270
+ setLocalValue(value);
271
+ onChangeProp?.(event, value, reason, details);
272
+ },
273
+ [onChangeProp, setLocalValue]
274
+ );
275
+
276
+ const onInputChange = useCallback<
277
+ NonNullable<
278
+ UseAutocompleteProps<
279
+ OptionType,
280
+ HasMultipleChoices,
281
+ undefined,
282
+ IsCustomValueAllowed
283
+ >["onInputChange"]
284
+ >
285
+ >(
286
+ (event, value, reason) => {
287
+ setLocalInputValue(value);
288
+ onInputChangeProp?.(event, value, reason);
289
+ },
290
+ [onInputChangeProp, setLocalInputValue]
291
+ );
292
+
197
293
  return (
198
294
  <MuiAutocomplete
199
295
  // AutoComplete is wrapped in a div within MUI which does not get the disabled attr. So this aria-disabled gets set in the div
200
296
  aria-disabled={isDisabled}
201
297
  data-se={testId}
298
+ defaultValue={defaultValuesProp}
202
299
  disableCloseOnSelect={hasMultipleChoices}
203
300
  disabled={isDisabled}
204
301
  freeSolo={isCustomValueAllowed}
@@ -213,7 +310,9 @@ const Autocomplete = <
213
310
  options={options}
214
311
  readOnly={isReadOnly}
215
312
  renderInput={renderInput}
216
- value={value}
313
+ value={localValue}
314
+ inputValue={localInputValue}
315
+ isOptionEqualToValue={getIsOptionEqualToValue}
217
316
  />
218
317
  );
219
318
  };
@@ -0,0 +1,335 @@
1
+ /*!
2
+ * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
3
+ * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
4
+ *
5
+ * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
6
+ * Unless required by applicable law or agreed to in writing, software
7
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
8
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ *
10
+ * See the License for the specific language governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {
14
+ Autocomplete as MuiAutocomplete,
15
+ AutocompleteProps as MuiAutocompleteProps,
16
+ InputBase,
17
+ UseAutocompleteProps,
18
+ AutocompleteValue,
19
+ } from "@mui/material";
20
+ import { memo, useCallback, useMemo } from "react";
21
+
22
+ import { Field } from "../Field";
23
+ import { FieldComponentProps } from "../FieldComponentProps";
24
+ import type { SeleniumProps } from "../SeleniumProps";
25
+ import { useControlledState } from "../useControlledState";
26
+
27
+ export type AutocompleteProps<
28
+ OptionType,
29
+ HasMultipleChoices extends boolean | undefined,
30
+ IsCustomValueAllowed extends boolean | undefined
31
+ > = {
32
+ /**
33
+ * The default value. Use when the component is not controlled.
34
+ * @default props.multiple ? [] : null
35
+ */
36
+ defaultValue?: UseAutocompleteProps<
37
+ OptionType,
38
+ HasMultipleChoices,
39
+ undefined,
40
+ IsCustomValueAllowed
41
+ >["defaultValue"];
42
+ /**
43
+ * Enables multiple choice selection
44
+ */
45
+ hasMultipleChoices?: MuiAutocompleteProps<
46
+ OptionType,
47
+ HasMultipleChoices,
48
+ undefined,
49
+ IsCustomValueAllowed
50
+ >["multiple"];
51
+ /**
52
+ * The value for the input
53
+ */
54
+ inputValue?: UseAutocompleteProps<
55
+ OptionType,
56
+ HasMultipleChoices,
57
+ undefined,
58
+ IsCustomValueAllowed
59
+ >["inputValue"];
60
+ /**
61
+ * Allows the input of custom values
62
+ */
63
+ isCustomValueAllowed?: MuiAutocompleteProps<
64
+ OptionType,
65
+ HasMultipleChoices,
66
+ undefined,
67
+ IsCustomValueAllowed
68
+ >["freeSolo"];
69
+ /**
70
+ * Disables the Autocomplete input
71
+ */
72
+ isDisabled?: MuiAutocompleteProps<
73
+ OptionType,
74
+ HasMultipleChoices,
75
+ undefined,
76
+ IsCustomValueAllowed
77
+ >["disabled"];
78
+ /**
79
+ * Displays a loading indicator
80
+ */
81
+ isLoading?: MuiAutocompleteProps<
82
+ OptionType,
83
+ HasMultipleChoices,
84
+ undefined,
85
+ IsCustomValueAllowed
86
+ >["loading"];
87
+ /**
88
+ * Makes the Autocomplete input read-only
89
+ */
90
+ isReadOnly?: MuiAutocompleteProps<
91
+ OptionType,
92
+ HasMultipleChoices,
93
+ undefined,
94
+ IsCustomValueAllowed
95
+ >["readOnly"];
96
+ /**
97
+ * The label text for the autocomplete input
98
+ */
99
+ label: string;
100
+ /**
101
+ * The component used to render the listbox.
102
+ */
103
+ ListboxComponent?: React.JSXElementConstructor<
104
+ React.HTMLAttributes<HTMLElement>
105
+ >;
106
+ /**
107
+ * Callback fired when the autocomplete loses focus.
108
+ */
109
+ onBlur?: MuiAutocompleteProps<
110
+ OptionType,
111
+ HasMultipleChoices,
112
+ undefined,
113
+ IsCustomValueAllowed
114
+ >["onBlur"];
115
+ /**
116
+ * Callback fired when a selection is made.
117
+ */
118
+ onChange?: UseAutocompleteProps<
119
+ OptionType,
120
+ HasMultipleChoices,
121
+ undefined,
122
+ IsCustomValueAllowed
123
+ >["onChange"];
124
+ /**
125
+ * Callback fired when the textbox receives typed characters.
126
+ */
127
+ onInputChange?: MuiAutocompleteProps<
128
+ OptionType,
129
+ HasMultipleChoices,
130
+ undefined,
131
+ IsCustomValueAllowed
132
+ >["onInputChange"];
133
+ /**
134
+ * Callback fired when the autocomplete gains focus.
135
+ */
136
+ onFocus?: MuiAutocompleteProps<
137
+ OptionType,
138
+ HasMultipleChoices,
139
+ undefined,
140
+ IsCustomValueAllowed
141
+ >["onFocus"];
142
+ /**
143
+ * The options for the Autocomplete input
144
+ */
145
+ options: ReadonlyArray<OptionType>;
146
+ /**
147
+ * The value of the Autocomplete input
148
+ */
149
+ value?: UseAutocompleteProps<
150
+ OptionType,
151
+ HasMultipleChoices,
152
+ undefined,
153
+ IsCustomValueAllowed
154
+ >["value"];
155
+
156
+ /**
157
+ * Used to determine if the option represents the given value. Uses strict equality by default if none provided.
158
+ * Both arguments need to be handled, an option can only match with one value.
159
+ * option: the option to test
160
+ * value: the value to test against
161
+ *
162
+ * You will need to implement this function if your `option` items are objects.
163
+ */
164
+ getIsOptionEqualToValue?: (option: OptionType, value: OptionType) => boolean;
165
+ } & Pick<
166
+ FieldComponentProps,
167
+ "errorMessage" | "hint" | "id" | "isOptional" | "name"
168
+ > &
169
+ SeleniumProps;
170
+
171
+ const VirtualizedAutocomplete = <
172
+ OptionType,
173
+ HasMultipleChoices extends boolean | undefined,
174
+ IsCustomValueAllowed extends boolean | undefined
175
+ >({
176
+ defaultValue,
177
+ errorMessage,
178
+ hasMultipleChoices,
179
+ id: idOverride,
180
+ inputValue,
181
+ isCustomValueAllowed,
182
+ isDisabled,
183
+ isLoading,
184
+ isOptional = false,
185
+ isReadOnly,
186
+ hint,
187
+ label,
188
+ ListboxComponent,
189
+ name: nameOverride,
190
+ onBlur,
191
+ onChange: onChangeProp,
192
+ onInputChange: onInputChangeProp,
193
+ onFocus,
194
+ options,
195
+ value,
196
+ getIsOptionEqualToValue,
197
+ testId,
198
+ }: AutocompleteProps<OptionType, HasMultipleChoices, IsCustomValueAllowed>) => {
199
+ const renderInput = useCallback(
200
+ ({ InputLabelProps, InputProps, ...params }) => (
201
+ <Field
202
+ errorMessage={errorMessage}
203
+ fieldType="single"
204
+ hasVisibleLabel
205
+ id={InputLabelProps.htmlFor}
206
+ hint={hint}
207
+ label={label}
208
+ isOptional={isOptional}
209
+ renderFieldComponent={({
210
+ ariaDescribedBy,
211
+ id,
212
+ errorMessageElementId,
213
+ labelElementId,
214
+ }) => (
215
+ <InputBase
216
+ {...params}
217
+ {...InputProps}
218
+ inputProps={{
219
+ ...params.inputProps,
220
+ "aria-errormessage": errorMessageElementId,
221
+ "aria-labelledby": labelElementId,
222
+ }}
223
+ aria-describedby={ariaDescribedBy}
224
+ id={id}
225
+ name={nameOverride ?? id}
226
+ required={!isOptional}
227
+ />
228
+ )}
229
+ />
230
+ ),
231
+ [errorMessage, hint, isOptional, label, nameOverride]
232
+ );
233
+
234
+ const defaultValuesProp = useMemo<
235
+ | AutocompleteValue<
236
+ OptionType,
237
+ HasMultipleChoices,
238
+ undefined,
239
+ IsCustomValueAllowed
240
+ >
241
+ | undefined
242
+ >(() => {
243
+ if (hasMultipleChoices) {
244
+ return defaultValue === undefined
245
+ ? ([] as AutocompleteValue<
246
+ OptionType,
247
+ HasMultipleChoices,
248
+ undefined,
249
+ IsCustomValueAllowed
250
+ >)
251
+ : defaultValue;
252
+ }
253
+ return defaultValue ?? undefined;
254
+ }, [defaultValue, hasMultipleChoices]);
255
+
256
+ const [localValue, setLocalValue] = useControlledState({
257
+ controlledValue: value,
258
+ uncontrolledValue: defaultValuesProp,
259
+ });
260
+
261
+ const [localInputValue, setLocalInputValue] = useControlledState({
262
+ controlledValue: inputValue,
263
+ uncontrolledValue: undefined,
264
+ });
265
+
266
+ const onChange = useCallback<
267
+ NonNullable<
268
+ UseAutocompleteProps<
269
+ OptionType,
270
+ HasMultipleChoices,
271
+ undefined,
272
+ IsCustomValueAllowed
273
+ >["onChange"]
274
+ >
275
+ >(
276
+ (event, value, reason, details) => {
277
+ setLocalValue(value);
278
+ onChangeProp?.(event, value, reason, details);
279
+ },
280
+ [onChangeProp, setLocalValue]
281
+ );
282
+
283
+ const onInputChange = useCallback<
284
+ NonNullable<
285
+ UseAutocompleteProps<
286
+ OptionType,
287
+ HasMultipleChoices,
288
+ undefined,
289
+ IsCustomValueAllowed
290
+ >["onInputChange"]
291
+ >
292
+ >(
293
+ (event, value, reason) => {
294
+ setLocalInputValue(value);
295
+ onInputChangeProp?.(event, value, reason);
296
+ },
297
+ [onInputChangeProp, setLocalInputValue]
298
+ );
299
+
300
+ return (
301
+ <MuiAutocomplete
302
+ // AutoComplete is wrapped in a div within MUI which does not get the disabled attr. So this aria-disabled gets set in the div
303
+ aria-disabled={isDisabled}
304
+ data-se={testId}
305
+ defaultValue={defaultValuesProp}
306
+ disableCloseOnSelect={hasMultipleChoices}
307
+ disabled={isDisabled}
308
+ freeSolo={isCustomValueAllowed}
309
+ filterSelectedOptions={true}
310
+ id={idOverride}
311
+ ListboxComponent={ListboxComponent}
312
+ loading={isLoading}
313
+ multiple={hasMultipleChoices}
314
+ onBlur={onBlur}
315
+ onChange={onChange}
316
+ onInputChange={onInputChange}
317
+ onFocus={onFocus}
318
+ options={options}
319
+ readOnly={isReadOnly}
320
+ renderInput={renderInput}
321
+ value={localValue}
322
+ inputValue={localInputValue}
323
+ isOptionEqualToValue={getIsOptionEqualToValue}
324
+ />
325
+ );
326
+ };
327
+
328
+ // Need the `typeof Autocomplete` because generics don't get passed through
329
+ const MemoizedAutocomplete = memo(
330
+ VirtualizedAutocomplete
331
+ ) as typeof VirtualizedAutocomplete;
332
+ // @ts-expect-error displayName is expected to not be on `typeof Autocomplete`
333
+ MemoizedAutocomplete.displayName = "Autocomplete";
334
+
335
+ export { MemoizedAutocomplete as VirtualizedAutocomplete };
package/src/labs/index.ts CHANGED
@@ -23,3 +23,4 @@ export * from "./materialReactTableTypes";
23
23
  export * from "./PaginatedTable";
24
24
  export * from "./StaticTable";
25
25
  export * from "./GroupPicker";
26
+ export * from "./VirtualizedAutocomplete";