Skip to content

Commit

Permalink
Allow overriding elements
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Oct 24, 2017
1 parent b66ec99 commit 715ff05
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 40 deletions.
File renamed without changes.
187 changes: 187 additions & 0 deletions examples/react-dnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/* eslint-disable no-unused-expressions,new-cap */
import React from 'react';
import ReactDOM from 'react-dom';
import { injectGlobal } from 'styled-components';
import update from 'immutability-helper';
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import Table from 'rc-table';
import 'rc-table/assets/index.less';

injectGlobal`
tr.drop-over-downward td {
border-bottom: 2px dashed red;
}
tr.drop-over-upward td {
border-top: 2px dashed red;
}
`;

function dragDirection(
dragIndex,
hoverIndex,
initialClientOffset,
clientOffset,
sourceClientOffset,
) {
const hoverMiddleY = (initialClientOffset.y - sourceClientOffset.y) / 2;
const hoverClientY = clientOffset.y - sourceClientOffset.y;
if (dragIndex < hoverIndex && hoverClientY > hoverMiddleY) {
return 'downward';
}
if (dragIndex > hoverIndex && hoverClientY < hoverMiddleY) {
return 'upward';
}
}

let BodyRow = (props) => {
const {
isOver,
connectDragSource,
connectDropTarget,
moveRow,
dragRow,
clientOffset,
sourceClientOffset,
initialClientOffset,
...restProps,
} = props;
const style = { cursor: 'move' };

let className = restProps.className;
if (isOver && initialClientOffset) {
const direction = dragDirection(
dragRow.index,
restProps.index,
initialClientOffset,
clientOffset,
sourceClientOffset
);
if (direction === 'downward') {
className += ' drop-over-downward';
}
if (direction === 'upward') {
className += ' drop-over-upward';
}
}

return connectDragSource(
connectDropTarget(
<tr
{...restProps}
className={className}
style={style}
/>
)
);
};

const rowSource = {
beginDrag(props) {
return {
index: props.index,
};
},
};

const rowTarget = {
drop(props, monitor) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;

// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}

// Time to actually perform the action
props.moveRow(dragIndex, hoverIndex);

// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;
},
};

BodyRow = DropTarget('row', rowTarget, (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
sourceClientOffset: monitor.getSourceClientOffset(),
}))(
DragSource('row', rowSource, (connect, monitor) => ({
connectDragSource: connect.dragSource(),
dragRow: monitor.getItem(),
clientOffset: monitor.getClientOffset(),
initialClientOffset: monitor.getInitialClientOffset(),
}))(BodyRow)
);

const columns = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
dataIndex: '',
key: 'd',
render() {
return <a href="#">Operations</a>;
},
},
];

class Demo extends React.Component {
state = {
data: [
{ a: '123', key: '1' },
{ a: 'cdd', b: 'edd', key: '2' },
{ a: '1333', c: 'eee', d: 2, key: '3' },
],
}

components = {
body: {
row: BodyRow,
},
}

moveRow = (dragIndex, hoverIndex) => {
const { data } = this.state;
const dragRow = data[dragIndex];

this.setState(
update(this.state, {
data: {
$splice: [[dragIndex, 1], [hoverIndex, 0, dragRow]],
},
}),
);
}

render() {
return (
<Table
columns={columns}
data={this.state.data}
components={this.components}
onRow={(record, index) => ({
index,
moveRow: this.moveRow,
})}
/>
);
}
}

Demo = DragDropContext(HTML5Backend)(Demo);

ReactDOM.render(
<div>
<h2>Custom Component</h2>
<Demo />
</div>,
document.getElementById('__react-content')
);
Empty file added examples/styled-components.html
Empty file.
21 changes: 6 additions & 15 deletions examples/custom-component.js → examples/styled-components.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';
import Table from 'rc-table';
import 'rc-table/assets/index.less';

Expand All @@ -23,25 +24,15 @@ const data = [
{ a: '1333', c: 'eee', d: 2, key: '3' },
];

const MyTable = props => <table name="my-table" {...props} />;
const HeaderWrapper = props => <thead name="my-header-wrapper" {...props} />;
const HeaderRow = props => <tr name="my-header-row" {...props} />;
const HeaderCell = props => <th name="my-header-cell" {...props} />;
const BodyWrapper = props => <tbody name="my-body-wrapper" {...props} />;
const BodyRow = props => <tr name="my-body-row" {...props} />;
const BodyCell = props => <td name="my-body-cell" {...props} />;
const BodyRow = styled.tr`
&:hover {
background: palevioletred !important;
}
`;

