Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix filter condition to work with nested object paths #3274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/ra-ui-materialui/src/list/FilterButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ContentFilter from '@material-ui/icons/FilterList';
import classnames from 'classnames';
import compose from 'recompose/compose';
import { translate } from 'ra-core';
import lodashGet from 'lodash/get';

import FilterButtonMenuItem from './FilterButtonMenuItem';
import Button from '../button/Button';
Expand All @@ -32,7 +33,7 @@ export class FilterButton extends Component {
filterElement =>
!filterElement.props.alwaysOn &&
!displayedFilters[filterElement.props.source] &&
!filterValues[filterElement.props.source]
typeof lodashGet(filterValues, filterElement.props.source) === 'undefined'
);
}

Expand Down
53 changes: 53 additions & 0 deletions packages/ra-ui-materialui/src/list/FilterButton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import expect from 'expect';
import { render, cleanup, fireEvent } from 'react-testing-library';

import { FilterButton } from './FilterButton';
import TextInput from '../input/TextInput';

describe('<FilterButton />', () => {
const defaultProps = {
resource: 'post',
filters: [
<TextInput source="title" label="Title" />,
<TextInput source="customer.name" label="Name" />,
],
displayedFilters: {
title: true,
"customer.name": true,
},
showFilter: () => {},
translate: () => {},
filterValues: {},
};

afterEach(cleanup);

describe('filter button', () => {
it('should not be rendered, if all filters are already being displayed', () => {
const { queryByText } = render(
<FilterButton
{...defaultProps}
/>
);
expect(queryByText('ra.action.add_filter')).toBeNull();
});

});

describe('filter selection menu', () => {
it('should display only hidden filters', () => {
const hiddenFilter = <TextInput source="Returned" label="Returned" />;
const { queryByText } = render(
<FilterButton
{...defaultProps}
filters={defaultProps.filters.concat(hiddenFilter)}
/>
);
fireEvent.click(queryByText('ra.action.add_filter'));

expect(queryByText('Returned')).not.toBeNull();
expect(queryByText('Name')).toBeNull();
});
});
});
3 changes: 2 additions & 1 deletion packages/ra-ui-materialui/src/list/FilterForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { withStyles, createStyles } from '@material-ui/core/styles';
import compose from 'recompose/compose';
import withProps from 'recompose/withProps';
import lodashSet from 'lodash/set';
import lodashGet from 'lodash/get';

import FilterFormInput from './FilterFormInput';

Expand Down Expand Up @@ -82,7 +83,7 @@ export class FilterForm extends Component {
filterElement =>
filterElement.props.alwaysOn ||
displayedFilters[filterElement.props.source] ||
typeof initialValues[filterElement.props.source] !== 'undefined'
typeof lodashGet(initialValues, filterElement.props.source) !== 'undefined'
);
}

Expand Down
12 changes: 9 additions & 3 deletions packages/ra-ui-materialui/src/list/FilterForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ describe('<FilterForm />', () => {
});

it('should display correctly passed filters', () => {
const filters = [<TextInput source="title" label="Title" />]; // eslint-disable-line react/jsx-key
const displayedFilters = { title: true };
const filters = [
<TextInput source="title" label="Title" />,
<TextInput source="customer.name" label="Name" />
]; // eslint-disable-line react/jsx-key
const displayedFilters = {
title: true,
"customer.name": true,
};

const muiTheme = createMuiTheme({ userAgent: false });
const wrapper = render(
Expand All @@ -43,7 +49,7 @@ describe('<FilterForm />', () => {
);

const titleFilter = wrapper.find('input[type="text"]');
assert.equal(titleFilter.length, 1);
assert.equal(titleFilter.length, 2);
});

describe('mergeInitialValuesWithDefaultValues', () => {
Expand Down