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

[DataGrid] Refactor string operator filter functions #14564

Merged
Merged
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
100 changes: 46 additions & 54 deletions packages/x-data-grid/src/colDef/gridStringOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,67 @@ export const getGridStringQuickFilterFn: GetApplyQuickFilterFn<any, unknown> = (
};
};

const createContainsFilterFn =
(disableTrim: boolean, negate: boolean) => (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const trimmedValue = disableTrim ? filterItem.value : filterItem.value.trim();
const filterRegex = new RegExp(escapeRegExp(trimmedValue), 'i');
return (value: any): boolean => {
if (value == null) {
return negate;
}
const matches = filterRegex.test(String(value));
return negate ? !matches : matches;
};
};

const createEqualityFilterFn =
(disableTrim: boolean, negate: boolean) => (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const trimmedValue = disableTrim ? filterItem.value : filterItem.value.trim();

const collator = new Intl.Collator(undefined, { sensitivity: 'base', usage: 'search' });
return (value: any): boolean => {
if (value == null) {
return negate;
}
const isEqual = collator.compare(trimmedValue, value.toString()) === 0;
return negate ? !isEqual : isEqual;
};
};

const createEmptyFilterFn = (negate: boolean) => () => {
return (value: any): boolean => {
const isEmpty = value === '' || value == null;
return negate ? !isEmpty : isEmpty;
};
};

export const getGridStringOperators = (
disableTrim: boolean = false,
): GridFilterOperator<any, number | string | null, any>[] => [
{
value: 'contains',
getApplyFilterFn: (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const filterItemValue = disableTrim ? filterItem.value : filterItem.value.trim();

const filterRegex = new RegExp(escapeRegExp(filterItemValue), 'i');
return (value): boolean => {
return value != null ? filterRegex.test(String(value)) : false;
};
},
getApplyFilterFn: createContainsFilterFn(disableTrim, false),
InputComponent: GridFilterInputValue,
},
{
value: 'doesNotContain',
getApplyFilterFn: (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const filterItemValue = disableTrim ? filterItem.value : filterItem.value.trim();

const filterRegex = new RegExp(escapeRegExp(filterItemValue), 'i');
return (value): boolean => {
return value != null ? !filterRegex.test(String(value)) : true;
};
},
getApplyFilterFn: createContainsFilterFn(disableTrim, true),
InputComponent: GridFilterInputValue,
},
{
value: 'equals',
getApplyFilterFn: (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const filterItemValue = disableTrim ? filterItem.value : filterItem.value.trim();

const collator = new Intl.Collator(undefined, { sensitivity: 'base', usage: 'search' });
return (value): boolean => {
return value != null ? collator.compare(filterItemValue, value.toString()) === 0 : false;
};
},
getApplyFilterFn: createEqualityFilterFn(disableTrim, false),
InputComponent: GridFilterInputValue,
},
{
value: 'doesNotEqual',
getApplyFilterFn: (filterItem: GridFilterItem) => {
if (!filterItem.value) {
return null;
}
const filterItemValue = disableTrim ? filterItem.value : filterItem.value.trim();

const collator = new Intl.Collator(undefined, { sensitivity: 'base', usage: 'search' });
return (value): boolean => {
return value != null ? collator.compare(filterItemValue, value.toString()) !== 0 : true;
};
},
getApplyFilterFn: createEqualityFilterFn(disableTrim, true),
InputComponent: GridFilterInputValue,
},
{
Expand Down Expand Up @@ -115,20 +115,12 @@ export const getGridStringOperators = (
},
{
value: 'isEmpty',
getApplyFilterFn: () => {
return (value): boolean => {
return value === '' || value == null;
};
},
getApplyFilterFn: createEmptyFilterFn(false),
requiresFilterValue: false,
},
{
value: 'isNotEmpty',
getApplyFilterFn: () => {
return (value): boolean => {
return value !== '' && value != null;
};
},
getApplyFilterFn: createEmptyFilterFn(true),
requiresFilterValue: false,
},
{
Expand Down