@lumx/react 4.3.2-alpha.21 → 4.3.2-alpha.23

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/index.js CHANGED
@@ -2,7 +2,7 @@ import { Kind as Kind$1, Size as Size$1, ColorPalette as ColorPalette$1, Emphasi
2
2
  export * from '@lumx/core/js/constants';
3
3
  export * from '@lumx/core/js/types';
4
4
  import * as React from 'react';
5
- import React__default, { useState, useEffect, useMemo, useRef, useCallback, useContext, createContext, Children, useLayoutEffect, cloneElement, isValidElement, useReducer } from 'react';
5
+ import React__default, { useState, useEffect, useMemo, useRef, useCallback, Children, useLayoutEffect, cloneElement, createContext, isValidElement, useContext, useReducer } from 'react';
6
6
  import { mdiAlert } from '@lumx/icons/esm/alert.js';
7
7
  import { mdiAlertCircle } from '@lumx/icons/esm/alert-circle.js';
8
8
  import { mdiCheckCircle } from '@lumx/icons/esm/check-circle.js';
@@ -16,7 +16,7 @@ import concat from 'lodash/concat.js';
16
16
  import dropRight from 'lodash/dropRight.js';
17
17
  import partition from 'lodash/partition.js';
18
18
  import reduce from 'lodash/reduce.js';
19
- import { u as useDisabledStateContext, M as MovingFocusContext, a as useVirtualFocus, A as A11YLiveMessage, P as Portal, C as ClickAwayProvider } from './_internal/CzTdCnO5.js';
19
+ import { u as useDisabledStateContext, a as useComboboxSectionId, b as useCombobox, c as useRegisterOption, g as generateOptionId, M as MovingFocusContext, d as useVirtualFocus, C as ComboboxOptionContextProvider, e as useComboboxOptionContext, f as useComboboxRefs, A as A11YLiveMessage, P as Portal, h as ClickAwayProvider } from './_internal/BSETDrpT.js';
20
20
  import isEmpty from 'lodash/isEmpty.js';
21
21
  import { getDisabledState } from '@lumx/core/js/utils/disabledState';
22
22
  import { mdiCloseCircle } from '@lumx/icons/esm/close-circle.js';
@@ -25,7 +25,6 @@ import { mdiClose } from '@lumx/icons/esm/close.js';
25
25
  import isFunction from 'lodash/isFunction.js';
26
26
  import { mdiImageBroken } from '@lumx/icons/esm/image-broken.js';
27
27
  import noop from 'lodash/noop.js';
28
- import uniqueId from 'lodash/uniqueId.js';
29
28
  import castArray from 'lodash/castArray.js';
30
29
  import { mdiCheck } from '@lumx/icons/esm/check.js';
31
30
  import { mdiMinus } from '@lumx/icons/esm/minus.js';
@@ -2220,275 +2219,6 @@ const SelectionChipGroup = ({
2220
2219
  });
2221
2220
  };
2222
2221
 
