Skip to content

Commit fc37bf5

Browse files
committed
refine #71
1 parent 94d21fe commit fc37bf5

File tree

5 files changed

+82
-47
lines changed

5 files changed

+82
-47
lines changed

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ storiesOf('Cell Editing', module)
9494
.add('Blur to Save Cell', () => <BlurToSaveTable />)
9595
.add('Row Level Editable', () => <RowLevelEditableTable />)
9696
.add('Column Level Editable', () => <ColumnLevelEditableTable />)
97+
.add('Cell Level Editable', () => <CellLevelEditable />)
9798
.add('Rich Hook Functions', () => <CellEditHooks />)
98-
.add('Validation', () => <CellEditValidator />)
99-
.add('Cell Level Editable', () => <CellLevelEditable />);
99+
.add('Validation', () => <CellEditValidator />);

packages/react-bootstrap-table2/src/cell.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ class Cell extends Component {
6060
? classes(content, row, rowIndex, columnIndex)
6161
: classes;
6262

63-
const setEditMode = () => {
64-
if (editMode === Const.CLICK_TO_CELL_EDIT) {
65-
cellAttrs.onClick = this.handleEditingCell;
66-
} else {
67-
cellAttrs.onDoubleClick = this.handleEditingCell;
68-
}
69-
};
70-
7163
if (style) {
7264
cellStyle = _.isFunction(style) ? style(content, row, rowIndex, columnIndex) : style;
7365
}
@@ -94,7 +86,11 @@ class Cell extends Component {
9486

9587
if (!_.isEmptyObject(cellStyle)) cellAttrs.style = cellStyle;
9688
if (editable && editMode !== Const.UNABLE_TO_CELL_EDIT) {
97-
setEditMode();
89+
if (editMode === Const.CLICK_TO_CELL_EDIT) {
90+
cellAttrs.onClick = this.handleEditingCell;
91+
} else {
92+
cellAttrs.onDoubleClick = this.handleEditingCell;
93+
}
9894
}
9995
return (
10096
<td { ...cellAttrs }>{ content }</td>

packages/react-bootstrap-table2/test/editing-cell.test.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ import sinon from 'sinon';
44
import { shallow, mount } from 'enzyme';
55

66
import { TableRowWrapper } from './test-helpers/table-wrapper';
7-
import BootstrapTable from '../src/bootstrap-table';
87
import EditingCell from '../src/editing-cell';
98
import TextEditor from '../src/text-editor';
109
import EditorIndicator from '../src/editor-indicator';
11-
import { productsGenerator } from './test-helpers/productGenerator';
12-
import Row from '../src/row';
13-
import Cell from '../src/cell';
1410

1511
describe('EditingCell', () => {
1612
let wrapper;
@@ -173,34 +169,4 @@ describe('EditingCell', () => {
173169
});
174170
});
175171
});
176-
177-
describe('when column.editable is function', () => {
178-
const products = productsGenerator();
179-
const mockFunction = jest.fn(content => content > 2102);
180-
const col = [{
181-
dataField: 'id',
182-
text: 'Product ID'
183-
}, {
184-
dataField: 'name',
185-
text: 'Product Name'
186-
}, {
187-
dataField: 'price',
188-
text: 'Product Price',
189-
editable: mockFunction
190-
}];
191-
const renderComponent = mount(<BootstrapTable keyField="id" data={ products } columns={ col } />);
192-
const rowComponent = renderComponent.find(Row);
193-
it(`column.editable function should be called ${products.length} times`, () => {
194-
expect(mockFunction).toHaveBeenCalledTimes(products.length);
195-
});
196-
it('should call callBack with right args', () => {
197-
expect(mockFunction).toHaveBeenLastCalledWith(2104, { id: 4, name: 'Item name 4', price: 2104 }, 4, 2);
198-
});
199-
it('should be "editable" === false', () => {
200-
expect(rowComponent.at(2).find(Cell).at(2).props().editable).toBeFalsy();
201-
});
202-
it('should be "editable" === true', () => {
203-
expect(rowComponent.at(3).find(Cell).at(2).props().editable).toBeTruthy();
204-
});
205-
});
206172
});

packages/react-bootstrap-table2/test/row.test.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Row', () => {
8686
}
8787
});
8888

89-
describe('when have column.editable defined false', () => {
89+
describe('and column.editable defined false', () => {
9090
const nonEditableColIndex = 1;
9191
beforeEach(() => {
9292
columns[nonEditableColIndex].editable = false;
@@ -101,7 +101,7 @@ describe('Row', () => {
101101
);
102102
});
103103

104-
it('Cell component should receive correct editMode props', () => {
104+
it('Cell component should receive correct editable props', () => {
105105
expect(wrapper.length).toBe(1);
106106
for (let i = 0; i < columns.length; i += 1) {
107107
const column = columns[i];
@@ -114,6 +114,79 @@ describe('Row', () => {
114114
});
115115
});
116116

117+
describe('and column.editable defined as function', () => {
118+
const nonEditableColIndex = 1;
119+
let editableCallBack;
120+
121+
afterEach(() => {
122+
editableCallBack.reset();
123+
});
124+
125+
describe('which return false', () => {
126+
beforeEach(() => {
127+
editableCallBack = sinon.stub().returns(false);
128+
columns[nonEditableColIndex].editable = editableCallBack;
129+
wrapper = shallow(
130+
<Row
131+
row={ row }
132+
rowIndex={ rowIndex }
133+
columns={ columns }
134+
keyField={ keyField }
135+
cellEdit={ cellEdit }
136+
/>
137+
);
138+
});
139+
140+
it('column.editable callback function should be called once', () => {
141+
expect(editableCallBack.callCount).toBe(1);
142+
});
143+
144+
it('Cell component should receive correct editable props', () => {
145+
expect(wrapper.length).toBe(1);
146+
for (let i = 0; i < columns.length; i += 1) {
147+
const column = columns[i];
148+
if (i === nonEditableColIndex || column.dataField === keyField) {
149+
expect(wrapper.find(Cell).get(i).props.editable).toBeFalsy();
150+
} else {
151+
expect(wrapper.find(Cell).get(i).props.editable).toBeTruthy();
152+
}
153+
}
154+
});
155+
});
156+
157+
describe('which return true', () => {
158+
beforeEach(() => {
159+
editableCallBack = sinon.stub().returns(true);
160+
columns[nonEditableColIndex].editable = editableCallBack;
161+
wrapper = shallow(
162+
<Row
163+
row={ row }
164+
rowIndex={ rowIndex }
165+
columns={ columns }
166+
keyField={ keyField }
167+
cellEdit={ cellEdit }
168+
/>
169+
);
170+
});
171+
172+
it('column.editable callback function should be called once', () => {
173+
expect(editableCallBack.callCount).toBe(1);
174+
});
175+
176+
it('Cell component should receive correct editable props', () => {
177+
expect(wrapper.length).toBe(1);
178+
for (let i = 0; i < columns.length; i += 1) {
179+
const column = columns[i];
180+
if (column.dataField === keyField) {
181+
expect(wrapper.find(Cell).get(i).props.editable).toBeFalsy();
182+
} else {
183+
expect(wrapper.find(Cell).get(i).props.editable).toBeTruthy();
184+
}
185+
}
186+
});
187+
});
188+
});
189+
117190
// Means user defined cellEdit.nonEditableRows
118191
// and some rows will be treated as noneditable by this rules
119192
describe('when editable prop is false', () => {

0 commit comments

Comments
 (0)