@mui/x-data-grid-pro
Version:
The Pro plan edition of the Data Grid components (MUI X).
46 lines (43 loc) • 3.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useGridRowAriaAttributes = void 0;
var React = _interopRequireWildcard(require("react"));
var _xDataGrid = require("@mui/x-data-grid");
var _internals = require("@mui/x-data-grid/internals");
var _useGridPrivateApiContext = require("../../utils/useGridPrivateApiContext");
var _useGridRootProps = require("../../utils/useGridRootProps");
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 && {}.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 useGridRowAriaAttributes = addTreeDataAttributes => {
const apiRef = (0, _useGridPrivateApiContext.useGridPrivateApiContext)();
const props = (0, _useGridRootProps.useGridRootProps)();
const getRowAriaAttributesCommunity = (0, _internals.useGridRowAriaAttributes)();
const filteredTopLevelRowCount = (0, _xDataGrid.useGridSelector)(apiRef, _xDataGrid.gridFilteredTopLevelRowCountSelector);
const filteredChildrenCountLookup = (0, _xDataGrid.useGridSelector)(apiRef, _internals.gridFilteredChildrenCountLookupSelector);
const sortedVisibleRowPositionsLookup = (0, _xDataGrid.useGridSelector)(apiRef, _internals.gridExpandedSortedRowTreeLevelPositionLookupSelector);
return React.useCallback((rowNode, index) => {
const ariaAttributes = getRowAriaAttributesCommunity(rowNode, index);
if (rowNode === null || !(props.treeData || addTreeDataAttributes)) {
return ariaAttributes;
}
// pinned and footer rows are not part of the rowgroup and should not get the set specific aria attributes
if (rowNode.type === 'footer' || rowNode.type === 'pinnedRow') {
return ariaAttributes;
}
ariaAttributes['aria-level'] = rowNode.depth + 1;
const filteredChildrenCount = filteredChildrenCountLookup[rowNode.id] ?? 0;
// aria-expanded should only be added to the rows that contain children
if (rowNode.type === 'group' && filteredChildrenCount > 0) {
ariaAttributes['aria-expanded'] = Boolean(rowNode.childrenExpanded);
}
// if the parent is null, set size and position cannot be determined
if (rowNode.parent !== null) {
ariaAttributes['aria-setsize'] = rowNode.parent === _xDataGrid.GRID_ROOT_GROUP_ID ? filteredTopLevelRowCount : filteredChildrenCountLookup[rowNode.parent];
ariaAttributes['aria-posinset'] = sortedVisibleRowPositionsLookup[rowNode.id];
}
return ariaAttributes;
}, [props.treeData, addTreeDataAttributes, filteredTopLevelRowCount, filteredChildrenCountLookup, sortedVisibleRowPositionsLookup, getRowAriaAttributesCommunity]);
};
exports.useGridRowAriaAttributes = useGridRowAriaAttributes;
;