Skip to content

Commit f723cf3

Browse files
authored
display predefined filter (#39)
1 parent 22414ae commit f723cf3

6 files changed

Lines changed: 54 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Display predefined filter in filter row
13+
1014
## [2.5.0] - 2023-05-25
1115

1216
### dependabot: \#33 Bump loader-utils from 1.4.0 to 1.4.2

src/lib/DataTable/DataTableInterfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint max-lines: ["error", 260] */ // Increased max-lines due to the addition of definitions going above the predefined limit.
1+
/* eslint max-lines: ["error", 270] */ // Increased max-lines due to the addition of definitions going above the predefined limit.
22
import { CSSProperties } from "react";
33
import { ActionsPosition, CellFunction, ColumnFilterType, ListSortDirection, QueryFunction, RouteParams } from "./DataTableTypes";
44
import { IconProp } from "@fortawesome/fontawesome-svg-core";
@@ -150,6 +150,7 @@ export interface DataTablePredefinedActionLink<T, TRouteNames> {
150150
interface LinkProps {
151151
route: any;
152152
params?: RouteParams;
153+
children: JSX.Element;
153154
}
154155

155156
export interface DataTablePredefinedActionLinkGetParamsParams<T> {

src/lib/DataTable/DataTableRouted.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function DataTableRouted<T, TFilter, TRouteNames>({
155155
)}
156156
{actionsPosition == ActionsPosition.Right && <ActionsHeaderTitleCell<T, TRouteNames> actions={actions} />}
157157
</tr>
158-
<DataTableFilterRow
158+
<DataTableFilterRow<T, TFilter>
159159
actions={actions}
160160
columns={columns}
161161
onSearch={onSearch}
@@ -164,6 +164,7 @@ export function DataTableRouted<T, TFilter, TRouteNames>({
164164
setFilterRef={setFilterRef}
165165
translations={dataTableTranslations}
166166
actionsPosition={actionsPosition}
167+
predefinedFilter={predefinedFilter}
167168
/>
168169
</thead>
169170
<tbody>

src/lib/DataTableFilterRow/DataTableFilterRow.tsx

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,73 @@ import { Input } from "reactstrap";
33
import { DataTableColumnDescription, DataTableActions, Filters, FilterTranslations } from "../DataTable/DataTableInterfaces";
44
import { ColumnFilterType, ActionsPosition } from "../DataTable/DataTableTypes";
55
import { ActionsHeaderFilterCell } from "../DataTable/Actions/ActionsHeaderFilterCell";
6+
import { getDeepValue } from "../Utils/DeepValue";
67

7-
interface DataTableFilterRowProps<T> {
8+
interface DataTableFilterRowProps<T, TFilter> {
89
columns: DataTableColumnDescription<T>[];
910
actions?: DataTableActions<T>;
1011
actionsPosition?: ActionsPosition;
1112
translations: FilterTranslations;
1213
filterPossible: boolean;
14+
predefinedFilter?: TFilter;
1315
getFilterRefs(): Filters;
1416
setFilterRef(filterName: string, ref: HTMLInputElement): void;
1517
onSearch(): void;
1618
}
1719

18-
export function DataTableFilterRow<T>({
20+
export function DataTableFilterRow<T, TFilter>({
1921
columns,
2022
actions,
2123
actionsPosition,
2224
filterPossible = true,
2325
translations,
26+
predefinedFilter,
2427
getFilterRefs,
2528
setFilterRef,
2629
onSearch,
27-
}: DataTableFilterRowProps<T>) {
30+
}: DataTableFilterRowProps<T, TFilter>) {
2831
if (!filterPossible || columns.filter((column) => column.filter).length <= 0) return <React.Fragment />;
2932
return (
3033
<tr>
3134
{actionsPosition == ActionsPosition.Left && (
3235
<ActionsHeaderFilterCell<T> onSearch={onSearch} translations={translations} actions={actions} getFilterRefs={getFilterRefs} />
3336
)}
34-
{columns.map((column) => (
35-
<th key={column.dataField}>
36-
{column.filter && column.filter.filterType === ColumnFilterType.String && (
37-
<Input
38-
bsSize="sm"
39-
id={`filter-${column.dataField}`}
40-
innerRef={(ref) => ref && ref instanceof HTMLInputElement && setFilterRef(column.dataField, ref)}
41-
onKeyDown={(e) => {
42-
if (e.key === "Enter") onSearch();
43-
}}
44-
/>
45-
)}
46-
{column.filter && column.filter.filterType === ColumnFilterType.Enum && column.enumValues && (
47-
<Input
48-
bsSize="sm"
49-
innerRef={(ref) => ref && ref instanceof HTMLSelectElement && setFilterRef(column.dataField, ref as HTMLInputElement)}
50-
type="select"
51-
onKeyDown={(e) => {
52-
if (e.key === "Enter") onSearch();
53-
}}
54-
onChange={() => onSearch()}
55-
>
56-
{column.enumValues.map((item) => (
57-
<option value={item.value ?? "null"} key={`${column.dataField}_filter_${item.value}`}>
58-
{item.text}
59-
</option>
60-
))}
61-
</Input>
62-
)}
63-
</th>
64-
))}
37+
{columns.map((column) => {
38+
var defaultValue = predefinedFilter ? getDeepValue(predefinedFilter, column.dataField) : undefined;
39+
return (
40+
<th key={column.dataField}>
41+
{column.filter && column.filter.filterType === ColumnFilterType.String && (
42+
<Input
43+
bsSize="sm"
44+
id={`filter-${column.dataField}`}
45+
innerRef={(ref) => ref && ref instanceof HTMLInputElement && setFilterRef(column.dataField, ref)}
46+
defaultValue={defaultValue}
47+
onKeyDown={(e) => {
48+
if (e.key === "Enter") onSearch();
49+
}}
50+
/>
51+
)}
52+
{column.filter && column.filter.filterType === ColumnFilterType.Enum && column.enumValues && (
53+
<Input
54+
bsSize="sm"
55+
innerRef={(ref) => ref && ref instanceof HTMLSelectElement && setFilterRef(column.dataField, ref as HTMLInputElement)}
56+
type="select"
57+
defaultValue={defaultValue}
58+
onKeyDown={(e) => {
59+
if (e.key === "Enter") onSearch();
60+
}}
61+
onChange={() => onSearch()}
62+
>
63+
{column.enumValues.map((item) => (
64+
<option value={item.value ?? "null"} key={`${column.dataField}_filter_${item.value}`}>
65+
{item.text}
66+
</option>
67+
))}
68+
</Input>
69+
)}
70+
</th>
71+
);
72+
})}
6573
{actionsPosition == ActionsPosition.Right && (
6674
<ActionsHeaderFilterCell<T> onSearch={onSearch} translations={translations} actions={actions} getFilterRefs={getFilterRefs} />
6775
)}

test/DataTable/DataTable.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ describe("DataTable", () => {
189189
predefinedItemsPerPage={20}
190190
tableClassName={tableClassName}
191191
tableStyle={tableStyle}
192+
predefinedFilter={{ name: "Name 001" }}
192193
/>,
193194
);
194195

test/DataTable/__snapshots__/DataTable.test.tsx.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ exports[`DataTable renders dynamic correctly 1`] = `
7373
class="form-control-sm form-control"
7474
id="filter-name"
7575
type="text"
76+
value="Name 001"
7677
/>
7778
</th>
7879
<th>
7980
<input
8081
class="form-control-sm form-control"
8182
id="filter-count"
8283
type="text"
84+
value=""
8385
/>
8486
</th>
8587
<th>
@@ -108,6 +110,7 @@ exports[`DataTable renders dynamic correctly 1`] = `
108110
class="form-control-sm form-control"
109111
id="filter-dateCreated"
110112
type="text"
113+
value=""
111114
/>
112115
</th>
113116
</tr>

0 commit comments

Comments
 (0)