Skip to content

Commit

Permalink
Fixes maheshsenni#19: canDeselect flag to disable row deselection on …
Browse files Browse the repository at this point in the history
…click
  • Loading branch information
mbarto committed Jan 10, 2019
1 parent da90a3b commit f59a774
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Component level options are accepted via the property, `options`.
|`height`|number|no|Height of the treelist body. When not provided, the component will expand to show all available rows.|
|`minimumColWidth`|number|no|Minimum width of columns. Columns can't be resized below this value.|
|`canSelect`|bool|no|Set this as `true` to enable the row selection handler. See `onSelectRow` in [handlers](#handlers) for details.|
|`canDeselect`|bool|no|Set this as `false` to disable deselection of the selected row (canSelect must be true, click on the selected row to disable it). Defaults to `true`.|
|`rowClass`|string | function|no|Class name (string) to be added to rows. Can be used for applying specific styles for the rows. A function can also be provided, which will be executed with the row's data object to determine the class name.|

### Handlers
Expand Down
14 changes: 9 additions & 5 deletions src/lib/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ class Body extends Component {
handleSelectRow(row) {
if (row[this.props.idField] === this.state.selectedRow) {
// deselect
this.setState({
selectedRow: null
});
this.props.onSelectRow(null);
if (this.props.canDeselect) {
this.setState({
selectedRow: null
});
this.props.onSelectRow(null);
}
} else {
this.setState({
selectedRow: row[this.props.idField]
Expand Down Expand Up @@ -216,6 +218,7 @@ Body.propTypes = {
itemHeight: PropTypes.number,
onSelectRow: PropTypes.func,
canSelect: PropTypes.bool,
canDeselect: PropTypes.bool,
rowClass: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
refresh: PropTypes.any
};
Expand All @@ -226,7 +229,8 @@ Body.defaultProps = {
expandAll: false,
itemHeight: 35,
onSelectRow: () => {},
canSelect: false
canSelect: false,
canDeselect: true
};

export default Body;
1 change: 1 addition & 0 deletions src/lib/TreeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class TreeList extends Component {
refresh={refresh}
expandAll={options.expandAll}
canSelect={options.canSelect}
canDeselect={options.canDeselect}
onSelectRow={handlers.onSelectRow}
rowClass={options.rowClass}
>
Expand Down
18 changes: 18 additions & 0 deletions src/lib/__tests__/Body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ describe('<Body />', () => {
expect(wrapper.find('.row-selected')).toHaveLength(0);
});

it("do not allow deselection of row clicking on the selected row if canDeselect is false", () => {
const handler = jest.fn();
const wrapper = mount(<Body reactKey={"test-key"} data={testData} columns={columns} canSelect canDeselect={false} idField="id" parentIdField="parentId" onHScroll={jest.fn()} metadata={getRowsWithChildren(testData, "id", "parentId")} updateHash="" height={100} itemHeight={10} onSelectRow={handler} />);
wrapper
.find("tr")
.at(1)
.simulate("click");
expect(handler.mock.calls).toHaveLength(1);
expect(wrapper.find(".row-selected")).toHaveLength(1);

wrapper
.find("tr")
.at(1)
.simulate("click");
expect(handler.mock.calls).toHaveLength(1);
expect(wrapper.find(".row-selected")).toHaveLength(1);
});

it('dynamic rows css', () => {
const className = function(data) {
return data.position;
Expand Down

0 comments on commit f59a774

Please sign in to comment.