@veeqo/ui 9.9.2 → 9.9.3
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/dist/components/DataGrid/DataGrid.cjs +6 -3
- package/dist/components/DataGrid/DataGrid.cjs.map +1 -1
- package/dist/components/DataGrid/DataGrid.d.ts +1 -1
- package/dist/components/DataGrid/DataGrid.js +6 -3
- package/dist/components/DataGrid/DataGrid.js.map +1 -1
- package/dist/components/DataGrid/hooks/index.d.ts +1 -0
- package/dist/components/DataGrid/hooks/useKeyboardNavigation.cjs +112 -0
- package/dist/components/DataGrid/hooks/useKeyboardNavigation.cjs.map +1 -0
- package/dist/components/DataGrid/hooks/useKeyboardNavigation.d.ts +14 -0
- package/dist/components/DataGrid/hooks/useKeyboardNavigation.js +110 -0
- package/dist/components/DataGrid/hooks/useKeyboardNavigation.js.map +1 -0
- package/dist/components/DataGrid/types/DataGridProps.d.ts +5 -0
- package/dist/components/DataGrid/utils/getAriaRoles.cjs +9 -3
- package/dist/components/DataGrid/utils/getAriaRoles.cjs.map +1 -1
- package/dist/components/DataGrid/utils/getAriaRoles.d.ts +1 -1
- package/dist/components/DataGrid/utils/getAriaRoles.js +9 -3
- package/dist/components/DataGrid/utils/getAriaRoles.js.map +1 -1
- package/dist/components/Popover/hooks/useHandleFocus.cjs +4 -4
- package/dist/components/Popover/hooks/useHandleFocus.cjs.map +1 -1
- package/dist/components/Popover/hooks/useHandleFocus.js +1 -1
- package/dist/components/Popover/hooks/useHandleFocus.js.map +1 -1
- package/dist/utils/tabbableSelectors.cjs +6 -0
- package/dist/utils/tabbableSelectors.cjs.map +1 -0
- package/dist/utils/tabbableSelectors.d.ts +1 -0
- package/dist/utils/tabbableSelectors.js +4 -0
- package/dist/utils/tabbableSelectors.js.map +1 -0
- package/package.json +1 -1
|
@@ -15,6 +15,7 @@ var Footer = require('./components/Footer/Footer.cjs');
|
|
|
15
15
|
var GridContainer = require('./components/GridContainer/GridContainer.cjs');
|
|
16
16
|
var useSortingState = require('./hooks/useSortingState.cjs');
|
|
17
17
|
var usePinnedColumnLayout = require('./hooks/usePinnedColumnLayout.cjs');
|
|
18
|
+
var useKeyboardNavigation = require('./hooks/useKeyboardNavigation.cjs');
|
|
18
19
|
var useSelectionState = require('./hooks/useSelectionState.cjs');
|
|
19
20
|
var constants = require('./constants.cjs');
|
|
20
21
|
var SelectionColumnDefinition = require('./columns/SelectionColumnDefinition.cjs');
|
|
@@ -41,10 +42,11 @@ var React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
|
41
42
|
*
|
|
42
43
|
* *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*
|
|
43
44
|
*/
|
|
44
|
-
const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resizeMode = 'off', containerStyle, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, 'aria-label': ariaLabel, }) => {
|
|
45
|
+
const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resizeMode = 'off', containerStyle, enableKeyboardNavigation = false, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, 'aria-label': ariaLabel, }) => {
|
|
45
46
|
const containerRef = React.useRef(null);
|
|
47
|
+
const tableRef = React.useRef(null);
|
|
46
48
|
const showFooter = React.useMemo(() => columns.some((column) => column.renderFooter), [columns]);
|
|
47
|
-
const ariaRoles = React.useMemo(() => getAriaRoles.getAriaRoles(), []);
|
|
49
|
+
const ariaRoles = React.useMemo(() => getAriaRoles.getAriaRoles(enableKeyboardNavigation), [enableKeyboardNavigation]);
|
|
48
50
|
const enableColumnResizing = React.useMemo(() => resizeMode !== 'off', [resizeMode]);
|
|
49
51
|
/**
|
|
50
52
|
* Hooks to handle state for selection and sorting.
|
|
@@ -147,7 +149,8 @@ const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resiz
|
|
|
147
149
|
* Other hooks
|
|
148
150
|
*/
|
|
149
151
|
useDragToScroll.useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });
|
|
150
|
-
|
|
152
|
+
useKeyboardNavigation.useKeyboardNavigation({ tableRef, enableKeyboardNavigation });
|
|
153
|
+
return (React__default.default.createElement(GridContainer.GridContainer, { containerRef: containerRef, tableRef: tableRef, borderMode: borderMode, containerStyles: { ...containerStyle, ...pinnedColumnLayoutStyles }, ariaRoles: ariaRoles, enableResizeableColumns: enableColumnResizing, density: density, table: table, "aria-label": ariaLabel },
|
|
151
154
|
enableColumnResizing && React__default.default.createElement(Columns.Columns, { table: table }),
|
|
152
155
|
React__default.default.createElement(Header.Header, { table: table }),
|
|
153
156
|
table.getState().columnSizingInfo.isResizingColumn ? (React__default.default.createElement(Body.MemoizedBody, { table: table, ariaRoles: ariaRoles, striped: striped })) : (React__default.default.createElement(Body.Body, { table: table, ariaRoles: ariaRoles, striped: striped })),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataGrid.cjs","sources":["../../../src/components/DataGrid/DataGrid.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react';\nimport { getCoreRowModel, useReactTable } from '@tanstack/react-table';\n\nimport { useDragToScroll } from '../../hooks';\n\nimport { DataGridProps } from './types';\nimport { ColumnMapper, getAriaRoles } from './utils';\nimport { GridContainer, Header, Body, Footer, Columns } from './components';\nimport { MemoizedBody } from './components/Body';\n\nimport { usePinnedColumnLayout, useSortingState } from './hooks';\n\nimport { useSelectionState } from './hooks/useSelectionState';\nimport { SELECTION_COLUMN_ID } from './constants';\nimport { SelectionColumnDefinition } from './columns';\n\n/**\n * [WIP]\n *\n * A DataGrid component which renders data in a two-dimensional format, with columns and rows. Unlike the `DataTable`\n * component, the `DataGrid` supports both flat and hierarchical (tree) data.\n *\n * Under the hood, it uses [TanStack Table](http://tanstack.com/table/) for table management, and aims to replicate functionality\n * available in major third party grid components such as CloudScape's Table component, AgGrid, etc.\n *\n * Supported features include:\n * - Pinning, resizing (incl. smooth resizing), reordering, and hiding of columns\n * - Selection (single and multiple) and disabling of rows.\n * - Rendering of arbitrarily nested data, expanding/collapsing, and custom full-width 'group' rows.\n * - In-built keyboard navigation with arrow keys, and drag to scroll.\n * - Configurable density.\n *\n * *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*\n */\nexport const DataGrid = ({\n density = 'base',\n striped = true,\n borderMode = 'full',\n resizeMode = 'off',\n containerStyle,\n\n columns,\n hiddenColumns,\n columnOrdering,\n pinnedColumns,\n\n data,\n getRowId,\n\n sortState,\n onSortChanged,\n\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n\n 'aria-label': ariaLabel,\n}: DataGridProps) => {\n const containerRef = useRef<HTMLTableElement>(null);\n\n const showFooter = useMemo(() => columns.some((column) => column.renderFooter), [columns]);\n const ariaRoles = useMemo(() => getAriaRoles(), []);\n const enableColumnResizing = useMemo(() => resizeMode !== 'off', [resizeMode]);\n\n /**\n * Hooks to handle state for selection and sorting.\n */\n\n const { enableRowSelection, enableMultiRowSelection, rowSelection, onRowSelectionChange } =\n useSelectionState({\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n });\n\n const {\n enableSorting,\n enableMultiRemove,\n enableSortingRemoval,\n manualSorting,\n sorting,\n onSortingChange,\n } = useSortingState({ columns, sortState, onSortChanged });\n\n /**\n * Mapping of column definitions.\n */\n const columnMapper = useRef(new ColumnMapper());\n const mappedColumnDefinitions = useMemo(() => {\n const mappedColumns = columns.map((column) => columnMapper.current.mapColumnDefinition(column));\n\n if (enableRowSelection) {\n mappedColumns.unshift(SelectionColumnDefinition);\n }\n\n return mappedColumns;\n }, [columns, enableRowSelection]);\n\n /**\n * Stable references for column states (visibility, order, pinning).\n */\n\n // Column render order - the order of the array indicates the order on screen.\n const columnOrder = useMemo(() => {\n if (!columnOrdering || columnOrdering.length === 0) {\n return undefined;\n }\n\n // If we have a custom column order, we need to make sure the selection column always appears first.\n if (enableRowSelection) {\n return ['selection', ...columnOrdering];\n }\n\n return columnOrdering;\n }, [columnOrdering, enableRowSelection]);\n\n // Column visibiilty - record of column ID's to visibility, where false/no value = hidden.\n const columnVisibility = useMemo(() => {\n // We can't have no columns visible - if the array is empty we show them all.\n if (!hiddenColumns || hiddenColumns.length === columns.length) {\n return undefined;\n }\n\n return columns.reduce(\n (acc, column) => ({\n ...acc,\n [column.id]: !hiddenColumns.includes(column.id),\n }),\n {},\n );\n }, [hiddenColumns, columns]);\n\n // Pinned columns\n const columnPinning = useMemo(() => {\n let left = pinnedColumns?.left ?? [];\n\n // If we have any left-pinned columns, we need to include the selection column as it should appear first.\n if (enableRowSelection && left.length > 0) {\n left = [SELECTION_COLUMN_ID, ...left];\n }\n\n return {\n left,\n right: pinnedColumns?.right ?? [],\n };\n }, [pinnedColumns, enableRowSelection]);\n\n /**\n * Setup the TanStack table, including mapped column definitions, row data, and any additional state such as\n * column visibility and ordering.\n */\n const table = useReactTable<any>({\n data,\n columns: mappedColumnDefinitions,\n\n // Column settings\n columnResizeMode: resizeMode === 'off' ? undefined : resizeMode,\n enableColumnResizing,\n\n // Row model\n getRowId,\n getCoreRowModel: getCoreRowModel(),\n\n // Sorting\n enableSorting,\n enableSortingRemoval,\n enableMultiRemove,\n manualSorting,\n onSortingChange,\n\n // Pinning\n enableColumnPinning: columnPinning.left.length + columnPinning.right.length > 0,\n\n // Selection\n enableRowSelection,\n enableMultiRowSelection,\n onRowSelectionChange,\n\n // Inject external table state\n state: {\n columnOrder,\n columnVisibility,\n columnPinning,\n sorting,\n rowSelection,\n },\n });\n\n /**\n * Pinned column layout\n */\n const { pinnedColumnLayoutStyles } = usePinnedColumnLayout({ table });\n\n /**\n * Other hooks\n */\n useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });\n\n return (\n <GridContainer\n containerRef={containerRef}\n borderMode={borderMode}\n containerStyles={{ ...containerStyle, ...pinnedColumnLayoutStyles }}\n ariaRoles={ariaRoles}\n enableResizeableColumns={enableColumnResizing}\n density={density}\n table={table}\n aria-label={ariaLabel}\n >\n {/* Columns */}\n {enableColumnResizing && <Columns table={table} />}\n\n {/* Header */}\n <Header table={table} />\n\n {/* Body */}\n {table.getState().columnSizingInfo.isResizingColumn ? (\n <MemoizedBody table={table} ariaRoles={ariaRoles} striped={striped} />\n ) : (\n <Body table={table} ariaRoles={ariaRoles} striped={striped} />\n )}\n\n {/* Footer */}\n {showFooter && <Footer table={table} ariaRoles={ariaRoles} />}\n </GridContainer>\n );\n};\n"],"names":["useRef","useMemo","getAriaRoles","useSelectionState","useSortingState","ColumnMapper","SelectionColumnDefinition","SELECTION_COLUMN_ID","useReactTable","getCoreRowModel","usePinnedColumnLayout","useDragToScroll","React","GridContainer","Columns","Header","MemoizedBody","Body","Footer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;AAiBG;AACU,MAAA,QAAQ,GAAG,CAAC,EACvB,OAAO,GAAG,MAAM,EAChB,OAAO,GAAG,IAAI,EACd,UAAU,GAAG,MAAM,EACnB,UAAU,GAAG,KAAK,EAClB,cAAc,EAEd,OAAO,EACP,aAAa,EACb,cAAc,EACd,aAAa,EAEb,IAAI,EACJ,QAAQ,EAER,SAAS,EACT,aAAa,EAEb,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAElB,YAAY,EAAE,SAAS,GACT,KAAI;AAClB,IAAA,MAAM,YAAY,GAAGA,YAAM,CAAmB,IAAI,CAAC;IAEnD,MAAM,UAAU,GAAGC,aAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;AAC1F,IAAA,MAAM,SAAS,GAAGA,aAAO,CAAC,MAAMC,yBAAY,EAAE,EAAE,EAAE,CAAC;AACnD,IAAA,MAAM,oBAAoB,GAAGD,aAAO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,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAI,CAC/D;AAGA,QAAA,UAAU,IAAIL,sBAAA,CAAA,aAAA,CAACM,aAAM,EAAA,EAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAI,CAAA,CAC/C;AAEpB;;;;"}
|
|
1
|
+
{"version":3,"file":"DataGrid.cjs","sources":["../../../src/components/DataGrid/DataGrid.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react';\nimport { getCoreRowModel, useReactTable } from '@tanstack/react-table';\n\nimport { useDragToScroll } from '../../hooks';\n\nimport { DataGridProps } from './types';\nimport { ColumnMapper, getAriaRoles } from './utils';\nimport { GridContainer, Header, Body, Footer, Columns } from './components';\nimport { MemoizedBody } from './components/Body';\n\nimport { usePinnedColumnLayout, useSortingState, useKeyboardNavigation } from './hooks';\n\nimport { useSelectionState } from './hooks/useSelectionState';\nimport { SELECTION_COLUMN_ID } from './constants';\nimport { SelectionColumnDefinition } from './columns';\n\n/**\n * [WIP]\n *\n * A DataGrid component which renders data in a two-dimensional format, with columns and rows. Unlike the `DataTable`\n * component, the `DataGrid` supports both flat and hierarchical (tree) data.\n *\n * Under the hood, it uses [TanStack Table](http://tanstack.com/table/) for table management, and aims to replicate functionality\n * available in major third party grid components such as CloudScape's Table component, AgGrid, etc.\n *\n * Supported features include:\n * - Pinning, resizing (incl. smooth resizing), reordering, and hiding of columns\n * - Selection (single and multiple) and disabling of rows.\n * - Rendering of arbitrarily nested data, expanding/collapsing, and custom full-width 'group' rows.\n * - In-built keyboard navigation with arrow keys, and drag to scroll.\n * - Configurable density.\n *\n * *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*\n */\nexport const DataGrid = ({\n density = 'base',\n striped = true,\n borderMode = 'full',\n resizeMode = 'off',\n containerStyle,\n enableKeyboardNavigation = false,\n\n columns,\n hiddenColumns,\n columnOrdering,\n pinnedColumns,\n\n data,\n getRowId,\n\n sortState,\n onSortChanged,\n\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n\n 'aria-label': ariaLabel,\n}: DataGridProps) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const tableRef = useRef<HTMLTableElement>(null);\n\n const showFooter = useMemo(() => columns.some((column) => column.renderFooter), [columns]);\n const ariaRoles = useMemo(\n () => getAriaRoles(enableKeyboardNavigation),\n [enableKeyboardNavigation],\n );\n const enableColumnResizing = useMemo(() => resizeMode !== 'off', [resizeMode]);\n\n /**\n * Hooks to handle state for selection and sorting.\n */\n\n const { enableRowSelection, enableMultiRowSelection, rowSelection, onRowSelectionChange } =\n useSelectionState({\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n });\n\n const {\n enableSorting,\n enableMultiRemove,\n enableSortingRemoval,\n manualSorting,\n sorting,\n onSortingChange,\n } = useSortingState({ columns, sortState, onSortChanged });\n\n /**\n * Mapping of column definitions.\n */\n const columnMapper = useRef(new ColumnMapper());\n const mappedColumnDefinitions = useMemo(() => {\n const mappedColumns = columns.map((column) => columnMapper.current.mapColumnDefinition(column));\n\n if (enableRowSelection) {\n mappedColumns.unshift(SelectionColumnDefinition);\n }\n\n return mappedColumns;\n }, [columns, enableRowSelection]);\n\n /**\n * Stable references for column states (visibility, order, pinning).\n */\n\n // Column render order - the order of the array indicates the order on screen.\n const columnOrder = useMemo(() => {\n if (!columnOrdering || columnOrdering.length === 0) {\n return undefined;\n }\n\n // If we have a custom column order, we need to make sure the selection column always appears first.\n if (enableRowSelection) {\n return ['selection', ...columnOrdering];\n }\n\n return columnOrdering;\n }, [columnOrdering, enableRowSelection]);\n\n // Column visibiilty - record of column ID's to visibility, where false/no value = hidden.\n const columnVisibility = useMemo(() => {\n // We can't have no columns visible - if the array is empty we show them all.\n if (!hiddenColumns || hiddenColumns.length === columns.length) {\n return undefined;\n }\n\n return columns.reduce(\n (acc, column) => ({\n ...acc,\n [column.id]: !hiddenColumns.includes(column.id),\n }),\n {},\n );\n }, [hiddenColumns, columns]);\n\n // Pinned columns\n const columnPinning = useMemo(() => {\n let left = pinnedColumns?.left ?? [];\n\n // If we have any left-pinned columns, we need to include the selection column as it should appear first.\n if (enableRowSelection && left.length > 0) {\n left = [SELECTION_COLUMN_ID, ...left];\n }\n\n return {\n left,\n right: pinnedColumns?.right ?? [],\n };\n }, [pinnedColumns, enableRowSelection]);\n\n /**\n * Setup the TanStack table, including mapped column definitions, row data, and any additional state such as\n * column visibility and ordering.\n */\n const table = useReactTable<any>({\n data,\n columns: mappedColumnDefinitions,\n\n // Column settings\n columnResizeMode: resizeMode === 'off' ? undefined : resizeMode,\n enableColumnResizing,\n\n // Row model\n getRowId,\n getCoreRowModel: getCoreRowModel(),\n\n // Sorting\n enableSorting,\n enableSortingRemoval,\n enableMultiRemove,\n manualSorting,\n onSortingChange,\n\n // Pinning\n enableColumnPinning: columnPinning.left.length + columnPinning.right.length > 0,\n\n // Selection\n enableRowSelection,\n enableMultiRowSelection,\n onRowSelectionChange,\n\n // Inject external table state\n state: {\n columnOrder,\n columnVisibility,\n columnPinning,\n sorting,\n rowSelection,\n },\n });\n\n /**\n * Pinned column layout\n */\n const { pinnedColumnLayoutStyles } = usePinnedColumnLayout({ table });\n\n /**\n * Other hooks\n */\n useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });\n useKeyboardNavigation({ tableRef, enableKeyboardNavigation });\n\n return (\n <GridContainer\n containerRef={containerRef}\n tableRef={tableRef}\n borderMode={borderMode}\n containerStyles={{ ...containerStyle, ...pinnedColumnLayoutStyles }}\n ariaRoles={ariaRoles}\n enableResizeableColumns={enableColumnResizing}\n density={density}\n table={table}\n aria-label={ariaLabel}\n >\n {/* Columns */}\n {enableColumnResizing && <Columns table={table} />}\n\n {/* Header */}\n <Header table={table} />\n\n {/* Body */}\n {table.getState().columnSizingInfo.isResizingColumn ? (\n <MemoizedBody table={table} ariaRoles={ariaRoles} striped={striped} />\n ) : (\n <Body table={table} ariaRoles={ariaRoles} striped={striped} />\n )}\n\n {/* Footer */}\n {showFooter && <Footer table={table} ariaRoles={ariaRoles} />}\n </GridContainer>\n );\n};\n"],"names":["useRef","useMemo","getAriaRoles","useSelectionState","useSortingState","ColumnMapper","SelectionColumnDefinition","SELECTION_COLUMN_ID","useReactTable","getCoreRowModel","usePinnedColumnLayout","useDragToScroll","useKeyboardNavigation","React","GridContainer","Columns","Header","MemoizedBody","Body","Footer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;AAiBG;AACU,MAAA,QAAQ,GAAG,CAAC,EACvB,OAAO,GAAG,MAAM,EAChB,OAAO,GAAG,IAAI,EACd,UAAU,GAAG,MAAM,EACnB,UAAU,GAAG,KAAK,EAClB,cAAc,EACd,wBAAwB,GAAG,KAAK,EAEhC,OAAO,EACP,aAAa,EACb,cAAc,EACd,aAAa,EAEb,IAAI,EACJ,QAAQ,EAER,SAAS,EACT,aAAa,EAEb,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAElB,YAAY,EAAE,SAAS,GACT,KAAI;AAClB,IAAA,MAAM,YAAY,GAAGA,YAAM,CAAiB,IAAI,CAAC;AACjD,IAAA,MAAM,QAAQ,GAAGA,YAAM,CAAmB,IAAI,CAAC;IAE/C,MAAM,UAAU,GAAGC,aAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;AAC1F,IAAA,MAAM,SAAS,GAAGA,aAAO,CACvB,MAAMC,yBAAY,CAAC,wBAAwB,CAAC,EAC5C,CAAC,wBAAwB,CAAC,CAC3B;AACD,IAAA,MAAM,oBAAoB,GAAGD,aAAO,CAAC,MAAM,UAAU,KAAK,KAAK,EAAE,CAAC,UAAU,CAAC,CAAC;AAE9E;;AAEG;IAEH,MAAM,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,YAAY,EAAE,oBAAoB,EAAE,GACvFE,mCAAiB,CA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AA,CAAA,aAAA,CAACM,aAAM,EAAA,EAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAI,CAAA,CAC/C;AAEpB;;;;"}
|
|
@@ -18,4 +18,4 @@ import { DataGridProps } from './types';
|
|
|
18
18
|
*
|
|
19
19
|
* *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*
|
|
20
20
|
*/
|
|
21
|
-
export declare const DataGrid: ({ density, striped, borderMode, resizeMode, containerStyle, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, "aria-label": ariaLabel, }: DataGridProps) => React.JSX.Element;
|
|
21
|
+
export declare const DataGrid: ({ density, striped, borderMode, resizeMode, containerStyle, enableKeyboardNavigation, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, "aria-label": ariaLabel, }: DataGridProps) => React.JSX.Element;
|
|
@@ -13,6 +13,7 @@ import { Footer } from './components/Footer/Footer.js';
|
|
|
13
13
|
import { GridContainer } from './components/GridContainer/GridContainer.js';
|
|
14
14
|
import { useSortingState } from './hooks/useSortingState.js';
|
|
15
15
|
import { usePinnedColumnLayout } from './hooks/usePinnedColumnLayout.js';
|
|
16
|
+
import { useKeyboardNavigation } from './hooks/useKeyboardNavigation.js';
|
|
16
17
|
import { useSelectionState } from './hooks/useSelectionState.js';
|
|
17
18
|
import { SELECTION_COLUMN_ID } from './constants.js';
|
|
18
19
|
import { SelectionColumnDefinition } from './columns/SelectionColumnDefinition.js';
|
|
@@ -35,10 +36,11 @@ import { SelectionColumnDefinition } from './columns/SelectionColumnDefinition.j
|
|
|
35
36
|
*
|
|
36
37
|
* *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*
|
|
37
38
|
*/
|
|
38
|
-
const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resizeMode = 'off', containerStyle, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, 'aria-label': ariaLabel, }) => {
|
|
39
|
+
const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resizeMode = 'off', containerStyle, enableKeyboardNavigation = false, columns, hiddenColumns, columnOrdering, pinnedColumns, data, getRowId, sortState, onSortChanged, selectionMode, selectedRows, disabledRows, onSelectionChanged, 'aria-label': ariaLabel, }) => {
|
|
39
40
|
const containerRef = useRef(null);
|
|
41
|
+
const tableRef = useRef(null);
|
|
40
42
|
const showFooter = useMemo(() => columns.some((column) => column.renderFooter), [columns]);
|
|
41
|
-
const ariaRoles = useMemo(() => getAriaRoles(), []);
|
|
43
|
+
const ariaRoles = useMemo(() => getAriaRoles(enableKeyboardNavigation), [enableKeyboardNavigation]);
|
|
42
44
|
const enableColumnResizing = useMemo(() => resizeMode !== 'off', [resizeMode]);
|
|
43
45
|
/**
|
|
44
46
|
* Hooks to handle state for selection and sorting.
|
|
@@ -141,7 +143,8 @@ const DataGrid = ({ density = 'base', striped = true, borderMode = 'full', resiz
|
|
|
141
143
|
* Other hooks
|
|
142
144
|
*/
|
|
143
145
|
useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });
|
|
144
|
-
|
|
146
|
+
useKeyboardNavigation({ tableRef, enableKeyboardNavigation });
|
|
147
|
+
return (React__default.createElement(GridContainer, { containerRef: containerRef, tableRef: tableRef, borderMode: borderMode, containerStyles: { ...containerStyle, ...pinnedColumnLayoutStyles }, ariaRoles: ariaRoles, enableResizeableColumns: enableColumnResizing, density: density, table: table, "aria-label": ariaLabel },
|
|
145
148
|
enableColumnResizing && React__default.createElement(Columns, { table: table }),
|
|
146
149
|
React__default.createElement(Header, { table: table }),
|
|
147
150
|
table.getState().columnSizingInfo.isResizingColumn ? (React__default.createElement(MemoizedBody, { table: table, ariaRoles: ariaRoles, striped: striped })) : (React__default.createElement(Body, { table: table, ariaRoles: ariaRoles, striped: striped })),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataGrid.js","sources":["../../../src/components/DataGrid/DataGrid.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react';\nimport { getCoreRowModel, useReactTable } from '@tanstack/react-table';\n\nimport { useDragToScroll } from '../../hooks';\n\nimport { DataGridProps } from './types';\nimport { ColumnMapper, getAriaRoles } from './utils';\nimport { GridContainer, Header, Body, Footer, Columns } from './components';\nimport { MemoizedBody } from './components/Body';\n\nimport { usePinnedColumnLayout, useSortingState } from './hooks';\n\nimport { useSelectionState } from './hooks/useSelectionState';\nimport { SELECTION_COLUMN_ID } from './constants';\nimport { SelectionColumnDefinition } from './columns';\n\n/**\n * [WIP]\n *\n * A DataGrid component which renders data in a two-dimensional format, with columns and rows. Unlike the `DataTable`\n * component, the `DataGrid` supports both flat and hierarchical (tree) data.\n *\n * Under the hood, it uses [TanStack Table](http://tanstack.com/table/) for table management, and aims to replicate functionality\n * available in major third party grid components such as CloudScape's Table component, AgGrid, etc.\n *\n * Supported features include:\n * - Pinning, resizing (incl. smooth resizing), reordering, and hiding of columns\n * - Selection (single and multiple) and disabling of rows.\n * - Rendering of arbitrarily nested data, expanding/collapsing, and custom full-width 'group' rows.\n * - In-built keyboard navigation with arrow keys, and drag to scroll.\n * - Configurable density.\n *\n * *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*\n */\nexport const DataGrid = ({\n density = 'base',\n striped = true,\n borderMode = 'full',\n resizeMode = 'off',\n containerStyle,\n\n columns,\n hiddenColumns,\n columnOrdering,\n pinnedColumns,\n\n data,\n getRowId,\n\n sortState,\n onSortChanged,\n\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n\n 'aria-label': ariaLabel,\n}: DataGridProps) => {\n const containerRef = useRef<HTMLTableElement>(null);\n\n const showFooter = useMemo(() => columns.some((column) => column.renderFooter), [columns]);\n const ariaRoles = useMemo(() => getAriaRoles(), []);\n const enableColumnResizing = useMemo(() => resizeMode !== 'off', [resizeMode]);\n\n /**\n * Hooks to handle state for selection and sorting.\n */\n\n const { enableRowSelection, enableMultiRowSelection, rowSelection, onRowSelectionChange } =\n useSelectionState({\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n });\n\n const {\n enableSorting,\n enableMultiRemove,\n enableSortingRemoval,\n manualSorting,\n sorting,\n onSortingChange,\n } = useSortingState({ columns, sortState, onSortChanged });\n\n /**\n * Mapping of column definitions.\n */\n const columnMapper = useRef(new ColumnMapper());\n const mappedColumnDefinitions = useMemo(() => {\n const mappedColumns = columns.map((column) => columnMapper.current.mapColumnDefinition(column));\n\n if (enableRowSelection) {\n mappedColumns.unshift(SelectionColumnDefinition);\n }\n\n return mappedColumns;\n }, [columns, enableRowSelection]);\n\n /**\n * Stable references for column states (visibility, order, pinning).\n */\n\n // Column render order - the order of the array indicates the order on screen.\n const columnOrder = useMemo(() => {\n if (!columnOrdering || columnOrdering.length === 0) {\n return undefined;\n }\n\n // If we have a custom column order, we need to make sure the selection column always appears first.\n if (enableRowSelection) {\n return ['selection', ...columnOrdering];\n }\n\n return columnOrdering;\n }, [columnOrdering, enableRowSelection]);\n\n // Column visibiilty - record of column ID's to visibility, where false/no value = hidden.\n const columnVisibility = useMemo(() => {\n // We can't have no columns visible - if the array is empty we show them all.\n if (!hiddenColumns || hiddenColumns.length === columns.length) {\n return undefined;\n }\n\n return columns.reduce(\n (acc, column) => ({\n ...acc,\n [column.id]: !hiddenColumns.includes(column.id),\n }),\n {},\n );\n }, [hiddenColumns, columns]);\n\n // Pinned columns\n const columnPinning = useMemo(() => {\n let left = pinnedColumns?.left ?? [];\n\n // If we have any left-pinned columns, we need to include the selection column as it should appear first.\n if (enableRowSelection && left.length > 0) {\n left = [SELECTION_COLUMN_ID, ...left];\n }\n\n return {\n left,\n right: pinnedColumns?.right ?? [],\n };\n }, [pinnedColumns, enableRowSelection]);\n\n /**\n * Setup the TanStack table, including mapped column definitions, row data, and any additional state such as\n * column visibility and ordering.\n */\n const table = useReactTable<any>({\n data,\n columns: mappedColumnDefinitions,\n\n // Column settings\n columnResizeMode: resizeMode === 'off' ? undefined : resizeMode,\n enableColumnResizing,\n\n // Row model\n getRowId,\n getCoreRowModel: getCoreRowModel(),\n\n // Sorting\n enableSorting,\n enableSortingRemoval,\n enableMultiRemove,\n manualSorting,\n onSortingChange,\n\n // Pinning\n enableColumnPinning: columnPinning.left.length + columnPinning.right.length > 0,\n\n // Selection\n enableRowSelection,\n enableMultiRowSelection,\n onRowSelectionChange,\n\n // Inject external table state\n state: {\n columnOrder,\n columnVisibility,\n columnPinning,\n sorting,\n rowSelection,\n },\n });\n\n /**\n * Pinned column layout\n */\n const { pinnedColumnLayoutStyles } = usePinnedColumnLayout({ table });\n\n /**\n * Other hooks\n */\n useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });\n\n return (\n <GridContainer\n containerRef={containerRef}\n borderMode={borderMode}\n containerStyles={{ ...containerStyle, ...pinnedColumnLayoutStyles }}\n ariaRoles={ariaRoles}\n enableResizeableColumns={enableColumnResizing}\n density={density}\n table={table}\n aria-label={ariaLabel}\n >\n {/* Columns */}\n {enableColumnResizing && <Columns table={table} />}\n\n {/* Header */}\n <Header table={table} />\n\n {/* Body */}\n {table.getState().columnSizingInfo.isResizingColumn ? (\n <MemoizedBody table={table} ariaRoles={ariaRoles} striped={striped} />\n ) : (\n <Body table={table} ariaRoles={ariaRoles} striped={striped} />\n )}\n\n {/* Footer */}\n {showFooter && <Footer table={table} ariaRoles={ariaRoles} />}\n </GridContainer>\n );\n};\n"],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;AAiBG;AACU,MAAA,QAAQ,GAAG,CAAC,EACvB,OAAO,GAAG,MAAM,EAChB,OAAO,GAAG,IAAI,EACd,UAAU,GAAG,MAAM,EACnB,UAAU,GAAG,KAAK,EAClB,cAAc,EAEd,OAAO,EACP,aAAa,EAC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AAIA,cAAC,CAAA,aAAA,CAAA,OAAO,IAAC,KAAK,EAAE,KAAK,EAAI,CAAA;AAGlD,QAAAA,cAAA,CAAA,aAAA,CAAC,MAAM,EAAA,EAAC,KAAK,EAAE,KAAK,EAAI,CAAA;QAGvB,KAAK,CAAC,QAAQ,EAAE,CAAC,gBAAgB,CAAC,gBAAgB,IACjDA,cAAC,CAAA,aAAA,CAAA,YAAY,IAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAI,KAEtEA,cAAA,CAAA,aAAA,CAAC,IAAI,EAAC,EAAA,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAI,CAC/D;AAGA,QAAA,UAAU,IAAIA,cAAA,CAAA,aAAA,CAAC,MAAM,EAAA,EAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAI,CAAA,CAC/C;AAEpB;;;;"}
|
|
1
|
+
{"version":3,"file":"DataGrid.js","sources":["../../../src/components/DataGrid/DataGrid.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react';\nimport { getCoreRowModel, useReactTable } from '@tanstack/react-table';\n\nimport { useDragToScroll } from '../../hooks';\n\nimport { DataGridProps } from './types';\nimport { ColumnMapper, getAriaRoles } from './utils';\nimport { GridContainer, Header, Body, Footer, Columns } from './components';\nimport { MemoizedBody } from './components/Body';\n\nimport { usePinnedColumnLayout, useSortingState, useKeyboardNavigation } from './hooks';\n\nimport { useSelectionState } from './hooks/useSelectionState';\nimport { SELECTION_COLUMN_ID } from './constants';\nimport { SelectionColumnDefinition } from './columns';\n\n/**\n * [WIP]\n *\n * A DataGrid component which renders data in a two-dimensional format, with columns and rows. Unlike the `DataTable`\n * component, the `DataGrid` supports both flat and hierarchical (tree) data.\n *\n * Under the hood, it uses [TanStack Table](http://tanstack.com/table/) for table management, and aims to replicate functionality\n * available in major third party grid components such as CloudScape's Table component, AgGrid, etc.\n *\n * Supported features include:\n * - Pinning, resizing (incl. smooth resizing), reordering, and hiding of columns\n * - Selection (single and multiple) and disabling of rows.\n * - Rendering of arbitrarily nested data, expanding/collapsing, and custom full-width 'group' rows.\n * - In-built keyboard navigation with arrow keys, and drag to scroll.\n * - Configurable density.\n *\n * *Important: This component uses CSS modules, and therefore requires a `<ThemeInjector />` to be present on your page.*\n */\nexport const DataGrid = ({\n density = 'base',\n striped = true,\n borderMode = 'full',\n resizeMode = 'off',\n containerStyle,\n enableKeyboardNavigation = false,\n\n columns,\n hiddenColumns,\n columnOrdering,\n pinnedColumns,\n\n data,\n getRowId,\n\n sortState,\n onSortChanged,\n\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n\n 'aria-label': ariaLabel,\n}: DataGridProps) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const tableRef = useRef<HTMLTableElement>(null);\n\n const showFooter = useMemo(() => columns.some((column) => column.renderFooter), [columns]);\n const ariaRoles = useMemo(\n () => getAriaRoles(enableKeyboardNavigation),\n [enableKeyboardNavigation],\n );\n const enableColumnResizing = useMemo(() => resizeMode !== 'off', [resizeMode]);\n\n /**\n * Hooks to handle state for selection and sorting.\n */\n\n const { enableRowSelection, enableMultiRowSelection, rowSelection, onRowSelectionChange } =\n useSelectionState({\n selectionMode,\n selectedRows,\n disabledRows,\n onSelectionChanged,\n });\n\n const {\n enableSorting,\n enableMultiRemove,\n enableSortingRemoval,\n manualSorting,\n sorting,\n onSortingChange,\n } = useSortingState({ columns, sortState, onSortChanged });\n\n /**\n * Mapping of column definitions.\n */\n const columnMapper = useRef(new ColumnMapper());\n const mappedColumnDefinitions = useMemo(() => {\n const mappedColumns = columns.map((column) => columnMapper.current.mapColumnDefinition(column));\n\n if (enableRowSelection) {\n mappedColumns.unshift(SelectionColumnDefinition);\n }\n\n return mappedColumns;\n }, [columns, enableRowSelection]);\n\n /**\n * Stable references for column states (visibility, order, pinning).\n */\n\n // Column render order - the order of the array indicates the order on screen.\n const columnOrder = useMemo(() => {\n if (!columnOrdering || columnOrdering.length === 0) {\n return undefined;\n }\n\n // If we have a custom column order, we need to make sure the selection column always appears first.\n if (enableRowSelection) {\n return ['selection', ...columnOrdering];\n }\n\n return columnOrdering;\n }, [columnOrdering, enableRowSelection]);\n\n // Column visibiilty - record of column ID's to visibility, where false/no value = hidden.\n const columnVisibility = useMemo(() => {\n // We can't have no columns visible - if the array is empty we show them all.\n if (!hiddenColumns || hiddenColumns.length === columns.length) {\n return undefined;\n }\n\n return columns.reduce(\n (acc, column) => ({\n ...acc,\n [column.id]: !hiddenColumns.includes(column.id),\n }),\n {},\n );\n }, [hiddenColumns, columns]);\n\n // Pinned columns\n const columnPinning = useMemo(() => {\n let left = pinnedColumns?.left ?? [];\n\n // If we have any left-pinned columns, we need to include the selection column as it should appear first.\n if (enableRowSelection && left.length > 0) {\n left = [SELECTION_COLUMN_ID, ...left];\n }\n\n return {\n left,\n right: pinnedColumns?.right ?? [],\n };\n }, [pinnedColumns, enableRowSelection]);\n\n /**\n * Setup the TanStack table, including mapped column definitions, row data, and any additional state such as\n * column visibility and ordering.\n */\n const table = useReactTable<any>({\n data,\n columns: mappedColumnDefinitions,\n\n // Column settings\n columnResizeMode: resizeMode === 'off' ? undefined : resizeMode,\n enableColumnResizing,\n\n // Row model\n getRowId,\n getCoreRowModel: getCoreRowModel(),\n\n // Sorting\n enableSorting,\n enableSortingRemoval,\n enableMultiRemove,\n manualSorting,\n onSortingChange,\n\n // Pinning\n enableColumnPinning: columnPinning.left.length + columnPinning.right.length > 0,\n\n // Selection\n enableRowSelection,\n enableMultiRowSelection,\n onRowSelectionChange,\n\n // Inject external table state\n state: {\n columnOrder,\n columnVisibility,\n columnPinning,\n sorting,\n rowSelection,\n },\n });\n\n /**\n * Pinned column layout\n */\n const { pinnedColumnLayoutStyles } = usePinnedColumnLayout({ table });\n\n /**\n * Other hooks\n */\n useDragToScroll({ targetRef: containerRef, ignoreClassName: 'data-grid-column-resizer' });\n useKeyboardNavigation({ tableRef, enableKeyboardNavigation });\n\n return (\n <GridContainer\n containerRef={containerRef}\n tableRef={tableRef}\n borderMode={borderMode}\n containerStyles={{ ...containerStyle, ...pinnedColumnLayoutStyles }}\n ariaRoles={ariaRoles}\n enableResizeableColumns={enableColumnResizing}\n density={density}\n table={table}\n aria-label={ariaLabel}\n >\n {/* Columns */}\n {enableColumnResizing && <Columns table={table} />}\n\n {/* Header */}\n <Header table={table} />\n\n {/* Body */}\n {table.getState().columnSizingInfo.isResizingColumn ? (\n <MemoizedBody table={table} ariaRoles={ariaRoles} striped={striped} />\n ) : (\n <Body table={table} ariaRoles={ariaRoles} striped={striped} />\n )}\n\n {/* Footer */}\n {showFooter && <Footer table={table} ariaRoles={ariaRoles} />}\n </GridContainer>\n );\n};\n"],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgBA;;;;;;;;;;;;;;;;;AAiBG;AACU,MAAA,QAAQ,GAAG,CAAC,EACvB,OAAO,GAAG,MAAM,EAChB,OAAO,GAAG,IAAI,EACd,UAAU,GAAG,MAAM,EACnB,UAAU,GAAG,KAAK,EAClB,cAAc,EACd,wBAAwB,GAAG,KAAK,EAEhC,OAAO,EACP,aAAa,EACb,cAAc,EACd,aAAa,EAEb,IAAI,EACJ,QAAQ,EAER,SAAS,EACT,aAAa,EAEb,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAElB,YAAY,EAAE,SAAS,GACT,KAAI;AAClB,IAAA,MAAM,YAAY,GAAG,MAAM,CAAiB,IAAI,CAAC;AACjD,IAAA,MAAM,QAAQ,GAAG,MAAM,CAAmB,IAAI,CAAC;IAE/C,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;AAC1F,IAAA,MAAM,SAAS,GAAG,OAAO,CACvB,MAAM,YAAY,CAAC,wBAAwB,CAA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AS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAI,KAEtEA,cAAA,CAAA,aAAA,CAAC,IAAI,EAAC,EAAA,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAI,CAC/D;AAGA,QAAA,UAAU,IAAIA,cAAA,CAAA,aAAA,CAAC,MAAM,EAAA,EAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAI,CAAA,CAC/C;AAEpB;;;;"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var tabbableSelectors = require('../../../utils/tabbableSelectors.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Finds a target element to be selected within a table cell element. If the table cell
|
|
8
|
+
* contains focusable children, the first child will be returned, otherwise we'll simply
|
|
9
|
+
* focus on the table cell.
|
|
10
|
+
*/
|
|
11
|
+
const findTargetElement = (tableCell) => {
|
|
12
|
+
const validChildTarget = tableCell.querySelector(tabbableSelectors.TABBABLE_SELECTORS);
|
|
13
|
+
return validChildTarget !== null && validChildTarget !== undefined ? validChildTarget : tableCell;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Finds the next available cell that can be focused by the user, within the next row.
|
|
17
|
+
*/
|
|
18
|
+
const findNextCell = (nextRow, nextCellIndex) => {
|
|
19
|
+
if (nextCellIndex >= nextRow.cells.length ||
|
|
20
|
+
nextRow.cells[nextCellIndex].hasAttribute('aria-hidden')) {
|
|
21
|
+
// Ignore (skip) cells that are marked as hidden. Find the next available cell (if there is one) that is visible.
|
|
22
|
+
return [...nextRow.cells].reverse().find((cell) => !cell.hasAttribute('aria-hidden'));
|
|
23
|
+
}
|
|
24
|
+
return nextRow.cells[nextCellIndex];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Handles a `keydown` event from within the table. Navigates between cells using the arrow keys.
|
|
28
|
+
* @param table The table element.
|
|
29
|
+
* @param event The keyboard event.
|
|
30
|
+
*/
|
|
31
|
+
const handleKeyDown = (table, event) => {
|
|
32
|
+
var _a;
|
|
33
|
+
const target = event.target;
|
|
34
|
+
if (!table)
|
|
35
|
+
return;
|
|
36
|
+
const currentCell = target.closest('td, th');
|
|
37
|
+
if (!currentCell)
|
|
38
|
+
return;
|
|
39
|
+
const currentRow = currentCell.parentElement;
|
|
40
|
+
if (!currentRow)
|
|
41
|
+
return;
|
|
42
|
+
const rowIndex = Array.from(table.querySelectorAll('tr')).indexOf(currentRow);
|
|
43
|
+
const cellIndex = Array.from(currentRow.children).indexOf(currentCell);
|
|
44
|
+
let nextRowIndex = rowIndex;
|
|
45
|
+
let nextCellIndex = cellIndex;
|
|
46
|
+
switch (event.key) {
|
|
47
|
+
case 'ArrowUp':
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
nextRowIndex = Math.max(0, rowIndex - 1);
|
|
50
|
+
break;
|
|
51
|
+
case 'ArrowDown':
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
nextRowIndex = Math.min(table.rows.length - 1, rowIndex + 1);
|
|
54
|
+
break;
|
|
55
|
+
case 'ArrowLeft':
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
nextCellIndex = Math.max(0, cellIndex - 1);
|
|
58
|
+
break;
|
|
59
|
+
case 'ArrowRight':
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
nextCellIndex = Math.min(currentRow.cells.length - 1, cellIndex + 1);
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const nextRow = table.rows[nextRowIndex];
|
|
67
|
+
const nextCell = findNextCell(nextRow, nextCellIndex);
|
|
68
|
+
if (nextCell) {
|
|
69
|
+
const nextTarget = findTargetElement(nextCell);
|
|
70
|
+
nextTarget.setAttribute('tabindex', '0');
|
|
71
|
+
(_a = nextTarget.scrollIntoView) === null || _a === undefined ? undefined : _a.call(nextTarget, { inline: 'nearest', block: 'nearest' });
|
|
72
|
+
nextTarget.focus();
|
|
73
|
+
// Clear the tabindex when the element loses focus
|
|
74
|
+
nextTarget.addEventListener('blur', () => {
|
|
75
|
+
if (nextTarget.attributes.getNamedItem('data-first')) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
nextTarget.removeAttribute('tabindex');
|
|
79
|
+
}, { once: true });
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Hook which sets up keyboard navigation within the `DataGrid` component using the arrow keys, can be disabled
|
|
84
|
+
* with the `enableKeyboardNavigation` prop.
|
|
85
|
+
*
|
|
86
|
+
* Supports navigating between cells and rows, with the focus directed either towards the cell element, or
|
|
87
|
+
* the first interactive child if one is present.
|
|
88
|
+
*/
|
|
89
|
+
const useKeyboardNavigation = ({ enableKeyboardNavigation, tableRef, }) => {
|
|
90
|
+
React.useEffect(() => {
|
|
91
|
+
if (!tableRef.current || !enableKeyboardNavigation) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const table = tableRef.current;
|
|
95
|
+
// Find the first column header in the table, and assign it a tabindex.
|
|
96
|
+
const firstElement = table.querySelector('th');
|
|
97
|
+
if (!firstElement)
|
|
98
|
+
return undefined;
|
|
99
|
+
const firstTarget = findTargetElement(firstElement);
|
|
100
|
+
firstTarget.setAttribute('tabindex', '0');
|
|
101
|
+
firstTarget.setAttribute('data-first', '');
|
|
102
|
+
const handler = (event) => handleKeyDown(table, event);
|
|
103
|
+
table.addEventListener('keydown', handler);
|
|
104
|
+
return () => {
|
|
105
|
+
table.removeEventListener('keydown', handler);
|
|
106
|
+
};
|
|
107
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
108
|
+
}, [tableRef, enableKeyboardNavigation]);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
exports.useKeyboardNavigation = useKeyboardNavigation;
|
|
112
|
+
//# sourceMappingURL=useKeyboardNavigation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useKeyboardNavigation.cjs","sources":["../../../../src/components/DataGrid/hooks/useKeyboardNavigation.ts"],"sourcesContent":["import { RefObject, useEffect } from 'react';\n\nimport { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors';\nimport { DataGridProps } from '../types';\n\ntype UseKeyboardNavigationProps = Pick<DataGridProps, 'enableKeyboardNavigation'> & {\n tableRef: RefObject<HTMLTableElement>;\n};\n\n/**\n * Finds a target element to be selected within a table cell element. If the table cell\n * contains focusable children, the first child will be returned, otherwise we'll simply\n * focus on the table cell.\n */\nconst findTargetElement = (tableCell: HTMLElement): HTMLElement => {\n const validChildTarget: HTMLElement | null = tableCell.querySelector(TABBABLE_SELECTORS);\n return validChildTarget ?? tableCell;\n};\n\n/**\n * Finds the next available cell that can be focused by the user, within the next row.\n */\nconst findNextCell = (nextRow: HTMLTableRowElement, nextCellIndex: number) => {\n if (\n nextCellIndex >= nextRow.cells.length ||\n nextRow.cells[nextCellIndex].hasAttribute('aria-hidden')\n ) {\n // Ignore (skip) cells that are marked as hidden. Find the next available cell (if there is one) that is visible.\n return [...nextRow.cells].reverse().find((cell) => !cell.hasAttribute('aria-hidden'));\n }\n\n return nextRow.cells[nextCellIndex];\n};\n\n/**\n * Handles a `keydown` event from within the table. Navigates between cells using the arrow keys.\n * @param table The table element.\n * @param event The keyboard event.\n */\nconst handleKeyDown = (table: HTMLTableElement, event: KeyboardEvent) => {\n const target = event.target as HTMLElement;\n\n if (!table) return;\n\n const currentCell = target.closest('td, th') as HTMLElement;\n if (!currentCell) return;\n\n const currentRow = currentCell.parentElement as HTMLTableRowElement;\n if (!currentRow) return;\n\n const rowIndex = Array.from(table.querySelectorAll('tr')).indexOf(currentRow);\n const cellIndex = Array.from(currentRow.children).indexOf(currentCell);\n\n let nextRowIndex = rowIndex;\n let nextCellIndex = cellIndex;\n\n switch (event.key) {\n case 'ArrowUp':\n event.preventDefault();\n nextRowIndex = Math.max(0, rowIndex - 1);\n break;\n case 'ArrowDown':\n event.preventDefault();\n nextRowIndex = Math.min(table.rows.length - 1, rowIndex + 1);\n break;\n case 'ArrowLeft':\n event.preventDefault();\n nextCellIndex = Math.max(0, cellIndex - 1);\n break;\n case 'ArrowRight':\n event.preventDefault();\n nextCellIndex = Math.min(currentRow.cells.length - 1, cellIndex + 1);\n break;\n default:\n return;\n }\n\n const nextRow = table.rows[nextRowIndex];\n const nextCell = findNextCell(nextRow, nextCellIndex);\n\n if (nextCell) {\n const nextTarget = findTargetElement(nextCell);\n nextTarget.setAttribute('tabindex', '0');\n nextTarget.scrollIntoView?.({ inline: 'nearest', block: 'nearest' });\n nextTarget.focus();\n\n // Clear the tabindex when the element loses focus\n nextTarget.addEventListener(\n 'blur',\n () => {\n if (nextTarget.attributes.getNamedItem('data-first')) {\n return;\n }\n\n nextTarget.removeAttribute('tabindex');\n },\n { once: true },\n );\n }\n};\n\n/**\n * Hook which sets up keyboard navigation within the `DataGrid` component using the arrow keys, can be disabled\n * with the `enableKeyboardNavigation` prop.\n *\n * Supports navigating between cells and rows, with the focus directed either towards the cell element, or\n * the first interactive child if one is present.\n */\nexport const useKeyboardNavigation = ({\n enableKeyboardNavigation,\n tableRef,\n}: UseKeyboardNavigationProps) => {\n useEffect(() => {\n if (!tableRef.current || !enableKeyboardNavigation) {\n return undefined;\n }\n\n const table = tableRef.current;\n\n // Find the first column header in the table, and assign it a tabindex.\n const firstElement = table.querySelector('th') as HTMLElement;\n if (!firstElement) return undefined;\n const firstTarget = findTargetElement(firstElement);\n\n firstTarget.setAttribute('tabindex', '0');\n firstTarget.setAttribute('data-first', '');\n\n const handler = (event: KeyboardEvent) => handleKeyDown(table, event);\n table.addEventListener('keydown', handler);\n\n return () => {\n table.removeEventListener('keydown', handler);\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [tableRef, enableKeyboardNavigation]);\n};\n"],"names":["TABBABLE_SELECTORS","useEffect"],"mappings":";;;;;AASA;;;;AAIG;AACH,MAAM,i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avigationProps = Pick<DataGridProps, 'enableKeyboardNavigation'> & {
|
|
4
|
+
tableRef: RefObject<HTMLTableElement>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Hook which sets up keyboard navigation within the `DataGrid` component using the arrow keys, can be disabled
|
|
8
|
+
* with the `enableKeyboardNavigation` prop.
|
|
9
|
+
*
|
|
10
|
+
* Supports navigating between cells and rows, with the focus directed either towards the cell element, or
|
|
11
|
+
* the first interactive child if one is present.
|
|
12
|
+
*/
|
|
13
|
+
export declare const useKeyboardNavigation: ({ enableKeyboardNavigation, tableRef, }: UseKeyboardNavigationProps) => void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Finds a target element to be selected within a table cell element. If the table cell
|
|
6
|
+
* contains focusable children, the first child will be returned, otherwise we'll simply
|
|
7
|
+
* focus on the table cell.
|
|
8
|
+
*/
|
|
9
|
+
const findTargetElement = (tableCell) => {
|
|
10
|
+
const validChildTarget = tableCell.querySelector(TABBABLE_SELECTORS);
|
|
11
|
+
return validChildTarget !== null && validChildTarget !== undefined ? validChildTarget : tableCell;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Finds the next available cell that can be focused by the user, within the next row.
|
|
15
|
+
*/
|
|
16
|
+
const findNextCell = (nextRow, nextCellIndex) => {
|
|
17
|
+
if (nextCellIndex >= nextRow.cells.length ||
|
|
18
|
+
nextRow.cells[nextCellIndex].hasAttribute('aria-hidden')) {
|
|
19
|
+
// Ignore (skip) cells that are marked as hidden. Find the next available cell (if there is one) that is visible.
|
|
20
|
+
return [...nextRow.cells].reverse().find((cell) => !cell.hasAttribute('aria-hidden'));
|
|
21
|
+
}
|
|
22
|
+
return nextRow.cells[nextCellIndex];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Handles a `keydown` event from within the table. Navigates between cells using the arrow keys.
|
|
26
|
+
* @param table The table element.
|
|
27
|
+
* @param event The keyboard event.
|
|
28
|
+
*/
|
|
29
|
+
const handleKeyDown = (table, event) => {
|
|
30
|
+
var _a;
|
|
31
|
+
const target = event.target;
|
|
32
|
+
if (!table)
|
|
33
|
+
return;
|
|
34
|
+
const currentCell = target.closest('td, th');
|
|
35
|
+
if (!currentCell)
|
|
36
|
+
return;
|
|
37
|
+
const currentRow = currentCell.parentElement;
|
|
38
|
+
if (!currentRow)
|
|
39
|
+
return;
|
|
40
|
+
const rowIndex = Array.from(table.querySelectorAll('tr')).indexOf(currentRow);
|
|
41
|
+
const cellIndex = Array.from(currentRow.children).indexOf(currentCell);
|
|
42
|
+
let nextRowIndex = rowIndex;
|
|
43
|
+
let nextCellIndex = cellIndex;
|
|
44
|
+
switch (event.key) {
|
|
45
|
+
case 'ArrowUp':
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
nextRowIndex = Math.max(0, rowIndex - 1);
|
|
48
|
+
break;
|
|
49
|
+
case 'ArrowDown':
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
nextRowIndex = Math.min(table.rows.length - 1, rowIndex + 1);
|
|
52
|
+
break;
|
|
53
|
+
case 'ArrowLeft':
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
nextCellIndex = Math.max(0, cellIndex - 1);
|
|
56
|
+
break;
|
|
57
|
+
case 'ArrowRight':
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
nextCellIndex = Math.min(currentRow.cells.length - 1, cellIndex + 1);
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const nextRow = table.rows[nextRowIndex];
|
|
65
|
+
const nextCell = findNextCell(nextRow, nextCellIndex);
|
|
66
|
+
if (nextCell) {
|
|
67
|
+
const nextTarget = findTargetElement(nextCell);
|
|
68
|
+
nextTarget.setAttribute('tabindex', '0');
|
|
69
|
+
(_a = nextTarget.scrollIntoView) === null || _a === undefined ? undefined : _a.call(nextTarget, { inline: 'nearest', block: 'nearest' });
|
|
70
|
+
nextTarget.focus();
|
|
71
|
+
// Clear the tabindex when the element loses focus
|
|
72
|
+
nextTarget.addEventListener('blur', () => {
|
|
73
|
+
if (nextTarget.attributes.getNamedItem('data-first')) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
nextTarget.removeAttribute('tabindex');
|
|
77
|
+
}, { once: true });
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Hook which sets up keyboard navigation within the `DataGrid` component using the arrow keys, can be disabled
|
|
82
|
+
* with the `enableKeyboardNavigation` prop.
|
|
83
|
+
*
|
|
84
|
+
* Supports navigating between cells and rows, with the focus directed either towards the cell element, or
|
|
85
|
+
* the first interactive child if one is present.
|
|
86
|
+
*/
|
|
87
|
+
const useKeyboardNavigation = ({ enableKeyboardNavigation, tableRef, }) => {
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (!tableRef.current || !enableKeyboardNavigation) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const table = tableRef.current;
|
|
93
|
+
// Find the first column header in the table, and assign it a tabindex.
|
|
94
|
+
const firstElement = table.querySelector('th');
|
|
95
|
+
if (!firstElement)
|
|
96
|
+
return undefined;
|
|
97
|
+
const firstTarget = findTargetElement(firstElement);
|
|
98
|
+
firstTarget.setAttribute('tabindex', '0');
|
|
99
|
+
firstTarget.setAttribute('data-first', '');
|
|
100
|
+
const handler = (event) => handleKeyDown(table, event);
|
|
101
|
+
table.addEventListener('keydown', handler);
|
|
102
|
+
return () => {
|
|
103
|
+
table.removeEventListener('keydown', handler);
|
|
104
|
+
};
|
|
105
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
106
|
+
}, [tableRef, enableKeyboardNavigation]);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export { useKeyboardNavigation };
|
|
110
|
+
//# sourceMappingURL=useKeyboardNavigation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useKeyboardNavigation.js","sources":["../../../../src/components/DataGrid/hooks/useKeyboardNavigation.ts"],"sourcesContent":["import { RefObject, useEffect } from 'react';\n\nimport { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors';\nimport { DataGridProps } from '../types';\n\ntype UseKeyboardNavigationProps = Pick<DataGridProps, 'enableKeyboardNavigation'> & {\n tableRef: RefObject<HTMLTableElement>;\n};\n\n/**\n * Finds a target element to be selected within a table cell element. If the table cell\n * contains focusable children, the first child will be returned, otherwise we'll simply\n * focus on the table cell.\n */\nconst findTargetElement = (tableCell: HTMLElement): HTMLElement => {\n const validChildTarget: HTMLElement | null = tableCell.querySelector(TABBABLE_SELECTORS);\n return validChildTarget ?? tableCell;\n};\n\n/**\n * Finds the next available cell that can be focused by the user, within the next row.\n */\nconst findNextCell = (nextRow: HTMLTableRowElement, nextCellIndex: number) => {\n if (\n nextCellIndex >= nextRow.cells.length ||\n nextRow.cells[nextCellIndex].hasAttribute('aria-hidden')\n ) {\n // Ignore (skip) cells that are marked as hidden. Find the next available cell (if there is one) that is visible.\n return [...nextRow.cells].reverse().find((cell) => !cell.hasAttribute('aria-hidden'));\n }\n\n return nextRow.cells[nextCellIndex];\n};\n\n/**\n * Handles a `keydown` event from within the table. Navigates between cells using the arrow keys.\n * @param table The table element.\n * @param event The keyboard event.\n */\nconst handleKeyDown = (table: HTMLTableElement, event: KeyboardEvent) => {\n const target = event.target as HTMLElement;\n\n if (!table) return;\n\n const currentCell = target.closest('td, th') as HTMLElement;\n if (!currentCell) return;\n\n const currentRow = currentCell.parentElement as HTMLTableRowElement;\n if (!currentRow) return;\n\n const rowIndex = Array.from(table.querySelectorAll('tr')).indexOf(currentRow);\n const cellIndex = Array.from(currentRow.children).indexOf(currentCell);\n\n let nextRowIndex = rowIndex;\n let nextCellIndex = cellIndex;\n\n switch (event.key) {\n case 'ArrowUp':\n event.preventDefault();\n nextRowIndex = Math.max(0, rowIndex - 1);\n break;\n case 'ArrowDown':\n event.preventDefault();\n nextRowIndex = Math.min(table.rows.length - 1, rowIndex + 1);\n break;\n case 'ArrowLeft':\n event.preventDefault();\n nextCellIndex = Math.max(0, cellIndex - 1);\n break;\n case 'ArrowRight':\n event.preventDefault();\n nextCellIndex = Math.min(currentRow.cells.length - 1, cellIndex + 1);\n break;\n default:\n return;\n }\n\n const nextRow = table.rows[nextRowIndex];\n const nextCell = findNextCell(nextRow, nextCellIndex);\n\n if (nextCell) {\n const nextTarget = findTargetElement(nextCell);\n nextTarget.setAttribute('tabindex', '0');\n nextTarget.scrollIntoView?.({ inline: 'nearest', block: 'nearest' });\n nextTarget.focus();\n\n // Clear the tabindex when the element loses focus\n nextTarget.addEventListener(\n 'blur',\n () => {\n if (nextTarget.attributes.getNamedItem('data-first')) {\n return;\n }\n\n nextTarget.removeAttribute('tabindex');\n },\n { once: true },\n );\n }\n};\n\n/**\n * Hook which sets up keyboard navigation within the `DataGrid` component using the arrow keys, can be disabled\n * with the `enableKeyboardNavigation` prop.\n *\n * Supports navigating between cells and rows, with the focus directed either towards the cell element, or\n * the first interactive child if one is present.\n */\nexport const useKeyboardNavigation = ({\n enableKeyboardNavigation,\n tableRef,\n}: UseKeyboardNavigationProps) => {\n useEffect(() => {\n if (!tableRef.current || !enableKeyboardNavigation) {\n return undefined;\n }\n\n const table = tableRef.current;\n\n // Find the first column header in the table, and assign it a tabindex.\n const firstElement = table.querySelector('th') as HTMLElement;\n if (!firstElement) return undefined;\n const firstTarget = findTargetElement(firstElement);\n\n firstTarget.setAttribute('tabindex', '0');\n firstTarget.setAttribute('data-first', '');\n\n const handler = (event: KeyboardEvent) => handleKeyDown(table, event);\n table.addEventListener('keydown', handler);\n\n return () => {\n table.removeEventListener('keydown', handler);\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [tableRef, enableKeyboardNavigation]);\n};\n"],"names":[],"mappings":";;;AASA;;;;AAIG;AACH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|
45
45
|
*/
|
|
46
46
|
containerStyle?: React.CSSProperties;
|
|
47
|
+
/**
|
|
48
|
+
* Enable/disable keyboard navigation within the grid. Using the arrow keys will move users between cells and rows, selecting either the cell (if no interactive children are found),
|
|
49
|
+
* or the first interactive child within the cell (if one is present).
|
|
50
|
+
*/
|
|
51
|
+
enableKeyboardNavigation?: boolean;
|
|
47
52
|
/** Columns */
|
|
48
53
|
/**
|
|
49
54
|
* Column definitions. It is not recommended that you modify these once the table has been initialized, since internal state
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const getAriaRoles = () => {
|
|
3
|
+
const getAriaRoles = (enableKeyboardNavigation) => {
|
|
4
|
+
if (!enableKeyboardNavigation) {
|
|
5
|
+
return {
|
|
6
|
+
table: 'table',
|
|
7
|
+
cell: 'cell',
|
|
8
|
+
};
|
|
9
|
+
}
|
|
4
10
|
return {
|
|
5
|
-
table: '
|
|
6
|
-
cell: '
|
|
11
|
+
table: 'grid',
|
|
12
|
+
cell: 'gridcell',
|
|
7
13
|
};
|
|
8
14
|
};
|
|
9
15
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getAriaRoles.cjs","sources":["../../../../src/components/DataGrid/utils/getAriaRoles.ts"],"sourcesContent":["import { AriaRoles } from '../types/AriaRoles';\n\nexport const getAriaRoles = (): AriaRoles => {\n return {\n
|
|
1
|
+
{"version":3,"file":"getAriaRoles.cjs","sources":["../../../../src/components/DataGrid/utils/getAriaRoles.ts"],"sourcesContent":["import { AriaRoles } from '../types/AriaRoles';\n\nexport const getAriaRoles = (enableKeyboardNavigation: boolean): AriaRoles => {\n if (!enableKeyboardNavigation) {\n return {\n table: 'table',\n cell: 'cell',\n };\n }\n\n return {\n table: 'grid',\n cell: 'gridcell',\n };\n};\n"],"names":[],"mappings":";;AAEa,MAAA,YAAY,GAAG,CAAC,wBAAiC,KAAe;IAC3E,IAAI,CAAC,wBAAwB,EAAE;QAC7B,OAAO;AACL,YAAA,KAAK,EAAE,OAAO;AACd,YAAA,IAAI,EAAE,MAAM;SACb;AACF;IAED,OAAO;AACL,QAAA,KAAK,EAAE,MAAM;AACb,QAAA,IAAI,EAAE,UAAU;KACjB;AACH;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AriaRoles } from '../types/AriaRoles';
|
|
2
|
-
export declare const getAriaRoles: () => AriaRoles;
|
|
2
|
+
export declare const getAriaRoles: (enableKeyboardNavigation: boolean) => AriaRoles;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
const getAriaRoles = () => {
|
|
1
|
+
const getAriaRoles = (enableKeyboardNavigation) => {
|
|
2
|
+
if (!enableKeyboardNavigation) {
|
|
3
|
+
return {
|
|
4
|
+
table: 'table',
|
|
5
|
+
cell: 'cell',
|
|
6
|
+
};
|
|
7
|
+
}
|
|
2
8
|
return {
|
|
3
|
-
table: '
|
|
4
|
-
cell: '
|
|
9
|
+
table: 'grid',
|
|
10
|
+
cell: 'gridcell',
|
|
5
11
|
};
|
|
6
12
|
};
|
|
7
13
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getAriaRoles.js","sources":["../../../../src/components/DataGrid/utils/getAriaRoles.ts"],"sourcesContent":["import { AriaRoles } from '../types/AriaRoles';\n\nexport const getAriaRoles = (): AriaRoles => {\n return {\n
|
|
1
|
+
{"version":3,"file":"getAriaRoles.js","sources":["../../../../src/components/DataGrid/utils/getAriaRoles.ts"],"sourcesContent":["import { AriaRoles } from '../types/AriaRoles';\n\nexport const getAriaRoles = (enableKeyboardNavigation: boolean): AriaRoles => {\n if (!enableKeyboardNavigation) {\n return {\n table: 'table',\n cell: 'cell',\n };\n }\n\n return {\n table: 'grid',\n cell: 'gridcell',\n };\n};\n"],"names":[],"mappings":"AAEa,MAAA,YAAY,GAAG,CAAC,wBAAiC,KAAe;IAC3E,IAAI,CAAC,wBAAwB,EAAE;QAC7B,OAAO;AACL,YAAA,KAAK,EAAE,OAAO;AACd,YAAA,IAAI,EAAE,MAAM;SACb;AACF;IAED,OAAO;AACL,QAAA,KAAK,EAAE,MAAM;AACb,QAAA,IAAI,EAAE,UAAU;KACjB;AACH;;;;"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var React = require('react');
|
|
4
|
+
var tabbableSelectors = require('../../../utils/tabbableSelectors.cjs');
|
|
4
5
|
|
|
5
|
-
const TABBABLE_SELECTORS = 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled])';
|
|
6
6
|
const useHandleFocus = ({ popperElement, anchorElement, onClose, disableFocusLock = false, }) => {
|
|
7
7
|
/**
|
|
8
8
|
* Focuses first element, when this gets rendered for the first time.
|
|
@@ -10,7 +10,7 @@ const useHandleFocus = ({ popperElement, anchorElement, onClose, disableFocusLoc
|
|
|
10
10
|
React.useEffect(() => {
|
|
11
11
|
if (!popperElement || disableFocusLock)
|
|
12
12
|
return;
|
|
13
|
-
const focusableElements = popperElement.querySelectorAll(TABBABLE_SELECTORS);
|
|
13
|
+
const focusableElements = popperElement.querySelectorAll(tabbableSelectors.TABBABLE_SELECTORS);
|
|
14
14
|
if (focusableElements.length < 1)
|
|
15
15
|
return;
|
|
16
16
|
setTimeout(() => {
|
|
@@ -28,7 +28,7 @@ const useHandleFocus = ({ popperElement, anchorElement, onClose, disableFocusLoc
|
|
|
28
28
|
return;
|
|
29
29
|
const focusWithinPopper = (event) => {
|
|
30
30
|
if (event.key === 'Tab' && !event.shiftKey) {
|
|
31
|
-
const focusableElements = popperElement.querySelectorAll(TABBABLE_SELECTORS);
|
|
31
|
+
const focusableElements = popperElement.querySelectorAll(tabbableSelectors.TABBABLE_SELECTORS);
|
|
32
32
|
// When nothing is focusable within the popover, close popover and allow natural tab order.
|
|
33
33
|
if (focusableElements.length < 1) {
|
|
34
34
|
onClose();
|
|
@@ -49,7 +49,7 @@ const useHandleFocus = ({ popperElement, anchorElement, onClose, disableFocusLoc
|
|
|
49
49
|
* popover, or forward past the last scrollable element within.
|
|
50
50
|
*/
|
|
51
51
|
const handleFocusTrap = (event) => {
|
|
52
|
-
const focusableElements = popperElement.querySelectorAll(TABBABLE_SELECTORS);
|
|
52
|
+
const focusableElements = popperElement.querySelectorAll(tabbableSelectors.TABBABLE_SELECTORS);
|
|
53
53
|
const firstElement = focusableElements[0];
|
|
54
54
|
const lastElement = focusableElements[focusableElements.length - 1];
|
|
55
55
|
// If shift tabbing (BACK) and on the first element, loop to last element.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHandleFocus.cjs","sources":["../../../../src/components/Popover/hooks/useHandleFocus.ts"],"sourcesContent":["import { KeyboardEventHandler, useEffect } from 'react';\n\
|
|
1
|
+
{"version":3,"file":"useHandleFocus.cjs","sources":["../../../../src/components/Popover/hooks/useHandleFocus.ts"],"sourcesContent":["import { KeyboardEventHandler, useEffect } from 'react';\n\nimport { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors';\n\ntype UseHandleFocusArgs = {\n popperElement: HTMLDialogElement | HTMLDivElement | null;\n anchorElement: HTMLElement | null;\n onClose: () => void;\n disableFocusLock?: boolean;\n};\n\nexport const useHandleFocus = ({\n popperElement,\n anchorElement,\n onClose,\n disableFocusLock = false,\n}: UseHandleFocusArgs) => {\n /**\n * Focuses first element, when this gets rendered for the first time.\n */\n useEffect(() => {\n if (!popperElement || disableFocusLock) return;\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n\n if (focusableElements.length < 1) return;\n\n setTimeout(() => {\n (focusableElements[0] as HTMLElement).focus();\n }, 0);\n }, [popperElement, disableFocusLock]);\n\n /**\n * Sets up tabbing into the popover.\n *\n * When the popover is open (pre-requisite for this being ran), and the user clicks `TAB`\n * then the tab should move **into** the popover, instead of the natural tab order.\n */\n useEffect(() => {\n if (!popperElement || !popperElement) return;\n\n const focusWithinPopper = (event: KeyboardEvent) => {\n if (event.key === 'Tab' && !event.shiftKey) {\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n // When nothing is focusable within the popover, close popover and allow natural tab order.\n if (focusableElements.length < 1) {\n onClose();\n return;\n }\n\n (focusableElements[0] as HTMLElement).focus();\n event.stopPropagation();\n event.preventDefault();\n }\n };\n\n // When tab occurs on ref focus, if focusable elements in popover focus first.\n anchorElement?.addEventListener('keydown', focusWithinPopper);\n\n // eslint-disable-next-line consistent-return\n return () => anchorElement?.removeEventListener('keydown', focusWithinPopper);\n }, [anchorElement, popperElement, onClose]);\n\n /**\n * Creates focus trap within the popover itself. Stopping users tabbing back to outside the\n * popover, or forward past the last scrollable element within.\n */\n const handleFocusTrap: KeyboardEventHandler<HTMLDialogElement | HTMLDivElement> = (event) => {\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n const firstElement = focusableElements[0];\n const lastElement = focusableElements[focusableElements.length - 1];\n\n // If shift tabbing (BACK) and on the first element, loop to last element.\n if (event.shiftKey && document.activeElement === firstElement) {\n event.preventDefault();\n (lastElement as HTMLElement).focus();\n\n // If tabbing (FORWARD) and on the last element, loop to first element.\n } else if (!event.shiftKey && document.activeElement === lastElement) {\n event.preventDefault();\n (firstElement as HTMLElement).focus();\n }\n };\n\n const handleKeyDown: KeyboardEventHandler<HTMLDialogElement | HTMLDivElement> = (event) => {\n if (!popperElement) return;\n\n // Handle escape\n if (event.key === 'Escape') {\n onClose();\n event.stopPropagation(); // Should only close THIS dialog.\n event.preventDefault(); // Shouldn't close any fullscreen dialogs (Modal) it's part of\n\n // Check we should enforce focus trap.\n } else if (!disableFocusLock && event.key === 'Tab') {\n handleFocusTrap(event);\n }\n };\n\n return {\n handleKeyDown,\n };\n};\n"],"names":["useEffect","TABBABLE_SELECTORS"],"mappings":";;;;;AAWa,MAAA,cAAc,GAAG,CAAC,EAC7B,aAAa,EACb,aAAa,EACb,OAAO,EACP,gBAAgB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AAE3C;;;AAGG;AACH,IAAA,MAAM,eAAe,GAA6D,CAAC,KAAK,KAAI;QAC1F,MAAM,iBAAiB,GAAG,aAAc,CAAC,gBAAgB,CAACA,oCAAkB,CAAC;AAC7E,QAAA,MAAM,YAAY,GAAG,iBAAiB,CAAC,CAAC,CAAC;QACzC,MAAM,WAAW,GAAG,iBAAiB,CAAC,iBAAiB,CAAC,MAAM,GAAG,CAAC,CAAC;;QAGnE,IAAI,KAAK,CAAC,QAAQ,IAAI,QAAQ,CAAC,aAAa,KAAK,YAAY,EAAE;YAC7D,KAAK,CAAC,cAAc,EAAE;YACrB,WAA2B,CAAC,KAAK,EAAE;;AAGrC;aAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,QAAQ,CAAC,aAAa,KAAK,WAAW,EAAE;YACpE,KAAK,CAAC,cAAc,EAAE;YACrB,YAA4B,CAAC,KAAK,EAAE;AACtC;AACH,KAAC;AAED,IAAA,MAAM,aAAa,GAA6D,CAAC,KAAK,KAAI;AACxF,QAAA,IAAI,CAAC,aAAa;YAAE;;AAGpB,QAAA,IAAI,KAAK,CAAC,GAAG,KAAK,QAAQ,EAAE;AAC1B,YAAA,OAAO,EAAE;AACT,YAAA,KAAK,CAAC,eAAe,EAAE,CAAC;AACxB,YAAA,KAAK,CAAC,cAAc,EAAE,CAAC;;AAGxB;aAAM,IAAI,CAAC,gBAAgB,IAAI,KAAK,CAAC,GAAG,KAAK,KAAK,EAAE;YACnD,eAAe,CAAC,KAAK,CAAC;AACvB;AACH,KAAC;IAED,OAAO;QACL,aAAa;KACd;AACH;;;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
+
import { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors.js';
|
|
2
3
|
|
|
3
|
-
const TABBABLE_SELECTORS = 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled])';
|
|
4
4
|
const useHandleFocus = ({ popperElement, anchorElement, onClose, disableFocusLock = false, }) => {
|
|
5
5
|
/**
|
|
6
6
|
* Focuses first element, when this gets rendered for the first time.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHandleFocus.js","sources":["../../../../src/components/Popover/hooks/useHandleFocus.ts"],"sourcesContent":["import { KeyboardEventHandler, useEffect } from 'react';\n\
|
|
1
|
+
{"version":3,"file":"useHandleFocus.js","sources":["../../../../src/components/Popover/hooks/useHandleFocus.ts"],"sourcesContent":["import { KeyboardEventHandler, useEffect } from 'react';\n\nimport { TABBABLE_SELECTORS } from '../../../utils/tabbableSelectors';\n\ntype UseHandleFocusArgs = {\n popperElement: HTMLDialogElement | HTMLDivElement | null;\n anchorElement: HTMLElement | null;\n onClose: () => void;\n disableFocusLock?: boolean;\n};\n\nexport const useHandleFocus = ({\n popperElement,\n anchorElement,\n onClose,\n disableFocusLock = false,\n}: UseHandleFocusArgs) => {\n /**\n * Focuses first element, when this gets rendered for the first time.\n */\n useEffect(() => {\n if (!popperElement || disableFocusLock) return;\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n\n if (focusableElements.length < 1) return;\n\n setTimeout(() => {\n (focusableElements[0] as HTMLElement).focus();\n }, 0);\n }, [popperElement, disableFocusLock]);\n\n /**\n * Sets up tabbing into the popover.\n *\n * When the popover is open (pre-requisite for this being ran), and the user clicks `TAB`\n * then the tab should move **into** the popover, instead of the natural tab order.\n */\n useEffect(() => {\n if (!popperElement || !popperElement) return;\n\n const focusWithinPopper = (event: KeyboardEvent) => {\n if (event.key === 'Tab' && !event.shiftKey) {\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n // When nothing is focusable within the popover, close popover and allow natural tab order.\n if (focusableElements.length < 1) {\n onClose();\n return;\n }\n\n (focusableElements[0] as HTMLElement).focus();\n event.stopPropagation();\n event.preventDefault();\n }\n };\n\n // When tab occurs on ref focus, if focusable elements in popover focus first.\n anchorElement?.addEventListener('keydown', focusWithinPopper);\n\n // eslint-disable-next-line consistent-return\n return () => anchorElement?.removeEventListener('keydown', focusWithinPopper);\n }, [anchorElement, popperElement, onClose]);\n\n /**\n * Creates focus trap within the popover itself. Stopping users tabbing back to outside the\n * popover, or forward past the last scrollable element within.\n */\n const handleFocusTrap: KeyboardEventHandler<HTMLDialogElement | HTMLDivElement> = (event) => {\n const focusableElements = popperElement!.querySelectorAll(TABBABLE_SELECTORS);\n const firstElement = focusableElements[0];\n const lastElement = focusableElements[focusableElements.length - 1];\n\n // If shift tabbing (BACK) and on the first element, loop to last element.\n if (event.shiftKey && document.activeElement === firstElement) {\n event.preventDefault();\n (lastElement as HTMLElement).focus();\n\n // If tabbing (FORWARD) and on the last element, loop to first element.\n } else if (!event.shiftKey && document.activeElement === lastElement) {\n event.preventDefault();\n (firstElement as HTMLElement).focus();\n }\n };\n\n const handleKeyDown: KeyboardEventHandler<HTMLDialogElement | HTMLDivElement> = (event) => {\n if (!popperElement) return;\n\n // Handle escape\n if (event.key === 'Escape') {\n onClose();\n event.stopPropagation(); // Should only close THIS dialog.\n event.preventDefault(); // Shouldn't close any fullscreen dialogs (Modal) it's part of\n\n // Check we should enforce focus trap.\n } else if (!disableFocusLock && event.key === 'Tab') {\n handleFocusTrap(event);\n }\n };\n\n return {\n handleKeyDown,\n };\n};\n"],"names":[],"mappings":";;;AAWa,MAAA,cAAc,GAAG,CAAC,EAC7B,aAAa,EACb,aAAa,EACb,OAAO,EACP,gBAAgB,GAAG,KAAK,GACL,KAAI;AACvB;;AAEG;IACH,SAAS,CAAC,MAAK;QACb,IAAI,CAAC,aAAa,IAAI,gBAAgB;YAAE;QACxC,MAAM,iBAAiB,GAAG,aAAc,CAAC,gBAAgB,CAAC,kBAAkB,CAAC;AAE7E,QAAA,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC;YAAE;QAElC,UAAU,CAAC,MAAK;AACb,YAAA,iBAAiB,CAAC,CAAC,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,CAAC,QAAQ,IAAI,QAAQ,CAAC,aAAa,KAAK,YAAY,EAAE;YAC7D,KAAK,CAAC,cAAc,EAAE;YACrB,WAA2B,CAAC,KAAK,EAAE;;AAGrC;aAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,QAAQ,CAAC,aAAa,KAAK,WAAW,EAAE;YACpE,KAAK,CAAC,cAAc,EAAE;YACrB,YAA4B,CAAC,KAAK,EAAE;AACtC;AACH,KAAC;AAED,IAAA,MAAM,aAAa,GAA6D,CAAC,KAAK,KAAI;AACxF,QAAA,IAAI,CAAC,aAAa;YAAE;;AAGpB,QAAA,IAAI,KAAK,CAAC,GAAG,KAAK,QAAQ,EAAE;AAC1B,YAAA,OAAO,EAAE;AACT,YAAA,KAAK,CAAC,eAAe,EAAE,CAAC;AACxB,YAAA,KAAK,CAAC,cAAc,EAAE,CAAC;;AAGxB;aAAM,IAAI,CAAC,gBAAgB,IAAI,KAAK,CAAC,GAAG,KAAK,KAAK,EAAE;YACnD,eAAe,CAAC,KAAK,CAAC;AACvB;AACH,KAAC;IAED,OAAO;QACL,aAAa;KACd;AACH;;;;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const TABBABLE_SELECTORS = 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled])';
|
|
4
|
+
|
|
5
|
+
exports.TABBABLE_SELECTORS = TABBABLE_SELECTORS;
|
|
6
|
+
//# sourceMappingURL=tabbableSelectors.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tabbableSelectors.cjs","sources":["../../src/utils/tabbableSelectors.ts"],"sourcesContent":["export const TABBABLE_SELECTORS =\n 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]):not([disabled])';\n"],"names":[],"mappings":";;AAAO,MAAM,kBAAkB,GAC7B;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TABBABLE_SELECTORS = "input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]):not([disabled])";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
const TABBABLE_SELECTORS = 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled])';
|
|
2
|
+
|
|
3
|
+
export { TABBABLE_SELECTORS };
|
|
4
|
+
//# sourceMappingURL=tabbableSelectors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tabbableSelectors.js","sources":["../../src/utils/tabbableSelectors.ts"],"sourcesContent":["export const TABBABLE_SELECTORS =\n 'input:not([disabled]), summary:not([disabled]), a[href]:not([disabled]), button:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]):not([disabled])';\n"],"names":[],"mappings":"AAAO,MAAM,kBAAkB,GAC7B;;;;"}
|