pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+5
@@ -0,0 +1,5 @@
|
||||
import * as React from 'react';
|
||||
export var ListContext = /*#__PURE__*/React.createContext(null);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
ListContext.displayName = 'ListContext';
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use client';
|
||||
|
||||
export { useList } from './useList';
|
||||
export * from './useList.types';
|
||||
export { useListItem } from './useListItem';
|
||||
export * from './useListItem.types';
|
||||
export * from './listReducer';
|
||||
export * from './listActions.types';
|
||||
export * from './ListContext';
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
export var ListActionTypes = {
|
||||
blur: 'list:blur',
|
||||
focus: 'list:focus',
|
||||
itemClick: 'list:itemClick',
|
||||
itemHover: 'list:itemHover',
|
||||
itemsChange: 'list:itemsChange',
|
||||
keyDown: 'list:keyDown',
|
||||
resetHighlight: 'list:resetHighlight',
|
||||
highlightLast: 'list:highlightLast',
|
||||
textNavigation: 'list:textNavigation',
|
||||
clearSelection: 'list:clearSelection'
|
||||
};
|
||||
|
||||
/**
|
||||
* A union of all standard actions that can be dispatched to the list reducer.
|
||||
*/
|
||||
+355
@@ -0,0 +1,355 @@
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
||||
import { ListActionTypes } from './listActions.types';
|
||||
/**
|
||||
* Looks up the next valid item to highlight within the list.
|
||||
*
|
||||
* @param currentIndex The index of the start of the search.
|
||||
* @param lookupDirection Whether to look for the next or previous item.
|
||||
* @param items The array of items to search.
|
||||
* @param includeDisabledItems Whether to include disabled items in the search.
|
||||
* @param isItemDisabled A function that determines whether an item is disabled.
|
||||
* @param wrapAround Whether to wrap around the list when searching.
|
||||
* @returns The index of the next valid item to highlight or -1 if no valid item is found.
|
||||
*/
|
||||
function findValidItemToHighlight(currentIndex, lookupDirection, items, includeDisabledItems, isItemDisabled, wrapAround) {
|
||||
if (items.length === 0 || !includeDisabledItems && items.every(function (item, itemIndex) {
|
||||
return isItemDisabled(item, itemIndex);
|
||||
})) {
|
||||
return -1;
|
||||
}
|
||||
var nextFocus = currentIndex;
|
||||
for (;;) {
|
||||
// No valid items found
|
||||
if (!wrapAround && lookupDirection === 'next' && nextFocus === items.length || !wrapAround && lookupDirection === 'previous' && nextFocus === -1) {
|
||||
return -1;
|
||||
}
|
||||
var nextFocusDisabled = includeDisabledItems ? false : isItemDisabled(items[nextFocus], nextFocus);
|
||||
if (nextFocusDisabled) {
|
||||
nextFocus += lookupDirection === 'next' ? 1 : -1;
|
||||
if (wrapAround) {
|
||||
nextFocus = (nextFocus + items.length) % items.length;
|
||||
}
|
||||
} else {
|
||||
return nextFocus;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next item to highlight based on the current highlighted item and the search direction.
|
||||
*
|
||||
* @param previouslyHighlightedValue The item from which to start the search for the next candidate.
|
||||
* @param offset The offset from the previously highlighted item to search for the next candidate or a special named value ('reset', 'start', 'end').
|
||||
* @param context The list action context.
|
||||
*
|
||||
* @returns The next item to highlight or null if no item is valid.
|
||||
*/
|
||||
export function moveHighlight(previouslyHighlightedValue, offset, context) {
|
||||
var _items$nextIndex;
|
||||
var items = context.items,
|
||||
isItemDisabled = context.isItemDisabled,
|
||||
disableListWrap = context.disableListWrap,
|
||||
disabledItemsFocusable = context.disabledItemsFocusable,
|
||||
itemComparer = context.itemComparer,
|
||||
focusManagement = context.focusManagement; // TODO: make this configurable
|
||||
// The always should be an item highlighted when focus is managed by the DOM
|
||||
// so that it's accessible by the `tab` key.
|
||||
var defaultHighlightedIndex = focusManagement === 'DOM' ? 0 : -1;
|
||||
var maxIndex = items.length - 1;
|
||||
var previouslyHighlightedIndex = previouslyHighlightedValue == null ? -1 : items.findIndex(function (item) {
|
||||
return itemComparer(item, previouslyHighlightedValue);
|
||||
});
|
||||
var nextIndexCandidate;
|
||||
var lookupDirection;
|
||||
var wrapAround = !disableListWrap;
|
||||
switch (offset) {
|
||||
case 'reset':
|
||||
if (defaultHighlightedIndex === -1) {
|
||||
return null;
|
||||
}
|
||||
nextIndexCandidate = 0;
|
||||
lookupDirection = 'next';
|
||||
wrapAround = false;
|
||||
break;
|
||||
case 'start':
|
||||
nextIndexCandidate = 0;
|
||||
lookupDirection = 'next';
|
||||
wrapAround = false;
|
||||
break;
|
||||
case 'end':
|
||||
nextIndexCandidate = maxIndex;
|
||||
lookupDirection = 'previous';
|
||||
wrapAround = false;
|
||||
break;
|
||||
default:
|
||||
{
|
||||
var newIndex = previouslyHighlightedIndex + offset;
|
||||
if (newIndex < 0) {
|
||||
if (!wrapAround && previouslyHighlightedIndex !== -1 || Math.abs(offset) > 1) {
|
||||
nextIndexCandidate = 0;
|
||||
lookupDirection = 'next';
|
||||
} else {
|
||||
nextIndexCandidate = maxIndex;
|
||||
lookupDirection = 'previous';
|
||||
}
|
||||
} else if (newIndex > maxIndex) {
|
||||
if (!wrapAround || Math.abs(offset) > 1) {
|
||||
nextIndexCandidate = maxIndex;
|
||||
lookupDirection = 'previous';
|
||||
} else {
|
||||
nextIndexCandidate = 0;
|
||||
lookupDirection = 'next';
|
||||
}
|
||||
} else {
|
||||
nextIndexCandidate = newIndex;
|
||||
lookupDirection = offset >= 0 ? 'next' : 'previous';
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextIndex = findValidItemToHighlight(nextIndexCandidate, lookupDirection, items, disabledItemsFocusable, isItemDisabled, wrapAround);
|
||||
|
||||
// If there are no valid items to highlight, return the previously highlighted item (if it's still valid).
|
||||
if (nextIndex === -1 && previouslyHighlightedValue !== null && !isItemDisabled(previouslyHighlightedValue, previouslyHighlightedIndex)) {
|
||||
return previouslyHighlightedValue;
|
||||
}
|
||||
return (_items$nextIndex = items[nextIndex]) != null ? _items$nextIndex : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the selection of an item.
|
||||
*
|
||||
* @param item Item to toggle.
|
||||
* @param selectedValues Already selected items.
|
||||
* @param selectionMode The number of items that can be simultanously selected.
|
||||
* @param itemComparer A custom item comparer function.
|
||||
*
|
||||
* @returns The new array of selected items.
|
||||
*/
|
||||
export function toggleSelection(item, selectedValues, selectionMode, itemComparer) {
|
||||
if (selectionMode === 'none') {
|
||||
return [];
|
||||
}
|
||||
if (selectionMode === 'single') {
|
||||
// if the item to select has already been selected, return the original array
|
||||
if (itemComparer(selectedValues[0], item)) {
|
||||
return selectedValues;
|
||||
}
|
||||
return [item];
|
||||
}
|
||||
|
||||
// The toggled item is selected; remove it from the selection.
|
||||
if (selectedValues.some(function (sv) {
|
||||
return itemComparer(sv, item);
|
||||
})) {
|
||||
return selectedValues.filter(function (sv) {
|
||||
return !itemComparer(sv, item);
|
||||
});
|
||||
}
|
||||
|
||||
// The toggled item is not selected - add it to the selection.
|
||||
return [].concat(_toConsumableArray(selectedValues), [item]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles item selection in a list.
|
||||
*
|
||||
* @param item - The item to be selected.
|
||||
* @param state - The current state of the list.
|
||||
* @param context - The context of the list action.
|
||||
* @returns The new state of the list after the item has been selected, or the original state if the item is disabled.
|
||||
*/
|
||||
export function handleItemSelection(item, state, context) {
|
||||
var itemComparer = context.itemComparer,
|
||||
isItemDisabled = context.isItemDisabled,
|
||||
selectionMode = context.selectionMode,
|
||||
items = context.items;
|
||||
var selectedValues = state.selectedValues;
|
||||
var itemIndex = items.findIndex(function (i) {
|
||||
return itemComparer(item, i);
|
||||
});
|
||||
if (isItemDisabled(item, itemIndex)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
// if the item is already selected, remove it from the selection, otherwise add it
|
||||
var newSelectedValues = toggleSelection(item, selectedValues, selectionMode, itemComparer);
|
||||
return _extends({}, state, {
|
||||
selectedValues: newSelectedValues,
|
||||
highlightedValue: item
|
||||
});
|
||||
}
|
||||
function handleKeyDown(key, state, context) {
|
||||
var previouslySelectedValue = state.highlightedValue;
|
||||
var orientation = context.orientation,
|
||||
pageSize = context.pageSize;
|
||||
switch (key) {
|
||||
case 'Home':
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 'start', context)
|
||||
});
|
||||
case 'End':
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 'end', context)
|
||||
});
|
||||
case 'PageUp':
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, -pageSize, context)
|
||||
});
|
||||
case 'PageDown':
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, pageSize, context)
|
||||
});
|
||||
case 'ArrowUp':
|
||||
if (orientation !== 'vertical') {
|
||||
break;
|
||||
}
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, -1, context)
|
||||
});
|
||||
case 'ArrowDown':
|
||||
if (orientation !== 'vertical') {
|
||||
break;
|
||||
}
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 1, context)
|
||||
});
|
||||
case 'ArrowLeft':
|
||||
{
|
||||
if (orientation === 'vertical') {
|
||||
break;
|
||||
}
|
||||
var offset = orientation === 'horizontal-ltr' ? -1 : 1;
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, offset, context)
|
||||
});
|
||||
}
|
||||
case 'ArrowRight':
|
||||
{
|
||||
if (orientation === 'vertical') {
|
||||
break;
|
||||
}
|
||||
var _offset = orientation === 'horizontal-ltr' ? 1 : -1;
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, _offset, context)
|
||||
});
|
||||
}
|
||||
case 'Enter':
|
||||
case ' ':
|
||||
if (state.highlightedValue === null) {
|
||||
return state;
|
||||
}
|
||||
return handleItemSelection(state.highlightedValue, state, context);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
function handleBlur(state, context) {
|
||||
if (context.focusManagement === 'DOM') {
|
||||
return state;
|
||||
}
|
||||
return _extends({}, state, {
|
||||
highlightedValue: null
|
||||
});
|
||||
}
|
||||
function textCriteriaMatches(nextFocus, searchString, stringifyItem) {
|
||||
var _stringifyItem;
|
||||
var text = (_stringifyItem = stringifyItem(nextFocus)) == null ? void 0 : _stringifyItem.trim().toLowerCase();
|
||||
if (!text || text.length === 0) {
|
||||
// Make item not navigable if stringification fails or results in empty string.
|
||||
return false;
|
||||
}
|
||||
return text.indexOf(searchString) === 0;
|
||||
}
|
||||
function handleTextNavigation(state, searchString, context) {
|
||||
var items = context.items,
|
||||
isItemDisabled = context.isItemDisabled,
|
||||
disabledItemsFocusable = context.disabledItemsFocusable,
|
||||
getItemAsString = context.getItemAsString;
|
||||
var startWithCurrentItem = searchString.length > 1;
|
||||
var nextItem = startWithCurrentItem ? state.highlightedValue : moveHighlight(state.highlightedValue, 1, context);
|
||||
for (var _index = 0; _index < items.length; _index += 1) {
|
||||
// Return un-mutated state if looped back to the currently highlighted value
|
||||
if (!nextItem || !startWithCurrentItem && state.highlightedValue === nextItem) {
|
||||
return state;
|
||||
}
|
||||
if (textCriteriaMatches(nextItem, searchString, getItemAsString) && (!isItemDisabled(nextItem, items.indexOf(nextItem)) || disabledItemsFocusable)) {
|
||||
// The nextItem is the element to be highlighted
|
||||
return _extends({}, state, {
|
||||
highlightedValue: nextItem
|
||||
});
|
||||
}
|
||||
// Move to the next element.
|
||||
nextItem = moveHighlight(nextItem, 1, context);
|
||||
}
|
||||
|
||||
// No item matches the text search criteria
|
||||
return state;
|
||||
}
|
||||
function handleItemsChange(items, previousItems, state, context) {
|
||||
var _state$selectedValues;
|
||||
var itemComparer = context.itemComparer,
|
||||
focusManagement = context.focusManagement;
|
||||
var newHighlightedValue = null;
|
||||
if (state.highlightedValue != null) {
|
||||
var _items$find;
|
||||
newHighlightedValue = (_items$find = items.find(function (item) {
|
||||
return itemComparer(item, state.highlightedValue);
|
||||
})) != null ? _items$find : null;
|
||||
} else if (focusManagement === 'DOM' && previousItems.length === 0) {
|
||||
newHighlightedValue = moveHighlight(null, 'reset', context);
|
||||
}
|
||||
|
||||
// exclude selected values that are no longer in the items list
|
||||
var selectedValues = (_state$selectedValues = state.selectedValues) != null ? _state$selectedValues : [];
|
||||
var newSelectedValues = selectedValues.filter(function (selectedValue) {
|
||||
return items.some(function (item) {
|
||||
return itemComparer(item, selectedValue);
|
||||
});
|
||||
});
|
||||
return _extends({}, state, {
|
||||
highlightedValue: newHighlightedValue,
|
||||
selectedValues: newSelectedValues
|
||||
});
|
||||
}
|
||||
function handleResetHighlight(state, context) {
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(null, 'reset', context)
|
||||
});
|
||||
}
|
||||
function handleHighlightLast(state, context) {
|
||||
return _extends({}, state, {
|
||||
highlightedValue: moveHighlight(null, 'end', context)
|
||||
});
|
||||
}
|
||||
function handleClearSelection(state, context) {
|
||||
return _extends({}, state, {
|
||||
selectedValues: [],
|
||||
highlightedValue: moveHighlight(null, 'reset', context)
|
||||
});
|
||||
}
|
||||
export function listReducer(state, action) {
|
||||
var type = action.type,
|
||||
context = action.context;
|
||||
switch (type) {
|
||||
case ListActionTypes.keyDown:
|
||||
return handleKeyDown(action.key, state, context);
|
||||
case ListActionTypes.itemClick:
|
||||
return handleItemSelection(action.item, state, context);
|
||||
case ListActionTypes.blur:
|
||||
return handleBlur(state, context);
|
||||
case ListActionTypes.textNavigation:
|
||||
return handleTextNavigation(state, action.searchString, context);
|
||||
case ListActionTypes.itemsChange:
|
||||
return handleItemsChange(action.items, action.previousItems, state, context);
|
||||
case ListActionTypes.resetHighlight:
|
||||
return handleResetHighlight(state, context);
|
||||
case ListActionTypes.highlightLast:
|
||||
return handleHighlightLast(state, context);
|
||||
case ListActionTypes.clearSelection:
|
||||
return handleClearSelection(state, context);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
+277
@@ -0,0 +1,277 @@
|
||||
'use client';
|
||||
|
||||
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useForkRef as useForkRef } from '@mui/utils';
|
||||
import { ListActionTypes } from './listActions.types';
|
||||
import { listReducer as defaultReducer } from './listReducer';
|
||||
import { useControllableReducer } from '../utils/useControllableReducer';
|
||||
import { areArraysEqual } from '../utils/areArraysEqual';
|
||||
import { useTextNavigation } from '../utils/useTextNavigation';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
var EMPTY_OBJECT = {};
|
||||
var NOOP = function NOOP() {};
|
||||
var defaultItemComparer = function defaultItemComparer(optionA, optionB) {
|
||||
return optionA === optionB;
|
||||
};
|
||||
var defaultIsItemDisabled = function defaultIsItemDisabled() {
|
||||
return false;
|
||||
};
|
||||
var defaultItemStringifier = function defaultItemStringifier(item) {
|
||||
return typeof item === 'string' ? item : String(item);
|
||||
};
|
||||
var defaultGetInitialState = function defaultGetInitialState() {
|
||||
return {
|
||||
highlightedValue: null,
|
||||
selectedValues: []
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* The useList is a lower-level utility that is used to build list-like components.
|
||||
* It's used to manage the state of the list and its items.
|
||||
*
|
||||
* Supports highlighting a single item and selecting an arbitrary number of items.
|
||||
*
|
||||
* The state of the list is managed by a controllable reducer - that is a reducer that can have its state
|
||||
* controlled from outside.
|
||||
*
|
||||
* By default, the state consists of `selectedValues` and `highlightedValue` but can be extended by the caller of the hook.
|
||||
* Also the actions that can be dispatched and the reducer function can be defined externally.
|
||||
*
|
||||
* @template ItemValue The type of the item values.
|
||||
* @template State The type of the list state. This should be a subtype of `ListState<ItemValue>`.
|
||||
* @template CustomAction The type of the actions that can be dispatched (besides the standard ListAction).
|
||||
* @template CustomActionContext The shape of additional properties that will be added to actions when dispatched.
|
||||
*
|
||||
* @ignore - internal hook.
|
||||
*/
|
||||
function useList(params) {
|
||||
var _params$controlledPro = params.controlledProps,
|
||||
controlledProps = _params$controlledPro === void 0 ? EMPTY_OBJECT : _params$controlledPro,
|
||||
_params$disabledItems = params.disabledItemsFocusable,
|
||||
disabledItemsFocusable = _params$disabledItems === void 0 ? false : _params$disabledItems,
|
||||
_params$disableListWr = params.disableListWrap,
|
||||
disableListWrap = _params$disableListWr === void 0 ? false : _params$disableListWr,
|
||||
_params$focusManageme = params.focusManagement,
|
||||
focusManagement = _params$focusManageme === void 0 ? 'activeDescendant' : _params$focusManageme,
|
||||
_params$getInitialSta = params.getInitialState,
|
||||
getInitialState = _params$getInitialSta === void 0 ? defaultGetInitialState : _params$getInitialSta,
|
||||
getItemDomElement = params.getItemDomElement,
|
||||
getItemId = params.getItemId,
|
||||
_params$isItemDisable = params.isItemDisabled,
|
||||
isItemDisabled = _params$isItemDisable === void 0 ? defaultIsItemDisabled : _params$isItemDisable,
|
||||
externalListRef = params.rootRef,
|
||||
_params$onStateChange = params.onStateChange,
|
||||
onStateChange = _params$onStateChange === void 0 ? NOOP : _params$onStateChange,
|
||||
items = params.items,
|
||||
_params$itemComparer = params.itemComparer,
|
||||
itemComparer = _params$itemComparer === void 0 ? defaultItemComparer : _params$itemComparer,
|
||||
_params$getItemAsStri = params.getItemAsString,
|
||||
getItemAsString = _params$getItemAsStri === void 0 ? defaultItemStringifier : _params$getItemAsStri,
|
||||
onChange = params.onChange,
|
||||
onHighlightChange = params.onHighlightChange,
|
||||
onItemsChange = params.onItemsChange,
|
||||
_params$orientation = params.orientation,
|
||||
orientation = _params$orientation === void 0 ? 'vertical' : _params$orientation,
|
||||
_params$pageSize = params.pageSize,
|
||||
pageSize = _params$pageSize === void 0 ? 5 : _params$pageSize,
|
||||
_params$reducerAction = params.reducerActionContext,
|
||||
reducerActionContext = _params$reducerAction === void 0 ? EMPTY_OBJECT : _params$reducerAction,
|
||||
_params$selectionMode = params.selectionMode,
|
||||
selectionMode = _params$selectionMode === void 0 ? 'single' : _params$selectionMode,
|
||||
externalReducer = params.stateReducer,
|
||||
_params$componentName = params.componentName,
|
||||
componentName = _params$componentName === void 0 ? 'useList' : _params$componentName;
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (focusManagement === 'DOM' && getItemDomElement == null) {
|
||||
throw new Error('useList: The `getItemDomElement` prop is required when using the `DOM` focus management.');
|
||||
}
|
||||
if (focusManagement === 'activeDescendant' && getItemId == null) {
|
||||
throw new Error('useList: The `getItemId` prop is required when using the `activeDescendant` focus management.');
|
||||
}
|
||||
}
|
||||
var listRef = React.useRef(null);
|
||||
var handleRef = useForkRef(externalListRef, listRef);
|
||||
var handleHighlightChange = React.useCallback(function (event, value, reason) {
|
||||
onHighlightChange == null || onHighlightChange(event, value, reason);
|
||||
if (focusManagement === 'DOM' && value != null && (reason === ListActionTypes.itemClick || reason === ListActionTypes.keyDown || reason === ListActionTypes.textNavigation)) {
|
||||
var _getItemDomElement;
|
||||
getItemDomElement == null || (_getItemDomElement = getItemDomElement(value)) == null || _getItemDomElement.focus();
|
||||
}
|
||||
}, [getItemDomElement, onHighlightChange, focusManagement]);
|
||||
var stateComparers = React.useMemo(function () {
|
||||
return {
|
||||
highlightedValue: itemComparer,
|
||||
selectedValues: function selectedValues(valuesArray1, valuesArray2) {
|
||||
return areArraysEqual(valuesArray1, valuesArray2, itemComparer);
|
||||
}
|
||||
};
|
||||
}, [itemComparer]);
|
||||
|
||||
// This gets called whenever a reducer changes the state.
|
||||
var handleStateChange = React.useCallback(function (event, field, value, reason, state) {
|
||||
onStateChange == null || onStateChange(event, field, value, reason, state);
|
||||
switch (field) {
|
||||
case 'highlightedValue':
|
||||
handleHighlightChange(event, value, reason);
|
||||
break;
|
||||
case 'selectedValues':
|
||||
onChange == null || onChange(event, value, reason);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}, [handleHighlightChange, onChange, onStateChange]);
|
||||
|
||||
// The following object is added to each action when it's dispatched.
|
||||
// It's accessible in the reducer via the `action.context` field.
|
||||
var listActionContext = React.useMemo(function () {
|
||||
return {
|
||||
disabledItemsFocusable: disabledItemsFocusable,
|
||||
disableListWrap: disableListWrap,
|
||||
focusManagement: focusManagement,
|
||||
isItemDisabled: isItemDisabled,
|
||||
itemComparer: itemComparer,
|
||||
items: items,
|
||||
getItemAsString: getItemAsString,
|
||||
onHighlightChange: handleHighlightChange,
|
||||
orientation: orientation,
|
||||
pageSize: pageSize,
|
||||
selectionMode: selectionMode,
|
||||
stateComparers: stateComparers
|
||||
};
|
||||
}, [disabledItemsFocusable, disableListWrap, focusManagement, isItemDisabled, itemComparer, items, getItemAsString, handleHighlightChange, orientation, pageSize, selectionMode, stateComparers]);
|
||||
var initialState = getInitialState();
|
||||
var reducer = externalReducer != null ? externalReducer : defaultReducer;
|
||||
var actionContext = React.useMemo(function () {
|
||||
return _extends({}, reducerActionContext, listActionContext);
|
||||
}, [reducerActionContext, listActionContext]);
|
||||
var _useControllableReduc = useControllableReducer({
|
||||
reducer: reducer,
|
||||
actionContext: actionContext,
|
||||
initialState: initialState,
|
||||
controlledProps: controlledProps,
|
||||
stateComparers: stateComparers,
|
||||
onStateChange: handleStateChange,
|
||||
componentName: componentName
|
||||
}),
|
||||
_useControllableReduc2 = _slicedToArray(_useControllableReduc, 2),
|
||||
state = _useControllableReduc2[0],
|
||||
dispatch = _useControllableReduc2[1];
|
||||
var highlightedValue = state.highlightedValue,
|
||||
selectedValues = state.selectedValues;
|
||||
var handleTextNavigation = useTextNavigation(function (searchString, event) {
|
||||
return dispatch({
|
||||
type: ListActionTypes.textNavigation,
|
||||
event: event,
|
||||
searchString: searchString
|
||||
});
|
||||
});
|
||||
var previousItems = React.useRef([]);
|
||||
React.useEffect(function () {
|
||||
// Whenever the `items` object changes, we need to determine if the actual items changed.
|
||||
// If they did, we need to dispatch an `itemsChange` action, so the selected/highlighted state is updated.
|
||||
if (areArraysEqual(previousItems.current, items, itemComparer)) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: ListActionTypes.itemsChange,
|
||||
event: null,
|
||||
items: items,
|
||||
previousItems: previousItems.current
|
||||
});
|
||||
previousItems.current = items;
|
||||
onItemsChange == null || onItemsChange(items);
|
||||
}, [items, itemComparer, dispatch, onItemsChange]);
|
||||
var createHandleKeyDown = function createHandleKeyDown(externalHandlers) {
|
||||
return function (event) {
|
||||
var _externalHandlers$onK;
|
||||
(_externalHandlers$onK = externalHandlers.onKeyDown) == null || _externalHandlers$onK.call(externalHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
var keysToPreventDefault = ['Home', 'End', 'PageUp', 'PageDown'];
|
||||
if (orientation === 'vertical') {
|
||||
keysToPreventDefault.push('ArrowUp', 'ArrowDown');
|
||||
} else {
|
||||
keysToPreventDefault.push('ArrowLeft', 'ArrowRight');
|
||||
}
|
||||
if (focusManagement === 'activeDescendant') {
|
||||
// When the child element is focused using the activeDescendant attribute,
|
||||
// the list handles keyboard events on its behalf.
|
||||
// We have to `preventDefault()` is this case to prevent the browser from
|
||||
// scrolling the view when space is pressed or submitting forms when enter is pressed.
|
||||
keysToPreventDefault.push(' ', 'Enter');
|
||||
}
|
||||
if (keysToPreventDefault.includes(event.key)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
dispatch({
|
||||
type: ListActionTypes.keyDown,
|
||||
key: event.key,
|
||||
event: event
|
||||
});
|
||||
handleTextNavigation(event);
|
||||
};
|
||||
};
|
||||
var createHandleBlur = function createHandleBlur(externalHandlers) {
|
||||
return function (event) {
|
||||
var _externalHandlers$onB, _listRef$current;
|
||||
(_externalHandlers$onB = externalHandlers.onBlur) == null || _externalHandlers$onB.call(externalHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
if ((_listRef$current = listRef.current) != null && _listRef$current.contains(event.relatedTarget)) {
|
||||
// focus remains within the list
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: ListActionTypes.blur,
|
||||
event: event
|
||||
});
|
||||
};
|
||||
};
|
||||
var getRootProps = function getRootProps() {
|
||||
var externalProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
||||
var externalEventHandlers = extractEventHandlers(externalProps);
|
||||
return _extends({}, externalProps, {
|
||||
'aria-activedescendant': focusManagement === 'activeDescendant' && highlightedValue != null ? getItemId(highlightedValue) : undefined,
|
||||
tabIndex: focusManagement === 'DOM' ? -1 : 0,
|
||||
ref: handleRef
|
||||
}, externalEventHandlers, {
|
||||
onBlur: createHandleBlur(externalEventHandlers),
|
||||
onKeyDown: createHandleKeyDown(externalEventHandlers)
|
||||
});
|
||||
};
|
||||
var getItemState = React.useCallback(function (item) {
|
||||
var selected = (selectedValues != null ? selectedValues : []).some(function (value) {
|
||||
return value != null && itemComparer(item, value);
|
||||
});
|
||||
var highlighted = highlightedValue != null && itemComparer(item, highlightedValue);
|
||||
var focusable = focusManagement === 'DOM';
|
||||
return {
|
||||
focusable: focusable,
|
||||
highlighted: highlighted,
|
||||
selected: selected
|
||||
};
|
||||
}, [itemComparer, selectedValues, highlightedValue, focusManagement]);
|
||||
var contextValue = React.useMemo(function () {
|
||||
return {
|
||||
dispatch: dispatch,
|
||||
getItemState: getItemState
|
||||
};
|
||||
}, [dispatch, getItemState]);
|
||||
React.useDebugValue({
|
||||
state: state
|
||||
});
|
||||
return {
|
||||
contextValue: contextValue,
|
||||
dispatch: dispatch,
|
||||
getRootProps: getRootProps,
|
||||
rootRef: handleRef,
|
||||
state: state
|
||||
};
|
||||
}
|
||||
export { useList };
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
import { ListActionTypes } from './listActions.types';
|
||||
import { ListContext } from './ListContext';
|
||||
|
||||
/**
|
||||
* Contains the logic for an item of a list-like component (for example Select, Menu, etc.).
|
||||
* It handles the item's mouse events and tab index.
|
||||
*
|
||||
* @template ItemValue The type of the item's value. This should be consistent with the type of useList's `items` parameter.
|
||||
* @ignore - internal hook.
|
||||
*/
|
||||
export function useListItem(parameters) {
|
||||
var _parameters$handlePoi = parameters.handlePointerOverEvents,
|
||||
handlePointerOverEvents = _parameters$handlePoi === void 0 ? false : _parameters$handlePoi,
|
||||
item = parameters.item;
|
||||
var listContext = React.useContext(ListContext);
|
||||
if (!listContext) {
|
||||
throw new Error('useListItem must be used within a ListProvider');
|
||||
}
|
||||
var dispatch = listContext.dispatch,
|
||||
getItemState = listContext.getItemState;
|
||||
var _getItemState = getItemState(item),
|
||||
highlighted = _getItemState.highlighted,
|
||||
selected = _getItemState.selected,
|
||||
focusable = _getItemState.focusable;
|
||||
var createHandleClick = React.useCallback(function (externalHandlers) {
|
||||
return function (event) {
|
||||
var _externalHandlers$onC;
|
||||
(_externalHandlers$onC = externalHandlers.onClick) == null || _externalHandlers$onC.call(externalHandlers, event);
|
||||
if (event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (item === undefined) {
|
||||
throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
||||
}
|
||||
}
|
||||
dispatch({
|
||||
type: ListActionTypes.itemClick,
|
||||
item: item,
|
||||
event: event
|
||||
});
|
||||
};
|
||||
}, [dispatch, item]);
|
||||
var createHandlePointerOver = React.useCallback(function (externalHandlers) {
|
||||
return function (event) {
|
||||
var _externalHandlers$onM;
|
||||
(_externalHandlers$onM = externalHandlers.onMouseOver) == null || _externalHandlers$onM.call(externalHandlers, event);
|
||||
if (event.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (item === undefined) {
|
||||
throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
||||
}
|
||||
}
|
||||
dispatch({
|
||||
type: ListActionTypes.itemHover,
|
||||
item: item,
|
||||
event: event
|
||||
});
|
||||
};
|
||||
}, [dispatch, item]);
|
||||
var tabIndex;
|
||||
if (focusable) {
|
||||
tabIndex = highlighted ? 0 : -1;
|
||||
}
|
||||
var getRootProps = function getRootProps() {
|
||||
var externalProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
||||
var externalEventHandlers = extractEventHandlers(externalProps);
|
||||
return _extends({}, externalProps, {
|
||||
onClick: createHandleClick(externalEventHandlers),
|
||||
onPointerOver: handlePointerOverEvents ? createHandlePointerOver(externalEventHandlers) : undefined,
|
||||
tabIndex: tabIndex
|
||||
});
|
||||
};
|
||||
return {
|
||||
getRootProps: getRootProps,
|
||||
highlighted: highlighted,
|
||||
selected: selected
|
||||
};
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in new issue
Block a user