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: check filter type before clearing #455

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
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,14 @@ export const MRT_FilterTextInput = <TData extends MRT_RowData>({
}
};

if (columnDef.Filter) {
return (
<>{columnDef.Filter?.({ column, header, rangeFilterIndex, table })}</>
);
}

const handleClearEmptyFilterChip = () => {
setFilterValue('');
column.setFilterValue(undefined);
if (isMultiSelectFilter) {
setFilterValue([]);
column.setFilterValue([]);
} else {
setFilterValue('');
column.setFilterValue(undefined);
}
setColumnFilterFns((prev) => ({
...prev,
[header.id]: allowedColumnFilterOptions?.[0] ?? 'fuzzy',
Expand Down Expand Up @@ -245,7 +244,8 @@ export const MRT_FilterTextInput = <TData extends MRT_RowData>({
: textInputProps?.style),
},
title: filterPlaceholder,
value: isMultiSelectFilter && !Array.isArray(filterValue) ? [] : filterValue,
value:
isMultiSelectFilter && !Array.isArray(filterValue) ? [] : filterValue,
variant: 'unstyled',
} as const;

Expand All @@ -262,103 +262,129 @@ export const MRT_FilterTextInput = <TData extends MRT_RowData>({
</ActionIcon>
) : null;

return filterChipLabel ? (
<Box style={commonProps.style}>
<Badge
className={classes['filter-chip-badge']}
onClick={handleClearEmptyFilterChip}
rightSection={ClearButton}
size="lg"
>
{filterChipLabel}
</Badge>
</Box>
) : isMultiSelectFilter ? (
<MultiSelect
{...commonProps}
searchable
{...multiSelectProps}
className={clsx(className, multiSelectProps.className)}
data={filterSelectOptions}
onChange={(value) => setFilterValue(value)}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (multiSelectProps.ref) {
multiSelectProps.ref.current = node;
if (columnDef.Filter) {
return (
<>{columnDef.Filter?.({ column, header, rangeFilterIndex, table })}</>
);
}

if (filterChipLabel) {
return (
<Box style={commonProps.style}>
<Badge
className={classes['filter-chip-badge']}
onClick={handleClearEmptyFilterChip}
rightSection={ClearButton}
size="lg"
>
{filterChipLabel}
</Badge>
</Box>
);
}

if (isMultiSelectFilter) {
return (
<MultiSelect
{...commonProps}
searchable
{...multiSelectProps}
className={clsx(className, multiSelectProps.className)}
data={filterSelectOptions}
onChange={(value) => setFilterValue(value)}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (multiSelectProps.ref) {
multiSelectProps.ref.current = node;
}
}
}}
rightSection={
filterValue?.toString()?.length && multiSelectProps?.clearable
? ClearButton
: undefined
}
}}
rightSection={
filterValue?.toString()?.length && multiSelectProps?.clearable
? ClearButton
: undefined
}
style={commonProps.style}
/>
) : isSelectFilter ? (
<Select
{...commonProps}
clearable
searchable
{...selectProps}
className={clsx(className, selectProps.className)}
clearButtonProps={{
size: 'md',
}}
data={filterSelectOptions}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (selectProps.ref) {
selectProps.ref.current = node;
style={commonProps.style}
/>
);
}

if (isSelectFilter) {
return (
<Select
{...commonProps}
clearable
searchable
{...selectProps}
className={clsx(className, selectProps.className)}
clearButtonProps={{
size: 'md',
}}
data={filterSelectOptions}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (selectProps.ref) {
selectProps.ref.current = node;
}
}
}
}}
style={commonProps.style}
/>
) : isDateFilter ? (
<DateInput
{...commonProps}
allowDeselect
clearable
popoverProps={{ withinPortal: columnFilterDisplayMode !== 'popover' }}
{...dateInputProps}
className={clsx(className, dateInputProps.className)}
onChange={(event) => commonProps.onChange(event === null ? '' : event)}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (dateInputProps.ref) {
dateInputProps.ref.current = node;
}}
style={commonProps.style}
/>
);
}

if (isDateFilter) {
return (
<DateInput
{...commonProps}
allowDeselect
clearable
popoverProps={{ withinPortal: columnFilterDisplayMode !== 'popover' }}
{...dateInputProps}
className={clsx(className, dateInputProps.className)}
onChange={(event) => commonProps.onChange(event === null ? '' : event)}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (dateInputProps.ref) {
dateInputProps.ref.current = node;
}
}
}
}}
style={commonProps.style}
/>
) : isAutoCompleteFilter ? (
<Autocomplete
{...commonProps}
onChange={(value) => setFilterValue(value)}
rightSection={filterValue?.toString()?.length ? ClearButton : undefined}
{...autoCompleteProps}
className={clsx(className, autoCompleteProps.className)}
data={filterSelectOptions}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (autoCompleteProps.ref) {
autoCompleteProps.ref.current = node;
}}
style={commonProps.style}
/>
);
}

if (isAutoCompleteFilter) {
return (
<Autocomplete
{...commonProps}
onChange={(value) => setFilterValue(value)}
rightSection={filterValue?.toString()?.length ? ClearButton : undefined}
{...autoCompleteProps}
className={clsx(className, autoCompleteProps.className)}
data={filterSelectOptions}
ref={(node) => {
if (node) {
filterInputRefs.current[`${column.id}-${rangeFilterIndex ?? 0}`] =
node;
if (autoCompleteProps.ref) {
autoCompleteProps.ref.current = node;
}
}
}
}}
style={commonProps.style}
/>
) : (
}}
style={commonProps.style}
/>
);
}

return (
<TextInput
{...commonProps}
onChange={(e) => setFilterValue(e.target.value)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const SwitchFromEmptyToEqualsArray = () => (
accessorKey: 'state',
header: 'State',
filterVariant: 'multi-select',
columnFilterModeOptions: ['equals', 'empty'],
columnFilterModeOptions: ['equals', 'empty', 'notEmpty'],
mantineFilterMultiSelectProps: {
data: ["Wyoming", "Delaware", "South Dakota", "Vermont", "Rhode Island"]
}
Expand Down
Loading