-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react-tree-grid): initial implementation (#80)
- Loading branch information
1 parent
ba9f178
commit 2bdc6e6
Showing
20 changed files
with
401 additions
and
1,149 deletions.
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
16 changes: 7 additions & 9 deletions
16
packages/react-tree-grid/src/components/TreeGrid/TreeGrid.tsx
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
1 change: 1 addition & 0 deletions
1
packages/react-tree-grid/src/components/TreeGrid/TreeGrid.types.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TreeGridProps = JSX.IntrinsicElements['div']; |
8 changes: 8 additions & 0 deletions
8
packages/react-tree-grid/src/components/TreeGrid/useTreeGridStyles.styles.ts
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { makeResetStyles, mergeClasses } from '@fluentui/react-components'; | ||
|
||
const useResetStyles = makeResetStyles({ | ||
display: 'block', | ||
}); | ||
|
||
export const useTreeGridStyles = () => | ||
mergeClasses('fui-TreeGrid', useResetStyles()); |
24 changes: 10 additions & 14 deletions
24
packages/react-tree-grid/src/components/TreeGridCell/TreeGridCell.tsx
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
8 changes: 8 additions & 0 deletions
8
packages/react-tree-grid/src/components/TreeGridCell/useTreeGridCellStyles.styles.ts
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { makeResetStyles, mergeClasses } from '@fluentui/react-components'; | ||
|
||
const useResetStyles = makeResetStyles({ | ||
flex: '1 1 auto', | ||
}); | ||
|
||
export const useTreeGridCellStyles = () => | ||
mergeClasses('fui-TreeGridCell', useResetStyles()); |
31 changes: 20 additions & 11 deletions
31
packages/react-tree-grid/src/components/TreeGridRow/TreeGridRow.tsx
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
4 changes: 4 additions & 0 deletions
4
packages/react-tree-grid/src/components/TreeGridRow/TreeGridRow.types.ts
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type TreeGridRowProps = JSX.IntrinsicElements['div'] & { | ||
// aria-level is required for screen readers to understand the nesting level of the row | ||
'aria-level': number | string; | ||
}; |
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 +1,2 @@ | ||
export * from './TreeGridRow'; | ||
export type * from './TreeGridRow.types'; |
16 changes: 16 additions & 0 deletions
16
packages/react-tree-grid/src/components/TreeGridRow/useTreeGridRowStyles.styles.ts
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
makeResetStyles, | ||
mergeClasses, | ||
createFocusOutlineStyle, | ||
} from '@fluentui/react-components'; | ||
|
||
const useResetStyles = makeResetStyles({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
position: 'relative', | ||
...createFocusOutlineStyle(), | ||
}); | ||
|
||
export const useTreeGridRowStyles = () => { | ||
return mergeClasses('fui-TreeGridRow', useResetStyles()); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as React from 'react'; | ||
import { | ||
useArrowNavigationGroup, | ||
useFocusFinders, | ||
} from '@fluentui/react-tabster'; | ||
import type { TreeGridProps } from '../components/TreeGrid/TreeGrid.types'; | ||
import { isHTMLElement, useEventCallback } from '@fluentui/react-utilities'; | ||
import { | ||
ArrowDown, | ||
ArrowLeft, | ||
ArrowRight, | ||
ArrowUp, | ||
Escape, | ||
keyCodes, | ||
} from '@fluentui/keyboard-keys'; | ||
|
||
export const useNavigation = (props: Pick<TreeGridProps, 'onKeyDown'>) => { | ||
const tabsterAttributes = useArrowNavigationGroup({ | ||
axis: 'vertical', | ||
memorizeCurrent: true, | ||
}); | ||
const { findFirstFocusable } = useFocusFinders(); | ||
const findParentRow = useFindParentRow(); | ||
const handleKeyDown = useEventCallback( | ||
(event: React.KeyboardEvent<HTMLDivElement>) => { | ||
props.onKeyDown?.(event); | ||
// TreeGridRow | ||
if (isHTMLElement(event.target) && event.target.role === 'row') { | ||
switch (event.key) { | ||
case ArrowRight: { | ||
const ariaExpanded = event.target.getAttribute('aria-expanded'); | ||
if (ariaExpanded === 'false') { | ||
return; | ||
} | ||
findFirstFocusable(event.target)?.focus(); | ||
return; | ||
} | ||
case ArrowLeft: { | ||
const ariaExpanded = event.target.getAttribute('aria-expanded'); | ||
if (ariaExpanded === 'true') { | ||
return; | ||
} | ||
findParentRow(event.target)?.focus(); | ||
return; | ||
} | ||
} | ||
return; | ||
} | ||
// TreeGridCell | ||
switch (event.key) { | ||
case ArrowDown: | ||
case ArrowUp: | ||
case ArrowLeft: { | ||
event.target.dispatchEvent( | ||
new KeyboardEvent('keydown', { | ||
key: Escape, | ||
keyCode: keyCodes.Escape, | ||
}) | ||
); | ||
} | ||
} | ||
} | ||
); | ||
return { onKeyDown: handleKeyDown, ...tabsterAttributes }; | ||
}; | ||
|
||
const useFindParentRow = () => { | ||
const { findPrevFocusable } = useFocusFinders(); | ||
return React.useCallback( | ||
(currentRow: HTMLElement): HTMLElement | null => { | ||
const currentLevel = Number(currentRow.getAttribute('aria-level')); | ||
if (isNaN(currentLevel)) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.error( | ||
`TreeGrid: aria-level ${currentLevel} is not a number, at row:`, | ||
currentRow | ||
); | ||
} | ||
return null; | ||
} | ||
if (currentLevel === 1) { | ||
return null; | ||
} | ||
let element = currentRow; | ||
while (Number(element.getAttribute('aria-level')) !== currentLevel - 1) { | ||
const nextElement = findPrevFocusable(element); | ||
if (!nextElement) { | ||
return null; | ||
} | ||
element = nextElement; | ||
} | ||
if (element !== currentRow) { | ||
return element; | ||
} | ||
return null; | ||
}, | ||
[findPrevFocusable] | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { ArrowLeft, ArrowRight, Enter } from '@fluentui/keyboard-keys'; | ||
import { isHTMLElement, useEventCallback } from '@fluentui/react-utilities'; | ||
import * as React from 'react'; | ||
|
||
export type TreeGridRowId = string; | ||
|
||
export const useTreeGridOpenRows = () => { | ||
const [openRows, setOpenRows] = React.useState( | ||
() => new Set<TreeGridRowId>() | ||
); | ||
const openRow = React.useCallback((rowId: TreeGridRowId) => { | ||
setOpenRows((previousSet) => new Set(previousSet).add(rowId)); | ||
}, []); | ||
const closeRow = React.useCallback((rowId: TreeGridRowId) => { | ||
setOpenRows((previousSet) => { | ||
const newSet = new Set(previousSet); | ||
newSet.delete(rowId); | ||
return newSet; | ||
}); | ||
}, []); | ||
const handleKeyDown = useEventCallback( | ||
(event: React.KeyboardEvent<HTMLDivElement>) => { | ||
if (isHTMLElement(event.target) && event.target.role === 'row') { | ||
switch (event.key) { | ||
case Enter: { | ||
return event.target.click(); | ||
} | ||
case ArrowRight: { | ||
if ( | ||
event.target.getAttribute('aria-expanded') === 'false' && | ||
event.target.dataset.treeGridRowId | ||
) { | ||
openRow(event.target.dataset.treeGridRowId); | ||
} | ||
return; | ||
} | ||
case ArrowLeft: { | ||
if ( | ||
event.target.getAttribute('aria-expanded') === 'true' && | ||
event.target.dataset.treeGridRowId | ||
) { | ||
closeRow(event.target.dataset.treeGridRowId); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
); | ||
const handleClick = useEventCallback( | ||
(event: React.MouseEvent<HTMLDivElement>) => { | ||
if (!isHTMLElement(event.target)) { | ||
return; | ||
} | ||
let target: HTMLElement; | ||
if (event.target.role === 'row') { | ||
target = event.target; | ||
} else if (event.target.parentElement?.role === 'row') { | ||
target = event.target.parentElement; | ||
} else { | ||
return; | ||
} | ||
if (target.dataset.treeGridRowId) { | ||
const ariaExpanded = target.getAttribute('aria-expanded'); | ||
if (ariaExpanded === 'false') { | ||
openRow(target.dataset.treeGridRowId); | ||
} else if (ariaExpanded === 'true') { | ||
closeRow(target.dataset.treeGridRowId); | ||
} | ||
} | ||
} | ||
); | ||
return { | ||
openRows, | ||
setOpenRows, | ||
getTreeGridProps: () => ({ | ||
onKeyDown: handleKeyDown, | ||
onClick: handleClick, | ||
}), | ||
getTreeGridRowProps: (props: { id?: TreeGridRowId }) => ({ | ||
'data-tree-grid-row-id': props.id, | ||
'aria-expanded': props.id ? openRows.has(props.id) : undefined, | ||
}), | ||
}; | ||
}; |
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,3 +1,4 @@ | ||
export * from './components/TreeGridCell'; | ||
export * from './components/TreeGridRow'; | ||
export * from './components/TreeGrid'; | ||
export { useTreeGridOpenRows } from './hooks/useTreeGridControl'; |
Oops, something went wrong.