-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds basic aria roles and grid navigation #2187
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6bf5334
Adds basic aria roles and grid navigation
56201f3
Adds tests for grid keyboard controls
ff57e3b
Simplifying data grid aria types
7fd7869
Update ariaLabel to aria-label in DataGrid docs example
myasonik 51efa7a
fixup! Adds basic aria roles and grid navigation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,8 @@ | |
&:first-of-type { | ||
border-left: $euiBorderThin; | ||
} | ||
|
||
&:focus { | ||
@include euiFocusRing; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import React, { Component, HTMLAttributes, ReactElement } from 'react'; | ||
import React, { | ||
Component, | ||
HTMLAttributes, | ||
ReactElement, | ||
KeyboardEvent, | ||
} from 'react'; | ||
import { EuiDataGridHeaderRow } from './data_grid_header_row'; | ||
import { EuiDataGridDataRow } from './data_grid_data_row'; | ||
import { CommonProps } from '../common'; | ||
import { CommonProps, Omit } from '../common'; | ||
import { Column, ColumnWidths } from './data_grid_types'; | ||
import { EuiDataGridCellProps } from './data_grid_cell'; | ||
import classNames from 'classnames'; | ||
import { keyCodes } from '../../services'; | ||
|
||
type EuiDataGridProps = CommonProps & | ||
type CommonGridProps = CommonProps & | ||
HTMLAttributes<HTMLDivElement> & { | ||
columns: Column[]; | ||
rowCount: number; | ||
renderCellValue: EuiDataGridCellProps['renderCellValue']; | ||
}; | ||
|
||
// This structure forces either aria-label or aria-labelledby to be defined | ||
// making some type of label a requirement | ||
type EuiDataGridProps = Omit<CommonGridProps, 'aria-label'> & | ||
({ 'aria-label': string } | { 'aria-labelledby': string }); | ||
myasonik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
interface EuiDataGridState { | ||
columnWidths: ColumnWidths; | ||
rows: ReactElement[]; | ||
focusedCell: [number, number]; | ||
} | ||
|
||
const ORIGIN: [number, number] = [0, 0]; | ||
|
||
export class EuiDataGrid extends Component<EuiDataGridProps, EuiDataGridState> { | ||
state = { | ||
columnWidths: {}, | ||
rows: this.renderRows(), | ||
focusedCell: ORIGIN, | ||
}; | ||
|
||
setColumnWidth = (columnName: string, width: number) => { | ||
|
@@ -33,25 +48,67 @@ export class EuiDataGrid extends Component<EuiDataGridProps, EuiDataGridState> { | |
); | ||
}; | ||
|
||
handleKeyDown = (e: KeyboardEvent<HTMLDivElement>) => { | ||
const colCount = this.props.columns.length - 1; | ||
const [x, y] = this.state.focusedCell; | ||
const rowCount = this.state.rows.length - 1; | ||
|
||
switch (e.keyCode) { | ||
case keyCodes.DOWN: | ||
e.preventDefault(); | ||
if (y < rowCount) { | ||
this.setState({ focusedCell: [x, y + 1] }, this.updateRows); | ||
} | ||
break; | ||
case keyCodes.LEFT: | ||
e.preventDefault(); | ||
if (x > 0) { | ||
this.setState({ focusedCell: [x - 1, y] }, this.updateRows); | ||
} | ||
break; | ||
case keyCodes.UP: | ||
e.preventDefault(); | ||
// TODO sort out when a user can arrow up into the column headers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO for a future PR |
||
if (y > 0) { | ||
this.setState({ focusedCell: [x, y - 1] }, this.updateRows); | ||
} | ||
break; | ||
case keyCodes.RIGHT: | ||
e.preventDefault(); | ||
if (x < colCount) { | ||
this.setState({ focusedCell: [x + 1, y] }, this.updateRows); | ||
} | ||
break; | ||
} | ||
}; | ||
|
||
onCellFocus = (x: number, y: number) => { | ||
this.setState({ focusedCell: [x, y] }); | ||
}; | ||
|
||
updateRows = () => { | ||
this.setState({ | ||
rows: this.renderRows(), | ||
}); | ||
}; | ||
|
||
renderRows() { | ||
const { columnWidths = {} } = this.state || {}; | ||
const { columnWidths = {}, focusedCell = ORIGIN as [number, number] } = | ||
this.state || {}; | ||
const { columns, rowCount, renderCellValue } = this.props; | ||
|
||
const onCellFocus = this.onCellFocus || function() {}; // TODO re-enable after PR#2188 | ||
const rows = []; | ||
|
||
for (let i = 0; i < rowCount; i++) { | ||
rows.push( | ||
<EuiDataGridDataRow | ||
key={i} | ||
rowIndex={i} | ||
focusedCell={focusedCell} | ||
columns={columns} | ||
renderCellValue={renderCellValue} | ||
columnWidths={columnWidths} | ||
onCellFocus={onCellFocus} | ||
/> | ||
); | ||
} | ||
|
@@ -68,9 +125,15 @@ export class EuiDataGrid extends Component<EuiDataGridProps, EuiDataGridState> { | |
className, | ||
...rest | ||
} = this.props; | ||
|
||
return ( | ||
<div {...rest} className={classNames(className, 'euiDataGrid')}> | ||
// Unsure why this element causes errors as focus follows spec | ||
// eslint-disable-next-line jsx-a11y/interactive-supports-focus | ||
<div | ||
role="grid" | ||
onKeyDown={this.handleKeyDown} | ||
// {...label} | ||
{...rest} | ||
className={classNames(className, 'euiDataGrid')}> | ||
<EuiDataGridHeaderRow | ||
columns={columns} | ||
columnWidths={columnWidths} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chandlerprall This doesn't show in our autodocs and I didn't know a good way to get this in.