Skip to content

Commit

Permalink
Clean up getRowKey
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Oct 20, 2017
1 parent ee088ab commit 4a87242
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/BaseTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class BaseTable extends React.Component {
const { table } = this.context;
const {
columnManager,
getRowKey,
onExpanded,
} = table;
const {
Expand All @@ -45,7 +44,7 @@ class BaseTable extends React.Component {
onRowMouseEnter,
onRowMouseLeave,
} = table.props;
const { columns, fixed, expander } = this.props;
const { getRowKey, columns, fixed, expander } = this.props;

let rows = [];

Expand Down
3 changes: 2 additions & 1 deletion src/BodyTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BaseTable from './BaseTable';

export default function BodyTable(props, { table }) {
const { prefixCls, scroll } = table.props;
const { columns, fixed, tableClassName, handleBodyScroll, expander } = props;
const { columns, fixed, tableClassName, getRowKey, handleBodyScroll, expander } = props;
const { saveRef } = table;
let { useFixedHeader } = table.props;
const bodyStyle = { ...table.props.bodyStyle };
Expand Down Expand Up @@ -46,6 +46,7 @@ export default function BodyTable(props, { table }) {
fixed={fixed}
columns={columns}
expander={expander}
getRowKey={getRowKey}
/>
);

Expand Down
12 changes: 6 additions & 6 deletions src/ExpandableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ class ExpandableRow extends React.Component {
return !this.expandIconAsCell && !expandRowByClick && columnIndex === this.expandIconColumnIndex;
}

handleExpandChange = (record, index, event) => {
const { onExpandedChange, expanded } = this.props;
handleExpandChange = (record, event) => {
const { onExpandedChange, expanded, rowKey } = this.props;
if (this.expandable) {
onExpandedChange(!expanded, record, event, index);
onExpandedChange(!expanded, record, event, rowKey);
}
}

handleDestroy() {
const { onExpandedChange, index, record } = this.props;
const { onExpandedChange, rowKey, record } = this.props;
if (this.expandable) {
onExpandedChange(false, record, null, index);
onExpandedChange(false, record, null, rowKey);
}
}

handleRowClick = (record, index, event) => {
const { expandRowByClick, onRowClick } = this.props;
if (expandRowByClick) {
this.handleExpandChange(record, index, event);
this.handleExpandChange(record, event);
}
onRowClick(record, index, event);
}
Expand Down
43 changes: 12 additions & 31 deletions src/ExpandableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,51 +72,32 @@ class ExpandableTable extends React.Component {
}
}

handleExpandChange = (expanded, record, event, index) => {
const { expandRowByClick, getRowKey, onExpand } = this.props;

handleExpandChange = (expanded, record, event, rowKey) => {
if (event) {
event.preventDefault();
event.stopPropagation();
}

const info = this.findExpandedRow(record);
const expandedRows = this.getExpandedRows().concat();
const { onExpandedRowsChange, onExpand } = this.props;
const { expandedRowKeys } = this.store.getState();

if (typeof info !== 'undefined' && !expanded) {
// row was unmounted
const rowKey = this.props.getRowKey(record, index);
let expandedRowIndex = expandedRows.indexOf(rowKey);
if (expanded) {
// row was expaned
expandedRowKeys.push(rowKey);
} else {
// row was collapse
let expandedRowIndex = expandedRowKeys.indexOf(rowKey);
if (expandedRowIndex !== -1) {
expandedRows.splice(expandedRowIndex, 1);
expandedRowKeys.splice(expandedRowIndex, 1);
}
this.updateExpandedRows(expandedRows);
} else if (!info && expanded) {
// row was collapse
expandedRows.push(getRowKey(record, index));
this.updateExpandedRows(expandedRows);
}
onExpand(expanded, record);
}

updateExpandedRows(expandedRowKeys) {
if (!this.props.expandedRowKeys) {
this.store.setState({ expandedRowKeys });
}
this.props.onExpandedRowsChange(expandedRowKeys);
}

findExpandedRow(record, index) {
const rows = this.getExpandedRows().filter(i => i === this.props.getRowKey(record, index));
return rows[0];
}

getExpandedRows() {
return this.store.getState().expandedRowKeys;
}

isRowExpanded(record, index) {
return typeof this.findExpandedRow(record, index) !== 'undefined';
onExpandedRowsChange(expandedRowKeys);
onExpand(expanded, record);
}

renderExpandIndentCell = (rows, fixed) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export default class Table extends React.Component {
props: this.props,
columnManager: this.columnManager,
saveRef: this.saveRef,
getRowKey: this.getRowKey,
},
};
}
Expand Down Expand Up @@ -342,6 +341,7 @@ export default class Table extends React.Component {
columns={columns}
fixed={fixed}
tableClassName={tableClassName}
getRowKey={this.getRowKey}
handleBodyScroll={this.handleBodyScroll}
expander={this.expander}
/>
Expand Down
2 changes: 1 addition & 1 deletion tests/Table.expandRow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('Table.expand', () => {
expect(onExpandedRowsChange).toBeCalledWith([1]);
});

it('expand row by click', () => {
fit('expand row by click', () => {
const wrapper = mount(createTable({ expandedRowRender }));
wrapper.find('ExpandIcon').first().simulate('click');
expect(mountToJson(wrapper)).toMatchSnapshot();
Expand Down
53 changes: 43 additions & 10 deletions tests/__snapshots__/Table.expandRow.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5021,7 +5021,11 @@ exports[`Table.expand expand row by click 1`] = `
]
}
defaultExpandAllRows={false}
defaultExpandedRowKeys={Array []}
defaultExpandedRowKeys={
Array [
0,
]
}
emptyText={[Function]}
expandIconAsCell={false}
expandIconColumnIndex={0}
Expand Down Expand Up @@ -5151,7 +5155,9 @@ exports[`Table.expand expand row by click 1`] = `
},
],
"defaultExpandAllRows": false,
"defaultExpandedRowKeys": Array [],
"defaultExpandedRowKeys": Array [
0,
],
"emptyText": [Function],
"expandIconAsCell": false,
"expandIconColumnIndex": 0,
Expand Down Expand Up @@ -5281,7 +5287,9 @@ exports[`Table.expand expand row by click 1`] = `
},
],
"defaultExpandAllRows": false,
"defaultExpandedRowKeys": Array [],
"defaultExpandedRowKeys": Array [
0,
],
"emptyText": [Function],
"expandIconAsCell": false,
"expandIconColumnIndex": 0,
Expand Down Expand Up @@ -5316,6 +5324,7 @@ exports[`Table.expand expand row by click 1`] = `
}
}
fixed={undefined}
getRowKey={[Function]}
handleBodyScroll={[Function]}
key="body"
tableClassName=""
Expand Down Expand Up @@ -5417,7 +5426,9 @@ exports[`Table.expand expand row by click 1`] = `
},
],
"defaultExpandAllRows": false,
"defaultExpandedRowKeys": Array [],
"defaultExpandedRowKeys": Array [
0,
],
"emptyText": [Function],
"expandIconAsCell": false,
"expandIconColumnIndex": 0,
Expand Down Expand Up @@ -5452,6 +5463,7 @@ exports[`Table.expand expand row by click 1`] = `
}
}
fixed={undefined}
getRowKey={[Function]}
hasBody={true}
hasHead={true}
tableClassName=""
Expand Down Expand Up @@ -5547,7 +5559,9 @@ exports[`Table.expand expand row by click 1`] = `
},
],
"defaultExpandAllRows": false,
"defaultExpandedRowKeys": Array [],
"defaultExpandedRowKeys": Array [
0,
],
"emptyText": [Function],
"expandIconAsCell": false,
"expandIconColumnIndex": 0,
Expand Down Expand Up @@ -5582,6 +5596,7 @@ exports[`Table.expand expand row by click 1`] = `
}
}
fixed={undefined}
getRowKey={[Function]}
hasBody={true}
hasHead={true}
store={
Expand Down Expand Up @@ -5727,7 +5742,9 @@ exports[`Table.expand expand row by click 1`] = `
},
],
"defaultExpandAllRows": false,
"defaultExpandedRowKeys": Array [],
"defaultExpandedRowKeys": Array [
0,
],
"emptyText": [Function],
"expandIconAsCell": false,
"expandIconColumnIndex": 0,
Expand Down Expand Up @@ -5973,7 +5990,11 @@ exports[`Table.expand expand row by click 1`] = `
]
}
defaultExpandAllRows={false}
defaultExpandedRowKeys={Array []}
defaultExpandedRowKeys={
Array [
0,
]
}
emptyText={[Function]}
expandIconAsCell={false}
expandIconColumnIndex={0}
Expand Down Expand Up @@ -6094,7 +6115,11 @@ exports[`Table.expand expand row by click 1`] = `
]
}
defaultExpandAllRows={false}
defaultExpandedRowKeys={Array []}
defaultExpandedRowKeys={
Array [
0,
]
}
emptyText={[Function]}
expandIconAsCell={false}
expandIconColumnIndex={0}
Expand Down Expand Up @@ -6571,7 +6596,11 @@ exports[`Table.expand expand row by click 1`] = `
]
}
defaultExpandAllRows={false}
defaultExpandedRowKeys={Array []}
defaultExpandedRowKeys={
Array [
0,
]
}
emptyText={[Function]}
expandIconAsCell={false}
expandIconColumnIndex={0}
Expand Down Expand Up @@ -6692,7 +6721,11 @@ exports[`Table.expand expand row by click 1`] = `
]
}
defaultExpandAllRows={false}
defaultExpandedRowKeys={Array []}
defaultExpandedRowKeys={
Array [
0,
]
}
emptyText={[Function]}
expandIconAsCell={false}
expandIconColumnIndex={0}
Expand Down

0 comments on commit 4a87242

Please sign in to comment.