@mui/x-data-grid
Version:
The Community plan edition of the Data Grid components (MUI X).
179 lines (174 loc) • 5.89 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["render", "className"];
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/system';
import composeClasses from '@mui/utils/composeClasses';
import clsx from 'clsx';
import { forwardRef } from '@mui/x-internals/forwardRef';
import { vars } from "../../constants/cssVariables.js";
import { getDataGridUtilityClass } from "../../constants/gridClasses.js";
import { useGridComponentRenderer } from "../../hooks/utils/useGridComponentRenderer.js";
import { ToolbarContext } from "./ToolbarContext.js";
import { useGridRootProps } from "../../hooks/utils/useGridRootProps.js";
import { jsx as _jsx } from "react/jsx-runtime";
const useUtilityClasses = ownerState => {
const {
classes
} = ownerState;
const slots = {
root: ['toolbar']
};
return composeClasses(slots, getDataGridUtilityClass, classes);
};
const ToolbarRoot = styled('div', {
name: 'MuiDataGrid',
slot: 'Toolbar'
})({
flex: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'end',
gap: vars.spacing(0.25),
padding: vars.spacing(0.75),
minHeight: 52,
boxSizing: 'border-box',
borderBottom: `1px solid ${vars.colors.border.base}`
});
/**
* The top level Toolbar component that provides context to child components.
* It renders a styled `<div />` element.
*
* Demos:
*
* - [Toolbar](https://mui.com/x/react-data-grid/components/toolbar/)
*
* API:
*
* - [Toolbar API](https://mui.com/x/api/data-grid/toolbar/)
*/
const Toolbar = forwardRef(function Toolbar(props, ref) {
const {
render,
className
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const rootProps = useGridRootProps();
const classes = useUtilityClasses(rootProps);
const [focusableItemId, setFocusableItemId] = React.useState(null);
const [items, setItems] = React.useState([]);
const registerItem = React.useCallback((id, itemRef) => {
setItems(prevItems => [...prevItems, {
id,
ref: itemRef
}]);
}, []);
const unregisterItem = React.useCallback(id => {
setItems(prevItems => prevItems.filter(i => i.id !== id));
if (focusableItemId === id) {
setFocusableItemId(null);
}
}, [focusableItemId]);
const findEnabledItem = React.useCallback((startIndex, step, wrap = true) => {
let index = startIndex;
const itemCount = items.length;
// Look for enabled items in the specified direction
for (let i = 0; i < itemCount; i += 1) {
index += step;
// Handle wrapping around the ends
if (index >= itemCount) {
if (!wrap) {
return -1;
}
index = 0;
} else if (index < 0) {
if (!wrap) {
return -1;
}
index = itemCount - 1;
}
// Return if we found an enabled item
if (!items[index].ref.current?.disabled && items[index].ref.current?.ariaDisabled !== 'true') {
return index;
}
}
// If we've checked all items and found none enabled
return -1;
}, [items]);
const onItemKeyDown = React.useCallback(event => {
if (!focusableItemId) {
return;
}
const currentIndex = items.findIndex(item => item.id === focusableItemId);
let newIndex = -1;
if (event.key === 'ArrowRight') {
event.preventDefault();
newIndex = findEnabledItem(currentIndex, 1);
} else if (event.key === 'ArrowLeft') {
event.preventDefault();
newIndex = findEnabledItem(currentIndex, -1);
} else if (event.key === 'Home') {
event.preventDefault();
newIndex = findEnabledItem(-1, 1, false);
} else if (event.key === 'End') {
event.preventDefault();
newIndex = findEnabledItem(items.length, -1, false);
}
if (newIndex >= 0 && newIndex < items.length) {
const item = items[newIndex];
setFocusableItemId(item.id);
item.ref.current?.focus();
}
}, [items, focusableItemId, findEnabledItem]);
const onItemFocus = React.useCallback(id => {
if (focusableItemId !== id) {
setFocusableItemId(id);
}
}, [focusableItemId]);
const onItemDisabled = React.useCallback(id => {
const currentIndex = items.findIndex(item => item.id === id);
const newIndex = findEnabledItem(currentIndex, 1);
if (newIndex >= 0 && newIndex < items.length) {
const item = items[newIndex];
setFocusableItemId(item.id);
item.ref.current?.focus();
}
}, [items, findEnabledItem]);
const contextValue = React.useMemo(() => ({
focusableItemId,
registerItem,
unregisterItem,
onItemKeyDown,
onItemFocus,
onItemDisabled
}), [focusableItemId, registerItem, unregisterItem, onItemKeyDown, onItemFocus, onItemDisabled]);
const element = useGridComponentRenderer(ToolbarRoot, render, _extends({
role: 'toolbar',
'aria-orientation': 'horizontal',
'aria-label': rootProps.label || undefined,
className: clsx(classes.root, className)
}, other, {
ref
}));
React.useEffect(() => {
if (items.length > 0) {
setFocusableItemId(items[0].id);
}
}, [items]);
return /*#__PURE__*/_jsx(ToolbarContext.Provider, {
value: contextValue,
children: element
});
});
process.env.NODE_ENV !== "production" ? Toolbar.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
/**
* A function to customize rendering of the component.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;
export { Toolbar };