Skip to content

Commit 6ea2104

Browse files
committed
pre release code amendments
1 parent e0999e7 commit 6ea2104

File tree

7 files changed

+66
-57
lines changed

7 files changed

+66
-57
lines changed

src/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { ReactGrid } from './lib/Components/ReactGrid';
55
// import './test/theming-test.scss';
66
import {
77
config, enablePinnedToBodyConfig, disabledInitialFocusLocationConfig, enableAdditionalContentConfig,
8-
enableAdditionalContentWithFlexRowConfig, enableSymetric, enableResponsiveSticky, enableResponsiveStickyPinnedToBody, enableSpannedCells
8+
enableAdditionalContentWithFlexRowConfig, enableSymetric, enableResponsiveSticky, enableResponsiveStickyPinnedToBody,
9+
enableSpannedCells
910
} from './test/testEnvConfig';
1011

1112
let component = <ExtTestGrid

src/lib/Components/CellFocus.tsx

+39-33
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
import * as React from 'react';
2+
import { translateLocationIdxToLookupKey } from '../Model/CellMatrix';
23
import { Location } from '../Model/InternalModel';
34
import { State } from '../Model/State';
45

5-
interface CellFocusProps {
6-
state?: State;
6+
7+
interface FeaturedCellProps {
78
location: Location;
8-
isHighlight?: boolean;
9+
state?: State;
910
borderColor?: string;
1011
className?: string;
1112
}
12-
export const Highlight: React.FC<CellFocusProps> = ({ borderColor, isHighlight, location, className, state }) => {
13-
const colIdx = location.column.idx;
14-
const rowIdx = location.row.idx;
15-
const range = state?.cellMatrix.rangesToRender[state.cellMatrix.getLocationToFindRangeByIds(colIdx, rowIdx)]?.range;
13+
14+
export const CellHighlight: React.FC<FeaturedCellProps> = ({ borderColor, location, className, state }) => {
15+
const { idx: colIdx } = location.column;
16+
const { idx: rowIdx } = location.row;
17+
const range = state?.cellMatrix.rangesToRender[translateLocationIdxToLookupKey(colIdx, rowIdx)]?.range;
1618
if (!range) {
1719
return null;
1820
}
1921
return (
20-
<div
21-
key={borderColor}
22-
className={`${isHighlight ? 'rg-cell-highlight' : 'rg-cell-focus'} ${className || ''}`}
23-
style={{
24-
top: location.row.top - (location.row.top === 0 ? 0 : 1),
25-
left: location.column.left - (location.column.left === 0 ? 0 : 1),
26-
width: range.width + (location.column.left === 0 ? 0 : 1),
27-
height: range.height + (location.row.top === 0 ? 0 : 1),
28-
borderColor: `${borderColor}`,
29-
}}
22+
<FeaturedCell
23+
location={location}
24+
className={`rg-cell-highlight ${className || ''}`}
25+
borderColor={borderColor}
26+
width={range.width}
27+
height={range.height}
3028
/>
3129
);
3230
}
3331

34-
// TODO make HOC for highlights
35-
export const CellFocus: React.FC<CellFocusProps> = ({ borderColor, isHighlight, location, className }) => {
32+
export const CellFocus: React.FC<FeaturedCellProps> = ({ borderColor, location, className }) => {
33+
return <FeaturedCell
34+
location={location}
35+
className={`rg-cell-focus ${className || ''}`}
36+
borderColor={borderColor}
37+
width={location.column.width}
38+
height={location.row.height}
39+
/>
40+
}
3641

37-
return (
38-
<div
39-
key={borderColor}
40-
className={`${isHighlight ? 'rg-cell-highlight' : 'rg-cell-focus'} ${className || ''}`}
41-
style={{
42-
top: location.row.top - (location.row.top === 0 ? 0 : 1),
43-
left: location.column.left - (location.column.left === 0 ? 0 : 1),
44-
width: location.column.width + (location.column.left === 0 ? 0 : 1),
45-
height: location.row.height + (location.row.top === 0 ? 0 : 1),
46-
borderColor: `${borderColor}`,
47-
}}
48-
/>
49-
);
50-
}
42+
const FeaturedCell: React.FC<FeaturedCellProps & { width: number, height: number }> =
43+
({ className, location, borderColor, height, width }) => {
44+
return (
45+
<div
46+
className={className}
47+
style={{
48+
top: location.row.top - (location.row.top === 0 ? 0 : 1),
49+
left: location.column.left - (location.column.left === 0 ? 0 : 1),
50+
width: width + (location.column.left === 0 ? 0 : 1),
51+
height: height + (location.row.top === 0 ? 0 : 1),
52+
borderColor: `${borderColor}`,
53+
}}
54+
/>
55+
)
56+
}

src/lib/Components/Pane.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Range } from '../Model/Range';
33
import { State } from '../Model/State';
44
import { Borders } from '../Model/InternalModel';
55
import { Highlight } from '../Model/PublicModel';
6-
import { CellFocus, Highlight as CellHighlight } from './CellFocus';
6+
import { CellFocus, CellHighlight } from './CellFocus';
77
import { RowRenderer } from './RowRenderer';
88
import { CellRendererProps } from './CellRenderer';
99
import { isMobileDevice } from '../Functions/isMobileDevice';
@@ -62,7 +62,7 @@ function renderHighlights(state: State, range: Range) {
6262
try {
6363
const location = state.cellMatrix.getLocationById(highlight.rowId, highlight.columnId);
6464
return location && range.contains(location) &&
65-
<CellHighlight key={id} location={location} state={state} borderColor={highlight.borderColor} isHighlight />;
65+
<CellHighlight key={id} location={location} state={state} borderColor={highlight.borderColor} />;
6666
} catch (error) {
6767
console.error(`Cell location fot found while rendering highlights at: ${(error as Error).message}`);
6868
return null;

src/lib/Components/RowRenderer.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { translateLocationIdxToLookupKey } from '../Model/CellMatrix';
23
import { GridRow, GridColumn, Borders, Location } from '../Model/InternalModel';
34
import { State } from '../Model/State';
45
import { CellRendererProps } from './CellRenderer';
@@ -26,11 +27,11 @@ const MappedColumns: React.FC<RowRendererProps> = ({ columns, row, cellRenderer,
2627
return (
2728
<>
2829
{columns.map(column => {
29-
const location: Location = { row, column };
30-
const range = state.cellMatrix.rangesToRender[state.cellMatrix.getLocationToFindRangeByIds(column.idx, row.idx)]?.range;
30+
const range = state.cellMatrix.rangesToRender[translateLocationIdxToLookupKey(column.idx, row.idx)]?.range;
3131
if (!range) {
3232
return null;
3333
}
34+
const location: Location = { row, column };
3435
return <CellRenderer
3536
key={row.idx + '-' + column.idx}
3637
borders={{

src/lib/Model/CellMatrix.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class CellMatrix<TStickyRanges extends StickyRanges = StickyRanges, TCell
6666
const column = this.columns[this.columnIndexLookup[columnId]];
6767
return this.validateLocation({ row, column });
6868
} catch (error) {
69-
throw new EvalError(`column: '${columnId}', row: '${rowId}'`)
69+
throw new RangeError(`column: '${columnId}', row: '${rowId}'`)
7070
}
7171
}
7272

@@ -76,10 +76,6 @@ export class CellMatrix<TStickyRanges extends StickyRanges = StickyRanges, TCell
7676
return this.getLocation(rowIdx, colIdx);
7777
}
7878

79-
getLocationToFindRangeByIds(idx: number, idy: number): string {
80-
return `${idx}, ${idy}`;
81-
}
82-
8379
validateRange(range: Range): Range {
8480
return this.getRange(this.validateLocation(range.first), this.validateLocation(range.last));
8581
}
@@ -89,3 +85,7 @@ export class CellMatrix<TStickyRanges extends StickyRanges = StickyRanges, TCell
8985
}
9086

9187
}
88+
89+
export function translateLocationIdxToLookupKey(idx: number, idy: number): string {
90+
return `${idx}, ${idy}`;
91+
}

src/lib/Model/CellMatrixBuilder.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CellMatrix, CellMatrixProps, StickyRanges, Span } from './CellMatrix';
1+
import { CellMatrix, CellMatrixProps, StickyRanges, translateLocationIdxToLookupKey } from './CellMatrix';
22
import { GridColumn, GridRow } from './InternalModel';
33
import { Range } from './Range';
44

@@ -76,22 +76,26 @@ export class CellMatrixBuilder implements ICellMatrixBuilder {
7676
const { rowspan = 0, colspan = 0 } = cell;
7777
const rows = rowspan ? this.cellMatrix.rows.slice(idy, idy + rowspan) : [this.cellMatrix.rows[idy]];
7878
const columns = colspan ? this.cellMatrix.columns.slice(idx, idx + colspan) : [this.cellMatrix.columns[idx]];
79-
const range = new Range(rows, columns)
80-
rangesToExclude = rangesToExclude.concat(this.getRangesToRender(range));
81-
this.cellMatrix.spanCellLookup[this.cellMatrix.getLocationToFindRangeByIds(idx, idy)] = {
82-
range: range
83-
}
79+
const range = new Range(rows, columns);
80+
rangesToExclude = [...rangesToExclude, ...this.getRangesToRender(range)];
81+
this.cellMatrix.spanCellLookup[translateLocationIdxToLookupKey(idx, idy)] = { range };
8482
})
8583
});
86-
const keys = rangesToExclude.map(range => `${range.columns[0].idx}, ${range.rows[0].idx}`);
87-
Object.keys(this.cellMatrix.spanCellLookup).filter(key => !keys.includes(key)).forEach(key => this.cellMatrix.rangesToRender[key] = this.cellMatrix.spanCellLookup[key]);
84+
85+
// TODO try to optimize by using only lookup
86+
const keys = rangesToExclude.map(range => translateLocationIdxToLookupKey(range.first.column.idx, range.first.row.idx));
87+
Object
88+
.keys(this.cellMatrix.spanCellLookup)
89+
.forEach(key => {
90+
if (!keys.includes(key)) {
91+
this.cellMatrix.rangesToRender[key] = this.cellMatrix.spanCellLookup[key]
92+
}
93+
});
8894
return this;
8995
}
9096

9197
getRangesToRender(range: Range): Range[] {
92-
const result = range.rows.flatMap(row => range.columns.map(column => new Range([row], [column])));
93-
result.shift();
94-
return result;
98+
return range.rows.flatMap(row => range.columns.map(column => new Range([row], [column]))).slice(1);
9599
}
96100

97101
fillSticky(edges: StickyEdges = { leftStickyColumns: 0, topStickyRows: 0 }): CellMatrixBuilder {

src/test/TestGrid.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ export const TestGrid: React.FC<TestGridProps> = (props) => {
9393
{ value: 'angular', label: 'Angular' }
9494
], currentValue: 'react', isDisabled: false
9595
}
96-
// case 9: // TODO allow user to pass non focusable cell (header cell) with arrows
97-
// return { type: 'header', text: `${ri} - ${ci}` }
9896
default:
9997
return { type: 'text', text: `${ri} - ${ci}`, validator: (text: string): boolean => true }
10098
}
@@ -367,7 +365,6 @@ export const TestGridOptionsSelect: React.FC<{ isPro?: boolean }> = ({ isPro })
367365
<option value='/enableResponsiveSticky'>Enable responsive sticky</option>
368366
<option value='/enableResponsiveStickyPinnedToBody'>Enable responsive sticky pinned to body</option>
369367
<option value='/enableSpannedCells'>Enable spanned cells</option>
370-
371368
{isPro && <>
372369
<option value='/enableColumnAndRowSelection'>Enable column and row selection</option>
373370
<option value='/enableColumnAndRowSelectionWithSticky'>Enable column and row selection with sticky</option>

0 commit comments

Comments
 (0)