Skip to content

Commit

Permalink
Merge 6a6e144 into abfe1b4
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenberger committed Nov 24, 2017
2 parents abfe1b4 + 6a6e144 commit c63d3f7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
14 changes: 11 additions & 3 deletions lib/Column.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Column extends Component {
this.setState({component, content});
}

updateFilterConfig(filterFunction, filterComponent){
updateFilterConfig(filterFunction, filterComponent, hide){
const { name } = this.props;
const { updateFilterConfig } = this.context;
updateFilterConfig(filterFunction, filterComponent, name);
updateFilterConfig(filterFunction, filterComponent, name, hide);
}

getChildContext(){
Expand Down Expand Up @@ -85,7 +85,15 @@ if (process.env.NODE_ENV !== 'production'){
name: PropTypes.string.isRequired,
sortable: PropTypes.bool,
label: PropTypes.node,
children: PropTypes.node
children: PropTypes.node,
hideTools: (props, propName, componentName, location, propFullName)=>{
if (props[propName]){
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Please use <Filter hide />.'
);
}
}
};
}

Expand Down
9 changes: 5 additions & 4 deletions lib/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ class Filter extends Component {
}

updateFilterConfig(){
const { match, component } = this.props;
const { match, component, hide } = this.props;
const { updateFilterConfig } = this.context;
updateFilterConfig(match, component);
updateFilterConfig(match, component, hide);
}

componentWillMount(){
this.updateFilterConfig();
}

componentDidUpdate(prevProps){
if (prevProps.match !== this.props.match || prevProps.component !== this.props.component){
if (prevProps.match !== this.props.match || prevProps.component !== this.props.component || prevProps.hide !== this.props.hide){
this.updateFilterConfig();
}
}
Expand All @@ -34,7 +34,8 @@ Filter.contextTypes = {
if (process.env.NODE_ENV !== 'production'){
Filter.propTypes = {
match: PropTypes.func,
component: PropTypes.func
component: PropTypes.func,
hide: PropTypes.bool
};
}

Expand Down
17 changes: 13 additions & 4 deletions lib/FilterHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const withFilterHandler = ComponentToWrap => {
filterComponents: {},
filterFunctions : {},
defaultFilterFunction: defaultFilterFunction,
defaultFilterComponent: TextFilter
defaultFilterComponent: TextFilter,
columnsWithHiddenFilters: []
};
}

Expand All @@ -58,7 +59,8 @@ const withFilterHandler = ComponentToWrap => {
filterComponents: this.state.filterComponents,
defaultFilterComponent: this.state.defaultFilterComponent,
updateFilter: this.updateFilter,
filter: this.state.filter
filter: this.state.filter,
columnsWithHiddenFilters: this.state.columnsWithHiddenFilters
};
}

Expand Down Expand Up @@ -86,7 +88,7 @@ const withFilterHandler = ComponentToWrap => {
});
}

updateFilterConfig(filterFunction, filterComponent, columnName){
updateFilterConfig(filterFunction, filterComponent, columnName, hide){
if (columnName !== undefined){
this.setState((prevState) => {
let newState = {};
Expand All @@ -98,6 +100,12 @@ const withFilterHandler = ComponentToWrap => {
newState.filterComponents = Object.assign({}, prevState.filterComponents);
newState.filterComponents[columnName] = filterComponent;
}
if (hide && prevState.columnsWithHiddenFilters.indexOf(columnName) === -1){
newState.columnsWithHiddenFilters = prevState.columnsWithHiddenFilters.slice(0).concat(columnName);
}
else if (!hide && prevState.columnsWithHiddenFilters.indexOf(columnName) !== -1){
newState.columnsWithHiddenFilters = prevState.columnsWithHiddenFilters.filter(col => col !== columnName);
}
return newState;
});

Expand Down Expand Up @@ -173,7 +181,8 @@ const withFilterHandler = ComponentToWrap => {
updateFilterConfig: PropTypes.func,
filter: PropTypes.array,
filterComponents: PropTypes.objectOf(PropTypes.func),
defaultFilterComponent : PropTypes.func
defaultFilterComponent : PropTypes.func,
columnsWithHiddenFilters: PropTypes.arrayOf(PropTypes.string)
};

if (process.env.NODE_ENV !== 'production'){
Expand Down
7 changes: 4 additions & 3 deletions lib/FilterRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class FilterRow extends Component {

render() {
const {component, children} = this.props;
const { columnConfigs, filter, filterComponents, defaultFilterComponent} = this.context;
const { columnConfigs, filter, filterComponents, defaultFilterComponent, columnsWithHiddenFilters } = this.context;
const { FilterCell } = this.state;
const childrenAsArray = React.Children.toArray(children);
const activeColumnKeys = activeColumnKeysFunction(columnConfigs);
const FilterRowComponent = component ? component : DefaultFilterRowComponent ;
const activeFilters = activeColumnKeys.filter(columnKey => !columnConfigs.find(c => c.name === columnKey).hideTools );
const activeFilters = activeColumnKeys.filter(columnKey => columnsWithHiddenFilters.indexOf(columnKey) === -1);
const filterCells = activeColumnKeys.map(columnKey => {
if (activeFilters.indexOf(columnKey) !== -1){
const FilterComponent = filterComponents[columnKey] || defaultFilterComponent;
Expand All @@ -96,7 +96,8 @@ FilterRow.contextTypes = {
filter: PropTypes.array,
updateFilter: PropTypes.func.isRequired,
filterComponents: PropTypes.objectOf(PropTypes.func).isRequired,
defaultFilterComponent: PropTypes.func.isRequired
defaultFilterComponent: PropTypes.func.isRequired,
columnsWithHiddenFilters: PropTypes.arrayOf(PropTypes.string).isRequired
};

FilterRow.childContextTypes = {
Expand Down
1 change: 0 additions & 1 deletion lib/proptypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const columnsConfig = PropTypes.objectOf(PropTypes.shape({
id: PropTypes.bool,
label: PropTypes.string,
order: PropTypes.number,
hideTools: PropTypes.bool,
sortable: PropTypes.bool,
sortValueGetter: PropTypes.func,
filterFunction: PropTypes.func,
Expand Down
5 changes: 4 additions & 1 deletion test/GridTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Cell from '../lib/Cell';
import Row from '../lib/Row';
import Body from '../lib/Body';
import HeaderRow from '../lib/HeaderRow';
import Filter from '../lib/Filter';
import { buildGridWithTemplate } from '../lib/GridBuilder';
import PagingHandler from "../lib/PagingHandler";
import chai, { expect } from 'chai'
Expand Down Expand Up @@ -579,7 +580,9 @@ describe('Grid render tests', function(){
it('Should be possible to hide the tools for a column', ()=>{
let grid = mount(
<Grid objects={[{a: "foo", b: "bar"}]}>
<Column name="b" hideTools={true}/>
<Column name="b">
<Filter hide />
</Column>
<Column name="id" hide/>
</Grid>);
expect(grid.find("th input").length).be.equal(1);
Expand Down

0 comments on commit c63d3f7

Please sign in to comment.