-
Notifications
You must be signed in to change notification settings - Fork 14
All/selected row switch #284
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,42 @@ | ||
| import React from 'react'; | ||
| import { mount } from 'cypress/react'; | ||
| import { BasicExample } from '../../packages/module/patternfly-docs/content/extensions/data-view/examples/Toolbar/AllSelectedExample'; | ||
|
|
||
| describe('BasicExample Component', () => { | ||
| beforeEach(() => { | ||
| mount(<BasicExample />); | ||
| }); | ||
|
|
||
| it('displays all rows by default', () => { | ||
| cy.get('table tbody tr').should('have.length', 5); | ||
| }); | ||
|
|
||
| it('filters to selected rows when toggled', () => { | ||
| cy.get('table tbody tr') | ||
| .first() | ||
| .find('input[type="checkbox"]') | ||
| .check(); | ||
|
|
||
| cy.get('#selected-row-switch').click(); | ||
|
|
||
| cy.get('table tbody tr').should('have.length', 1); | ||
| }); | ||
|
|
||
| it('switches back to all when last selected row is unselected', () => { | ||
| cy.get('table tbody tr') | ||
| .first() | ||
| .find('input[type="checkbox"]') | ||
| .check(); | ||
|
|
||
| cy.get('#selected-row-switch').click(); | ||
|
|
||
| cy.get('table tbody tr').should('have.length', 1); | ||
|
|
||
| cy.get('table tbody tr') | ||
| .first() | ||
| .find('input[type="checkbox"]') | ||
| .uncheck(); | ||
|
|
||
| cy.get('table tbody tr').should('have.length', 5); | ||
| }); | ||
| }); |
116 changes: 116 additions & 0 deletions
116
...dule/patternfly-docs/content/extensions/data-view/examples/Toolbar/AllSelectedExample.tsx
This file contains hidden or 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,116 @@ | ||
| import React, { useEffect, useMemo, useState } from 'react'; | ||
| import { Pagination, ToggleGroup, ToggleGroupItem, Tooltip } from '@patternfly/react-core'; | ||
| import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks'; | ||
| import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; | ||
| import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar'; | ||
| import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; | ||
| import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups'; | ||
|
|
||
| const TOGGLE_ALL = 'all'; | ||
| const TOGGLE_SELECTED = 'selected'; | ||
|
|
||
| const perPageOptions = [ | ||
| { title: '5', value: 5 }, | ||
| { title: '10', value: 10 } | ||
| ]; | ||
|
|
||
| interface Repository { | ||
| name: string; | ||
| branches: string | null; | ||
| prs: string | null; | ||
| workspaces: string; | ||
| lastCommit: string; | ||
| }; | ||
|
|
||
| const repositories: Repository[] = [ | ||
| { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' }, | ||
| { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' }, | ||
| { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' }, | ||
| { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' }, | ||
| { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }, | ||
| { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' } | ||
| ]; | ||
|
|
||
| const rows = repositories.map(item => Object.values(item)); | ||
|
|
||
| const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ]; | ||
|
|
||
| const ouiaId = 'LayoutExample'; | ||
|
|
||
| export const BasicExample: React.FunctionComponent = () => { | ||
| const pagination = useDataViewPagination({ perPage: 5 }); | ||
| const selection = useDataViewSelection({ matchOption: (a, b) => a[0] === b[0] }); | ||
| const [ selectedToggle, setSelectedToggle ] = useState(TOGGLE_ALL); | ||
| const { selected, onSelect } = selection; | ||
| const { page, perPage, onSetPage } = pagination; | ||
|
|
||
| const pageRows = useMemo(() => (selectedToggle === TOGGLE_SELECTED ? selected : rows).slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage, selectedToggle, selected ]); | ||
|
|
||
| const handleBulkSelect = (value: BulkSelectValue) => { | ||
| value === BulkSelectValue.none && onSelect(false); | ||
| value === BulkSelectValue.all && onSelect(true, rows); | ||
| }; | ||
|
|
||
| const handleItemClick = (event: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent, _isSelected: boolean) => { | ||
| const id = event.currentTarget.id; | ||
|
|
||
| if (id === TOGGLE_SELECTED && selected.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (selectedToggle !== id) { | ||
| setSelectedToggle(id); | ||
| onSetPage(undefined, 1); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (selected.length === 0) { | ||
| setSelectedToggle(TOGGLE_ALL); | ||
| } | ||
| }, [ selected ]); | ||
|
|
||
| return ( | ||
| <DataView selection={selection}> | ||
| <DataViewToolbar | ||
| ouiaId='DataViewHeader' | ||
| bulkSelect={ | ||
| <BulkSelect | ||
| canSelectAll | ||
| isDataPaginated={false} | ||
| totalCount={repositories.length} | ||
| selectedCount={selected.length} | ||
| onSelect={handleBulkSelect} | ||
| /> | ||
| } | ||
| toggleGroup={ | ||
| <ToggleGroup aria-label="Toggle group to switch between all / selected table rows"> | ||
| <ToggleGroupItem | ||
| text="All" | ||
| buttonId={TOGGLE_ALL} | ||
| isSelected={selectedToggle === TOGGLE_ALL} | ||
| onChange={handleItemClick} | ||
| /> | ||
| <ToggleGroupItem | ||
| id="selected-row-switch" | ||
| text="Selected" | ||
| buttonId={TOGGLE_SELECTED} | ||
| isSelected={selectedToggle === TOGGLE_SELECTED} | ||
| onChange={handleItemClick} | ||
| aria-disabled={selected.length === 0} | ||
| /> | ||
| </ToggleGroup> | ||
| } | ||
| pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} | ||
| /> | ||
|
|
||
| {selected.length === 0 && (<Tooltip | ||
| id="selected-row-switch-tooltip" | ||
| content="Select at least one row to enable this filter" | ||
| triggerRef={() => document.getElementById('selected-row-switch') as HTMLButtonElement} | ||
| />)} | ||
|
|
||
| <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} /> | ||
| </DataView> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
how about introducing constants for all/selected strings?
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.
Implemented.