const components = {
table: MyTable,
header: {
wrapper: HeaderWrapper,
row: HeaderRow,
cell: HeaderCell,
},
body: {
wrapper: BodyWrapper,
row: BodyRow,
cell: BodyCell,
},
};

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"babel-runtime": "6.x",
"component-classes": "^1.2.6",
"lodash.get": "^4.4.2",
"lodash.merge": "^4.6.0",
"mini-store": "^1.0.2",
"prop-types": "^15.5.8",
"rc-util": "^4.0.4",
Expand All @@ -82,15 +83,20 @@
"enzyme": "^3.1.0",
"enzyme-adapter-react-16": "^1.0.1",
"enzyme-to-json": "^3.1.2",
"immutability-helper": "^2.4.0",
"jest": "^21.2.1",
"pre-commit": "1.x",
"rc-animate": "^2.3.0",
"rc-dropdown": "~2.0.1",
"rc-menu": "^5.0.11",
"rc-tools": "7.x",
"react": "^16.0.0",
"react-dnd": "^2.5.4",
"react-dnd-html5-backend": "^2.5.4",
"react-dom": "^16.0.0",
"react-test-renderer": "^16.0.0"
"react-test-renderer": "^16.0.0",
"react-virtualized": "^9.12.0",
"styled-components": "^2.2.1"
},
"pre-commit": [
"lint"
Expand Down
14 changes: 8 additions & 6 deletions src/BaseTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BaseTable extends React.Component {

renderRows = (renderData, indent, ancestorKeys = []) => {
const { table } = this.context;
const { columnManager } = table;
const { columnManager, components } = table;
const {
prefixCls,
childrenColumnName,
Expand All @@ -44,7 +44,7 @@ class BaseTable extends React.Component {
onRowContextMenu,
onRowMouseEnter,
onRowMouseLeave,
components,
onRow,
} = table.props;
const { getRowKey, fixed, expander } = this.props;

Expand Down Expand Up @@ -96,6 +96,7 @@ class BaseTable extends React.Component {
prefixCls={rowPrefixCls}
childrenColumnName={childrenColumnName}
columns={leafColumns}
onRow={onRow}
onRowDoubleClick={onRowDoubleClick}
onRowContextMenu={onRowContextMenu}
onRowMouseEnter={onRowMouseEnter}
Expand Down Expand Up @@ -131,7 +132,9 @@ class BaseTable extends React.Component {
}

render() {
const { prefixCls, scroll, data, getBodyWrapper, components } = this.context.table.props;
const { table } = this.context;
const { components } = table;
const { prefixCls, scroll, data, getBodyWrapper } = table.props;
const { expander, tableClassName, hasHead, hasBody, fixed, columns } = this.props;
const tableStyle = {};

Expand All @@ -144,9 +147,8 @@ class BaseTable extends React.Component {
}
}

const Table = hasBody ? (components.table || 'table') : 'table';
const BodyWrapper = components.body && components.body.wrapper || 'tbody';

const Table = hasBody ? components.table : 'table';
const BodyWrapper = components.body.wrapper;

return (
<Table className={tableClassName} style={tableStyle} key="table">
Expand Down
7 changes: 7 additions & 0 deletions src/ExpandableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class ExpandableTable extends React.Component {
}

const rowKey = `${ancestorKeys[0]}-extra-row`;
const components = {
body: {
row: 'tr',
cell: 'td',
},
};

return (
<TableRow
Expand All @@ -157,6 +163,7 @@ class ExpandableTable extends React.Component {
prefixCls={`${prefixCls}-expanded-row`}
indent={1}
fixed={fixed}
components={components}
/>
);
}
Expand Down
18 changes: 17 additions & 1 deletion src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { debounce, warningOnce } from './utils';
import shallowequal from 'shallowequal';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import { Provider, create } from 'mini-store';
import merge from 'lodash.merge';
import ColumnManager from './ColumnManager';
import classes from 'component-classes';
import HeadTable from './HeadTable';
Expand All @@ -20,6 +21,7 @@ export default class Table extends React.Component {
style: PropTypes.object,
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
rowClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
onRow: PropTypes.func,
onRowClick: PropTypes.func,
onRowDoubleClick: PropTypes.func,
onRowContextMenu: PropTypes.func,
Expand Down Expand Up @@ -51,13 +53,15 @@ export default class Table extends React.Component {

static childContextTypes = {
table: PropTypes.any,
components: PropTypes.any,
}

static defaultProps = {
data: [],
useFixedHeader: false,
rowKey: 'key',
rowClassName: () => '',
onRow() {},
onRowClick() {},
onRowDoubleClick() {},
onRowContextMenu() {},
Expand All @@ -71,7 +75,6 @@ export default class Table extends React.Component {
rowRef: () => null,
getBodyWrapper: body => body,
emptyText: () => 'No Data',
components: {},
}

constructor(props) {
Expand All @@ -96,6 +99,19 @@ export default class Table extends React.Component {
props: this.props,
columnManager: this.columnManager,
saveRef: this.saveRef,
components: merge({
table: 'table',
header: {
wrapper: 'thead',
row: 'tr',
cell: 'th',
},
body: {
wrapper: 'tbody',
row: 'tr',
cell: 'td',
},
}, this.props.components),
},
};
}
Expand Down
Loading

0 comments on commit 715ff05

Please sign in to comment.