Skip to content

Paul.diaz/make spreadsheet component able to handle selection column #144

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/swag-sheet/components/Spreadsheet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { styled } from '@stitches/react'
import { useTable, useFilters, useSortBy, usePagination } from 'react-table'
import { useTable, useFilters, useSortBy, usePagination, useRowSelect } from 'react-table'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line limit

import { LoadingBar, Button, StyledDiv } from '@generates/swag'
import { merge } from '@generates/merger'
import StyledTable from './styled/StyledTable.js'
Expand Down Expand Up @@ -125,6 +125,7 @@ export default function Spreadsheet (props) {
rows,
prepareRow,
setAllFilters,
selectedFlatRows,
state: { pageIndex, pageSize, sortBy, filters }
} = useTable(
merge(
Expand All @@ -137,10 +138,11 @@ export default function Spreadsheet (props) {
),
useFilters,
useSortBy,
usePagination
usePagination,
useRowSelect
)

const { onPageIndex, onPageSize, onSortBy, onFilter } = props
const { onPageIndex, onPageSize, onSortBy, onFilter, onRowIsSelected } = props

React.useEffect(
() => onPageIndex && !initialRender.current && onPageIndex(pageIndex),
Expand Down Expand Up @@ -174,6 +176,15 @@ export default function Spreadsheet (props) {
]
)

React.useEffect(
() => onRowIsSelected && !initialRender.current &&
onRowIsSelected(selectedFlatRows.map(row => row.original)),
Copy link
Collaborator

@ianwalter ianwalter Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is probably unnecessary? Why not just send the whole row?

[
onRowIsSelected,
selectedFlatRows
]
)

React.useEffect(() => (initialRender.current = false), [])

React.useEffect(
Expand Down Expand Up @@ -257,18 +268,19 @@ export default function Spreadsheet (props) {
>
{row.cells.map(cell => {
const { key, ...rest } = cell.getCellProps()
const columnSelection = cell.column.id === 'Selection'
return (
<SpreadsheetCell
key={key}
id={key}
cell={cell}
styles={props.css?.tableCell}
canEdit={canEdit}
canEdit={!columnSelection && canEdit}
isSelected={selectedCell === key}
isFocused={focusedCell === key}
onSelectCell={(evt, id) => setSelectedCell(id)}
onFocusCell={(evt, id) => setFocusedCell(id)}
onBlur={onBlur}
onSelectCell={(evt, id) => !columnSelection && setSelectedCell(id)}
onFocusCell={(evt, id) => !columnSelection && setFocusedCell(id)}
Comment on lines +281 to +282
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line length

onBlur={(evt) => !columnSelection && onBlur(evt)}
onEscape={onEscape}
onTab={onTab}
onShiftTab={onShiftTab}
Expand Down
2 changes: 1 addition & 1 deletion packages/swag-sheet/components/SpreadsheetCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function SpreadsheetCell (props) {
onBlur={evt => props.onBlur(evt, ctx)}
{...props.cell.getCellProps()}
>
{value}
{value || props.cell.render('Cell')}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be handled differently. What if the value is falsy?

</StyledTableCell>
)
}
17 changes: 17 additions & 0 deletions packages/swag-sheet/pages/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default function AddPage () {
sortBy => console.log('Sort by', sortBy),
[]
)
const onRowIsSelected = React.useCallback(
rows => console.log('Selected row', rows),
[]
)

function onSaveAddedRows (input) {
return new Promise(resolve => {
Expand Down Expand Up @@ -62,6 +66,18 @@ export default function AddPage () {
<div>
<Spreadsheet
columns={[
{
id: 'Selection',
Cell: function RowSelection ({ row }) {
const { indeterminate, ...rest } = row.getToggleRowSelectedProps()
return <input
type="checkbox"
className="form-checkbox"
{...rest}
/>
},
disableSortBy: false
},
{
id: 'Driver Name',
disableSortBy: false
Expand All @@ -79,6 +95,7 @@ export default function AddPage () {
onSortBy={onSortBy}
onSaveAddedRows={onSaveAddedRows}
onUpdateCell={onUpdateCell}
onRowIsSelected={onRowIsSelected}
data={data}
showLoading={true}
isLoading={isLoading}
Expand Down