selectively add icontains to only text-based searches

This commit is contained in:
John Mitchell 2019-09-13 12:42:56 -04:00
parent ec3edb07e8
commit 86029934ad
7 changed files with 26 additions and 4 deletions

View File

@ -15,6 +15,8 @@ import Search from '../Search';
import Sort from '../Sort';
import VerticalSeparator from '../VerticalSeparator';
import { QSConfig } from '@types';
const AWXToolbar = styled.div`
--awx-toolbar--BackgroundColor: var(--pf-global--BackgroundColor--light-100);
--awx-toolbar--BorderColor: #ebebeb;
@ -98,6 +100,7 @@ class DataListToolbar extends React.Component {
sortedColumnKey,
additionalControls,
i18n,
qsConfig,
} = this.props;
const showExpandCollapse = onCompact && onExpand;
@ -120,6 +123,7 @@ class DataListToolbar extends React.Component {
)}
<ToolbarItem css="flex-grow: 1;">
<Search
qsConfig={qsConfig}
columns={columns}
onSearch={onSearch}
sortedColumnKey={sortedColumnKey}
@ -160,6 +164,7 @@ class DataListToolbar extends React.Component {
}
DataListToolbar.propTypes = {
qsConfig: QSConfig.isRequired,
columns: PropTypes.arrayOf(PropTypes.object).isRequired,
showSelectAll: PropTypes.bool,
isAllSelected: PropTypes.bool,

View File

@ -14,6 +14,8 @@ import {
} from '@patternfly/react-core';
import { SearchIcon } from '@patternfly/react-icons';
import { QSConfig } from '@types';
import styled from 'styled-components';
const TextInput = styled(PFTextInput)`
@ -110,10 +112,18 @@ class Search extends React.Component {
e.preventDefault();
const { searchKey, searchValue } = this.state;
const { onSearch } = this.props;
const { onSearch, qsConfig } = this.props;
// TODO: probably not _always_ add icontains. I don't think icontains works for numbers.
onSearch(`${searchKey}__icontains`, searchValue);
const isNonStringField = key => qsConfig
.integerFields
.filter(field => field === key).length || qsConfig.dateFields
.filter(field => field === key).length;
// TODO: this will probably become more sophisticated, where date
// fields and string fields are passed to a formatter
const actualSearchKey = isNonStringField(searchKey) ? searchKey : `${searchKey}__icontains`;
onSearch(actualSearchKey, searchValue);
this.setState({ searchValue: '' });
}
@ -202,6 +212,7 @@ class Search extends React.Component {
}
Search.propTypes = {
qsConfig: QSConfig.isRequired,
columns: PropTypes.arrayOf(PropTypes.object).isRequired,
onSearch: PropTypes.func,
sortedColumnKey: PropTypes.string,

View File

@ -183,6 +183,7 @@ class JobList extends Component {
showExpandCollapse
isAllSelected={isAllSelected}
onSelectAll={this.handleSelectAll}
qsConfig={QS_CONFIG}
additionalControls={[
<ToolbarDeleteButton
key="delete"

View File

@ -190,6 +190,7 @@ class OrganizationAccess extends React.Component {
renderToolbar={props => (
<DataListToolbar
{...props}
qsConfig={QS_CONFIG}
additionalControls={
canEdit
? [

View File

@ -181,6 +181,7 @@ class OrganizationsList extends Component {
showSelectAll
isAllSelected={isAllSelected}
onSelectAll={this.handleSelectAll}
qsConfig={QS_CONFIG}
additionalControls={[
<ToolbarDeleteButton
key="delete"

View File

@ -207,6 +207,7 @@ class TemplatesList extends Component {
showExpandCollapse
isAllSelected={isAllSelected}
onSelectAll={this.handleSelectAll}
qsConfig={QS_CONFIG}
additionalControls={[
<ToolbarDeleteButton
key="delete"

View File

@ -161,7 +161,8 @@ export const encodeNonDefaultQueryString = (config, params) => {
export function getQSConfig(
namespace,
defaultParams = { page: 1, page_size: 5, order_by: 'name' },
integerFields = ['page', 'page_size']
integerFields = ['page', 'page_size'],
dateFields = ['modified', 'created']
) {
if (!namespace) {
throw new Error('a QS namespace is required');
@ -170,6 +171,7 @@ export function getQSConfig(
namespace,
defaultParams,
integerFields,
dateFields
};
}