mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2026-08-12 04:00:16 +00:00
fix bug for default sort and filter have potential issue when remote is enable
This commit is contained in:
@@ -36,7 +36,7 @@ class DateFilter extends Component {
|
||||
const comparator = this.dateFilterComparator.value;
|
||||
const date = this.inputDate.value;
|
||||
if (comparator && date) {
|
||||
this.applyFilter(date, comparator);
|
||||
this.applyFilter(date, comparator, true);
|
||||
}
|
||||
|
||||
// export onFilter function to allow users to access
|
||||
@@ -103,7 +103,7 @@ class DateFilter extends Component {
|
||||
// instead of parsing an invalid Date. The filter function will interpret
|
||||
// null as an empty date field
|
||||
const date = value === '' ? null : new Date(value);
|
||||
onFilter(column, FILTER_TYPE.DATE)({ date, comparator });
|
||||
onFilter(column, FILTER_TYPE.DATE, )({ date, comparator });
|
||||
};
|
||||
if (delay) {
|
||||
this.timeout = setTimeout(() => { execute(); }, delay);
|
||||
|
||||
@@ -36,7 +36,7 @@ class NumberFilter extends Component {
|
||||
const comparator = this.numberFilterComparator.value;
|
||||
const number = this.numberFilter.value;
|
||||
if (comparator && number) {
|
||||
onFilter(column, FILTER_TYPE.NUMBER)({ number, comparator });
|
||||
onFilter(column, FILTER_TYPE.NUMBER, true)({ number, comparator });
|
||||
}
|
||||
|
||||
// export onFilter function to allow users to access
|
||||
|
||||
@@ -29,7 +29,7 @@ class SelectFilter extends Component {
|
||||
|
||||
const value = this.selectInput.value;
|
||||
if (value && value !== '') {
|
||||
onFilter(column, FILTER_TYPE.SELECT)(value);
|
||||
onFilter(column, FILTER_TYPE.SELECT, true)(value);
|
||||
}
|
||||
|
||||
// export onFilter function to allow users to access
|
||||
|
||||
@@ -23,7 +23,7 @@ class TextFilter extends Component {
|
||||
const defaultValue = this.input.value;
|
||||
|
||||
if (defaultValue) {
|
||||
onFilter(this.props.column, FILTER_TYPE.TEXT)(defaultValue);
|
||||
onFilter(this.props.column, FILTER_TYPE.TEXT, true)(defaultValue);
|
||||
}
|
||||
|
||||
// export onFilter function to allow users to access
|
||||
|
||||
+10
-5
@@ -27,7 +27,13 @@ export default (
|
||||
this.onExternalFilter = this.onExternalFilter.bind(this);
|
||||
}
|
||||
|
||||
onFilter(column, filterType) {
|
||||
componentDidMount() {
|
||||
if (isRemoteFiltering() && Object.keys(this.currFilters).length > 0) {
|
||||
handleFilterChange(this.currFilters);
|
||||
}
|
||||
}
|
||||
|
||||
onFilter(column, filterType, initialize = false) {
|
||||
return (filterVal) => {
|
||||
// watch out here if migration to context API, #334
|
||||
const currFilters = Object.assign({}, this.currFilters);
|
||||
@@ -52,10 +58,9 @@ export default (
|
||||
this.currFilters = currFilters;
|
||||
|
||||
if (isRemoteFiltering()) {
|
||||
handleFilterChange(currFilters);
|
||||
// when remote filtering is enable, dont set currFilters state
|
||||
// in the componentWillReceiveProps,
|
||||
// it's the key point that we can know the filter is changed
|
||||
if (!initialize) {
|
||||
handleFilterChange(this.currFilters);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ describe('Date Filter', () => {
|
||||
|
||||
it('should do onFilter correctly when exported function was executed', () => {
|
||||
expect(onFilter).toHaveBeenCalledTimes(1);
|
||||
expect(onFilter).toHaveBeenCalledWith(column, FILTER_TYPE.DATE);
|
||||
expect(onFilter).toHaveBeenCalledWith(column, FILTER_TYPE.DATE, false);
|
||||
expect(onFilterFirstReturn).toHaveBeenCalledTimes(1);
|
||||
expect(onFilterFirstReturn).toHaveBeenCalledWith({ comparator, date });
|
||||
});
|
||||
@@ -148,7 +148,7 @@ describe('Date Filter', () => {
|
||||
|
||||
it('should calling onFilter on componentDidMount', () => {
|
||||
expect(onFilter).toHaveBeenCalledTimes(1);
|
||||
expect(onFilter).toHaveBeenCalledWith(column, FILTER_TYPE.DATE);
|
||||
expect(onFilter).toHaveBeenCalledWith(column, FILTER_TYPE.DATE, true);
|
||||
expect(onFilterFirstReturn).toHaveBeenCalledTimes(1);
|
||||
// expect(onFilterFirstReturn).toHaveBeenCalledWith({ comparator, date });
|
||||
});
|
||||
|
||||
@@ -147,7 +147,7 @@ describe('Number Filter', () => {
|
||||
|
||||
it('should calling onFilter on componentDidMount', () => {
|
||||
expect(onFilter.calledOnce).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.NUMBER)).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.NUMBER, true)).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledOnce).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledWith({ number: `${number}`, comparator })).toBeTruthy();
|
||||
});
|
||||
|
||||
@@ -91,7 +91,7 @@ describe('Select Filter', () => {
|
||||
|
||||
it('should calling onFilter on componentDidMount', () => {
|
||||
expect(onFilter.calledOnce).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.SELECT)).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.SELECT, true)).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledOnce).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledWith(defaultValue)).toBeTruthy();
|
||||
});
|
||||
|
||||
@@ -65,7 +65,7 @@ describe('Text Filter', () => {
|
||||
|
||||
it('should calling onFilter on componentDidMount', () => {
|
||||
expect(onFilter.calledOnce).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.TEXT)).toBeTruthy();
|
||||
expect(onFilter.calledWith(column, FILTER_TYPE.TEXT, true)).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledOnce).toBeTruthy();
|
||||
expect(onFilterFirstReturn.calledWith(defaultValue)).toBeTruthy();
|
||||
});
|
||||
|
||||
@@ -12,7 +12,6 @@ import { textFilter } from '../index';
|
||||
|
||||
describe('FilterContext', () => {
|
||||
let wrapper;
|
||||
// let filter;
|
||||
let FilterContext;
|
||||
|
||||
const data = [{
|
||||
@@ -33,8 +32,6 @@ describe('FilterContext', () => {
|
||||
filter: textFilter()
|
||||
}];
|
||||
|
||||
// const defaultFilter = {};
|
||||
|
||||
const mockBase = jest.fn((props => (
|
||||
<BootstrapTable
|
||||
keyField="id"
|
||||
@@ -47,7 +44,6 @@ describe('FilterContext', () => {
|
||||
const handleFilterChange = jest.fn();
|
||||
|
||||
function shallowContext(
|
||||
// customFilter = defaultFilter,
|
||||
enableRemote = false
|
||||
) {
|
||||
mockBase.mockReset();
|
||||
@@ -115,6 +111,45 @@ describe('FilterContext', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('componentDidMount', () => {
|
||||
describe('when remote filter is disabled', () => {
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(shallowContext());
|
||||
wrapper.render();
|
||||
wrapper.instance().componentDidMount();
|
||||
});
|
||||
|
||||
it('should not call handleFilterChange', () => {
|
||||
expect(handleFilterChange).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when remote filter is enable but currFilters is empty', () => {
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(shallowContext(true));
|
||||
wrapper.render();
|
||||
wrapper.instance().componentDidMount();
|
||||
});
|
||||
|
||||
it('should not call handleFilterChange', () => {
|
||||
expect(handleFilterChange).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when remote filter is enable and currFilters is not empty', () => {
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(shallowContext(true));
|
||||
wrapper.instance().currFilters.price = { filterVal: 40, filterType: FILTER_TYPE.TEXT };
|
||||
});
|
||||
|
||||
it('should not call handleFilterChange', () => {
|
||||
wrapper.instance().componentDidMount();
|
||||
expect(handleFilterChange).toHaveBeenCalledTimes(1);
|
||||
expect(handleFilterChange).toHaveBeenCalledWith(wrapper.instance().currFilters);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('onFilter', () => {
|
||||
let instance;
|
||||
describe('when filterVal is empty or undefined', () => {
|
||||
@@ -169,6 +204,25 @@ describe('FilterContext', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when remote filter is enabled but initialize argument is true', () => {
|
||||
const filterVal = '3';
|
||||
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(shallowContext(true));
|
||||
wrapper.render();
|
||||
instance = wrapper.instance();
|
||||
instance.onFilter(columns[1], FILTER_TYPE.TEXT, true)(filterVal);
|
||||
});
|
||||
|
||||
it('should correct currFilters', () => {
|
||||
expect(Object.keys(instance.currFilters)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should not call handleFilterChange correctly', () => {
|
||||
expect(handleFilterChange).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('combination', () => {
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(shallowContext());
|
||||
|
||||
@@ -38,15 +38,18 @@ export default (
|
||||
if (sortColumn.onSort) {
|
||||
sortColumn.onSort(sortField, sortOrder);
|
||||
}
|
||||
|
||||
if (isRemoteSort()) {
|
||||
handleSortChange(sortField, sortOrder);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.state = { sortOrder, sortColumn };
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const { sortOrder, sortColumn } = this.state;
|
||||
if (isRemoteSort() && sortOrder && sortColumn) {
|
||||
handleSortChange(sortColumn.dataField, sortOrder);
|
||||
}
|
||||
}
|
||||
|
||||
handleSort = (column) => {
|
||||
const sortOrder = dataOperator.nextOrder(column, this.state, this.props.defaultSortDirection);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user