2223
- const ComboboxOptionContext = /*#__PURE__*/createContext({});
2224
- /** Context Provider to store the current combobox option id. */
2225
- const ComboboxOptionContextProvider = ({
2226
- optionId,
2227
- isKeyboardHighlighted,
2228
- children
2229
- }) => {
2230
- const value = React__default.useMemo(() => ({
2231
- optionId,
2232
- isKeyboardHighlighted
2233
- }), [optionId, isKeyboardHighlighted]);
2234
- return /*#__PURE__*/jsx(ComboboxOptionContext.Provider, {
2235
- value: value,
2236
- children: children
2237
- });
2238
- };
2239
-
2240
- /**
2241
- * Retrieve the current combobox option id.
2242
- * Must be used within a ComboboxOptionIdProvider
2243
- */
2244
- const useComboboxOptionContext = () => {
2245
- const comboboxOption = useContext(ComboboxOptionContext);
2246
- if (!comboboxOption?.optionId) {
2247
- throw new Error('This hook must be used within a ComboboxOptionIdProvider');
2248
- }
2249
- return comboboxOption;
2250
- };
2251
-
2252
- /** Generate the combobox option id from the combobox id and the given id */
2253
- const generateOptionId = (comboboxId, optionId) => `${comboboxId}-option-${optionId}`;
2254
-
2255
- /** Verifies that the combobox registered option is an action */
2256
- const isComboboxAction = option => Boolean(option?.isAction);
2257
-
2258
- /** Verifies that the combobox registered option is the option's value */
2259
- const isComboboxValue = option => {
2260
- return !isComboboxAction(option);
2261
- };
2262
-
2263
- const comboboxId = `combobox-${uniqueId()}`;
2264
- const initialState = {
2265
- comboboxId,
2266
- listboxId: `${comboboxId}-popover`,
2267
- status: 'idle',
2268
- isOpen: false,
2269
- inputValue: '',
2270
- showAll: true,
2271
- options: {},
2272
- type: 'listbox',
2273
- optionsLength: 0
2274
- };
2275
-
2276
- /** Dispatch for the combobox component */
2277
-
2278
- /** Context for the Combobox component */
2279
- const ComboboxContext = /*#__PURE__*/React__default.createContext({
2280
- ...initialState,
2281
- openOnFocus: false,
2282
- openOnClick: false,
2283
- selectionType: 'single',
2284
- optionsLength: 0,
2285
- onSelect: noop,
2286
- onInputChange: noop,
2287
- onOpen: noop,
2288
- dispatch: noop,
2289
- translations: {
2290
- clearLabel: '',
2291
- tryReloadLabel: '',
2292
- showSuggestionsLabel: '',
2293
- noResultsForInputLabel: input => input || '',
2294
- loadingLabel: '',
2295
- serviceUnavailableLabel: '',
2296
- nbOptionsLabel: options => `${options}`
2297
- }
2298
- });
2299
-
2300
- /** Context for a combobox section to store its unique id */
2301
- const SectionContext = /*#__PURE__*/React__default.createContext({
2302
- sectionId: ''
2303
- });
2304
-
2305
- /** Context to store the refs of the combobox elements */
2306
- const ComboboxRefsContext = /*#__PURE__*/createContext({
2307
- triggerRef: {
2308
- current: null
2309
- },
2310
- anchorRef: {
2311
- current: null
2312
- }
2313
- });
2314
-
2315
- /** Retrieves the combobox elements references from context */
2316
- const useComboboxRefs = () => {
2317
- const refs = useContext(ComboboxRefsContext);
2318
- if (!refs) {
2319
- throw new Error('The useComboboxRefs hook must be called within a ComboboxRefsProvider');
2320
- }
2321
- return refs;
2322
- };
2323
-
2324
- /** Retrieve the current combobox state and actions */
2325
- const useCombobox = () => {
2326
- const comboboxContext = React__default.useContext(ComboboxContext);
2327
- const {
2328
- dispatch: movingFocusDispatch
2329
- } = React__default.useContext(MovingFocusContext);
2330
- const {
2331
- onSelect,
2332
- onInputChange,
2333
- onOpen,
2334
- dispatch,
2335
- inputValue,
2336
- ...contextValues
2337
- } = comboboxContext;
2338
- const {
2339
- triggerRef
2340
- } = useComboboxRefs();
2341
-
2342
- /** Action triggered when the listBox is closed without selecting any option */
2343
- const handleClose = React__default.useCallback(() => {
2344
- dispatch({
2345
- type: 'CLOSE_COMBOBOX'
2346
- });
2347
- // Reset visual focus
2348
- movingFocusDispatch({
2349
- type: 'RESET_SELECTED_TAB_STOP'
2350
- });
2351
- }, [dispatch, movingFocusDispatch]);
2352
-
2353
- // Handle callbacks on options mounted
2354
- const [optionsMountedCallbacks, setOptionsMountedCallback] = React__default.useState();
2355
- React__default.useEffect(() => {
2356
- if (comboboxContext.optionsLength > 0 && optionsMountedCallbacks?.length) {
2357
- const optionsArray = Object.values(comboboxContext.options);
2358
- // Execute callbacks
2359
- for (const callback of optionsMountedCallbacks) {
2360
- callback(optionsArray);
2361
- }
2362
- setOptionsMountedCallback(undefined);
2363
- }
2364
- }, [comboboxContext.options, comboboxContext.optionsLength, optionsMountedCallbacks]);
2365
-
2366
- /** Callback for when an option is selected */
2367
- const handleSelected = React__default.useCallback((option, source) => {
2368
- if (option?.isDisabled) {
2369
- return;
2370
- }
2371
- if (isComboboxValue(option)) {
2372
- /**
2373
- * We only close the list if the selection type is single.
2374
- * If it is multiple, we want to allow the user to continue
2375
- * selecting multiple options.
2376
- */
2377
- if (comboboxContext.selectionType !== 'multiple') {
2378
- handleClose();
2379
- }
2380
- /** Call parent onSelect callback */
2381
- if (onSelect) {
2382
- onSelect(option);
2383
- }
2384
- }
2385
-
2386
- /** If the option itself has a custom action, also call it */
2387
- if (option?.onSelect) {
2388
- option.onSelect(option, source);
2389
- }
2390
-
2391
- /** Reset focus on input */
2392
- if (triggerRef?.current) {
2393
- triggerRef.current?.focus();
2394
- }
2395
- }, [comboboxContext.selectionType, handleClose, onSelect, triggerRef]);
2396
-
2397
- /** Callback for when the input must be updated */
2398
- const handleInputChange = React__default.useCallback((value, ...args) => {
2399
- // Update the local state
2400
- dispatch({
2401
- type: 'SET_INPUT_VALUE',
2402
- payload: value
2403
- });
2404
- // If a callback if given, call it with the value
2405
- if (onInputChange) {
2406
- onInputChange(value, ...args);
2407
- }
2408
- // Reset visual focus
2409
- movingFocusDispatch({
2410
- type: 'RESET_SELECTED_TAB_STOP'
2411
- });
2412
- }, [dispatch, movingFocusDispatch, onInputChange]);
2413
-
2414
- /**
2415
- * Open the popover
2416
- *
2417
- * @returns a promise with the updated context once all options are mounted
2418
- */
2419
- const handleOpen = React__default.useCallback(params => {
2420
- /** update the local state */
2421
- dispatch({
2422
- type: 'OPEN_COMBOBOX',
2423
- payload: params
2424
- });
2425
- /** If a parent callback was given, trigger it with state information */
2426
- if (onOpen) {
2427
- onOpen({
2428
- currentValue: inputValue,
2429
- manual: Boolean(params?.manual)
2430
- });
2431
- }
2432
-
2433
- // Promise resolving options on mount
2434
- return new Promise(resolve => {
2435
- // Append to the list of callback on options mounted
2436
- setOptionsMountedCallback((callbacks = []) => {
2437
- callbacks.push(resolve);
2438
- return callbacks;
2439
- });
2440
- });
2441
- }, [dispatch, inputValue, onOpen]);
2442
- return React__default.useMemo(() => ({
2443
- handleClose,
2444
- handleOpen,
2445
- handleInputChange,
2446
- handleSelected,
2447
- dispatch,
2448
- inputValue,
2449
- ...contextValues
2450
- }), [contextValues, dispatch, handleClose, handleInputChange, handleOpen, handleSelected, inputValue]);
2451
- };
2452
-
2453
- /** Retrieve the current combobox section id */
2454
- const useComboboxSectionId = () => {
2455
- return useContext(SectionContext);
2456
- };
2457
-
2458
- /**
2459
- * Register the given option to the context
2460
- */
2461
- const useRegisterOption = (registerId, option, shouldRegister) => {
2462
- const {
2463
- dispatch
2464
- } = useCombobox();
2465
-
2466
- /** Register the given options */
2467
- React__default.useEffect(() => {
2468
- if (option && shouldRegister) {
2469
- dispatch({
2470
- type: 'ADD_OPTION',
2471
- payload: {
2472
- id: registerId,
2473
- option
2474
- }
2475
- });
2476
- }
2477
-
2478
- // Unregister ids if/when the component unmounts or if option changes
2479
- return () => {
2480
- if (option) {
2481
- dispatch({
2482
- type: 'REMOVE_OPTION',
2483
- payload: {
2484
- id: registerId
2485
- }
2486
- });
2487
- }
2488
- };
2489
- }, [dispatch, option, registerId, shouldRegister]);
2490
- };
2491
-
2492
2222
  /* eslint-disable jsx-a11y/click-events-have-key-events */
2493
2223
  const COMBOBOX_OPTION_CLASSNAME = 'lumx-combobox-option';
2494
2224
  const LUMX_CLASSNAME = 'lumx-list-item';