pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.ListContext = void 0;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
const ListContext = exports.ListContext = /*#__PURE__*/React.createContext(null);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
ListContext.displayName = 'ListContext';
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
"use strict";
|
||||
'use client';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
var _exportNames = {
|
||||
useList: true,
|
||||
useListItem: true
|
||||
};
|
||||
Object.defineProperty(exports, "useList", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _useList.useList;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "useListItem", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _useListItem.useListItem;
|
||||
}
|
||||
});
|
||||
var _useList = require("./useList");
|
||||
var _useList2 = require("./useList.types");
|
||||
Object.keys(_useList2).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _useList2[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _useList2[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
var _useListItem = require("./useListItem");
|
||||
var _useListItem2 = require("./useListItem.types");
|
||||
Object.keys(_useListItem2).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _useListItem2[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _useListItem2[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
var _listReducer = require("./listReducer");
|
||||
Object.keys(_listReducer).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _listReducer[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _listReducer[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
var _listActions = require("./listActions.types");
|
||||
Object.keys(_listActions).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _listActions[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _listActions[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
var _ListContext = require("./ListContext");
|
||||
Object.keys(_ListContext).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _ListContext[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _ListContext[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.ListActionTypes = void 0;
|
||||
const ListActionTypes = exports.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.
|
||||
*/
|
||||
+364
@@ -0,0 +1,364 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.handleItemSelection = handleItemSelection;
|
||||
exports.listReducer = listReducer;
|
||||
exports.moveHighlight = moveHighlight;
|
||||
exports.toggleSelection = toggleSelection;
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
var _listActions = require("./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((item, itemIndex) => isItemDisabled(item, itemIndex))) {
|
||||
return -1;
|
||||
}
|
||||
let nextFocus = currentIndex;
|
||||
for (;;) {
|
||||
// No valid items found
|
||||
if (!wrapAround && lookupDirection === 'next' && nextFocus === items.length || !wrapAround && lookupDirection === 'previous' && nextFocus === -1) {
|
||||
return -1;
|
||||
}
|
||||
const 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.
|
||||
*/
|
||||
function moveHighlight(previouslyHighlightedValue, offset, context) {
|
||||
var _items$nextIndex;
|
||||
const {
|
||||
items,
|
||||
isItemDisabled,
|
||||
disableListWrap,
|
||||
disabledItemsFocusable,
|
||||
itemComparer,
|
||||
focusManagement
|
||||
} = context;
|
||||
|
||||
// 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.
|
||||
const defaultHighlightedIndex = focusManagement === 'DOM' ? 0 : -1;
|
||||
const maxIndex = items.length - 1;
|
||||
const previouslyHighlightedIndex = previouslyHighlightedValue == null ? -1 : items.findIndex(item => itemComparer(item, previouslyHighlightedValue));
|
||||
let nextIndexCandidate;
|
||||
let lookupDirection;
|
||||
let 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:
|
||||
{
|
||||
const 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';
|
||||
}
|
||||
}
|
||||
}
|
||||
const 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.
|
||||
*/
|
||||
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(sv => itemComparer(sv, item))) {
|
||||
return selectedValues.filter(sv => !itemComparer(sv, item));
|
||||
}
|
||||
|
||||
// The toggled item is not selected - add it to the selection.
|
||||
return [...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.
|
||||
*/
|
||||
function handleItemSelection(item, state, context) {
|
||||
const {
|
||||
itemComparer,
|
||||
isItemDisabled,
|
||||
selectionMode,
|
||||
items
|
||||
} = context;
|
||||
const {
|
||||
selectedValues
|
||||
} = state;
|
||||
const itemIndex = items.findIndex(i => itemComparer(item, i));
|
||||
if (isItemDisabled(item, itemIndex)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
// if the item is already selected, remove it from the selection, otherwise add it
|
||||
const newSelectedValues = toggleSelection(item, selectedValues, selectionMode, itemComparer);
|
||||
return (0, _extends2.default)({}, state, {
|
||||
selectedValues: newSelectedValues,
|
||||
highlightedValue: item
|
||||
});
|
||||
}
|
||||
function handleKeyDown(key, state, context) {
|
||||
const previouslySelectedValue = state.highlightedValue;
|
||||
const {
|
||||
orientation,
|
||||
pageSize
|
||||
} = context;
|
||||
switch (key) {
|
||||
case 'Home':
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 'start', context)
|
||||
});
|
||||
case 'End':
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 'end', context)
|
||||
});
|
||||
case 'PageUp':
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, -pageSize, context)
|
||||
});
|
||||
case 'PageDown':
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, pageSize, context)
|
||||
});
|
||||
case 'ArrowUp':
|
||||
if (orientation !== 'vertical') {
|
||||
break;
|
||||
}
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, -1, context)
|
||||
});
|
||||
case 'ArrowDown':
|
||||
if (orientation !== 'vertical') {
|
||||
break;
|
||||
}
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, 1, context)
|
||||
});
|
||||
case 'ArrowLeft':
|
||||
{
|
||||
if (orientation === 'vertical') {
|
||||
break;
|
||||
}
|
||||
const offset = orientation === 'horizontal-ltr' ? -1 : 1;
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(previouslySelectedValue, offset, context)
|
||||
});
|
||||
}
|
||||
case 'ArrowRight':
|
||||
{
|
||||
if (orientation === 'vertical') {
|
||||
break;
|
||||
}
|
||||
const offset = orientation === 'horizontal-ltr' ? 1 : -1;
|
||||
return (0, _extends2.default)({}, 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 (0, _extends2.default)({}, state, {
|
||||
highlightedValue: null
|
||||
});
|
||||
}
|
||||
function textCriteriaMatches(nextFocus, searchString, stringifyItem) {
|
||||
var _stringifyItem;
|
||||
const 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) {
|
||||
const {
|
||||
items,
|
||||
isItemDisabled,
|
||||
disabledItemsFocusable,
|
||||
getItemAsString
|
||||
} = context;
|
||||
const startWithCurrentItem = searchString.length > 1;
|
||||
let nextItem = startWithCurrentItem ? state.highlightedValue : moveHighlight(state.highlightedValue, 1, context);
|
||||
for (let 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 (0, _extends2.default)({}, 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;
|
||||
const {
|
||||
itemComparer,
|
||||
focusManagement
|
||||
} = context;
|
||||
let newHighlightedValue = null;
|
||||
if (state.highlightedValue != null) {
|
||||
var _items$find;
|
||||
newHighlightedValue = (_items$find = items.find(item => 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
|
||||
const selectedValues = (_state$selectedValues = state.selectedValues) != null ? _state$selectedValues : [];
|
||||
const newSelectedValues = selectedValues.filter(selectedValue => items.some(item => itemComparer(item, selectedValue)));
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: newHighlightedValue,
|
||||
selectedValues: newSelectedValues
|
||||
});
|
||||
}
|
||||
function handleResetHighlight(state, context) {
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(null, 'reset', context)
|
||||
});
|
||||
}
|
||||
function handleHighlightLast(state, context) {
|
||||
return (0, _extends2.default)({}, state, {
|
||||
highlightedValue: moveHighlight(null, 'end', context)
|
||||
});
|
||||
}
|
||||
function handleClearSelection(state, context) {
|
||||
return (0, _extends2.default)({}, state, {
|
||||
selectedValues: [],
|
||||
highlightedValue: moveHighlight(null, 'reset', context)
|
||||
});
|
||||
}
|
||||
function listReducer(state, action) {
|
||||
const {
|
||||
type,
|
||||
context
|
||||
} = action;
|
||||
switch (type) {
|
||||
case _listActions.ListActionTypes.keyDown:
|
||||
return handleKeyDown(action.key, state, context);
|
||||
case _listActions.ListActionTypes.itemClick:
|
||||
return handleItemSelection(action.item, state, context);
|
||||
case _listActions.ListActionTypes.blur:
|
||||
return handleBlur(state, context);
|
||||
case _listActions.ListActionTypes.textNavigation:
|
||||
return handleTextNavigation(state, action.searchString, context);
|
||||
case _listActions.ListActionTypes.itemsChange:
|
||||
return handleItemsChange(action.items, action.previousItems, state, context);
|
||||
case _listActions.ListActionTypes.resetHighlight:
|
||||
return handleResetHighlight(state, context);
|
||||
case _listActions.ListActionTypes.highlightLast:
|
||||
return handleHighlightLast(state, context);
|
||||
case _listActions.ListActionTypes.clearSelection:
|
||||
return handleClearSelection(state, context);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
"use strict";
|
||||
'use client';
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.useList = useList;
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _utils = require("@mui/utils");
|
||||
var _listActions = require("./listActions.types");
|
||||
var _listReducer = require("./listReducer");
|
||||
var _useControllableReducer = require("../utils/useControllableReducer");
|
||||
var _areArraysEqual = require("../utils/areArraysEqual");
|
||||
var _useTextNavigation = require("../utils/useTextNavigation");
|
||||
var _extractEventHandlers = require("../utils/extractEventHandlers");
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
const EMPTY_OBJECT = {};
|
||||
const NOOP = () => {};
|
||||
const defaultItemComparer = (optionA, optionB) => optionA === optionB;
|
||||
const defaultIsItemDisabled = () => false;
|
||||
const defaultItemStringifier = item => typeof item === 'string' ? item : String(item);
|
||||
const defaultGetInitialState = () => ({
|
||||
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) {
|
||||
const {
|
||||
controlledProps = EMPTY_OBJECT,
|
||||
disabledItemsFocusable = false,
|
||||
disableListWrap = false,
|
||||
focusManagement = 'activeDescendant',
|
||||
getInitialState = defaultGetInitialState,
|
||||
getItemDomElement,
|
||||
getItemId,
|
||||
isItemDisabled = defaultIsItemDisabled,
|
||||
rootRef: externalListRef,
|
||||
onStateChange = NOOP,
|
||||
items,
|
||||
itemComparer = defaultItemComparer,
|
||||
getItemAsString = defaultItemStringifier,
|
||||
onChange,
|
||||
onHighlightChange,
|
||||
onItemsChange,
|
||||
orientation = 'vertical',
|
||||
pageSize = 5,
|
||||
reducerActionContext = EMPTY_OBJECT,
|
||||
selectionMode = 'single',
|
||||
stateReducer: externalReducer,
|
||||
componentName = 'useList'
|
||||
} = params;
|
||||
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.');
|
||||
}
|
||||
}
|
||||
const listRef = React.useRef(null);
|
||||
const handleRef = (0, _utils.unstable_useForkRef)(externalListRef, listRef);
|
||||
const handleHighlightChange = React.useCallback((event, value, reason) => {
|
||||
onHighlightChange == null || onHighlightChange(event, value, reason);
|
||||
if (focusManagement === 'DOM' && value != null && (reason === _listActions.ListActionTypes.itemClick || reason === _listActions.ListActionTypes.keyDown || reason === _listActions.ListActionTypes.textNavigation)) {
|
||||
var _getItemDomElement;
|
||||
getItemDomElement == null || (_getItemDomElement = getItemDomElement(value)) == null || _getItemDomElement.focus();
|
||||
}
|
||||
}, [getItemDomElement, onHighlightChange, focusManagement]);
|
||||
const stateComparers = React.useMemo(() => ({
|
||||
highlightedValue: itemComparer,
|
||||
selectedValues: (valuesArray1, valuesArray2) => (0, _areArraysEqual.areArraysEqual)(valuesArray1, valuesArray2, itemComparer)
|
||||
}), [itemComparer]);
|
||||
|
||||
// This gets called whenever a reducer changes the state.
|
||||
const handleStateChange = React.useCallback((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.
|
||||
const listActionContext = React.useMemo(() => {
|
||||
return {
|
||||
disabledItemsFocusable,
|
||||
disableListWrap,
|
||||
focusManagement,
|
||||
isItemDisabled,
|
||||
itemComparer,
|
||||
items,
|
||||
getItemAsString,
|
||||
onHighlightChange: handleHighlightChange,
|
||||
orientation,
|
||||
pageSize,
|
||||
selectionMode,
|
||||
stateComparers
|
||||
};
|
||||
}, [disabledItemsFocusable, disableListWrap, focusManagement, isItemDisabled, itemComparer, items, getItemAsString, handleHighlightChange, orientation, pageSize, selectionMode, stateComparers]);
|
||||
const initialState = getInitialState();
|
||||
const reducer = externalReducer != null ? externalReducer : _listReducer.listReducer;
|
||||
const actionContext = React.useMemo(() => (0, _extends2.default)({}, reducerActionContext, listActionContext), [reducerActionContext, listActionContext]);
|
||||
const [state, dispatch] = (0, _useControllableReducer.useControllableReducer)({
|
||||
reducer,
|
||||
actionContext,
|
||||
initialState: initialState,
|
||||
controlledProps,
|
||||
stateComparers,
|
||||
onStateChange: handleStateChange,
|
||||
componentName
|
||||
});
|
||||
const {
|
||||
highlightedValue,
|
||||
selectedValues
|
||||
} = state;
|
||||
const handleTextNavigation = (0, _useTextNavigation.useTextNavigation)((searchString, event) => dispatch({
|
||||
type: _listActions.ListActionTypes.textNavigation,
|
||||
event,
|
||||
searchString
|
||||
}));
|
||||
const previousItems = React.useRef([]);
|
||||
React.useEffect(() => {
|
||||
// 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 ((0, _areArraysEqual.areArraysEqual)(previousItems.current, items, itemComparer)) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: _listActions.ListActionTypes.itemsChange,
|
||||
event: null,
|
||||
items,
|
||||
previousItems: previousItems.current
|
||||
});
|
||||
previousItems.current = items;
|
||||
onItemsChange == null || onItemsChange(items);
|
||||
}, [items, itemComparer, dispatch, onItemsChange]);
|
||||
const createHandleKeyDown = externalHandlers => event => {
|
||||
var _externalHandlers$onK;
|
||||
(_externalHandlers$onK = externalHandlers.onKeyDown) == null || _externalHandlers$onK.call(externalHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
const 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: _listActions.ListActionTypes.keyDown,
|
||||
key: event.key,
|
||||
event
|
||||
});
|
||||
handleTextNavigation(event);
|
||||
};
|
||||
const createHandleBlur = externalHandlers => 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: _listActions.ListActionTypes.blur,
|
||||
event
|
||||
});
|
||||
};
|
||||
const getRootProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = (0, _extractEventHandlers.extractEventHandlers)(externalProps);
|
||||
return (0, _extends2.default)({}, externalProps, {
|
||||
'aria-activedescendant': focusManagement === 'activeDescendant' && highlightedValue != null ? getItemId(highlightedValue) : undefined,
|
||||
tabIndex: focusManagement === 'DOM' ? -1 : 0,
|
||||
ref: handleRef
|
||||
}, externalEventHandlers, {
|
||||
onBlur: createHandleBlur(externalEventHandlers),
|
||||
onKeyDown: createHandleKeyDown(externalEventHandlers)
|
||||
});
|
||||
};
|
||||
const getItemState = React.useCallback(item => {
|
||||
const selected = (selectedValues != null ? selectedValues : []).some(value => value != null && itemComparer(item, value));
|
||||
const highlighted = highlightedValue != null && itemComparer(item, highlightedValue);
|
||||
const focusable = focusManagement === 'DOM';
|
||||
return {
|
||||
focusable,
|
||||
highlighted,
|
||||
selected
|
||||
};
|
||||
}, [itemComparer, selectedValues, highlightedValue, focusManagement]);
|
||||
const contextValue = React.useMemo(() => ({
|
||||
dispatch,
|
||||
getItemState
|
||||
}), [dispatch, getItemState]);
|
||||
React.useDebugValue({
|
||||
state
|
||||
});
|
||||
return {
|
||||
contextValue,
|
||||
dispatch,
|
||||
getRootProps,
|
||||
rootRef: handleRef,
|
||||
state
|
||||
};
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
"use strict";
|
||||
'use client';
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.useListItem = useListItem;
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _extractEventHandlers = require("../utils/extractEventHandlers");
|
||||
var _listActions = require("./listActions.types");
|
||||
var _ListContext = require("./ListContext");
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
function useListItem(parameters) {
|
||||
const {
|
||||
handlePointerOverEvents = false,
|
||||
item
|
||||
} = parameters;
|
||||
const listContext = React.useContext(_ListContext.ListContext);
|
||||
if (!listContext) {
|
||||
throw new Error('useListItem must be used within a ListProvider');
|
||||
}
|
||||
const {
|
||||
dispatch,
|
||||
getItemState
|
||||
} = listContext;
|
||||
const {
|
||||
highlighted,
|
||||
selected,
|
||||
focusable
|
||||
} = getItemState(item);
|
||||
const createHandleClick = React.useCallback(externalHandlers => 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: _listActions.ListActionTypes.itemClick,
|
||||
item: item,
|
||||
event
|
||||
});
|
||||
}, [dispatch, item]);
|
||||
const createHandlePointerOver = React.useCallback(externalHandlers => 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: _listActions.ListActionTypes.itemHover,
|
||||
item: item,
|
||||
event
|
||||
});
|
||||
}, [dispatch, item]);
|
||||
let tabIndex;
|
||||
if (focusable) {
|
||||
tabIndex = highlighted ? 0 : -1;
|
||||
}
|
||||
const getRootProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = (0, _extractEventHandlers.extractEventHandlers)(externalProps);
|
||||
return (0, _extends2.default)({}, externalProps, {
|
||||
onClick: createHandleClick(externalEventHandlers),
|
||||
onPointerOver: handlePointerOverEvents ? createHandlePointerOver(externalEventHandlers) : undefined,
|
||||
tabIndex
|
||||
});
|
||||
};
|
||||
return {
|
||||
getRootProps,
|
||||
highlighted,
|
||||
selected
|
||||
};
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Reference in new issue
Block a user