Skip to content

Commit

Permalink
Remove ReactDataGrid dependency from addons bundle (#1272)
Browse files Browse the repository at this point in the history
* Fix some zIndex issues around frozen cells and editors

* Remove rdg dependency from addons package

* Fix object assign

* Adress PR comments

* Fix tests

* Fix lint error

* add ref to draggable header cell

* Adress PR comments
  • Loading branch information
malonecj authored and amanmahajan7 committed Sep 11, 2018
1 parent 5134c89 commit ab2da43
Show file tree
Hide file tree
Showing 81 changed files with 261 additions and 265 deletions.
5 changes: 4 additions & 1 deletion config/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ module.exports = function (config) {
loaders: webpackConfig.module.loaders
},
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx']
extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx'],
alias: {
common: path.resolve('packages/common/')
}
},
plugins: [
new RewirePlugin()
Expand Down
9 changes: 8 additions & 1 deletion config/webpack.common.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const webpack = require('webpack');
const argv = require('minimist')(process.argv.slice(2));
const RELEASE = argv.release;
const path = require('path');

function getPlugins() {
const nodeEnv = RELEASE ? '"production"' : '"development"';
Expand Down Expand Up @@ -36,7 +37,8 @@ const config = {
amd: 'react-dom'
},
'react/addons': 'React',
moment: 'moment'
moment: 'moment',
immutable: 'immutable'
},
module: {
loaders: [
Expand All @@ -45,6 +47,11 @@ const config = {
]
},
plugins: getPlugins(),
resolve: {
alias: {
common: path.resolve('packages/common/')
}
},
postLoaders: [
{
test: /\.js$/,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const React = require('react');
const ExcelColumn = require('../../PropTypeShapes/ExcelColumn');
import React from 'react';
import ExcelColumn from 'common/prop-shapes/ExcelColumn';
import PropTypes from 'prop-types';

class FilterableHeaderCell extends React.Component {
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions packages/common/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as CellNavigationMode from './CellNavigationMode';
import * as EventTypes from './EventTypes';
import keyMirror from 'keymirror';


const UpdateActions = keyMirror({
CELL_UPDATE: null,
COLUMN_FILL: null,
COPY_PASTE: null,
CELL_DRAG: null
});

const DragItemTypes = {
Column: 'column'
};

const CellExpand = {
DOWN_TRIANGLE: String.fromCharCode(9660),
RIGHT_TRIANGLE: String.fromCharCode(9654)
};


export {
CellNavigationMode,
EventTypes,
UpdateActions,
CellExpand,
DragItemTypes
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const React = require('react');
import PropTypes from 'prop-types';
require('../../../../themes/react-data-grid-checkbox.css');
require('../../../themes/react-data-grid-checkbox.css');

class CheckboxEditor extends React.Component {
static propTypes = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const React = require('react');
const ReactDOM = require('react-dom');
const ExcelColumn = require('../PropTypeShapes/ExcelColumn');
import ExcelColumn from 'common/prop-shapes/ExcelColumn';
import PropTypes from 'prop-types';

class EditorBase extends React.Component {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import joinClasses from 'classnames';
import SimpleTextEditor from './SimpleTextEditor';
import isFunction from'../utils/isFunction';
import { isKeyPrintable, isCtrlKeyHeldDown } from '../utils/keyboardUtils';
import zIndexes from '../constants/zIndexes';
import columnUtils from '../ColumnUtils';
require('../../../../themes/react-data-grid-core.css');
import {isFunction} from 'common/utils';
import { isKeyPrintable, isCtrlKeyHeldDown } from 'common/utils/keyboardUtils';
import zIndexes from 'common/constants/zIndexes';
require('../../../themes/react-data-grid-core.css');

const isFrozen = column => column.frozen === true || column.locked === true;

class EditorContainer extends React.Component {
static displayName = 'EditorContainer';
Expand Down Expand Up @@ -322,8 +323,8 @@ class EditorContainer extends React.Component {

render() {
const { left, top, width, height, column, scrollLeft } = this.props;
const editorLeft = columnUtils.isFrozen(column) ? left + scrollLeft : left;
const zIndex = columnUtils.isFrozen(column) ? zIndexes.FROZEN_EDITOR_CONTAINER : zIndexes.EDITOR_CONTAINER;
const editorLeft = isFrozen(column) ? left + scrollLeft : left;
const zIndex = isFrozen(column) ? zIndexes.FROZEN_EDITOR_CONTAINER : zIndexes.EDITOR_CONTAINER;
const style = { position: 'absolute', height, width, zIndex, transform: `translate(${editorLeft}px, ${top}px)` };
return (
<div style={style} className={this.getContainerClass()} onBlur={this.handleBlur} onKeyDown={this.onKeyDown} onContextMenu={this.handleRightClick}>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import ColumnMetrics from './ColumnMetrics';

export const shouldRowUpdate = (nextProps, currentProps) => {
return !(ColumnMetrics.sameColumns(currentProps.columns, nextProps.columns, ColumnMetrics.sameColumn)) ||
nextProps.row !== currentProps.row ||
currentProps.colOverscanStartIdx !== nextProps.colOverscanStartIdx ||
currentProps.colOverscanEndIdx !== nextProps.colOverscanEndIdx ||
currentProps.colVisibleStartIdx !== nextProps.colVisibleStartIdx ||
currentProps.colVisibleEndIdx !== nextProps.colVisibleEndIdx ||
currentProps.isSelected !== nextProps.isSelected ||
currentProps.isScrolling !== nextProps.isScrolling ||
nextProps.height !== currentProps.height ||
currentProps.isOver !== nextProps.isOver ||
currentProps.expandedRows !== nextProps.expandedRows ||
currentProps.canDrop !== nextProps.canDrop ||
currentProps.forceUpdate === true ||
currentProps.extraClasses !== nextProps.extraClasses;
};

export default shouldRowUpdate;
export default function shouldRowUpdate(nextProps, currentProps) {
return currentProps.columns !== nextProps.columns ||
nextProps.row !== currentProps.row ||
currentProps.colOverscanStartIdx !== nextProps.colOverscanStartIdx ||
currentProps.colOverscanEndIdx !== nextProps.colOverscanEndIdx ||
currentProps.colVisibleStartIdx !== nextProps.colVisibleStartIdx ||
currentProps.colVisibleEndIdx !== nextProps.colVisibleEndIdx ||
currentProps.isSelected !== nextProps.isSelected ||
currentProps.isScrolling !== nextProps.isScrolling ||
nextProps.height !== currentProps.height ||
currentProps.isOver !== nextProps.isOver ||
currentProps.expandedRows !== nextProps.expandedRows ||
currentProps.canDrop !== nextProps.canDrop ||
currentProps.forceUpdate === true ||
currentProps.extraClasses !== nextProps.extraClasses;
}

50 changes: 50 additions & 0 deletions packages/common/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { List, Iterable, Map } from 'immutable';

export const isColumnsImmutable = (columns) => {
return (typeof Immutable !== 'undefined' && (columns instanceof Immutable.List));
};

export const isEmptyArray = (obj) => {
return Array.isArray(obj) && obj.length === 0;
};

export const isFunction = (functionToCheck) => {
let getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
};

export const isEmptyObject = (obj) => {
return Object.keys(obj).length === 0 && obj.constructor === Object;
};

export const isImmutableCollection = objToVerify => {
return Iterable.isIterable(objToVerify);
};

export const getMixedTypeValueRetriever = (isImmutable) => {
let retObj = {};
const retriever = (item, key) => { return item[key]; };
const immutableRetriever = (immutable, key) => { return immutable.get(key); };

retObj.getValue = isImmutable ? immutableRetriever : retriever;

return retObj;
};

export const isImmutableMap = Map.isMap;

export const last = arrayOrList => {
if (arrayOrList == null) {
throw new Error('arrayOrCollection is null');
}

if (List.isList(arrayOrList)) {
return arrayOrList.last();
}

if (Array.isArray(arrayOrList)) {
return arrayOrList[arrayOrList.length - 1];
}

throw new Error('Cant get last of: ' + typeof(arrayOrList));
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import 'react-select/dist/react-select.css';
import React from 'react';
import PropTypes from 'prop-types';
import Select from 'react-select';
import { utils, shapes } from 'react-data-grid';
const { isEmptyArray } = utils;
const { ExcelColumn } = shapes;
import {isEmptyArray} from 'common/utils';
import ExcelColumn from 'common/prop-shapes/ExcelColumn';

class AutoCompleteFilter extends React.Component {
constructor(props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { shapes } from 'react-data-grid';
const { ExcelColumn } = shapes;
import ExcelColumn from 'common/prop-shapes/ExcelColumn';

const RuleType = {
Number: 1,
Range: 2,
Expand Down
3 changes: 1 addition & 2 deletions packages/react-data-grid-addons/src/data/RowFilterer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { utils } from 'react-data-grid';
const { getMixedTypeValueRetriever, isImmutableCollection } = utils;
import {isImmutableCollection, getMixedTypeValueRetriever} from 'common/utils';

const filterRows = (filters, rows = []) => {
return rows.filter(r => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-data-grid-addons/src/data/RowGrouper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { utils } from 'react-data-grid';
import {isImmutableCollection} from 'common/utils';
import Resolver from './RowGrouperResolver';
const { isImmutableCollection } = utils;


class RowGrouper {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {List} from 'immutable';
import groupBy from 'lodash/groupBy';
import { utils } from 'react-data-grid';
const { getMixedTypeValueRetriever, isImmutableMap } = utils;
import { isImmutableMap, getMixedTypeValueRetriever} from 'common/utils';

export default class RowGrouperResolver {

Expand Down
3 changes: 1 addition & 2 deletions packages/react-data-grid-addons/src/data/RowSorter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { utils } from 'react-data-grid';
const { getMixedTypeValueRetriever, isImmutableCollection } = utils;
import {getMixedTypeValueRetriever, isImmutableCollection} from 'common/utils';

const comparer = (a, b) => {
if (a > b) {
Expand Down
3 changes: 1 addition & 2 deletions packages/react-data-grid-addons/src/data/Selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createSelector } from 'reselect';
import { utils } from 'react-data-grid';
const { isEmptyArray, isEmptyObject } = utils;
import {isEmptyObject, isEmptyArray} from 'common/utils';
const groupRows = require('./RowGrouper');
const filterRows = require('./RowFilterer');
const sortRows = require('./RowSorter');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import React from 'react';
import PropTypes from 'prop-types';
import { DragSource, DropTarget } from 'react-dnd';
import { HeaderCell } from 'react-data-grid';

class HeaderCell extends React.Component {
static propTypes = {
column: PropTypes.isRequired,
height: PropTypes.number.isRequired,
className: PropTypes.string
};

getStyle() {
return {
width: this.props.column.width,
left: this.props.column.left,
display: 'inline-block',
position: 'absolute',
height: this.props.height,
margin: 0,
textOverflow: 'ellipsis',
whiteSpace: 'nowrap'
};
}

setScrollLeft = (scrollLeft) => {
if (this.node) {
node.style.webkitTransform = `translate3d(${scrollLeft}px, 0px, 0px)`;
node.style.transform = `translate3d(${scrollLeft}px, 0px, 0px)`;
}
};

render() {
return (
<div ref={node => this.node = node} className="react-grid-HeaderCell" style={this.getStyle()}>
{this.props.column.name}
</div>
);
}
}

class DraggableHeaderCell extends React.Component {
render() {
Expand All @@ -26,7 +61,7 @@ class DraggableHeaderCell extends React.Component {
style={{ width: 0, cursor: 'move', opacity }}
className={isOver && canDrop ? 'rdg-can-drop' : ''}
>
<HeaderCell {...this.props} />
<HeaderCell {...this.props}/>
</div>
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { shallow } from 'enzyme';

import DraggableContainer from '../DraggableContainer';
import DraggableHeaderCell from '../DraggableHeaderCell';
import { HeaderCell } from 'react-data-grid';

const HeaderCell = () => <div/>;

describe('<DraggableHeaderCell />', () => {
it('should render grid HeaderCell wrapper with cursor: move ', () => {
const wrapper = shallow(
<DraggableContainer>
<DraggableHeaderCell />
<DraggableHeaderCell renderHeaderCell={() => <HeaderCell/>}/>
</DraggableContainer>
);
expect(wrapper.find(HeaderCell));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, {Component} from 'react';
import html5DragDropContext from '../shared/html5DragDropContext';
import DraggableHeaderCell from './DraggableHeaderCell';
import RowDragLayer from './RowDragLayer';
import { utils } from 'react-data-grid';
const { isColumnsImmutable } = utils;
import {isColumnsImmutable} from 'common/utils';
import PropTypes from 'prop-types';

class DraggableContainer extends Component {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DragSource } from 'react-dnd';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { _constants, HeaderCell } from 'react-data-grid';
const { DragItemTypes } = _constants;
import { DragItemTypes } from 'common/constants';

class DraggableHeaderCell extends Component {

Expand All @@ -16,24 +15,26 @@ class DraggableHeaderCell extends Component {
}

setScrollLeft(scrollLeft) {
let node = ReactDOM.findDOMNode(this);
node.style.webkitTransform = `translate3d(${scrollLeft}px, 0px, 0px)`;
node.style.transform = `translate3d(${scrollLeft}px, 0px, 0px)`;
if (this.node) {
node.style.webkitTransform = `translate3d(${scrollLeft}px, 0px, 0px)`;
node.style.transform = `translate3d(${scrollLeft}px, 0px, 0px)`;
}
}

render() {
const { connectDragSource, isDragging} = this.props;
if (isDragging) {
return null;
}
return connectDragSource(<div style={{cursor: 'move'}}><HeaderCell {...this.props}/></div>);
return connectDragSource(<div ref={node => this.node = node} style={{cursor: 'move'}}>{this.props.renderHeaderCell(this.props)}</div>);
}
}

DraggableHeaderCell.propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDragPreview: PropTypes.func.isRequired,
isDragging: PropTypes.bool.isRequired
isDragging: PropTypes.bool.isRequired,
renderHeaderCell: PropTypes.func.isRequired
};

function collect(connect, monitor) {
Expand Down
Loading

0 comments on commit ab2da43

Please sign in to comment.