-
Notifications
You must be signed in to change notification settings - Fork 381
improvement(Table): implement indeterminate checkbox state #3909
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
29 changes: 29 additions & 0 deletions
29
packages/react-integration/cypress/integration/tableselectableexpandable.spec.ts
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,29 @@ | ||
| describe('Table Selectable Expandable Test', () => { | ||
| it('Navigate to demo section', () => { | ||
| cy.visit('http://localhost:3000/'); | ||
| cy.get('#table-selectable-expandable-demo-nav-item-link').click(); | ||
| cy.url().should('eq', 'http://localhost:3000/table-selectable-expandable-demo-nav-link'); | ||
| }); | ||
|
|
||
| it('Verify table string', () => { | ||
| cy.get('caption').contains('Selectable expandable Table'); | ||
| }); | ||
|
|
||
| it('Check number of rows', () => { | ||
| cy.get('.pf-c-table') | ||
| .find('tr') | ||
| .should('have.length', 5); | ||
| }); | ||
|
|
||
| it('Checks that initially checked inputs carry checked property', () => { | ||
| cy.get('.pf-c-table') | ||
| .find('[name="checkrow0"]') | ||
| .should('have.prop', 'checked'); | ||
| }); | ||
|
|
||
| it('Checks that check all checkbox is indeterminate', () => { | ||
| cy.get('.pf-c-table') | ||
| .find('[name="check-all"]') | ||
| .should('have.prop', 'indeterminate', true); | ||
| }); | ||
| }); |
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
111 changes: 111 additions & 0 deletions
111
...-integration/demo-app-ts/src/components/demos/TableDemo/TableSelectableExpandableDemo.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,111 @@ | ||
| import * as React from 'react'; | ||
| import { Table, TableHeader, TableBody, TableProps, headerCol, ICell, IRow, expandable } from '@patternfly/react-table'; | ||
| import { Checkbox } from '@patternfly/react-core'; | ||
|
|
||
| interface TableState { | ||
| columns: (ICell | string)[]; | ||
| rows: IRow[]; | ||
| canSelectAll: boolean; | ||
| } | ||
|
|
||
| export class TableSelectableExpandableDemo extends React.Component<TableProps, TableState> { | ||
| static displayName = 'TableSelectableExpandableDemo'; | ||
| constructor(props: TableProps) { | ||
| super(props); | ||
| this.state = { | ||
| columns: [ | ||
| { title: 'Repositories', cellTransforms: [headerCol()], cellFormatters: [expandable] }, | ||
| 'Branches', | ||
| { title: 'Pull requests' } | ||
| ], | ||
| rows: [ | ||
| { | ||
| cells: ['row0 - one', 'row0 - two', 'row0 - three'], | ||
| selected: true, | ||
| showSelect: true, | ||
| isOpen: false | ||
| }, | ||
| { | ||
| cells: ['row1 - one', 'row1 - two', 'row1 - three'], | ||
| selected: false, | ||
| parent: 0 | ||
| }, | ||
| { | ||
| cells: ['row2 - one', 'row2 - two', 'row2 - three'], | ||
| selected: false, | ||
| isOpen: false | ||
| }, | ||
| { | ||
| cells: ['row3 - one', 'row3 - two', 'row3 - three'], | ||
| selected: false, | ||
| parent: 2 | ||
| } | ||
| ], | ||
| canSelectAll: true | ||
| }; | ||
| this.onSelect = this.onSelect.bind(this); | ||
| this.onCollapse = this.onCollapse.bind(this); | ||
| } | ||
|
|
||
| onCollapse(event: React.FormEvent, rowKey: number, isOpen: boolean) { | ||
| const { rows } = this.state; | ||
| rows[rowKey].isOpen = isOpen; | ||
| this.setState({ | ||
| rows | ||
| }); | ||
| } | ||
|
|
||
| onSelect(event: React.FormEvent, isSelected: boolean, rowId: number) { | ||
| let rows: IRow[]; | ||
| if (rowId === -1) { | ||
| rows = this.state.rows.map(oneRow => { | ||
| oneRow.selected = isSelected; | ||
| return oneRow; | ||
| }); | ||
| } else { | ||
| rows = [...this.state.rows]; | ||
| rows[rowId].selected = isSelected; | ||
| } | ||
| this.setState({ | ||
| rows | ||
| }); | ||
| } | ||
|
|
||
| toggleSelect = (checked: boolean) => { | ||
| this.setState({ | ||
| canSelectAll: checked | ||
| }); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| window.scrollTo(0, 0); | ||
| } | ||
|
|
||
| render() { | ||
| const { columns, rows } = this.state; | ||
|
|
||
| return ( | ||
| <div> | ||
| <Table | ||
| caption="Selectable expandable Table" | ||
| onCollapse={this.onCollapse} | ||
| onSelect={this.onSelect} | ||
| cells={columns} | ||
| rows={rows} | ||
| canSelectAll={this.state.canSelectAll} | ||
| > | ||
| <TableHeader /> | ||
| <TableBody /> | ||
| </Table> | ||
| <Checkbox | ||
| label="canSelectAll" | ||
| isChecked={this.state.canSelectAll} | ||
| onChange={this.toggleSelect} | ||
| aria-label="toggle select all checkbox" | ||
| id="toggle-select-all" | ||
| name="toggle-select-all" | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| } |
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
50 changes: 37 additions & 13 deletions
50
packages/react-table/src/components/Table/SelectColumn.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 |
|---|---|---|
| @@ -1,22 +1,46 @@ | ||
| import * as React from 'react'; | ||
| import { SelectedRowsAmount } from './Table'; | ||
|
|
||
| export interface SelectColumnProps { | ||
| name?: string; | ||
| 'data-selectedrowsamount'?: SelectedRowsAmount; | ||
| children?: React.ReactNode; | ||
| className?: string; | ||
| onSelect?: (event: React.FormEvent<HTMLInputElement>) => void; | ||
| } | ||
|
|
||
| export const SelectColumn: React.FunctionComponent<SelectColumnProps> = ({ | ||
| children = null as React.ReactNode, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| className, | ||
| onSelect = null as (event: React.FormEvent<HTMLInputElement>) => void, | ||
| ...props | ||
| }: SelectColumnProps) => ( | ||
| <React.Fragment> | ||
| <input {...props} type="checkbox" onChange={onSelect} /> | ||
| {children} | ||
| </React.Fragment> | ||
| ); | ||
| SelectColumn.displayName = 'SelectColumn'; | ||
| export class SelectColumn extends React.Component<SelectColumnProps> { | ||
| static displayName = 'SelectColumn'; | ||
| static defaultProps: SelectColumnProps = { | ||
| children: null as React.ReactNode, | ||
| onSelect: null as (event: React.FormEvent<HTMLInputElement>) => void | ||
| }; | ||
|
|
||
| ref: React.RefObject<HTMLInputElement> = React.createRef(); | ||
|
|
||
| setCheckStatus() { | ||
| if (this.props.name === 'check-all') { | ||
| this.ref.current.indeterminate = this.props['data-selectedrowsamount'] === SelectedRowsAmount.some; | ||
| this.ref.current.checked = this.props['data-selectedrowsamount'] === SelectedRowsAmount.all; | ||
| } | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| this.setCheckStatus(); | ||
| } | ||
|
|
||
| componentDidUpdate() { | ||
| this.setCheckStatus(); | ||
| } | ||
|
|
||
| render() { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { children, onSelect, className, ...props } = this.props; | ||
| return ( | ||
| <React.Fragment> | ||
| <input {...props} type="checkbox" onChange={onSelect} ref={this.ref} /> | ||
| {children} | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
| } |
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.
Do you think we could assign strings to represent these enum values? I think using the enum indices is really elegant but makes it a little difficult to debug when comparing against the resulting markup. In other words I thought
data-rowsamount="1"meant there was a single row selected instead of it referring to "some".