Skip to content
26 changes: 21 additions & 5 deletions src/module.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as Immutable from 'immutable';
import { connect as originalConnect } from 'react-redux';
import { ActionCreator, Middleware } from 'redux';

Expand Down Expand Up @@ -203,6 +204,18 @@ const SettingsContainer: (OriginalComponent: any) => any;

const SettingsComponents: PropertyBag<GriddleComponent<any>>;

export interface FilterProps {
setFilter?: (filter: GriddleFilter) => void;
placeholder?: string;
className?: string;
style?: React.CSSProperties;

[x: string]: any;
}

class Filter extends React.Component<FilterProps, any> {
}

} // namespace components

export interface GriddleComponents {
Expand All @@ -216,10 +229,10 @@ export interface GriddleComponents {
StyleContainer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;
StyleContainerEnhancer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;

Filter?: GriddleComponent<any>;
FilterEnhancer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;
FilterContainer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;
FilterContainerEnhancer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;
Filter?: GriddleComponent<components.FilterProps>;
FilterEnhancer?: (OriginalComponent: GriddleComponent<components.FilterProps>) => GriddleComponent<components.FilterProps>;
FilterContainer?: (OriginalComponent: GriddleComponent<components.FilterProps>) => GriddleComponent<components.FilterProps>;
FilterContainerEnhancer?: (OriginalComponent: GriddleComponent<components.FilterProps>) => GriddleComponent<components.FilterProps>;

SettingsWrapper?: GriddleComponent<components.SettingsWrapperProps>;
SettingsWrapperEnhancer?: (OriginalComponent: GriddleComponent<components.SettingsWrapperProps>) => GriddleComponent<components.SettingsWrapperProps>;
Expand Down Expand Up @@ -294,12 +307,15 @@ export interface GriddleComponents {
PreviousButtonContainerEnhancer?: (OriginalComponent: GriddleComponent<any>) => GriddleComponent<any>;
}

export type RowFilter = (row: any, index: number, data: Immutable.List<any>) => boolean;
export type GriddleFilter = string | RowFilter | PropertyBag<string | RowFilter>;

export interface GriddleActions extends PropertyBag<ActionCreator<any> | undefined> {
onSort?: (sortProperties: any) => void;
onNext?: () => void;
onPrevious?: () => void;
onGetPage?: (pageNumber: number) => void;
setFilter?: (filterText: string) => void;
setFilter?: (filter: GriddleFilter) => void;
}

export interface GriddleEvents extends GriddleActions {
Expand Down
40 changes: 37 additions & 3 deletions src/plugins/local/reducers/__tests__/localReducerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,46 @@ test('sets page size', test => {
test.is(state.getIn(['pageProperties', 'pageSize']), 11);
});

test('sets filter', test => {
test('sets filter null', test => {
const state = reducers.GRIDDLE_SET_FILTER(new Immutable.Map(), {
filter: 'onetwothree',
filter: null,
});

test.is(state.get('filter'), 'onetwothree');
test.is(state.get('filter'), null);
test.is(state.getIn(['pageProperties', 'currentPage']), 1)
});

test('sets filter string', test => {
const filter = 'onetwothree';
const state = reducers.GRIDDLE_SET_FILTER(new Immutable.Map(), {
filter
});

test.is(state.get('filter'), filter);
test.is(state.getIn(['pageProperties', 'currentPage']), 1)
});

test('sets filter function', test => {
const filter = (v, i) => i % 2;
const state = reducers.GRIDDLE_SET_FILTER(new Immutable.Map(), {
filter,
});

test.is(state.get('filter'), filter);
test.is(state.getIn(['pageProperties', 'currentPage']), 1)
});

test('sets filter object', test => {
const filter = {
id: (v, i) => i % 2,
name: 'ben',
};
const state = reducers.GRIDDLE_SET_FILTER(new Immutable.Map(), {
filter,
});

test.not(state.get('filter'), filter);
test.deepEqual(state.get('filter').toJS(), filter);
test.is(state.getIn(['pageProperties', 'currentPage']), 1)
});

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/local/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Immutable from 'immutable';
import { maxPageSelector, currentPageSelector } from '../selectors/localSelectors';

import * as dataReducers from '../../../reducers//dataReducer';
Expand Down Expand Up @@ -63,7 +64,7 @@ export function GRIDDLE_PREVIOUS_PAGE(state, action) {
*/
export function GRIDDLE_SET_FILTER(state, action) {
return state
.set('filter', action.filter)
.set('filter', action.filter && Immutable.fromJS(action.filter))
.setIn(['pageProperties', 'currentPage'], 1);
};

Expand Down
Loading