Skip to content
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

Add stickyFirstColumn prop #327

Open
wants to merge 1 commit into
base: master
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
3 changes: 3 additions & 0 deletions src/components/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cx from 'classnames'
export const Cell: FC<{
gutter: boolean
stickyRight: boolean
stickyFirstColumn?: boolean
disabled?: boolean
className?: string
active?: boolean
Expand All @@ -14,6 +15,7 @@ export const Cell: FC<{
children,
gutter,
stickyRight,
stickyFirstColumn,
active,
disabled,
className,
Expand All @@ -28,6 +30,7 @@ export const Cell: FC<{
disabled && 'dsg-cell-disabled',
gutter && active && 'dsg-cell-gutter-active',
stickyRight && 'dsg-cell-sticky-right',
stickyFirstColumn && 'dsg-cell-sticky-first',
className
)}
style={{
Expand Down
11 changes: 11 additions & 0 deletions src/components/DataSheetGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const DataSheetGrid = React.memo(
headerRowHeight = typeof rowHeight === 'number' ? rowHeight : 40,
gutterColumn,
stickyRightColumn,
stickyFirstColumn,
rowKey,
addRowsComponent: AddRowsComponent = AddRows,
createRow = DEFAULT_CREATE_ROW as () => T,
Expand Down Expand Up @@ -251,6 +252,13 @@ export const DataSheetGrid = React.memo(
x = 0
}

if (
stickyFirstColumn &&
event.clientX - outerBoundingClientRect.left <= columnRights[1]
) {
x = event.clientX - outerBoundingClientRect.left
}

if (
hasStickyRightColumn &&
outerBoundingClientRect.right - event.clientX <=
Expand All @@ -276,6 +284,7 @@ export const DataSheetGrid = React.memo(
getOuterBoundingClientRect,
headerRowHeight,
hasStickyRightColumn,
stickyFirstColumn,
getRowIndex,
]
)
Expand Down Expand Up @@ -1779,6 +1788,7 @@ export const DataSheetGrid = React.memo(
outerRef={outerRef}
columnWidths={columnWidths}
hasStickyRightColumn={hasStickyRightColumn}
stickyFirstColumn={stickyFirstColumn}
displayHeight={displayHeight}
data={data}
fullWidth={fullWidth}
Expand Down Expand Up @@ -1807,6 +1817,7 @@ export const DataSheetGrid = React.memo(
headerRowHeight={headerRowHeight}
rowHeight={getRowSize}
hasStickyRightColumn={hasStickyRightColumn}
stickyFirstColumn={stickyFirstColumn}
dataLength={data.length}
viewHeight={height}
viewWidth={width}
Expand Down
10 changes: 10 additions & 0 deletions src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const Grid = <T extends any>({
innerRef,
columnWidths,
hasStickyRightColumn,
stickyFirstColumn,
displayHeight,
headerRowHeight,
rowHeight,
Expand All @@ -43,6 +44,7 @@ export const Grid = <T extends any>({
innerRef: RefObject<HTMLDivElement>
columnWidths?: number[]
hasStickyRightColumn: boolean
stickyFirstColumn?: boolean
displayHeight: number
headerRowHeight: number
rowHeight: (index: number) => { height: number }
Expand Down Expand Up @@ -97,9 +99,15 @@ export const Grid = <T extends any>({
overscan: 1,
rangeExtractor: (range) => {
const result = defaultRangeExtractor(range)

if (result[0] !== 0) {
result.unshift(0)
}

if (stickyFirstColumn && result[1] !== 1) {
result.splice(1, 0, 1)
}

if (
hasStickyRightColumn &&
result[result.length - 1] !== columns.length - 1
Expand Down Expand Up @@ -150,6 +158,7 @@ export const Grid = <T extends any>({
<CellComponent
key={col.key}
gutter={col.index === 0}
stickyFirstColumn={stickyFirstColumn && col.index === 1}
stickyRight={
hasStickyRightColumn && col.index === columns.length - 1
}
Expand Down Expand Up @@ -215,6 +224,7 @@ export const Grid = <T extends any>({
<CellComponent
key={col.key}
gutter={col.index === 0}
stickyFirstColumn={stickyFirstColumn && col.index === 1}
stickyRight={
hasStickyRightColumn && col.index === columns.length - 1
}
Expand Down
31 changes: 24 additions & 7 deletions src/components/SelectionRect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const SelectionRect = React.memo<SelectionContextType>(
rowHeight,
activeCell,
hasStickyRightColumn,
stickyFirstColumn,
dataLength,
viewWidth,
viewHeight,
Expand Down Expand Up @@ -170,20 +171,22 @@ export const SelectionRect = React.memo<SelectionContextType>(
})}
style={{
top: headerRowHeight,
left: columnWidths[0],
left: columnWidths[0] + (stickyFirstColumn ? columnWidths[1] : 0),
height: viewHeight ? viewHeight - headerRowHeight : 0,
width:
contentWidth && viewWidth
? viewWidth -
columnWidths[0] -
(hasStickyRightColumn
? columnWidths[columnWidths.length - 1]
: 0)
: 0) -
(stickyFirstColumn ? columnWidths[1] : 0)
: `calc(100% - ${
columnWidths[0] +
(hasStickyRightColumn
? columnWidths[columnWidths.length - 1]
: 0)
: 0) +
(stickyFirstColumn ? columnWidths[1] : 0)
}px)`,
}}
/>
Expand Down Expand Up @@ -232,6 +235,8 @@ export const SelectionRect = React.memo<SelectionContextType>(
className={cx('dsg-active-cell', {
'dsg-active-cell-focus': editing,
'dsg-active-cell-disabled': activeCellIsDisabled,
'dsg-active-cell-sticky-first':
stickyFirstColumn && activeCell.col === 0,
})}
style={activeCellRect}
/>
Expand All @@ -253,18 +258,30 @@ export const SelectionRect = React.memo<SelectionContextType>(
}}
/>
)}
{expandRowsRect && (
<div className={cx('dsg-expand-rows-rect')} style={expandRowsRect} />
)}
{expandRowsIndicator && (
<div
className={cx(
'dsg-expand-rows-indicator',
selectionIsDisabled && 'dsg-expand-rows-indicator-disabled'
selectionIsDisabled && 'dsg-expand-rows-indicator-disabled',
stickyFirstColumn &&
((!selection && activeCell?.col === 0) ||
selection?.max.col === 0) &&
'dsg-expand-rows-indicator-sticky-first'
)}
style={expandRowsIndicator}
/>
)}
{expandRowsRect && (
<div
className={cx(
'dsg-expand-rows-rect',
stickyFirstColumn &&
activeCell?.col === 0 &&
'dsg-expand-rows-rect-sticky-first'
)}
style={expandRowsRect}
/>
)}
</>
)
}
Expand Down
28 changes: 28 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@
transform: translateY(-100%);
}

.dsg-cell-sticky-first {
position: sticky;
top: 0;
z-index: 20;
margin-right: auto;
transform: translateY(-100%);
}

.dsg-cell-disabled {
background: var(--dsg-cell-disabled-background-color);
}
Expand All @@ -101,6 +109,10 @@
box-shadow: 1px 0 var(--dsg-border-color), 0 1px var(--dsg-border-color);
}

.dsg-cell-header.dsg-cell-sticky-first {
box-shadow: 1px 0 var(--dsg-border-color), 0 1px var(--dsg-border-color);
}

.dsg-cell-header.dsg-cell-sticky-right {
box-shadow: 0 1px var(--dsg-border-color);
}
Expand Down Expand Up @@ -140,6 +152,11 @@
border-color: var(--dsg-selection-disabled-border-color);
}

.dsg-active-cell-sticky-first {
position: sticky;
z-index: 40;
}

.dsg-selection-rect {
background: var(--dsg-selection-background-color);
}
Expand Down Expand Up @@ -405,6 +422,7 @@
.dsg-selection-col-marker-container {
position: absolute;
top: 0;
z-index: 20;
}

.dsg-selection-col-marker {
Expand Down Expand Up @@ -570,10 +588,20 @@
border: solid 1px var(--dsg-selection-disabled-border-color);
}

.dsg-expand-rows-indicator-sticky-first {
position: sticky;
z-index: 40;
}

.dsg-expand-rows-rect {
position: absolute;
box-sizing: border-box;
transition: all var(--dsg-transition-duration);
pointer-events: none;
background: rgba(0, 0, 0, 0.03);
}

.dsg-expand-rows-rect-sticky-first{
position: sticky;
z-index: 40;
}
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type SelectionContextType = {
selection: Selection | null
dataLength: number
rowHeight: (index: number) => { height: number; top: number }
stickyFirstColumn?: boolean
hasStickyRightColumn: boolean
editing: boolean
isCellDisabled: (cell: Cell) => boolean
Expand Down Expand Up @@ -93,7 +94,13 @@ export type AddRowsComponentProps = {

export type ContextMenuItem =
| {
type: 'INSERT_ROW_BELLOW' | 'DELETE_ROW' | 'DUPLICATE_ROW' | 'COPY' | 'CUT' | 'PASTE'
type:
| 'INSERT_ROW_BELLOW'
| 'DELETE_ROW'
| 'DUPLICATE_ROW'
| 'COPY'
| 'CUT'
| 'PASTE'
action: () => void
}
| {
Expand Down Expand Up @@ -135,6 +142,7 @@ export type DataSheetGridProps<T> = {
columns?: Partial<Column<T, any, any>>[]
gutterColumn?: SimpleColumn<T, any> | false
stickyRightColumn?: SimpleColumn<T, any>
stickyFirstColumn?: boolean
rowKey?: string | ((opts: { rowData: T; rowIndex: number }) => string)
height?: number
rowHeight?: number | ((opt: { rowData: T; rowIndex: number }) => number)
Expand Down