Skip to content

Commit c2b631d

Browse files
committed
fix(AnalyticalTable - TypeScript): adjust column types for filtering
1 parent c87c690 commit c2b631d

File tree

2 files changed

+33
-10
lines changed
  • packages/main/src/components/AnalyticalTable

2 files changed

+33
-10
lines changed
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import type { FC } from 'react';
21
import { useCallback } from 'react';
32
import { stopPropagation } from '../../../../internal/stopPropagation.js';
3+
import type { InputPropTypes } from '../../../../webComponents/Input/index.js';
44
import { Input } from '../../../../webComponents/Input/index.js';
5+
import type { FilterProps } from '../../types/index.js';
56

6-
export const DefaultFilterComponent: FC<any> = ({ column }) => {
7-
const handleChange = useCallback(
7+
export const DefaultFilterComponent = ({ column }: FilterProps) => {
8+
const handleInput: InputPropTypes['onInput'] = useCallback(
89
(e) => {
10+
// Setting the filter to `undefined` removes it
911
column.setFilter(e.target.value || undefined);
1012
},
1113
[column.setFilter]
1214
);
13-
const handleKeyDown = (e) => {
15+
16+
const handleKeyDown: InputPropTypes['onKeyDown'] = (e) => {
1417
if (e.key !== 'Enter') {
1518
stopPropagation(e);
1619
}
1720
};
18-
// todo remove "undefined" check if wc issue has been fixed (https://github.com/SAP/ui5-webcomponents/issues/2616)
19-
return <Input onInput={handleChange} value={column.filterValue ?? ''} showClearIcon onKeyDown={handleKeyDown} />;
21+
22+
return <Input onInput={handleInput} value={column.filterValue} showClearIcon onKeyDown={handleKeyDown} />;
2023
};
24+
25+
DefaultFilterComponent.displayName = 'DefaultFilterComponent';

packages/main/src/components/AnalyticalTable/types/index.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
VerticalAlign
1515
} from '../../../enums/index.js';
1616
import type { CommonProps } from '../../../types/index.js';
17+
import type { PopoverDomRef } from '../../../webComponents/Popover/index.js';
1718

1819
export enum RenderColumnTypes {
1920
Filter = 'Filter',
@@ -60,7 +61,12 @@ export interface ColumnType extends Omit<AnalyticalTableColumnDefinition, 'id'>
6061
* @param props The props are added to the table instance
6162
*/
6263
render?: (type: RenderColumnTypes | keyof typeof RenderColumnTypes, props: Record<string, any>) => ReactNode;
63-
setFilter?: (val: string) => void;
64+
/**
65+
* Set the filter value for the current column.
66+
*
67+
* __Note:__ If set to `undefined`, the filter is removed.
68+
*/
69+
setFilter?: (val: string | undefined) => void;
6470
sortDescFirst?: boolean;
6571
sortedIndex?: number;
6672
toggleHidden?: (hidden?: boolean) => void;
@@ -139,7 +145,12 @@ export interface TableInstance {
139145
selectedFlatRows?: RowType[];
140146
setAllFilters?: (filtersObjectArray: Record<string, any>[]) => void;
141147
setColumnOrder?: any;
142-
setFilter?: (columnId: string, filterValue: string) => void;
148+
/**
149+
* Set the filter value for the defined column.
150+
*
151+
* __Note:__ If set to `undefined`, the filter is removed.
152+
*/
153+
setFilter?: (columnId: string, filterValue: string | undefined) => void;
143154
setGlobalFilter?: (filterValue: string) => void;
144155
setGroupBy?: (columnIds: string[]) => void;
145156
setHiddenColumns?: (columnIds: string[]) => void;
@@ -323,6 +334,11 @@ export interface TableInstanceWithPopoverProps extends TableInstance {
323334
popoverProps: PopoverProps;
324335
}
325336

337+
export interface FilterProps {
338+
column: ColumnType;
339+
popoverRef: MutableRefObject<PopoverDomRef>;
340+
}
341+
326342
export interface AnalyticalTableColumnDefinition {
327343
// base properties
328344
/**
@@ -403,7 +419,7 @@ export interface AnalyticalTableColumnDefinition {
403419
/**
404420
* Filter Component to be rendered in the Header.
405421
*/
406-
Filter?: string | ComponentType<any> | ((props?: any) => ReactNode);
422+
Filter?: ComponentType<FilterProps> | ((props: FilterProps) => ReactNode);
407423
/**
408424
* Disable filters for this column.
409425
*/
@@ -420,8 +436,10 @@ export interface AnalyticalTableColumnDefinition {
420436
* * `exactText`
421437
* * `exactTextCase`
422438
* * `equals`
439+
*
440+
* __Note:__ When the standard `Filter` component is used, the filter function is not triggered if the `filterValue` is empty, as the filter is then removed.
423441
*/
424-
filter?: string | ((rows: any[], columnIds: string[], filterValue: string) => any);
442+
filter?: string | ((rows: RowType[], columnIds: string[], filterValue: string) => RowType[]);
425443

426444
// useGlobalFilter
427445
/**

0 commit comments

Comments
 (0)