Skip to content

Commit

Permalink
Promote editor2 (#2149)
Browse files Browse the repository at this point in the history
* editor -> _old_editor

* fix types

* editor2 -> editor

* rename file

* Add wrapOldEditor

* mark onRowsUpdate as deprecated

* working edits

* simplify

* changelog

* _old_editor -> oldEditor

* fix lint

* start nuking the old editor code

* progress

* accidentally removed this line

* Use SimpleTextEditor

* autofocus SimpleTextEditor

* rename class, remove css prefixes

* tweak editor styles

* rm TextEditor

* -Simple

* Update AllFeatures/DropDownEditor

* rm EditorPortal

* rm src/editors/index.ts

* rm SimpleCellFormatter

* update useClickOutside, rm a couple extraneous exports

* fix type issue

* inline wrapRefs

* tweaks

* remove `deprecated` for onRowsUpdate

* TextEditor: autofocus+select

* rm 1 type assertion

* update canEdit

* update onRowsUpdate doc
  • Loading branch information
nstepien authored Oct 29, 2020
1 parent e9df859 commit 509963f
Show file tree
Hide file tree
Showing 35 changed files with 259 additions and 945 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
- `column.minWidth`
- `column.maxWidth`
- `column.headerCellClass`
- `column.editor2`
- `column.editor`
- New API
- `column.editorOptions`
- More info in [#2102](https://github.com/adazzle/react-data-grid/pull/2102)
- `column.groupFormatter`
Expand Down
10 changes: 2 additions & 8 deletions src/Cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mount } from 'enzyme';

import Cell from './Cell';
import helpers, { Row } from './test/GridPropHelpers';
import { SimpleCellFormatter } from './formatters';
import { ValueFormatter } from './formatters';
import { CalculatedColumn, CellRendererProps, FormatterProps } from './types';
import EventBus from './EventBus';

Expand All @@ -15,7 +15,7 @@ const defaultColumn: CalculatedColumn<Row> = {
left: 0,
resizable: false,
sortable: false,
formatter: SimpleCellFormatter
formatter: ValueFormatter
};

const testProps: CellRendererProps<Row> = {
Expand All @@ -34,12 +34,6 @@ const renderComponent = (extraProps?: PropsWithChildren<Partial<CellRendererProp
};

describe('Cell', () => {
it('should render a SimpleCellFormatter with value', () => {
const wrapper = renderComponent();
const formatter = wrapper.find(SimpleCellFormatter);
expect(formatter.props().row[defaultColumn.key]).toStrictEqual('Wicklow');
});

it('should render a custom formatter when specified on column', () => {
const CustomFormatter = (props: FormatterProps) => <div>{props.row[props.column.key]}</div>;

Expand Down
23 changes: 10 additions & 13 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ export interface DataGridProps<R, SR = unknown> extends SharedDivProps {
/**
* Callback called whenever row data is updated
* When editing is enabled, this callback will be called for the following scenarios
* 1. Using the supplied editor of the column. The default editor is the SimpleTextEditor.
* 2. Copy/pasting the value from one cell to another <kbd>CTRL</kbd>+<kbd>C</kbd>, <kbd>CTRL</kbd>+<kbd>V</kbd>
* 3. Update multiple cells by dragging the fill handle of a cell up or down to a destination cell.
* 4. Update all cells under a given cell by double clicking the cell's fill handle.
* 1. Copy/pasting the value from one cell to another <kbd>CTRL</kbd>+<kbd>C</kbd>, <kbd>CTRL</kbd>+<kbd>V</kbd>
* 2. Update multiple cells by dragging the fill handle of a cell up or down to a destination cell.
* 3. Update all cells under a given cell by double clicking the cell's fill handle.
*/
onRowsUpdate?: <E extends RowsUpdateEvent>(event: E) => void;
onRowsChange?: (rows: R[]) => void;
Expand Down Expand Up @@ -481,9 +480,9 @@ function DataGrid<R, SR>({
closeEditor();
}

function commitEditor2Changes() {
function commitEditorChanges() {
if (
columns[selectedPosition.idx]?.editor2 === undefined
columns[selectedPosition.idx]?.editor === undefined
|| selectedPosition.mode === 'SELECT'
|| selectedPosition.row === selectedPosition.originalRow) {
return;
Expand Down Expand Up @@ -535,7 +534,7 @@ function DataGrid<R, SR>({
if (selectedPosition.mode === 'EDIT') {
if (key === 'Enter') {
// Custom editors can listen for the event and stop propagation to prevent commit
commitEditor2Changes();
commitEditorChanges();
closeEditor();
}
return;
Expand Down Expand Up @@ -626,7 +625,7 @@ function DataGrid<R, SR>({

function handleOnClose(commitChanges?: boolean) {
if (commitChanges) {
commitEditor2Changes();
commitEditorChanges();
}
closeEditor();
}
Expand All @@ -645,7 +644,7 @@ function DataGrid<R, SR>({

function selectCell(position: Position, enableEditor = false): void {
if (!isCellWithinBounds(position)) return;
commitEditor2Changes();
commitEditorChanges();

if (enableEditor && isCellEditable(position)) {
const row = rows[position.rowIdx] as R;
Expand Down Expand Up @@ -802,7 +801,7 @@ function DataGrid<R, SR>({
onCommit: handleCommit,
onCommitCancel: closeEditor
},
editor2Props: {
editorProps: {
rowHeight,
row: selectedPosition.row,
onRowChange: handleRowChange,
Expand Down Expand Up @@ -963,6 +962,4 @@ function DataGrid<R, SR>({
);
}

export default forwardRef(
DataGrid as React.ForwardRefRenderFunction<DataGridHandle>
) as <R, SR = unknown>(props: DataGridProps<R, SR> & React.RefAttributes<DataGridHandle>) => JSX.Element;
export default forwardRef(DataGrid) as <R, SR = unknown>(props: DataGridProps<R, SR> & React.RefAttributes<DataGridHandle>) => JSX.Element;
56 changes: 15 additions & 41 deletions src/EditCell.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import React, { forwardRef, useState, useCallback } from 'react';
import React, { useState, useCallback } from 'react';
import clsx from 'clsx';

import { EditorContainer, EditorContainer2, EditorPortal } from './editors';
import { CellRendererProps, SharedEditorContainerProps, SharedEditor2Props } from './types';
import { useCombinedRefs } from './hooks';
import EditorContainer from './editors/EditorContainer';
import { CellRendererProps, SharedEditorContainerProps, SharedEditorProps } from './types';

type SharedCellRendererProps<R, SR> = Pick<CellRendererProps<R, SR>,
| 'rowIdx'
| 'row'
| 'column'
| 'rowIdx'
| 'row'
| 'column'
>;

interface EditCellRendererProps<R, SR> extends SharedCellRendererProps<R, SR>, Omit<React.HTMLAttributes<HTMLDivElement>, 'style' | 'children'> {
editorPortalTarget: Element;
editorContainerProps: SharedEditorContainerProps;
editor2Props: SharedEditor2Props<R>;
editorProps: SharedEditorProps<R>;
}

function EditCell<R, SR>({
export default function EditCell<R, SR>({
className,
column,
row,
rowIdx,
editorPortalTarget,
editorContainerProps,
editor2Props,
editorProps,
onKeyDown,
...props
}: EditCellRendererProps<R, SR>, ref: React.Ref<HTMLDivElement>) {
}: EditCellRendererProps<R, SR>) {
const [dimensions, setDimensions] = useState<{ left: number; top: number } | null>(null);

const cellRef = useCallback(node => {
Expand Down Expand Up @@ -57,47 +56,24 @@ function EditCell<R, SR>({
const gridLeft = left + docLeft;
const gridTop = top + docTop;

if (column.editor2 !== undefined) {
return (
<EditorContainer2
{...editor2Props}
editorPortalTarget={editorPortalTarget}
rowIdx={rowIdx}
column={column}
left={gridLeft}
top={gridTop}
/>
);
}

const editor = (
<EditorContainer<R, SR>
{...editorContainerProps}
return (
<EditorContainer
{...editorProps}
editorPortalTarget={editorPortalTarget}
rowIdx={rowIdx}
row={row}
column={column}
left={gridLeft}
top={gridTop}
/>
);

if (column.editorOptions?.createPortal !== false) {
return (
<EditorPortal target={editorPortalTarget}>
{editor}
</EditorPortal>
);
}

return editor;
}

return (
<div
role="gridcell"
aria-colindex={column.idx + 1} // aria-colindex is 1-based
aria-selected
ref={useCombinedRefs(cellRef, ref)}
ref={cellRef}
className={className}
style={{
width: column.width,
Expand All @@ -110,5 +86,3 @@ function EditCell<R, SR>({
</div>
);
}

export default forwardRef(EditCell) as <R, SR = unknown>(props: EditCellRendererProps<R, SR> & React.RefAttributes<HTMLDivElement>) => JSX.Element;
2 changes: 1 addition & 1 deletion src/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function Row<R, SR = unknown>({
onKeyDown={selectedCellProps.onKeyDown}
editorPortalTarget={selectedCellProps.editorPortalTarget}
editorContainerProps={selectedCellProps.editorContainerProps}
editor2Props={selectedCellProps.editor2Props}
editorProps={selectedCellProps.editorProps}
/>
);
}
Expand Down
34 changes: 0 additions & 34 deletions src/editors/Editor2Container.tsx

This file was deleted.

Loading

0 comments on commit 509963f

Please sign in to comment.